diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-12-15 13:41:29 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-12-15 13:41:29 +0100 |
commit | ef492409892eedb85d7555002eebebe3f046b873 (patch) | |
tree | a5dea6c76fbe3a21641d9ddf91c3ce2c677fd690 | |
parent | 2f504b6f264c90ded6b0226a565301da97d3918f (diff) | |
download | sink-ef492409892eedb85d7555002eebebe3f046b873.tar.gz sink-ef492409892eedb85d7555002eebebe3f046b873.zip |
Avoid duplication accross resource implementations
-rw-r--r-- | common/genericresource.cpp | 6 | ||||
-rw-r--r-- | common/genericresource.h | 3 | ||||
-rw-r--r-- | common/indexupdater.h | 87 | ||||
-rw-r--r-- | examples/dummyresource/resourcefactory.cpp | 101 |
4 files changed, 103 insertions, 94 deletions
diff --git a/common/genericresource.cpp b/common/genericresource.cpp index 3655a63..4250a02 100644 --- a/common/genericresource.cpp +++ b/common/genericresource.cpp | |||
@@ -285,6 +285,12 @@ GenericResource::~GenericResource() | |||
285 | delete mSourceChangeReplay; | 285 | delete mSourceChangeReplay; |
286 | } | 286 | } |
287 | 287 | ||
288 | void GenericResource::addType(const QByteArray &type, DomainTypeAdaptorFactoryInterface::Ptr factory, const QVector<Akonadi2::Preprocessor*> &preprocessors) | ||
289 | { | ||
290 | mPipeline->setPreprocessors(type, preprocessors); | ||
291 | mPipeline->setAdaptorFactory(type, factory); | ||
292 | } | ||
293 | |||
288 | KAsync::Job<void> GenericResource::replay(const QByteArray &type, const QByteArray &key, const QByteArray &value) | 294 | KAsync::Job<void> GenericResource::replay(const QByteArray &type, const QByteArray &key, const QByteArray &value) |
289 | { | 295 | { |
290 | return KAsync::null<void>(); | 296 | return KAsync::null<void>(); |
diff --git a/common/genericresource.h b/common/genericresource.h index f756273..82c8a49 100644 --- a/common/genericresource.h +++ b/common/genericresource.h | |||
@@ -23,6 +23,7 @@ | |||
23 | #include <resource.h> | 23 | #include <resource.h> |
24 | #include <messagequeue.h> | 24 | #include <messagequeue.h> |
25 | #include <flatbuffers/flatbuffers.h> | 25 | #include <flatbuffers/flatbuffers.h> |
26 | #include <domainadaptor.h> | ||
26 | #include <QTimer> | 27 | #include <QTimer> |
27 | 28 | ||
28 | class Processor; | 29 | class Processor; |
@@ -31,6 +32,7 @@ class ChangeReplay; | |||
31 | namespace Akonadi2 | 32 | namespace Akonadi2 |
32 | { | 33 | { |
33 | class Pipeline; | 34 | class Pipeline; |
35 | class Preprocessor; | ||
34 | 36 | ||
35 | /** | 37 | /** |
36 | * Generic Resource implementation. | 38 | * Generic Resource implementation. |
@@ -54,6 +56,7 @@ private Q_SLOTS: | |||
54 | void updateLowerBoundRevision(); | 56 | void updateLowerBoundRevision(); |
55 | 57 | ||
56 | protected: | 58 | protected: |
59 | void addType(const QByteArray &type, DomainTypeAdaptorFactoryInterface::Ptr factory, const QVector<Akonadi2::Preprocessor*> &preprocessors); | ||
57 | virtual KAsync::Job<void> replay(const QByteArray &type, const QByteArray &key, const QByteArray &value); | 60 | virtual KAsync::Job<void> replay(const QByteArray &type, const QByteArray &key, const QByteArray &value); |
58 | void onProcessorError(int errorCode, const QString &errorMessage); | 61 | void onProcessorError(int errorCode, const QString &errorMessage); |
59 | void enqueueCommand(MessageQueue &mq, int commandId, const QByteArray &data); | 62 | void enqueueCommand(MessageQueue &mq, int commandId, const QByteArray &data); |
diff --git a/common/indexupdater.h b/common/indexupdater.h new file mode 100644 index 0000000..48144e6 --- /dev/null +++ b/common/indexupdater.h | |||
@@ -0,0 +1,87 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2015 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | #pragma once | ||
20 | |||
21 | #include <pipeline.h> | ||
22 | #include <index.h> | ||
23 | |||
24 | class IndexUpdater : public Akonadi2::Preprocessor { | ||
25 | public: | ||
26 | IndexUpdater(const QByteArray &index, const QByteArray &type, const QByteArray &property) | ||
27 | :mIndexIdentifier(index), | ||
28 | mBufferType(type), | ||
29 | mProperty(property) | ||
30 | { | ||
31 | |||
32 | } | ||
33 | |||
34 | void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
35 | { | ||
36 | add(newEntity.getProperty(mProperty), uid, transaction); | ||
37 | } | ||
38 | |||
39 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
40 | { | ||
41 | remove(oldEntity.getProperty(mProperty), uid, transaction); | ||
42 | add(newEntity.getProperty(mProperty), uid, transaction); | ||
43 | } | ||
44 | |||
45 | void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
46 | { | ||
47 | remove(oldEntity.getProperty(mProperty), uid, transaction); | ||
48 | } | ||
49 | |||
50 | private: | ||
51 | void add(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) | ||
52 | { | ||
53 | if (value.isValid()) { | ||
54 | Index(mIndexIdentifier, transaction).add(value.toByteArray(), uid); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | void remove(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) | ||
59 | { | ||
60 | //TODO hide notfound error | ||
61 | Index(mIndexIdentifier, transaction).remove(value.toByteArray(), uid); | ||
62 | } | ||
63 | |||
64 | QByteArray mIndexIdentifier; | ||
65 | QByteArray mBufferType; | ||
66 | QByteArray mProperty; | ||
67 | }; | ||
68 | |||
69 | template<typename DomainType> | ||
70 | class DefaultIndexUpdater : public Akonadi2::Preprocessor { | ||
71 | public: | ||
72 | void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
73 | { | ||
74 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction); | ||
75 | } | ||
76 | |||
77 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
78 | { | ||
79 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction); | ||
80 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction); | ||
81 | } | ||
82 | |||
83 | void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
84 | { | ||
85 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction); | ||
86 | } | ||
87 | }; | ||
diff --git a/examples/dummyresource/resourcefactory.cpp b/examples/dummyresource/resourcefactory.cpp index 67e36d1..51000a7 100644 --- a/examples/dummyresource/resourcefactory.cpp +++ b/examples/dummyresource/resourcefactory.cpp | |||
@@ -24,7 +24,6 @@ | |||
24 | #include "pipeline.h" | 24 | #include "pipeline.h" |
25 | #include "dummycalendar_generated.h" | 25 | #include "dummycalendar_generated.h" |
26 | #include "mail_generated.h" | 26 | #include "mail_generated.h" |
27 | #include "queuedcommand_generated.h" | ||
28 | #include "createentity_generated.h" | 27 | #include "createentity_generated.h" |
29 | #include "domainadaptor.h" | 28 | #include "domainadaptor.h" |
30 | #include "commands.h" | 29 | #include "commands.h" |
@@ -35,6 +34,7 @@ | |||
35 | #include "dummystore.h" | 34 | #include "dummystore.h" |
36 | #include "definitions.h" | 35 | #include "definitions.h" |
37 | #include "facadefactory.h" | 36 | #include "facadefactory.h" |
37 | #include "indexupdater.h" | ||
38 | #include <QDate> | 38 | #include <QDate> |
39 | #include <QUuid> | 39 | #include <QUuid> |
40 | 40 | ||
@@ -43,102 +43,15 @@ | |||
43 | #define ENTITY_TYPE_MAIL "mail" | 43 | #define ENTITY_TYPE_MAIL "mail" |
44 | #define ENTITY_TYPE_FOLDER "folder" | 44 | #define ENTITY_TYPE_FOLDER "folder" |
45 | 45 | ||
46 | /** | ||
47 | * Index types: | ||
48 | * * uid - property | ||
49 | * | ||
50 | * * Property can be: | ||
51 | * * fixed value like uid | ||
52 | * * fixed value where we want to do smaller/greater-than comparisons. (like start date) | ||
53 | * * range indexes like what date range an event affects. | ||
54 | * * group indexes like tree hierarchies as nested sets | ||
55 | */ | ||
56 | class IndexUpdater : public Akonadi2::Preprocessor { | ||
57 | public: | ||
58 | IndexUpdater(const QByteArray &index, const QByteArray &type, const QByteArray &property) | ||
59 | :mIndexIdentifier(index), | ||
60 | mBufferType(type), | ||
61 | mProperty(property) | ||
62 | { | ||
63 | |||
64 | } | ||
65 | |||
66 | void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
67 | { | ||
68 | add(newEntity.getProperty(mProperty), uid, transaction); | ||
69 | } | ||
70 | |||
71 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
72 | { | ||
73 | remove(oldEntity.getProperty(mProperty), uid, transaction); | ||
74 | add(newEntity.getProperty(mProperty), uid, transaction); | ||
75 | } | ||
76 | |||
77 | void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
78 | { | ||
79 | remove(oldEntity.getProperty(mProperty), uid, transaction); | ||
80 | } | ||
81 | |||
82 | private: | ||
83 | void add(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) | ||
84 | { | ||
85 | if (value.isValid()) { | ||
86 | Index(mIndexIdentifier, transaction).add(value.toByteArray(), uid); | ||
87 | } | ||
88 | } | ||
89 | |||
90 | void remove(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) | ||
91 | { | ||
92 | //TODO hide notfound error | ||
93 | Index(mIndexIdentifier, transaction).remove(value.toByteArray(), uid); | ||
94 | } | ||
95 | |||
96 | QByteArray mIndexIdentifier; | ||
97 | QByteArray mBufferType; | ||
98 | QByteArray mProperty; | ||
99 | }; | ||
100 | |||
101 | template<typename DomainType> | ||
102 | class DefaultIndexUpdater : public Akonadi2::Preprocessor { | ||
103 | public: | ||
104 | void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
105 | { | ||
106 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction); | ||
107 | } | ||
108 | |||
109 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
110 | { | ||
111 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction); | ||
112 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction); | ||
113 | } | ||
114 | |||
115 | void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
116 | { | ||
117 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction); | ||
118 | } | ||
119 | }; | ||
120 | |||
121 | DummyResource::DummyResource(const QByteArray &instanceIdentifier, const QSharedPointer<Akonadi2::Pipeline> &pipeline) | 46 | DummyResource::DummyResource(const QByteArray &instanceIdentifier, const QSharedPointer<Akonadi2::Pipeline> &pipeline) |
122 | : Akonadi2::GenericResource(instanceIdentifier, pipeline) | 47 | : Akonadi2::GenericResource(instanceIdentifier, pipeline) |
123 | { | 48 | { |
124 | { | 49 | addType(ENTITY_TYPE_MAIL, QSharedPointer<DummyMailAdaptorFactory>::create(), |
125 | QVector<Akonadi2::Preprocessor*> eventPreprocessors; | 50 | QVector<Akonadi2::Preprocessor*>() << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Mail>); |
126 | eventPreprocessors << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Event>; | 51 | addType(ENTITY_TYPE_FOLDER, QSharedPointer<DummyFolderAdaptorFactory>::create(), |
127 | mPipeline->setPreprocessors(ENTITY_TYPE_EVENT, eventPreprocessors); | 52 | QVector<Akonadi2::Preprocessor*>() << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Folder>); |
128 | mPipeline->setAdaptorFactory(ENTITY_TYPE_EVENT, QSharedPointer<DummyEventAdaptorFactory>::create()); | 53 | addType(ENTITY_TYPE_EVENT, QSharedPointer<DummyEventAdaptorFactory>::create(), |
129 | } | 54 | QVector<Akonadi2::Preprocessor*>() << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Event>); |
130 | { | ||
131 | QVector<Akonadi2::Preprocessor*> mailPreprocessors; | ||
132 | mailPreprocessors << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Mail>; | ||
133 | mPipeline->setPreprocessors(ENTITY_TYPE_MAIL, mailPreprocessors); | ||
134 | mPipeline->setAdaptorFactory(ENTITY_TYPE_MAIL, QSharedPointer<DummyMailAdaptorFactory>::create()); | ||
135 | } | ||
136 | { | ||
137 | QVector<Akonadi2::Preprocessor*> folderPreprocessors; | ||
138 | folderPreprocessors << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Folder>; | ||
139 | mPipeline->setPreprocessors(ENTITY_TYPE_FOLDER, folderPreprocessors); | ||
140 | mPipeline->setAdaptorFactory(ENTITY_TYPE_FOLDER, QSharedPointer<DummyFolderAdaptorFactory>::create()); | ||
141 | } | ||
142 | } | 55 | } |
143 | 56 | ||
144 | void DummyResource::createEvent(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb, Akonadi2::Storage::Transaction &transaction) | 57 | void DummyResource::createEvent(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb, Akonadi2::Storage::Transaction &transaction) |