From 7bd184460952932a237dc6f8bea7a8cd220afadf Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 27 Jul 2015 23:00:23 +0200 Subject: Abstracted the storage so the facade can be tested. --- common/CMakeLists.txt | 1 + common/entitystorage.cpp | 122 +++++++++++++++++++++++++++++++++++++++++ common/entitystorage.h | 138 ++++++++++++----------------------------------- common/facade.h | 8 +-- 4 files changed, 163 insertions(+), 106 deletions(-) create mode 100644 common/entitystorage.cpp (limited to 'common') diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index a69c62c..b242256 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -13,6 +13,7 @@ endif (STORAGE_unqlite) set(command_SRCS log.cpp entitybuffer.cpp + entitystorage.cpp clientapi.cpp facadefactory.cpp commands.cpp diff --git a/common/entitystorage.cpp b/common/entitystorage.cpp new file mode 100644 index 0000000..f84e9f5 --- /dev/null +++ b/common/entitystorage.cpp @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2014 Christian Mollekopf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "entitystorage.h" + +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; + }); +} + +void EntityStorageBase::readValue(const QSharedPointer &storage, const QByteArray &key, const std::function &resultCallback) +{ + 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 = create(key, revision, mDomainTypeAdaptorFactory->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); +} + +ResultSet EntityStorageBase::filteredSet(const ResultSet &resultSet, const std::function &filter, const QSharedPointer &storage, qint64 baseRevision, qint64 topRevision) +{ + auto resultSetPtr = QSharedPointer::create(resultSet); + + //Read through the source values and return whatever matches the filter + std::function)> generator = [this, resultSetPtr, storage, filter](std::function callback) -> bool { + while (resultSetPtr->next()) { + readValue(storage, resultSetPtr->id(), [this, filter, callback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) { + if (filter(domainObject)) { + callback(domainObject); + } + }); + } + return false; + }; + return ResultSet(generator); +} + +ResultSet EntityStorageBase::getResultSet(const Akonadi2::Query &query, const QSharedPointer &storage, qint64 baseRevision, qint64 topRevision) +{ + QSet appliedFilters; + ResultSet resultSet = queryIndexes(query, mResourceInstanceIdentifier, 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) { + Trace() << "filtering by revision " << domainObject->revision(); + 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, baseRevision, topRevision); +} 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 @@ /** * Wraps storage, entity adaptor factory and indexes into one. */ -template -class EntityStorage +class EntityStorageBase { - -public: - EntityStorage(const QByteArray &instanceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory) +protected: + EntityStorageBase(const QByteArray &instanceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory) : mResourceInstanceIdentifier(instanceIdentifier), mDomainTypeAdaptorFactory(adaptorFactory) { } -private: - 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; - }); - } + virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr create(const QByteArray &key, qint64 revision, const QSharedPointer &adaptor) = 0; + virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr copy(const Akonadi2::ApplicationDomain::ApplicationDomainType &) = 0; + virtual ResultSet queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier, QSet &appliedFilters) = 0; + + void readValue(const QSharedPointer &storage, const QByteArray &key, const std::function &resultCallback); + ResultSet filteredSet(const ResultSet &resultSet, const std::function &filter, const QSharedPointer &storage, qint64 baseRevision, qint64 topRevision); + ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer &storage, qint64 baseRevision, qint64 topRevision); + +protected: + QByteArray mResourceInstanceIdentifier; + DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; +}; + +template +class EntityStorage : public EntityStorageBase +{ - static void readValue(const QSharedPointer &storage, const QByteArray &key, const std::function &resultCallback, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, const QByteArray &instanceIdentifier) +public: + EntityStorage(const QByteArray &instanceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory) + : EntityStorageBase(instanceIdentifier, adaptorFactory) { - 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) +protected: + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr create(const QByteArray &key, qint64 revision, const QSharedPointer &adaptor) Q_DECL_OVERRIDE { - //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); + return DomainType::Ptr::create(mResourceInstanceIdentifier, key, revision, adaptor); } - static ResultSet filteredSet(const ResultSet &resultSet, const std::function &filter, const QSharedPointer &storage, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, qint64 baseRevision, qint64 topRevision, const QByteArray &instanceIdentifier) + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr copy(const Akonadi2::ApplicationDomain::ApplicationDomainType &object) Q_DECL_OVERRIDE { - 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); + return Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation(object); } - static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer &storage, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, const QByteArray &resourceInstanceIdentifier, qint64 baseRevision, qint64 topRevision) + ResultSet queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier, QSet &appliedFilters) Q_DECL_OVERRIDE { - QSet appliedFilters; - ResultSet resultSet = Akonadi2::ApplicationDomain::TypeImplementation::queryIndexes(query, resourceInstanceIdentifier, appliedFilters, qMakePair(baseRevision, topRevision)); - 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) { - Trace() << "filtering by revision " << domainObject->revision(); - 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); + return Akonadi2::ApplicationDomain::TypeImplementation::queryIndexes(query, resourceInstanceIdentifier, appliedFilters); } public: - void read(const Akonadi2::Query &query, const QPair &revisionRange, const QSharedPointer > &resultProvider) + virtual void read(const Akonadi2::Query &query, const QPair &revisionRange, const QSharedPointer > &resultProvider) { auto storage = QSharedPointer::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier); storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) { @@ -159,17 +94,14 @@ public: //TODO start transaction on indexes as well Log() << "Querying" << revisionRange.first << revisionRange.second; - auto resultSet = getResultSet(query, storage, mDomainTypeAdaptorFactory, mResourceInstanceIdentifier, revisionRange.first, revisionRange.second); - 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)); + auto resultSet = getResultSet(query, storage, revisionRange.first, revisionRange.second); + while(resultSet.next([this, resultProvider](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value) -> bool { + auto cloned = copy(*value); + resultProvider->add(cloned.template staticCast()); return true; })){}; //TODO replay removals and modifications storage->abortTransaction(); } -private: - DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; - QByteArray mResourceInstanceIdentifier; }; diff --git a/common/facade.h b/common/facade.h index ef3bbbc..254f671 100644 --- a/common/facade.h +++ b/common/facade.h @@ -112,9 +112,10 @@ public: * @param resourceIdentifier is the identifier of the resource instance * @param adaptorFactory is the adaptor factory used to generate the mappings from domain to resource types and vice versa */ - GenericFacade(const QByteArray &resourceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory = DomainTypeAdaptorFactoryInterface::Ptr()) + GenericFacade(const QByteArray &resourceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory = DomainTypeAdaptorFactoryInterface::Ptr(), const QSharedPointer > storage = QSharedPointer >()) : Akonadi2::StoreFacade(), mResourceAccess(new ResourceAccess(resourceIdentifier)), + mStorage(storage ? storage : QSharedPointer >::create(resourceIdentifier, adaptorFactory)), mDomainTypeAdaptorFactory(adaptorFactory), mResourceInstanceIdentifier(resourceIdentifier) { @@ -257,11 +258,11 @@ protected: } +private: virtual KAsync::Job load(const Akonadi2::Query &query, const QSharedPointer > &resultProvider, qint64 oldRevision, qint64 newRevision) { return KAsync::start([=]() -> qint64 { - EntityStorage storage(mResourceInstanceIdentifier, mDomainTypeAdaptorFactory); - storage.read(query, qMakePair(oldRevision, newRevision), resultProvider); + mStorage->read(query, qMakePair(oldRevision, newRevision), resultProvider); return newRevision; }); } @@ -269,6 +270,7 @@ protected: protected: //TODO use one resource access instance per application & per resource QSharedPointer mResourceAccess; + QSharedPointer > mStorage; DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; QByteArray mResourceInstanceIdentifier; }; -- cgit v1.2.3