summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/facade.h128
-rw-r--r--examples/dummyresource/facade.cpp128
-rw-r--r--examples/dummyresource/facade.h13
3 files changed, 127 insertions, 142 deletions
diff --git a/common/facade.h b/common/facade.h
index d9ec0c9..e32ee96 100644
--- a/common/facade.h
+++ b/common/facade.h
@@ -31,6 +31,8 @@
31#include "domainadaptor.h" 31#include "domainadaptor.h"
32#include "entitybuffer.h" 32#include "entitybuffer.h"
33#include "log.h" 33#include "log.h"
34#include "storage.h"
35#include "resultset.h"
34 36
35/** 37/**
36 * A QueryRunner runs a query and updates the corresponding result set. 38 * A QueryRunner runs a query and updates the corresponding result set.
@@ -107,7 +109,8 @@ public:
107 GenericFacade(const QByteArray &resourceIdentifier, const QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > &adaptorFactory = QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> >()) 109 GenericFacade(const QByteArray &resourceIdentifier, const QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > &adaptorFactory = QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> >())
108 : Akonadi2::StoreFacade<DomainType>(), 110 : Akonadi2::StoreFacade<DomainType>(),
109 mResourceAccess(new ResourceAccess(resourceIdentifier)), 111 mResourceAccess(new ResourceAccess(resourceIdentifier)),
110 mDomainTypeAdaptorFactory(adaptorFactory) 112 mDomainTypeAdaptorFactory(adaptorFactory),
113 mResourceInstanceIdentifier(resourceIdentifier)
111 { 114 {
112 } 115 }
113 116
@@ -214,12 +217,133 @@ protected:
214 return KAsync::null<void>(); 217 return KAsync::null<void>();
215 } 218 }
216 219
217 virtual KAsync::Job<qint64> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > &resultProvider, qint64 oldRevision, qint64 newRevision) { return KAsync::null<qint64>(); }; 220 static void scan(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, std::function<bool(const QByteArray &key, const Akonadi2::Entity &entity)> callback)
221 {
222 storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool {
223 //Skip internals
224 if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) {
225 return true;
226 }
227
228 //Extract buffers
229 Akonadi2::EntityBuffer buffer(dataValue, dataSize);
230
231 //FIXME implement buffer.isValid()
232 // const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<DummyEvent>(buffer.entity().resource());
233 // const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local());
234 // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata());
235
236 // if ((!resourceBuffer && !localBuffer) || !metadataBuffer) {
237 // qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast<char*>(keyValue), keySize);
238 // return true;
239 // }
240 return callback(QByteArray::fromRawData(static_cast<char*>(keyValue), keySize), buffer.entity());
241 },
242 [](const Akonadi2::Storage::Error &error) {
243 qWarning() << "Error during query: " << error.message;
244 });
245 }
246
247 static void readValue(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, const std::function<void(const typename DomainType::Ptr &)> &resultCallback, const QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > &adaptorFactory)
248 {
249 scan(storage, key, [=](const QByteArray &key, const Akonadi2::Entity &entity) {
250 const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(entity.metadata());
251 qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1;
252 //This only works for a 1:1 mapping of resource to domain types.
253 //Not i.e. for tags that are stored as flags in each entity of an imap store.
254 //additional properties that don't have a 1:1 mapping (such as separately stored tags),
255 //could be added to the adaptor
256 auto domainObject = QSharedPointer<DomainType>::create("org.kde.dummy.instance1", key, revision, adaptorFactory->createAdaptor(entity));
257 resultCallback(domainObject);
258 return true;
259 });
260 }
261
262 static ResultSet fullScan(const QSharedPointer<Akonadi2::Storage> &storage)
263 {
264 //TODO use a result set with an iterator, to read values on demand
265 QVector<QByteArray> keys;
266 scan(storage, QByteArray(), [=, &keys](const QByteArray &key, const Akonadi2::Entity &) {
267 keys << key;
268 return true;
269 });
270 return ResultSet(keys);
271 }
272
273 static ResultSet filteredSet(const ResultSet &resultSet, const std::function<bool(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> &filter, const QSharedPointer<Akonadi2::Storage> &storage, const QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > &adaptorFactory)
274 {
275 auto resultSetPtr = QSharedPointer<ResultSet>::create(resultSet);
276
277 //Read through the source values and return whatever matches the filter
278 std::function<bool(std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)>)> generator = [resultSetPtr, storage, adaptorFactory, filter](std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)> callback) -> bool {
279 while (resultSetPtr->next()) {
280 readValue(storage, resultSetPtr->id(), [filter, callback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) {
281 if (filter(domainObject)) {
282 callback(domainObject);
283 }
284 }, adaptorFactory);
285 }
286 return false;
287 };
288 return ResultSet(generator);
289 }
218 290
291 static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage, const QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > &adaptorFactory, const QByteArray &resourceInstanceIdentifier)
292 {
293 QSet<QByteArray> appliedFilters;
294 ResultSet resultSet = Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::queryIndexes(query, resourceInstanceIdentifier, appliedFilters);
295 const auto remainingFilters = query.propertyFilter.keys().toSet() - appliedFilters;
296
297 //We do a full scan if there were no indexes available to create the initial set.
298 if (appliedFilters.isEmpty()) {
299 resultSet = fullScan(storage);
300 }
301
302 auto filter = [remainingFilters, query](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) -> bool {
303 for (const auto &filterProperty : remainingFilters) {
304 //TODO implement other comparison operators than equality
305 if (domainObject->getProperty(filterProperty) != query.propertyFilter.value(filterProperty)) {
306 return false;
307 }
308 }
309 return true;
310 };
311
312 return filteredSet(resultSet, filter, storage, adaptorFactory);
313 }
314
315 virtual KAsync::Job<qint64> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider, qint64 oldRevision, qint64 newRevision)
316 {
317 return KAsync::start<qint64>([=]() -> qint64 {
318 auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier);
319 storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) {
320 Warning() << "Error during query: " << error.store << error.message;
321 });
322
323 storage->startTransaction(Akonadi2::Storage::ReadOnly);
324 //TODO start transaction on indexes as well
325 const qint64 revision = storage->maxRevision();
326
327 auto resultSet = getResultSet(query, storage, mDomainTypeAdaptorFactory, mResourceInstanceIdentifier);
328
329 // TODO only emit changes and don't replace everything
330 resultProvider->clear();
331 auto resultCallback = std::bind(&Akonadi2::ResultProvider<typename DomainType::Ptr>::add, resultProvider, std::placeholders::_1);
332 while(resultSet.next([resultCallback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value) -> bool {
333 resultCallback(Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation<DomainType>(value));
334 return true;
335 })){};
336 storage->abortTransaction();
337 return revision;
338 });
339 }
340
341private:
219protected: 342protected:
220 //TODO use one resource access instance per application => make static 343 //TODO use one resource access instance per application => make static
221 QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess; 344 QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess;
222 QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > mDomainTypeAdaptorFactory; 345 QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > mDomainTypeAdaptorFactory;
346 QByteArray mResourceInstanceIdentifier;
223}; 347};
224 348
225} 349}
diff --git a/examples/dummyresource/facade.cpp b/examples/dummyresource/facade.cpp
index 002b836..d6e9286 100644
--- a/examples/dummyresource/facade.cpp
+++ b/examples/dummyresource/facade.cpp
@@ -19,25 +19,7 @@
19 19
20#include "facade.h" 20#include "facade.h"
21 21
22#include <QDebug>
23#include <functional>
24
25#include "common/resourceaccess.h"
26#include "common/commands.h"
27#include "common/resultset.h"
28#include "common/domain/event.h"
29#include "dummycalendar_generated.h"
30#include "event_generated.h"
31#include "entity_generated.h"
32#include "metadata_generated.h"
33#include "domainadaptor.h" 22#include "domainadaptor.h"
34#include <common/entitybuffer.h>
35#include <common/index.h>
36#include <common/log.h>
37
38using namespace DummyCalendar;
39using namespace flatbuffers;
40
41 23
42DummyResourceFacade::DummyResourceFacade() 24DummyResourceFacade::DummyResourceFacade()
43 : Akonadi2::GenericFacade<Akonadi2::ApplicationDomain::Event>("org.kde.dummy.instance1", QSharedPointer<DummyEventAdaptorFactory>::create()) 25 : Akonadi2::GenericFacade<Akonadi2::ApplicationDomain::Event>("org.kde.dummy.instance1", QSharedPointer<DummyEventAdaptorFactory>::create())
@@ -48,113 +30,3 @@ DummyResourceFacade::~DummyResourceFacade()
48{ 30{
49} 31}
50 32
51static void scan(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, std::function<bool(const QByteArray &key, const Akonadi2::Entity &entity, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local, Akonadi2::Metadata const *metadata)> callback)
52{
53 storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool {
54 //Skip internals
55 if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) {
56 return true;
57 }
58
59 //Extract buffers
60 Akonadi2::EntityBuffer buffer(dataValue, dataSize);
61
62 const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<DummyEvent>(buffer.entity().resource());
63 const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local());
64 const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata());
65
66 if ((!resourceBuffer && !localBuffer) || !metadataBuffer) {
67 qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast<char*>(keyValue), keySize);
68 return true;
69 }
70 return callback(QByteArray::fromRawData(static_cast<char*>(keyValue), keySize), buffer.entity(), resourceBuffer, localBuffer, metadataBuffer);
71 },
72 [](const Akonadi2::Storage::Error &error) {
73 qWarning() << "Error during query: " << error.message;
74 });
75}
76
77static void readValue(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback, const QSharedPointer<DomainTypeAdaptorFactoryInterface<Akonadi2::ApplicationDomain::Event> > &adaptorFactory)
78{
79 scan(storage, key, [=](const QByteArray &key, const Akonadi2::Entity &entity, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local, Akonadi2::Metadata const *metadataBuffer) {
80 qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1;
81 //This only works for a 1:1 mapping of resource to domain types.
82 //Not i.e. for tags that are stored as flags in each entity of an imap store.
83 //additional properties that don't have a 1:1 mapping (such as separately stored tags),
84 //could be added to the adaptor
85 auto event = QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("org.kde.dummy.instance1", key, revision, adaptorFactory->createAdaptor(entity));
86 resultCallback(event);
87 return true;
88 });
89}
90
91static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage, const QSharedPointer<DomainTypeAdaptorFactoryInterface<Akonadi2::ApplicationDomain::Event> > &adaptorFactory)
92{
93 QSet<QByteArray> appliedFilters;
94 ResultSet resultSet = Akonadi2::ApplicationDomain::TypeImplementation<Akonadi2::ApplicationDomain::Event>::queryIndexes(query, "org.kde.dummy.instance1", appliedFilters);
95 const auto remainingFilters = query.propertyFilter.keys().toSet() - appliedFilters;
96
97 //We do a full scan if there were no indexes available to create the initial set.
98 //TODO use a result set with an iterator, to read values on demand
99 if (appliedFilters.isEmpty()) {
100 QVector<QByteArray> keys;
101 scan(storage, QByteArray(), [=, &keys](const QByteArray &key, const Akonadi2::Entity &entity, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local, Akonadi2::Metadata const *metadataBuffer) {
102 keys << key;
103 return true;
104 });
105 resultSet = ResultSet(keys);
106 }
107
108 auto filter = [remainingFilters, query](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &event) -> bool {
109 for (const auto &filterProperty : remainingFilters) {
110 //TODO implement other comparison operators than equality
111 if (event->getProperty(filterProperty) != query.propertyFilter.value(filterProperty)) {
112 return false;
113 }
114 }
115 return true;
116 };
117
118 auto resultSetPtr = QSharedPointer<ResultSet>::create(resultSet);
119
120 //Read through the source values and return whatever matches the filter
121 std::function<bool(std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)>)> generator = [resultSetPtr, storage, adaptorFactory, filter](std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)> callback) -> bool {
122 while (resultSetPtr->next()) {
123 Akonadi2::ApplicationDomain::Event::Ptr event;
124 readValue(storage, resultSetPtr->id(), [filter, callback](const Akonadi2::ApplicationDomain::Event::Ptr &event) {
125 if (filter(event)) {
126 callback(event);
127 }
128 }, adaptorFactory);
129 }
130 return false;
131 };
132 return ResultSet(generator);
133}
134
135//TODO generalize
136KAsync::Job<qint64> DummyResourceFacade::load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > &resultProvider, qint64 oldRevision, qint64 newRevision)
137{
138 return KAsync::start<qint64>([=]() {
139 auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), "org.kde.dummy.instance1");
140 storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) {
141 Warning() << "Error during query: " << error.store << error.message;
142 });
143
144 storage->startTransaction(Akonadi2::Storage::ReadOnly);
145 //TODO start transaction on indexes as well
146 const qint64 revision = storage->maxRevision();
147
148 auto resultSet = getResultSet(query, storage, mDomainTypeAdaptorFactory);
149
150 // TODO only emit changes and don't replace everything
151 resultProvider->clear();
152 auto resultCallback = std::bind(&Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr>::add, resultProvider, std::placeholders::_1);
153 while(resultSet.next([resultCallback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value) -> bool {
154 resultCallback(Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation<Akonadi2::ApplicationDomain::Event>(value));
155 })){};
156 storage->abortTransaction();
157 return revision;
158 });
159}
160
diff --git a/examples/dummyresource/facade.h b/examples/dummyresource/facade.h
index 7c894f1..441dc38 100644
--- a/examples/dummyresource/facade.h
+++ b/examples/dummyresource/facade.h
@@ -20,22 +20,11 @@
20#pragma once 20#pragma once
21 21
22#include "common/facade.h" 22#include "common/facade.h"
23 23#include "common/domain/event.h"
24#include "common/clientapi.h"
25#include "common/storage.h"
26#include "resourcefactory.h"
27#include "entity_generated.h"
28#include "event_generated.h"
29#include "dummycalendar_generated.h"
30#include "common/domainadaptor.h"
31 24
32class DummyResourceFacade : public Akonadi2::GenericFacade<Akonadi2::ApplicationDomain::Event> 25class DummyResourceFacade : public Akonadi2::GenericFacade<Akonadi2::ApplicationDomain::Event>
33{ 26{
34public: 27public:
35 DummyResourceFacade(); 28 DummyResourceFacade();
36 virtual ~DummyResourceFacade(); 29 virtual ~DummyResourceFacade();
37 KAsync::Job<qint64> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > &resultProvider, qint64 oldRevision, qint64 newRevision) Q_DECL_OVERRIDE;
38
39private:
40 void readValue(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback);
41}; 30};