diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/entitystorage.h | 175 | ||||
-rw-r--r-- | common/facade.h | 122 |
2 files changed, 179 insertions, 118 deletions
diff --git a/common/entitystorage.h b/common/entitystorage.h new file mode 100644 index 0000000..6a41e0e --- /dev/null +++ b/common/entitystorage.h | |||
@@ -0,0 +1,175 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | #pragma once | ||
20 | |||
21 | #include "clientapi.h" | ||
22 | |||
23 | #include <QByteArray> | ||
24 | |||
25 | #include "domainadaptor.h" | ||
26 | #include "entitybuffer.h" | ||
27 | #include "log.h" | ||
28 | #include "storage.h" | ||
29 | #include "resultset.h" | ||
30 | |||
31 | /** | ||
32 | * Wraps storage, entity adaptor factory and indexes into one. | ||
33 | */ | ||
34 | template <typename DomainType> | ||
35 | class EntityStorage | ||
36 | { | ||
37 | |||
38 | public: | ||
39 | EntityStorage(const QByteArray &instanceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory) | ||
40 | : mResourceInstanceIdentifier(instanceIdentifier), | ||
41 | mDomainTypeAdaptorFactory(adaptorFactory) | ||
42 | { | ||
43 | |||
44 | } | ||
45 | |||
46 | private: | ||
47 | static void scan(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, std::function<bool(const QByteArray &key, const Akonadi2::Entity &entity)> callback) | ||
48 | { | ||
49 | storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | ||
50 | //Skip internals | ||
51 | if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) { | ||
52 | return true; | ||
53 | } | ||
54 | |||
55 | //Extract buffers | ||
56 | Akonadi2::EntityBuffer buffer(dataValue, dataSize); | ||
57 | |||
58 | //FIXME implement buffer.isValid() | ||
59 | // const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<DummyEvent>(buffer.entity().resource()); | ||
60 | // const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local()); | ||
61 | // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata()); | ||
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 | |||
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) | ||
75 | { | ||
76 | scan(storage, key, [=](const QByteArray &key, const Akonadi2::Entity &entity) { | ||
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 | } | ||
88 | |||
89 | static ResultSet fullScan(const QSharedPointer<Akonadi2::Storage> &storage) | ||
90 | { | ||
91 | //TODO use a result set with an iterator, to read values on demand | ||
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 | } | ||
100 | |||
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) | ||
102 | { | ||
103 | auto resultSetPtr = QSharedPointer<ResultSet>::create(resultSet); | ||
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 | } | ||
118 | |||
119 | static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, const QByteArray &resourceInstanceIdentifier, qint64 baseRevision, qint64 topRevision) | ||
120 | { | ||
121 | QSet<QByteArray> 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 | } | ||
148 | |||
149 | public: | ||
150 | |||
151 | void read(const Akonadi2::Query &query, const QPair<qint64, qint64> &revisionRange, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider) | ||
152 | { | ||
153 | auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier); | ||
154 | storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) { | ||
155 | Warning() << "Error during query: " << error.store << error.message; | ||
156 | }); | ||
157 | |||
158 | storage->startTransaction(Akonadi2::Storage::ReadOnly); | ||
159 | //TODO start transaction on indexes as well | ||
160 | |||
161 | Log() << "Querying" << revisionRange.first << revisionRange.second; | ||
162 | auto resultSet = getResultSet(query, storage, mDomainTypeAdaptorFactory, mResourceInstanceIdentifier, revisionRange.first, revisionRange.second); | ||
163 | auto resultCallback = std::bind(&Akonadi2::ResultProvider<typename DomainType::Ptr>::add, resultProvider, std::placeholders::_1); | ||
164 | while(resultSet.next([resultCallback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value) -> bool { | ||
165 | resultCallback(Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation<DomainType>(*value)); | ||
166 | return true; | ||
167 | })){}; | ||
168 | //TODO replay removals and modifications | ||
169 | storage->abortTransaction(); | ||
170 | } | ||
171 | |||
172 | private: | ||
173 | DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; | ||
174 | QByteArray mResourceInstanceIdentifier; | ||
175 | }; | ||
diff --git a/common/facade.h b/common/facade.h index 9222e26..ef3bbbc 100644 --- a/common/facade.h +++ b/common/facade.h | |||
@@ -33,8 +33,8 @@ | |||
33 | #include "domainadaptor.h" | 33 | #include "domainadaptor.h" |
34 | #include "entitybuffer.h" | 34 | #include "entitybuffer.h" |
35 | #include "log.h" | 35 | #include "log.h" |
36 | #include "storage.h" | ||
37 | #include "resultset.h" | 36 | #include "resultset.h" |
37 | #include "entitystorage.h" | ||
38 | 38 | ||
39 | /** | 39 | /** |
40 | * A QueryRunner runs a query and updates the corresponding result set. | 40 | * A QueryRunner runs a query and updates the corresponding result set. |
@@ -256,132 +256,18 @@ protected: | |||
256 | return KAsync::null<void>(); | 256 | return KAsync::null<void>(); |
257 | } | 257 | } |
258 | 258 | ||
259 | static void scan(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, std::function<bool(const QByteArray &key, const Akonadi2::Entity &entity)> callback) | ||
260 | { | ||
261 | storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | ||
262 | //Skip internals | ||
263 | if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) { | ||
264 | return true; | ||
265 | } | ||
266 | |||
267 | //Extract buffers | ||
268 | Akonadi2::EntityBuffer buffer(dataValue, dataSize); | ||
269 | |||
270 | //FIXME implement buffer.isValid() | ||
271 | // const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<DummyEvent>(buffer.entity().resource()); | ||
272 | // const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local()); | ||
273 | // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata()); | ||
274 | |||
275 | // if ((!resourceBuffer && !localBuffer) || !metadataBuffer) { | ||
276 | // qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast<char*>(keyValue), keySize); | ||
277 | // return true; | ||
278 | // } | ||
279 | return callback(QByteArray::fromRawData(static_cast<char*>(keyValue), keySize), buffer.entity()); | ||
280 | }, | ||
281 | [](const Akonadi2::Storage::Error &error) { | ||
282 | qWarning() << "Error during query: " << error.message; | ||
283 | }); | ||
284 | } | ||
285 | |||
286 | 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) | ||
287 | { | ||
288 | scan(storage, key, [=](const QByteArray &key, const Akonadi2::Entity &entity) { | ||
289 | const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(entity.metadata()); | ||
290 | qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; | ||
291 | //This only works for a 1:1 mapping of resource to domain types. | ||
292 | //Not i.e. for tags that are stored as flags in each entity of an imap store. | ||
293 | //additional properties that don't have a 1:1 mapping (such as separately stored tags), | ||
294 | //could be added to the adaptor | ||
295 | auto domainObject = QSharedPointer<DomainType>::create(instanceIdentifier, key, revision, adaptorFactory->createAdaptor(entity)); | ||
296 | resultCallback(domainObject); | ||
297 | return true; | ||
298 | }); | ||
299 | } | ||
300 | |||
301 | static ResultSet fullScan(const QSharedPointer<Akonadi2::Storage> &storage) | ||
302 | { | ||
303 | //TODO use a result set with an iterator, to read values on demand | ||
304 | QVector<QByteArray> keys; | ||
305 | scan(storage, QByteArray(), [=, &keys](const QByteArray &key, const Akonadi2::Entity &) { | ||
306 | keys << key; | ||
307 | return true; | ||
308 | }); | ||
309 | Trace() << "Full scan found " << keys.size() << " results"; | ||
310 | return ResultSet(keys); | ||
311 | } | ||
312 | |||
313 | 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) | ||
314 | { | ||
315 | auto resultSetPtr = QSharedPointer<ResultSet>::create(resultSet); | ||
316 | |||
317 | //Read through the source values and return whatever matches the filter | ||
318 | 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 { | ||
319 | while (resultSetPtr->next()) { | ||
320 | readValue(storage, resultSetPtr->id(), [filter, callback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) { | ||
321 | if (filter(domainObject)) { | ||
322 | callback(domainObject); | ||
323 | } | ||
324 | }, adaptorFactory, instanceIdentifier); | ||
325 | } | ||
326 | return false; | ||
327 | }; | ||
328 | return ResultSet(generator); | ||
329 | } | ||
330 | |||
331 | static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, const QByteArray &resourceInstanceIdentifier, qint64 baseRevision, qint64 topRevision) | ||
332 | { | ||
333 | QSet<QByteArray> appliedFilters; | ||
334 | ResultSet resultSet = Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::queryIndexes(query, resourceInstanceIdentifier, appliedFilters); | ||
335 | const auto remainingFilters = query.propertyFilter.keys().toSet() - appliedFilters; | ||
336 | |||
337 | //We do a full scan if there were no indexes available to create the initial set. | ||
338 | if (appliedFilters.isEmpty()) { | ||
339 | resultSet = fullScan(storage); | ||
340 | } | ||
341 | |||
342 | auto filter = [remainingFilters, query, baseRevision, topRevision](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) -> bool { | ||
343 | if (topRevision > 0) { | ||
344 | if (domainObject->revision() < baseRevision || domainObject->revision() > topRevision) { | ||
345 | return false; | ||
346 | } | ||
347 | } | ||
348 | for (const auto &filterProperty : remainingFilters) { | ||
349 | //TODO implement other comparison operators than equality | ||
350 | if (domainObject->getProperty(filterProperty) != query.propertyFilter.value(filterProperty)) { | ||
351 | return false; | ||
352 | } | ||
353 | } | ||
354 | return true; | ||
355 | }; | ||
356 | |||
357 | return filteredSet(resultSet, filter, storage, adaptorFactory, baseRevision, topRevision, resourceInstanceIdentifier); | ||
358 | } | ||
359 | 259 | ||
360 | virtual KAsync::Job<qint64> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider, qint64 oldRevision, qint64 newRevision) | 260 | virtual KAsync::Job<qint64> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider, qint64 oldRevision, qint64 newRevision) |
361 | { | 261 | { |
362 | return KAsync::start<qint64>([=]() -> qint64 { | 262 | return KAsync::start<qint64>([=]() -> qint64 { |
363 | auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier); | 263 | EntityStorage<DomainType> storage(mResourceInstanceIdentifier, mDomainTypeAdaptorFactory); |
364 | storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) { | 264 | storage.read(query, qMakePair(oldRevision, newRevision), resultProvider); |
365 | Warning() << "Error during query: " << error.store << error.message; | ||
366 | }); | ||
367 | |||
368 | storage->startTransaction(Akonadi2::Storage::ReadOnly); | ||
369 | //TODO start transaction on indexes as well | ||
370 | |||
371 | auto resultSet = getResultSet(query, storage, mDomainTypeAdaptorFactory, mResourceInstanceIdentifier, oldRevision, newRevision); | ||
372 | auto resultCallback = std::bind(&Akonadi2::ResultProvider<typename DomainType::Ptr>::add, resultProvider, std::placeholders::_1); | ||
373 | while(resultSet.next([resultCallback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value) -> bool { | ||
374 | resultCallback(Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation<DomainType>(*value)); | ||
375 | return true; | ||
376 | })){}; | ||
377 | storage->abortTransaction(); | ||
378 | return newRevision; | 265 | return newRevision; |
379 | }); | 266 | }); |
380 | } | 267 | } |
381 | 268 | ||
382 | private: | ||
383 | protected: | 269 | protected: |
384 | //TODO use one resource access instance per application => make static | 270 | //TODO use one resource access instance per application & per resource |
385 | QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess; | 271 | QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess; |
386 | DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; | 272 | DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; |
387 | QByteArray mResourceInstanceIdentifier; | 273 | QByteArray mResourceInstanceIdentifier; |