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. --- common/CMakeLists.txt | 1 + common/clientapi.h | 11 ++++++--- common/domain/event.cpp | 53 ++++++++++++++++++++++++++++++++++++++++ common/domain/event.h | 46 ++++++++++++++++++++++++++++++++++ common/genericresource.cpp | 9 ++++--- common/genericresource.h | 3 ++- common/resultset.h | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 175 insertions(+), 9 deletions(-) create mode 100644 common/domain/event.cpp create mode 100644 common/domain/event.h create mode 100644 common/resultset.h (limited to 'common') diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 2ece210..37b5b3f 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -25,6 +25,7 @@ set(command_SRCS threadboundary.cpp messagequeue.cpp index.cpp + domain/event.cpp ${storage_SRCS}) add_library(${PROJECT_NAME} SHARED ${command_SRCS}) diff --git a/common/clientapi.h b/common/clientapi.h index ee4ef3f..d26a2ad 100644 --- a/common/clientapi.h +++ b/common/clientapi.h @@ -104,9 +104,10 @@ public: { } - ApplicationDomainType(const QByteArray &resourceName, const QByteArray &identifier, qint64 revision, const QSharedPointer &adaptor) + + ApplicationDomainType(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier, qint64 revision, const QSharedPointer &adaptor) : mAdaptor(adaptor), - mResourceName(resourceName), + mResourceInstanceIdentifier(resourceInstanceIdentifier), mIdentifier(identifier), mRevision(revision) { @@ -117,7 +118,7 @@ public: { //TODO only copy requested properties auto memoryAdaptor = QSharedPointer::create(*(domainType->mAdaptor)); - return QSharedPointer::create(domainType->mResourceName, domainType->mIdentifier, domainType->mRevision, memoryAdaptor); + return QSharedPointer::create(domainType->mResourceInstanceIdentifier, domainType->mIdentifier, domainType->mRevision, memoryAdaptor); } virtual ~ApplicationDomainType() {} @@ -126,6 +127,8 @@ public: virtual void setProperty(const QByteArray &key, const QVariant &value){ mChangeSet.insert(key, value); mAdaptor->setProperty(key, value); } virtual QByteArrayList changedProperties() const { return mChangeSet.keys(); } qint64 revision() const { return mRevision; } + QByteArray resourceInstanceIdentifier() const { return mResourceInstanceIdentifier; } + QByteArray identifier() const { return mIdentifier; } private: QSharedPointer mAdaptor; @@ -133,7 +136,7 @@ private: /* * Each domain object needs to store the resource, identifier, revision triple so we can link back to the storage location. */ - QByteArray mResourceName; + QByteArray mResourceInstanceIdentifier; QByteArray mIdentifier; qint64 mRevision; }; diff --git a/common/domain/event.cpp b/common/domain/event.cpp new file mode 100644 index 0000000..86100b7 --- /dev/null +++ b/common/domain/event.cpp @@ -0,0 +1,53 @@ +/* + * 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 "event.h" + +#include +#include + +#include "../resultset.h" +#include "../index.h" +#include "../storage.h" +#include "../log.h" + +using namespace Akonadi2::ApplicationDomain; + +ResultSet EventImplementation::queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier) +{ + QVector keys; + if (query.propertyFilter.contains("uid")) { + Index uidIndex(Akonadi2::Store::storageLocation(), resourceInstanceIdentifier + "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; + }); + } + return ResultSet(keys); +} + +void EventImplementation::index(const Event &type) +{ + Index uidIndex(Akonadi2::Store::storageLocation(), type.resourceInstanceIdentifier() + "index.uid", Akonadi2::Storage::ReadWrite); + const auto uid = type.getProperty("uid"); + if (uid.isValid()) { + uidIndex.add(uid.toByteArray(), type.identifier()); + } +} diff --git a/common/domain/event.h b/common/domain/event.h new file mode 100644 index 0000000..4cb0d34 --- /dev/null +++ b/common/domain/event.h @@ -0,0 +1,46 @@ +/* + * 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. + */ +#pragma once + +#include "../clientapi.h" + +class ResultSet; +class QByteArray; + +namespace Akonadi2 { + class Query; + +namespace ApplicationDomain { + +/** + * Implements all type-specific code such as updating and querying indexes. + */ +namespace EventImplementation { + typedef Event DomainType; + /** + * Returns the potential result set based on the indexes. + * + * An empty result set indicates that a full scan is required. + */ + ResultSet queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier); + void index(const Event &type); +}; + +} +} diff --git a/common/genericresource.cpp b/common/genericresource.cpp index 4467e86..fdc8b14 100644 --- a/common/genericresource.cpp +++ b/common/genericresource.cpp @@ -153,11 +153,12 @@ private: }; -GenericResource::GenericResource(const QByteArray &resourceIdentifier) +GenericResource::GenericResource(const QByteArray &resourceInstanceIdentifier) : Akonadi2::Resource(), - mUserQueue(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/storage", "org.kde." + resourceIdentifier + ".userqueue"), - mSynchronizerQueue(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/storage", "org.kde." + resourceIdentifier + ".synchronizerqueue"), - mError(0) + mUserQueue(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/storage", "org.kde." + resourceInstanceIdentifier + ".userqueue"), + mSynchronizerQueue(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/storage", "org.kde." + resourceInstanceIdentifier + ".synchronizerqueue"), + mError(0), + mResourceInstanceIdentifier(resourceInstanceIdentifier) { } diff --git a/common/genericresource.h b/common/genericresource.h index ac28575..c44989e 100644 --- a/common/genericresource.h +++ b/common/genericresource.h @@ -34,7 +34,7 @@ namespace Akonadi2 class AKONADI2COMMON_EXPORT GenericResource : public Resource { public: - GenericResource(const QByteArray &resourceIdentifier); + GenericResource(const QByteArray &resourceInstanceIdentifier); virtual ~GenericResource(); virtual void processCommand(int commandId, const QByteArray &data, uint size, Pipeline *pipeline) Q_DECL_OVERRIDE; @@ -50,6 +50,7 @@ protected: flatbuffers::FlatBufferBuilder m_fbb; MessageQueue mUserQueue; MessageQueue mSynchronizerQueue; + QByteArray mResourceInstanceIdentifier; private: Processor *mProcessor; diff --git a/common/resultset.h b/common/resultset.h new file mode 100644 index 0000000..7d7f19a --- /dev/null +++ b/common/resultset.h @@ -0,0 +1,61 @@ +/* + * 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. + */ +#pragma once + +#include + +/* + * 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; + } + + bool isEmpty() + { + mResultSet.isEmpty(); + } + + private: + QVector mResultSet; + QVector::ConstIterator mIt; +}; + -- cgit v1.2.3