From 3601ee575f833bf204540f4fac41d87a0d977a79 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 25 May 2015 23:14:57 +0200 Subject: Centralized type specific code. --- examples/dummyresource/facade.cpp | 61 +++++------------------------- examples/dummyresource/resourcefactory.cpp | 28 +++++--------- 2 files changed, 20 insertions(+), 69 deletions(-) (limited to 'examples') diff --git a/examples/dummyresource/facade.cpp b/examples/dummyresource/facade.cpp index 9722335..611217f 100644 --- a/examples/dummyresource/facade.cpp +++ b/examples/dummyresource/facade.cpp @@ -24,6 +24,8 @@ #include "common/resourceaccess.h" #include "common/commands.h" +#include "common/resultset.h" +#include "common/domain/event.h" #include "dummycalendar_generated.h" #include "event_generated.h" #include "entity_generated.h" @@ -126,60 +128,16 @@ void DummyResourceFacade::readValue(const QSharedPointer &sto }); } -/* - * An iterator to a result set. - * - * We'll eventually want to lazy load results in next(). - */ -class ResultSet { - public: - ResultSet(const QVector &resultSet) - : mResultSet(resultSet), - mIt(nullptr) - { - - } - - bool next() - { - if (!mIt) { - mIt = mResultSet.constBegin(); - } else { - mIt++; - } - return mIt != mResultSet.constEnd(); - } - - QByteArray id() - { - return *mIt; - } - - private: - QVector mResultSet; - QVector::ConstIterator mIt; -}; - static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer &storage) { - //Now that the sync is complete we can execute the query - const auto preparedQuery = prepareQuery(query); - - //Index lookups - //TODO query standard indexes - QVector keys; - if (query.propertyFilter.contains("uid")) { - static Index uidIndex(Akonadi2::Store::storageLocation(), "org.kde.dummy.index.uid", Akonadi2::Storage::ReadOnly); - uidIndex.lookup(query.propertyFilter.value("uid").toByteArray(), [&](const QByteArray &value) { - keys << value; - }, - [](const Index::Error &error) { - Warning() << "Error in index: " << error.message; - }); - } + auto resultSet = Akonadi2::ApplicationDomain::EventImplementation::queryIndexes(query, "org.kde.dummy"); //Scan for where we don't have an index - if (keys.isEmpty()) { + //TODO: we may want a way for queryIndexes to indicate that the resultSet is not final, and that a scan over the remaining set is required + //TODO: the prepared query should be generalized in EventImplementation on top of domain adaptors + if (resultSet.isEmpty()) { + QVector keys; + const auto preparedQuery = prepareQuery(query); scan(storage, QByteArray(), [preparedQuery, &keys](const QByteArray &key, const Akonadi2::Entity &entity, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local, Akonadi2::Metadata const *metadataBuffer) { //TODO use adapter for query and scan? if (preparedQuery && preparedQuery(std::string(key.constData(), key.size()), buffer, local)) { @@ -187,9 +145,10 @@ static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer } return true; }); + return ResultSet(keys); } - return ResultSet(keys); + return resultSet; } KAsync::Job DummyResourceFacade::load(const Akonadi2::Query &query, const QSharedPointer > &resultProvider, qint64 oldRevision, qint64 newRevision) diff --git a/examples/dummyresource/resourcefactory.cpp b/examples/dummyresource/resourcefactory.cpp index e16a693..71a4ac5 100644 --- a/examples/dummyresource/resourcefactory.cpp +++ b/examples/dummyresource/resourcefactory.cpp @@ -30,6 +30,7 @@ #include "clientapi.h" #include "index.h" #include "log.h" +#include "domain/event.h" #include #include @@ -106,35 +107,26 @@ static QMap s_dataSource = populate(); //FIXME We need to pass the resource-instance name to generic resource, not the plugin name DummyResource::DummyResource() - : Akonadi2::GenericResource(PLUGIN_NAME) + : Akonadi2::GenericResource(PLUGIN_NAME ".instance1") { } void DummyResource::configurePipeline(Akonadi2::Pipeline *pipeline) { - auto eventFactory = QSharedPointer::create(); - //FIXME we should setup for each resource entity type, not for each domain type + //TODO In case of a non 1:1 mapping between resource and domain types special handling is required. //i.e. If a resource stores tags as part of each message it needs to update the tag index - //TODO setup preprocessors for each resource entity type and pipeline type allowing full customization - //Eventually the order should be self configuring, for now it's hardcoded. - auto eventIndexer = new SimpleProcessor("summaryprocessor", [eventFactory](const Akonadi2::PipelineState &state, const Akonadi2::Entity &entity) { - auto adaptor = eventFactory->createAdaptor(entity); - // Log() << "Summary preprocessor: " << adaptor->getProperty("summary").toString(); - }); - auto uidIndexer = new SimpleProcessor("uidIndexer", [eventFactory](const Akonadi2::PipelineState &state, const Akonadi2::Entity &entity) { - static Index uidIndex(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/storage", "org.kde.dummy.index.uid", Akonadi2::Storage::ReadWrite); - - //TODO: Benchmark if this is performance wise acceptable, or if we have to access the buffer directly + auto eventFactory = QSharedPointer::create(); + const auto resourceIdentifier = mResourceInstanceIdentifier; + auto eventIndexer = new SimpleProcessor("eventIndexer", [eventFactory, resourceIdentifier](const Akonadi2::PipelineState &state, const Akonadi2::Entity &entity) { auto adaptor = eventFactory->createAdaptor(entity); - const auto uid = adaptor->getProperty("uid"); - if (uid.isValid()) { - uidIndex.add(uid.toByteArray(), state.key()); - } + //FIXME set revision? + Akonadi2::ApplicationDomain::Event event(resourceIdentifier, state.key(), -1, adaptor); + Akonadi2::ApplicationDomain::EventImplementation::index(event); }); //event is the entitytype and not the domain type - pipeline->setPreprocessors("event", Akonadi2::Pipeline::NewPipeline, QVector() << eventIndexer << uidIndexer); + pipeline->setPreprocessors("event", Akonadi2::Pipeline::NewPipeline, QVector() << eventIndexer); GenericResource::configurePipeline(pipeline); } -- cgit v1.2.3