From 60514ebd866c8c49e90e501198588e2806c5fe7b Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 26 Jul 2015 11:21:29 +0200 Subject: Moved storage access, adaptor conversion and index access to central place --- common/facade.h | 122 ++------------------------------------------------------ 1 file changed, 4 insertions(+), 118 deletions(-) (limited to 'common/facade.h') 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 @@ #include "domainadaptor.h" #include "entitybuffer.h" #include "log.h" -#include "storage.h" #include "resultset.h" +#include "entitystorage.h" /** * A QueryRunner runs a query and updates the corresponding result set. @@ -256,132 +256,18 @@ protected: return KAsync::null(); } - static void scan(const QSharedPointer &storage, const QByteArray &key, std::function callback) - { - storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { - //Skip internals - if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) { - return true; - } - - //Extract buffers - Akonadi2::EntityBuffer buffer(dataValue, dataSize); - - //FIXME implement buffer.isValid() - // const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer(buffer.entity().resource()); - // const auto localBuffer = Akonadi2::EntityBuffer::readBuffer(buffer.entity().local()); - // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer(buffer.entity().metadata()); - - // if ((!resourceBuffer && !localBuffer) || !metadataBuffer) { - // qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast(keyValue), keySize); - // return true; - // } - return callback(QByteArray::fromRawData(static_cast(keyValue), keySize), buffer.entity()); - }, - [](const Akonadi2::Storage::Error &error) { - qWarning() << "Error during query: " << error.message; - }); - } - - static void readValue(const QSharedPointer &storage, const QByteArray &key, const std::function &resultCallback, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, const QByteArray &instanceIdentifier) - { - scan(storage, key, [=](const QByteArray &key, const Akonadi2::Entity &entity) { - const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer(entity.metadata()); - qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; - //This only works for a 1:1 mapping of resource to domain types. - //Not i.e. for tags that are stored as flags in each entity of an imap store. - //additional properties that don't have a 1:1 mapping (such as separately stored tags), - //could be added to the adaptor - auto domainObject = QSharedPointer::create(instanceIdentifier, key, revision, adaptorFactory->createAdaptor(entity)); - resultCallback(domainObject); - return true; - }); - } - - static ResultSet fullScan(const QSharedPointer &storage) - { - //TODO use a result set with an iterator, to read values on demand - QVector keys; - scan(storage, QByteArray(), [=, &keys](const QByteArray &key, const Akonadi2::Entity &) { - keys << key; - return true; - }); - Trace() << "Full scan found " << keys.size() << " results"; - return ResultSet(keys); - } - - static ResultSet filteredSet(const ResultSet &resultSet, const std::function &filter, const QSharedPointer &storage, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, qint64 baseRevision, qint64 topRevision, const QByteArray &instanceIdentifier) - { - auto resultSetPtr = QSharedPointer::create(resultSet); - - //Read through the source values and return whatever matches the filter - std::function)> generator = [resultSetPtr, storage, adaptorFactory, filter, instanceIdentifier](std::function callback) -> bool { - while (resultSetPtr->next()) { - readValue(storage, resultSetPtr->id(), [filter, callback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) { - if (filter(domainObject)) { - callback(domainObject); - } - }, adaptorFactory, instanceIdentifier); - } - return false; - }; - return ResultSet(generator); - } - - static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer &storage, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, const QByteArray &resourceInstanceIdentifier, qint64 baseRevision, qint64 topRevision) - { - QSet appliedFilters; - ResultSet resultSet = Akonadi2::ApplicationDomain::TypeImplementation::queryIndexes(query, resourceInstanceIdentifier, appliedFilters); - const auto remainingFilters = query.propertyFilter.keys().toSet() - appliedFilters; - - //We do a full scan if there were no indexes available to create the initial set. - if (appliedFilters.isEmpty()) { - resultSet = fullScan(storage); - } - - auto filter = [remainingFilters, query, baseRevision, topRevision](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) -> bool { - if (topRevision > 0) { - if (domainObject->revision() < baseRevision || domainObject->revision() > topRevision) { - return false; - } - } - for (const auto &filterProperty : remainingFilters) { - //TODO implement other comparison operators than equality - if (domainObject->getProperty(filterProperty) != query.propertyFilter.value(filterProperty)) { - return false; - } - } - return true; - }; - - return filteredSet(resultSet, filter, storage, adaptorFactory, baseRevision, topRevision, resourceInstanceIdentifier); - } virtual KAsync::Job load(const Akonadi2::Query &query, const QSharedPointer > &resultProvider, qint64 oldRevision, qint64 newRevision) { return KAsync::start([=]() -> qint64 { - auto storage = QSharedPointer::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier); - storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) { - Warning() << "Error during query: " << error.store << error.message; - }); - - storage->startTransaction(Akonadi2::Storage::ReadOnly); - //TODO start transaction on indexes as well - - auto resultSet = getResultSet(query, storage, mDomainTypeAdaptorFactory, mResourceInstanceIdentifier, oldRevision, newRevision); - auto resultCallback = std::bind(&Akonadi2::ResultProvider::add, resultProvider, std::placeholders::_1); - while(resultSet.next([resultCallback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value) -> bool { - resultCallback(Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation(*value)); - return true; - })){}; - storage->abortTransaction(); + EntityStorage storage(mResourceInstanceIdentifier, mDomainTypeAdaptorFactory); + storage.read(query, qMakePair(oldRevision, newRevision), resultProvider); return newRevision; }); } -private: protected: - //TODO use one resource access instance per application => make static + //TODO use one resource access instance per application & per resource QSharedPointer mResourceAccess; DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; QByteArray mResourceInstanceIdentifier; -- cgit v1.2.3