From c5e4f2965ee31b1966971aa6fdcad38db393014d Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 9 Jul 2015 23:30:36 +0200 Subject: Use remoteId index instead of scan --- examples/dummyresource/domainadaptor.cpp | 7 ++++++ 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() mResourceMapper->addMapping("summary", [](DummyEvent const *buffer) -> QVariant { return propertyToVariant(buffer->summary()); }); + mResourceMapper->addMapping("remoteId", [](DummyEvent const *buffer) -> QVariant { + return propertyToVariant(buffer->remoteId()); + }); mResourceWriteMapper->addMapping("summary", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function { auto offset = variantToProperty(value, fbb); return [offset](DummyEventBuilder &builder) { builder.add_summary(offset); }; }); + mResourceWriteMapper->addMapping("remoteId", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function { + auto offset = variantToProperty(value, fbb); + return [offset](DummyEventBuilder &builder) { builder.add_remoteId(offset); }; + }); } 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) //FIXME set revision? Akonadi2::ApplicationDomain::Event event(resourceIdentifier, state.key(), -1, adaptor); Akonadi2::ApplicationDomain::TypeImplementation::index(event); + + Index ridIndex(Akonadi2::Store::storageLocation(), resourceIdentifier + ".index.rid", Akonadi2::Storage::ReadWrite); + const auto rid = event.getProperty("remoteId"); + if (rid.isValid()) { + ridIndex.add(rid.toByteArray(), event.identifier()); + } }); //event is the entitytype and not the domain type pipeline->setPreprocessors("event", Akonadi2::Pipeline::NewPipeline, QVector() << eventIndexer); + //TODO cleanup indexes during removal GenericResource::configurePipeline(pipeline); } -void findByRemoteId(QSharedPointer storage, const QString &rid, std::function callback) -{ - //TODO lookup in rid index instead of doing a full scan - const std::string ridString = rid.toStdString(); - storage->scan("", [&](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { - if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) { - return true; - } - - Akonadi2::EntityBuffer::extractResourceBuffer(dataValue, dataSize, [&](const uint8_t *buffer, size_t size) { - flatbuffers::Verifier verifier(buffer, size); - if (DummyCalendar::VerifyDummyEventBuffer(verifier)) { - DummyCalendar::DummyEvent const *resourceBuffer = DummyCalendar::GetDummyEvent(buffer); - if (resourceBuffer && resourceBuffer->remoteId()) { - if (std::string(resourceBuffer->remoteId()->c_str(), resourceBuffer->remoteId()->size()) == ridString) { - callback(keyValue, keySize, dataValue, dataSize); - } - } - } - }); - return true; - }); -} - KAsync::Job DummyResource::synchronizeWithSource(Akonadi2::Pipeline *pipeline) { return KAsync::start([this, pipeline](KAsync::Future &f) { //TODO use a read-only transaction during the complete sync to sync against a defined revision auto storage = QSharedPointer::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier); + + Index uidIndex(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier + ".index.uid", Akonadi2::Storage::ReadOnly); + const auto data = DummyStore::instance().data(); for (auto it = data.constBegin(); it != data.constEnd(); it++) { bool isNew = true; if (storage->exists()) { - findByRemoteId(storage, it.key(), [&](void *keyValue, int keySize, void *dataValue, int dataSize) { + uidIndex.lookup(it.key().toLatin1(), [&](const QByteArray &value) { isNew = false; + }, + [](const Index::Error &error) { + Warning() << "Error in index: " << error.message; }); } if (isNew) { -- cgit v1.2.3