File indexing completed on 2024-05-12 05:26:08
0001 /* 0002 Copyright (c) 2015 Christian Mollekopf <mollekopf@kolabsys.com> 0003 0004 This library is free software; you can redistribute it and/or modify it 0005 under the terms of the GNU Library General Public License as published by 0006 the Free Software Foundation; either version 2 of the License, or (at your 0007 option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, but WITHOUT 0010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 0012 License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to the 0016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0017 02110-1301, USA. 0018 */ 0019 #pragma once 0020 0021 #include "resultset.h" 0022 #include "storage.h" 0023 #include "query.h" 0024 #include "log.h" 0025 #include "indexer.h" 0026 #include "storage/key.h" 0027 #include <QByteArray> 0028 0029 namespace Sink { 0030 namespace Storage { 0031 class EntityStore; 0032 } 0033 } 0034 0035 class TypeIndex 0036 { 0037 public: 0038 TypeIndex(const QByteArray &type, const Sink::Log::Context &); 0039 0040 void addProperty(const QByteArray &property); 0041 0042 //FIXME We currently simply serialize based on the QVariant we get and ignore the index type 0043 template <typename T> 0044 void addProperty(const QByteArray &property) 0045 { 0046 addProperty(property); 0047 } 0048 0049 template <typename T> 0050 void addSortedProperty(const QByteArray &property); 0051 template <typename T, typename S> 0052 void addPropertyWithSorting(const QByteArray &property, const QByteArray &sortProperty); 0053 0054 template <typename T, typename S> 0055 void addPropertyWithSorting() 0056 { 0057 addPropertyWithSorting<typename T::Type, typename S::Type>(T::name, S::name); 0058 } 0059 0060 template <typename T> 0061 void addProperty() 0062 { 0063 addProperty<typename T::Type>(T::name); 0064 } 0065 0066 template <typename T> 0067 void addSortedProperty() 0068 { 0069 addSortedProperty<typename T::Type>(T::name); 0070 } 0071 0072 template <typename Left, typename Right> 0073 void addSecondaryProperty() 0074 { 0075 mSecondaryProperties.insert(Left::name, Right::name); 0076 } 0077 0078 template <typename Left, typename Right, typename CustomIndexer> 0079 void addSecondaryPropertyIndexer() 0080 { 0081 mCustomIndexer << CustomIndexer::Ptr::create(); 0082 } 0083 0084 template <typename Begin, typename End> 0085 void addSampledPeriodIndex(const QByteArray &beginProperty, const QByteArray &endProperty); 0086 0087 template <typename Begin, typename End> 0088 void addSampledPeriodIndex() 0089 { 0090 addSampledPeriodIndex<typename Begin::Type, typename End::Type>(Begin::name, End::name); 0091 } 0092 0093 void add(const Sink::Storage::Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); 0094 void modify(const Sink::Storage::Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &oldEntity, const Sink::ApplicationDomain::ApplicationDomainType &newEntity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); 0095 void remove(const Sink::Storage::Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); 0096 0097 QVector<Sink::Storage::Identifier> query(const Sink::QueryBase &query, QSet<QByteArrayList> &appliedFilters, QByteArray &appliedSorting, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); 0098 QVector<Sink::Storage::Identifier> lookup(const QByteArray &property, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId = {}, const QVector<Sink::Storage::Identifier> &filter = {}); 0099 0100 template <typename Left, typename Right> 0101 QVector<QByteArray> secondaryLookup(const QVariant &value) 0102 { 0103 return secondaryLookup<typename Left::Type>(Left::name, Right::name, value); 0104 } 0105 0106 template <typename Type> 0107 QVector<QByteArray> secondaryLookup(const QByteArray &leftName, const QByteArray &rightName, const QVariant &value); 0108 0109 template <typename Left, typename Right> 0110 void index(const QVariant &leftValue, const QVariant &rightValue, Sink::Storage::DataStore::Transaction &transaction) 0111 { 0112 index<typename Left::Type, typename Right::Type>(Left::name, Right::name, leftValue, rightValue, transaction); 0113 } 0114 0115 template <typename LeftType, typename RightType> 0116 void index(const QByteArray &leftName, const QByteArray &rightName, const QVariant &leftValue, const QVariant &rightValue, Sink::Storage::DataStore::Transaction &transaction); 0117 0118 template <typename Left, typename Right> 0119 void unindex(const QVariant &leftValue, const QVariant &rightValue, Sink::Storage::DataStore::Transaction &transaction) 0120 { 0121 index<typename Left::Type, typename Right::Type>(Left::name, Right::name, leftValue, rightValue, transaction); 0122 } 0123 0124 template <typename LeftType, typename RightType> 0125 void unindex(const QByteArray &leftName, const QByteArray &rightName, const QVariant &leftValue, const QVariant &rightValue, Sink::Storage::DataStore::Transaction &transaction); 0126 0127 void commitTransaction(); 0128 void abortTransaction(); 0129 0130 enum Action { 0131 Add, 0132 Remove 0133 }; 0134 0135 private: 0136 friend class Sink::Storage::EntityStore; 0137 void updateIndex(Action action, const Sink::Storage::Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); 0138 QByteArray indexName(const QByteArray &property, const QByteArray &sortProperty = QByteArray()) const; 0139 QByteArray sortedIndexName(const QByteArray &property) const; 0140 QByteArray sampledPeriodIndexName(const QByteArray &rangeBeginProperty, const QByteArray &rangeEndProperty) const; 0141 Sink::Log::Context mLogCtx; 0142 QByteArray mType; 0143 QByteArrayList mProperties; 0144 QByteArrayList mSortedProperties; 0145 QMap<QByteArray, QByteArray> mGroupedSortedProperties; 0146 //<Property, ResultProperty> 0147 QMap<QByteArray, QByteArray> mSecondaryProperties; 0148 QSet<QPair<QByteArray, QByteArray>> mSampledPeriodProperties; 0149 QList<Sink::Indexer::Ptr> mCustomIndexer; 0150 Sink::Storage::DataStore::Transaction *mTransaction; 0151 QHash<QByteArray, std::function<void(Action, const Sink::Storage::Identifier &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction)>> mIndexer; 0152 QHash<QByteArray, std::function<void(Action, const Sink::Storage::Identifier &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction)>> mSortIndexer; 0153 QHash<QByteArray, std::function<void(Action, const Sink::Storage::Identifier &identifier, const QVariant &value, const QVariant &sortValue, Sink::Storage::DataStore::Transaction &transaction)>> mGroupedSortIndexer; 0154 QHash<QPair<QByteArray, QByteArray>, std::function<void(Action, const Sink::Storage::Identifier &identifier, const QVariant &begin, const QVariant &end, Sink::Storage::DataStore::Transaction &transaction)>> mSampledPeriodIndexer; 0155 };