From 166aa563ad41c4566c02cff583df612e328d1520 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 20 Jul 2015 20:42:52 +0200 Subject: Deal with non-existing index --- common/index.cpp | 4 ++++ common/index.h | 4 ++++ examples/dummyresource/resourcefactory.cpp | 21 +++++++++------------ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/common/index.cpp b/common/index.cpp index 9bb467b..a8de697 100644 --- a/common/index.cpp +++ b/common/index.cpp @@ -17,6 +17,10 @@ void Index::add(const QByteArray &key, const QByteArray &value) void Index::lookup(const QByteArray &key, const std::function &resultHandler, const std::function &errorHandler) { + if (!mStorage.exists()) { + errorHandler(Error("index", IndexNotAvailable, "Index not existing")); + return; + } mStorage.scan(key, [this, resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) -> bool { resultHandler(QByteArray(static_cast(valuePtr), valueSize)); return true; diff --git a/common/index.h b/common/index.h index 3cd7cc8..08b499f 100644 --- a/common/index.h +++ b/common/index.h @@ -11,6 +11,10 @@ class Index { public: + enum ErrorCodes { + IndexNotAvailable = -1 + }; + class Error { public: diff --git a/examples/dummyresource/resourcefactory.cpp b/examples/dummyresource/resourcefactory.cpp index b9123d5..c7a3eef 100644 --- a/examples/dummyresource/resourcefactory.cpp +++ b/examples/dummyresource/resourcefactory.cpp @@ -66,22 +66,20 @@ void DummyResource::configurePipeline(Akonadi2::Pipeline *pipeline) KAsync::Job DummyResource::synchronizeWithSource(Akonadi2::Pipeline *pipeline) { return KAsync::start([this, pipeline](KAsync::Future &f) { - auto storage = QSharedPointer::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier); - storage->startTransaction(Akonadi2::Storage::ReadOnly); - + //TODO start transaction on index 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()) { - uidIndex.lookup(it.key().toLatin1(), [&](const QByteArray &value) { - isNew = false; - }, - [](const Index::Error &error) { - Warning() << "Error in index: " << error.message; - }); - } + 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(); @@ -117,7 +115,6 @@ KAsync::Job DummyResource::synchronizeWithSource(Akonadi2::Pipeline *pipel //TODO diff and create modification if necessary } } - storage->abortTransaction(); //TODO find items to remove f.setFinished(); }); -- cgit v1.2.3