summaryrefslogtreecommitdiffstats
path: root/examples/dummyresource/resourcefactory.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-07-09 23:30:36 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-07-09 23:30:36 +0200
commitc5e4f2965ee31b1966971aa6fdcad38db393014d (patch)
tree3f61a8f4b667e1c5d07422a142f355820b9b8018 /examples/dummyresource/resourcefactory.cpp
parent6fce18743841a03c82384705d70e0a19dac7cfe9 (diff)
downloadsink-c5e4f2965ee31b1966971aa6fdcad38db393014d.tar.gz
sink-c5e4f2965ee31b1966971aa6fdcad38db393014d.zip
Use remoteId index instead of scan
Diffstat (limited to 'examples/dummyresource/resourcefactory.cpp')
-rw-r--r--examples/dummyresource/resourcefactory.cpp39
1 files changed, 14 insertions, 25 deletions
diff --git a/examples/dummyresource/resourcefactory.cpp b/examples/dummyresource/resourcefactory.cpp
index c746130..e61881e 100644
--- a/examples/dummyresource/resourcefactory.cpp
+++ b/examples/dummyresource/resourcefactory.cpp
@@ -49,48 +49,37 @@ void DummyResource::configurePipeline(Akonadi2::Pipeline *pipeline)
49 //FIXME set revision? 49 //FIXME set revision?
50 Akonadi2::ApplicationDomain::Event event(resourceIdentifier, state.key(), -1, adaptor); 50 Akonadi2::ApplicationDomain::Event event(resourceIdentifier, state.key(), -1, adaptor);
51 Akonadi2::ApplicationDomain::TypeImplementation<Akonadi2::ApplicationDomain::Event>::index(event); 51 Akonadi2::ApplicationDomain::TypeImplementation<Akonadi2::ApplicationDomain::Event>::index(event);
52
53 Index ridIndex(Akonadi2::Store::storageLocation(), resourceIdentifier + ".index.rid", Akonadi2::Storage::ReadWrite);
54 const auto rid = event.getProperty("remoteId");
55 if (rid.isValid()) {
56 ridIndex.add(rid.toByteArray(), event.identifier());
57 }
52 }); 58 });
53 59
54 //event is the entitytype and not the domain type 60 //event is the entitytype and not the domain type
55 pipeline->setPreprocessors("event", Akonadi2::Pipeline::NewPipeline, QVector<Akonadi2::Preprocessor*>() << eventIndexer); 61 pipeline->setPreprocessors("event", Akonadi2::Pipeline::NewPipeline, QVector<Akonadi2::Preprocessor*>() << eventIndexer);
62 //TODO cleanup indexes during removal
56 GenericResource::configurePipeline(pipeline); 63 GenericResource::configurePipeline(pipeline);
57} 64}
58 65
59void findByRemoteId(QSharedPointer<Akonadi2::Storage> storage, const QString &rid, std::function<void(void *keyValue, int keySize, void *dataValue, int dataSize)> callback)
60{
61 //TODO lookup in rid index instead of doing a full scan
62 const std::string ridString = rid.toStdString();
63 storage->scan("", [&](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool {
64 if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) {
65 return true;
66 }
67
68 Akonadi2::EntityBuffer::extractResourceBuffer(dataValue, dataSize, [&](const uint8_t *buffer, size_t size) {
69 flatbuffers::Verifier verifier(buffer, size);
70 if (DummyCalendar::VerifyDummyEventBuffer(verifier)) {
71 DummyCalendar::DummyEvent const *resourceBuffer = DummyCalendar::GetDummyEvent(buffer);
72 if (resourceBuffer && resourceBuffer->remoteId()) {
73 if (std::string(resourceBuffer->remoteId()->c_str(), resourceBuffer->remoteId()->size()) == ridString) {
74 callback(keyValue, keySize, dataValue, dataSize);
75 }
76 }
77 }
78 });
79 return true;
80 });
81}
82
83KAsync::Job<void> DummyResource::synchronizeWithSource(Akonadi2::Pipeline *pipeline) 66KAsync::Job<void> DummyResource::synchronizeWithSource(Akonadi2::Pipeline *pipeline)
84{ 67{
85 return KAsync::start<void>([this, pipeline](KAsync::Future<void> &f) { 68 return KAsync::start<void>([this, pipeline](KAsync::Future<void> &f) {
86 //TODO use a read-only transaction during the complete sync to sync against a defined revision 69 //TODO use a read-only transaction during the complete sync to sync against a defined revision
87 auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier); 70 auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier);
71
72 Index uidIndex(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier + ".index.uid", Akonadi2::Storage::ReadOnly);
73
88 const auto data = DummyStore::instance().data(); 74 const auto data = DummyStore::instance().data();
89 for (auto it = data.constBegin(); it != data.constEnd(); it++) { 75 for (auto it = data.constBegin(); it != data.constEnd(); it++) {
90 bool isNew = true; 76 bool isNew = true;
91 if (storage->exists()) { 77 if (storage->exists()) {
92 findByRemoteId(storage, it.key(), [&](void *keyValue, int keySize, void *dataValue, int dataSize) { 78 uidIndex.lookup(it.key().toLatin1(), [&](const QByteArray &value) {
93 isNew = false; 79 isNew = false;
80 },
81 [](const Index::Error &error) {
82 Warning() << "Error in index: " << error.message;
94 }); 83 });
95 } 84 }
96 if (isNew) { 85 if (isNew) {