diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-07-27 23:00:23 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-07-27 23:22:10 +0200 |
commit | 7bd184460952932a237dc6f8bea7a8cd220afadf (patch) | |
tree | dd520096627edd9c7c06d7d1a52b30bb0876be22 /common/entitystorage.h | |
parent | 60514ebd866c8c49e90e501198588e2806c5fe7b (diff) | |
download | sink-7bd184460952932a237dc6f8bea7a8cd220afadf.tar.gz sink-7bd184460952932a237dc6f8bea7a8cd220afadf.zip |
Abstracted the storage so the facade can be tested.
Diffstat (limited to 'common/entitystorage.h')
-rw-r--r-- | common/entitystorage.h | 138 |
1 files changed, 35 insertions, 103 deletions
diff --git a/common/entitystorage.h b/common/entitystorage.h index 6a41e0e..a62d474 100644 --- a/common/entitystorage.h +++ b/common/entitystorage.h | |||
@@ -31,124 +31,59 @@ | |||
31 | /** | 31 | /** |
32 | * Wraps storage, entity adaptor factory and indexes into one. | 32 | * Wraps storage, entity adaptor factory and indexes into one. |
33 | */ | 33 | */ |
34 | template <typename DomainType> | 34 | class EntityStorageBase |
35 | class EntityStorage | ||
36 | { | 35 | { |
37 | 36 | protected: | |
38 | public: | 37 | EntityStorageBase(const QByteArray &instanceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory) |
39 | EntityStorage(const QByteArray &instanceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory) | ||
40 | : mResourceInstanceIdentifier(instanceIdentifier), | 38 | : mResourceInstanceIdentifier(instanceIdentifier), |
41 | mDomainTypeAdaptorFactory(adaptorFactory) | 39 | mDomainTypeAdaptorFactory(adaptorFactory) |
42 | { | 40 | { |
43 | 41 | ||
44 | } | 42 | } |
45 | 43 | ||
46 | private: | 44 | virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr create(const QByteArray &key, qint64 revision, const QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> &adaptor) = 0; |
47 | static void scan(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, std::function<bool(const QByteArray &key, const Akonadi2::Entity &entity)> callback) | 45 | virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr copy(const Akonadi2::ApplicationDomain::ApplicationDomainType &) = 0; |
48 | { | 46 | virtual ResultSet queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier, QSet<QByteArray> &appliedFilters) = 0; |
49 | storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | 47 | |
50 | //Skip internals | 48 | void readValue(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)> &resultCallback); |
51 | if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) { | 49 | ResultSet filteredSet(const ResultSet &resultSet, const std::function<bool(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> &filter, const QSharedPointer<Akonadi2::Storage> &storage, qint64 baseRevision, qint64 topRevision); |
52 | return true; | 50 | ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage, qint64 baseRevision, qint64 topRevision); |
53 | } | 51 | |
54 | 52 | protected: | |
55 | //Extract buffers | 53 | QByteArray mResourceInstanceIdentifier; |
56 | Akonadi2::EntityBuffer buffer(dataValue, dataSize); | 54 | DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; |
57 | 55 | }; | |
58 | //FIXME implement buffer.isValid() | 56 | |
59 | // const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<DummyEvent>(buffer.entity().resource()); | 57 | template<typename DomainType> |
60 | // const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local()); | 58 | class EntityStorage : public EntityStorageBase |
61 | // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata()); | 59 | { |
62 | |||
63 | // if ((!resourceBuffer && !localBuffer) || !metadataBuffer) { | ||
64 | // qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast<char*>(keyValue), keySize); | ||
65 | // return true; | ||
66 | // } | ||
67 | return callback(QByteArray::fromRawData(static_cast<char*>(keyValue), keySize), buffer.entity()); | ||
68 | }, | ||
69 | [](const Akonadi2::Storage::Error &error) { | ||
70 | qWarning() << "Error during query: " << error.message; | ||
71 | }); | ||
72 | } | ||
73 | 60 | ||
74 | static void readValue(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, const std::function<void(const typename DomainType::Ptr &)> &resultCallback, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, const QByteArray &instanceIdentifier) | 61 | public: |
62 | EntityStorage(const QByteArray &instanceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory) | ||
63 | : EntityStorageBase(instanceIdentifier, adaptorFactory) | ||
75 | { | 64 | { |
76 | scan(storage, key, [=](const QByteArray &key, const Akonadi2::Entity &entity) { | 65 | |
77 | const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(entity.metadata()); | ||
78 | qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; | ||
79 | //This only works for a 1:1 mapping of resource to domain types. | ||
80 | //Not i.e. for tags that are stored as flags in each entity of an imap store. | ||
81 | //additional properties that don't have a 1:1 mapping (such as separately stored tags), | ||
82 | //could be added to the adaptor | ||
83 | auto domainObject = QSharedPointer<DomainType>::create(instanceIdentifier, key, revision, adaptorFactory->createAdaptor(entity)); | ||
84 | resultCallback(domainObject); | ||
85 | return true; | ||
86 | }); | ||
87 | } | 66 | } |
88 | 67 | ||
89 | static ResultSet fullScan(const QSharedPointer<Akonadi2::Storage> &storage) | 68 | protected: |
69 | Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr create(const QByteArray &key, qint64 revision, const QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> &adaptor) Q_DECL_OVERRIDE | ||
90 | { | 70 | { |
91 | //TODO use a result set with an iterator, to read values on demand | 71 | return DomainType::Ptr::create(mResourceInstanceIdentifier, key, revision, adaptor); |
92 | QVector<QByteArray> keys; | ||
93 | scan(storage, QByteArray(), [=, &keys](const QByteArray &key, const Akonadi2::Entity &) { | ||
94 | keys << key; | ||
95 | return true; | ||
96 | }); | ||
97 | Trace() << "Full scan found " << keys.size() << " results"; | ||
98 | return ResultSet(keys); | ||
99 | } | 72 | } |
100 | 73 | ||
101 | static ResultSet filteredSet(const ResultSet &resultSet, const std::function<bool(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> &filter, const QSharedPointer<Akonadi2::Storage> &storage, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, qint64 baseRevision, qint64 topRevision, const QByteArray &instanceIdentifier) | 74 | Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr copy(const Akonadi2::ApplicationDomain::ApplicationDomainType &object) Q_DECL_OVERRIDE |
102 | { | 75 | { |
103 | auto resultSetPtr = QSharedPointer<ResultSet>::create(resultSet); | 76 | return Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation<DomainType>(object); |
104 | |||
105 | //Read through the source values and return whatever matches the filter | ||
106 | std::function<bool(std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)>)> generator = [resultSetPtr, storage, adaptorFactory, filter, instanceIdentifier](std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)> callback) -> bool { | ||
107 | while (resultSetPtr->next()) { | ||
108 | readValue(storage, resultSetPtr->id(), [filter, callback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) { | ||
109 | if (filter(domainObject)) { | ||
110 | callback(domainObject); | ||
111 | } | ||
112 | }, adaptorFactory, instanceIdentifier); | ||
113 | } | ||
114 | return false; | ||
115 | }; | ||
116 | return ResultSet(generator); | ||
117 | } | 77 | } |
118 | 78 | ||
119 | static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, const QByteArray &resourceInstanceIdentifier, qint64 baseRevision, qint64 topRevision) | 79 | ResultSet queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier, QSet<QByteArray> &appliedFilters) Q_DECL_OVERRIDE |
120 | { | 80 | { |
121 | QSet<QByteArray> appliedFilters; | 81 | return Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::queryIndexes(query, resourceInstanceIdentifier, appliedFilters); |
122 | ResultSet resultSet = Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::queryIndexes(query, resourceInstanceIdentifier, appliedFilters, qMakePair(baseRevision, topRevision)); | ||
123 | const auto remainingFilters = query.propertyFilter.keys().toSet() - appliedFilters; | ||
124 | |||
125 | //We do a full scan if there were no indexes available to create the initial set. | ||
126 | if (appliedFilters.isEmpty()) { | ||
127 | resultSet = fullScan(storage); | ||
128 | } | ||
129 | |||
130 | auto filter = [remainingFilters, query, baseRevision, topRevision](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) -> bool { | ||
131 | if (topRevision > 0) { | ||
132 | Trace() << "filtering by revision " << domainObject->revision(); | ||
133 | if (domainObject->revision() < baseRevision || domainObject->revision() > topRevision) { | ||
134 | return false; | ||
135 | } | ||
136 | } | ||
137 | for (const auto &filterProperty : remainingFilters) { | ||
138 | //TODO implement other comparison operators than equality | ||
139 | if (domainObject->getProperty(filterProperty) != query.propertyFilter.value(filterProperty)) { | ||
140 | return false; | ||
141 | } | ||
142 | } | ||
143 | return true; | ||
144 | }; | ||
145 | |||
146 | return filteredSet(resultSet, filter, storage, adaptorFactory, baseRevision, topRevision, resourceInstanceIdentifier); | ||
147 | } | 82 | } |
148 | 83 | ||
149 | public: | 84 | public: |
150 | 85 | ||
151 | void read(const Akonadi2::Query &query, const QPair<qint64, qint64> &revisionRange, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider) | 86 | virtual void read(const Akonadi2::Query &query, const QPair<qint64, qint64> &revisionRange, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider) |
152 | { | 87 | { |
153 | auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier); | 88 | auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier); |
154 | storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) { | 89 | storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) { |
@@ -159,17 +94,14 @@ public: | |||
159 | //TODO start transaction on indexes as well | 94 | //TODO start transaction on indexes as well |
160 | 95 | ||
161 | Log() << "Querying" << revisionRange.first << revisionRange.second; | 96 | Log() << "Querying" << revisionRange.first << revisionRange.second; |
162 | auto resultSet = getResultSet(query, storage, mDomainTypeAdaptorFactory, mResourceInstanceIdentifier, revisionRange.first, revisionRange.second); | 97 | auto resultSet = getResultSet(query, storage, revisionRange.first, revisionRange.second); |
163 | auto resultCallback = std::bind(&Akonadi2::ResultProvider<typename DomainType::Ptr>::add, resultProvider, std::placeholders::_1); | 98 | while(resultSet.next([this, resultProvider](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value) -> bool { |
164 | while(resultSet.next([resultCallback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value) -> bool { | 99 | auto cloned = copy(*value); |
165 | resultCallback(Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation<DomainType>(*value)); | 100 | resultProvider->add(cloned.template staticCast<DomainType>()); |
166 | return true; | 101 | return true; |
167 | })){}; | 102 | })){}; |
168 | //TODO replay removals and modifications | 103 | //TODO replay removals and modifications |
169 | storage->abortTransaction(); | 104 | storage->abortTransaction(); |
170 | } | 105 | } |
171 | 106 | ||
172 | private: | ||
173 | DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; | ||
174 | QByteArray mResourceInstanceIdentifier; | ||
175 | }; | 107 | }; |