From 9eed6888562440294fdbd0ba4bbdb47553f38d86 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 8 Sep 2015 23:40:34 +0200 Subject: Reduced duplication in dummy synchronization code --- examples/dummyresource/resourcefactory.cpp | 117 +++++++++++------------------ examples/dummyresource/resourcefactory.h | 1 + 2 files changed, 46 insertions(+), 72 deletions(-) (limited to 'examples') diff --git a/examples/dummyresource/resourcefactory.cpp b/examples/dummyresource/resourcefactory.cpp index 9bb20e5..65b3c66 100644 --- a/examples/dummyresource/resourcefactory.cpp +++ b/examples/dummyresource/resourcefactory.cpp @@ -35,6 +35,7 @@ #include "dummystore.h" #include "definitions.h" #include "facadefactory.h" +#include //This is the resources entity type, and not the domain type #define ENTITY_TYPE_EVENT "event" @@ -104,18 +105,17 @@ void DummyResource::createMail(const QByteArray &ridBuffer, const QMap > &data, Akonadi2::Storage::Transaction &transaction, std::function &data, flatbuffers::FlatBufferBuilder &entityFbb)> createEntity) +{ + Index uidIndex("index.uid", transaction); + for (auto it = data.constBegin(); it != data.constEnd(); it++) { + bool isNew = true; + uidIndex.lookup(it.key().toLatin1(), [&](const QByteArray &value) { + isNew = false; + }, + [](const Index::Error &error) { + if (error.code != Index::IndexNotAvailable) { + Warning() << "Error in uid index: " << error.message; + } + }); + if (isNew) { + m_fbb.Clear(); + + flatbuffers::FlatBufferBuilder entityFbb; + createEntity(it.key().toUtf8(), it.value(), entityFbb); + + flatbuffers::FlatBufferBuilder fbb; + //This is the resource type and not the domain type + auto type = fbb.CreateString(bufferType.toStdString()); + auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize()); + auto location = Akonadi2::Commands::CreateCreateEntity(fbb, type, delta); + Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location); + + enqueueCommand(mSynchronizerQueue, Akonadi2::Commands::CreateEntityCommand, QByteArray::fromRawData(reinterpret_cast(fbb.GetBufferPointer()), fbb.GetSize())); + } else { //modification + //TODO diff and create modification if necessary + } + } + //TODO find items to remove +} + KAsync::Job DummyResource::synchronizeWithSource() { return KAsync::start([this](KAsync::Future &f) { auto transaction = Akonadi2::Storage(Akonadi2::storageLocation(), mResourceInstanceIdentifier, Akonadi2::Storage::ReadOnly).createTransaction(Akonadi2::Storage::ReadOnly); - Index uidIndex("index.uid", transaction); - - const auto data = DummyStore::instance().events(); - for (auto it = data.constBegin(); it != data.constEnd(); it++) { - bool isNew = true; - uidIndex.lookup(it.key().toLatin1(), [&](const QByteArray &value) { - isNew = false; - }, - [](const Index::Error &error) { - if (error.code != Index::IndexNotAvailable) { - Warning() << "Error in uid index: " << error.message; - } - }); - if (isNew) { - m_fbb.Clear(); - - flatbuffers::FlatBufferBuilder entityFbb; - createEvent(it.key().toUtf8(), it.value(), entityFbb); - - flatbuffers::FlatBufferBuilder fbb; - //This is the resource type and not the domain type - auto type = fbb.CreateString(ENTITY_TYPE_EVENT); - auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize()); - auto location = Akonadi2::Commands::CreateCreateEntity(fbb, type, delta); - Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location); - - enqueueCommand(mSynchronizerQueue, Akonadi2::Commands::CreateEntityCommand, QByteArray::fromRawData(reinterpret_cast(fbb.GetBufferPointer()), fbb.GetSize())); - } else { //modification - //TODO diff and create modification if necessary - } - } - //TODO find items to remove - - const auto mails = DummyStore::instance().mails(); - for (auto it = mails.constBegin(); it != mails.constEnd(); it++) { - bool isNew = true; - uidIndex.lookup(it.key().toLatin1(), [&](const QByteArray &value) { - isNew = false; - }, - [](const Index::Error &error) { - if (error.code != Index::IndexNotAvailable) { - Warning() << "Error in uid index: " << error.message; - } - }); - if (isNew) { - m_fbb.Clear(); - - flatbuffers::FlatBufferBuilder entityFbb; - createMail(it.key().toUtf8(), it.value(), entityFbb); - - flatbuffers::Verifier verifyer(reinterpret_cast(entityFbb.GetBufferPointer()), entityFbb.GetSize()); - if (!Akonadi2::ApplicationDomain::Buffer::VerifyMailBuffer(verifyer)) { - Warning() << "invalid buffer, not a mail buffer"; - } - - flatbuffers::FlatBufferBuilder fbb; - //This is the resource type and not the domain type - auto type = fbb.CreateString(ENTITY_TYPE_MAIL); - auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize()); - auto location = Akonadi2::Commands::CreateCreateEntity(fbb, type, delta); - Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location); - - enqueueCommand(mSynchronizerQueue, Akonadi2::Commands::CreateEntityCommand, QByteArray::fromRawData(reinterpret_cast(fbb.GetBufferPointer()), fbb.GetSize())); - } else { //modification - //TODO diff and create modification if necessary - } - } - //TODO find items to remove + + synchronize(ENTITY_TYPE_EVENT, DummyStore::instance().events(), transaction, [this](const QByteArray &ridBuffer, const QMap &data, flatbuffers::FlatBufferBuilder &entityFbb) { + createEvent(ridBuffer, data, entityFbb); + }); + synchronize(ENTITY_TYPE_MAIL, DummyStore::instance().mails(), transaction, [this](const QByteArray &ridBuffer, const QMap &data, flatbuffers::FlatBufferBuilder &entityFbb) { + createMail(ridBuffer, data, entityFbb); + }); f.setFinished(); }); diff --git a/examples/dummyresource/resourcefactory.h b/examples/dummyresource/resourcefactory.h index 55d84d3..55cfbd3 100644 --- a/examples/dummyresource/resourcefactory.h +++ b/examples/dummyresource/resourcefactory.h @@ -37,6 +37,7 @@ public: private: void createEvent(const QByteArray &rid, const QMap &data, flatbuffers::FlatBufferBuilder &entityFbb); void createMail(const QByteArray &rid, const QMap &data, flatbuffers::FlatBufferBuilder &entityFbb); + void synchronize(const QString &bufferType, const QMap > &data, Akonadi2::Storage::Transaction &transaction, std::function &data, flatbuffers::FlatBufferBuilder &entityFbb)> createEntity); }; class DummyResourceFactory : public Akonadi2::ResourceFactory -- cgit v1.2.3