diff options
Diffstat (limited to 'common/facade.h')
-rw-r--r-- | common/facade.h | 122 |
1 files changed, 4 insertions, 118 deletions
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; |