diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-07-09 23:30:36 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-07-09 23:30:36 +0200 |
commit | c5e4f2965ee31b1966971aa6fdcad38db393014d (patch) | |
tree | 3f61a8f4b667e1c5d07422a142f355820b9b8018 | |
parent | 6fce18743841a03c82384705d70e0a19dac7cfe9 (diff) | |
download | sink-c5e4f2965ee31b1966971aa6fdcad38db393014d.tar.gz sink-c5e4f2965ee31b1966971aa6fdcad38db393014d.zip |
Use remoteId index instead of scan
-rw-r--r-- | examples/dummyresource/domainadaptor.cpp | 7 | ||||
-rw-r--r-- | examples/dummyresource/resourcefactory.cpp | 39 |
2 files changed, 21 insertions, 25 deletions
diff --git a/examples/dummyresource/domainadaptor.cpp b/examples/dummyresource/domainadaptor.cpp index 67595af..cb54586 100644 --- a/examples/dummyresource/domainadaptor.cpp +++ b/examples/dummyresource/domainadaptor.cpp | |||
@@ -31,10 +31,17 @@ DummyEventAdaptorFactory::DummyEventAdaptorFactory() | |||
31 | mResourceMapper->addMapping("summary", [](DummyEvent const *buffer) -> QVariant { | 31 | mResourceMapper->addMapping("summary", [](DummyEvent const *buffer) -> QVariant { |
32 | return propertyToVariant<QString>(buffer->summary()); | 32 | return propertyToVariant<QString>(buffer->summary()); |
33 | }); | 33 | }); |
34 | mResourceMapper->addMapping("remoteId", [](DummyEvent const *buffer) -> QVariant { | ||
35 | return propertyToVariant<QString>(buffer->remoteId()); | ||
36 | }); | ||
34 | 37 | ||
35 | mResourceWriteMapper->addMapping("summary", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(DummyEventBuilder &)> { | 38 | mResourceWriteMapper->addMapping("summary", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(DummyEventBuilder &)> { |
36 | auto offset = variantToProperty<QString>(value, fbb); | 39 | auto offset = variantToProperty<QString>(value, fbb); |
37 | return [offset](DummyEventBuilder &builder) { builder.add_summary(offset); }; | 40 | return [offset](DummyEventBuilder &builder) { builder.add_summary(offset); }; |
38 | }); | 41 | }); |
42 | mResourceWriteMapper->addMapping("remoteId", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(DummyEventBuilder &)> { | ||
43 | auto offset = variantToProperty<QString>(value, fbb); | ||
44 | return [offset](DummyEventBuilder &builder) { builder.add_remoteId(offset); }; | ||
45 | }); | ||
39 | } | 46 | } |
40 | 47 | ||
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 | ||
59 | void 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 | |||
83 | KAsync::Job<void> DummyResource::synchronizeWithSource(Akonadi2::Pipeline *pipeline) | 66 | KAsync::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) { |