From b36ae2d0e0b0ee9865fdc7e628374853d160b55a Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 21 Jan 2015 10:52:02 +0100 Subject: An index implementation. --- common/index.cpp | 30 ++++++++++++++++++++++++++ common/index.h | 35 ++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 1 + tests/indextest.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 common/index.cpp create mode 100644 common/index.h create mode 100644 tests/indextest.cpp diff --git a/common/index.cpp b/common/index.cpp new file mode 100644 index 0000000..7b0c682 --- /dev/null +++ b/common/index.cpp @@ -0,0 +1,30 @@ +#include "index.h" +#include + +Index::Index(const QString &storageRoot, const QString &name, Akonadi2::Storage::AccessMode mode) + : mStorage(storageRoot, name, mode, true) +{ + +} + +void Index::add(const QByteArray &key, const QByteArray &value) +{ + mStorage.startTransaction(Akonadi2::Storage::ReadWrite); + mStorage.write(key.data(), key.size(), value.data(), value.size()); + mStorage.commitTransaction(); +} + +void Index::lookup(const QByteArray &key, const std::function &resultHandler, + const std::function &errorHandler) +{ + mStorage.scan(key.data(), key.size(), [this, resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) -> bool { + resultHandler(QByteArray(static_cast(valuePtr), valueSize)); + return true; + }, + [errorHandler](const Akonadi2::Storage::Error &error) { + qDebug() << "Error while retrieving value" << QString::fromStdString(error.message); + errorHandler(Error(error.store, error.code, error.message)); + } + ); +} + diff --git a/common/index.h b/common/index.h new file mode 100644 index 0000000..aea654a --- /dev/null +++ b/common/index.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include +#include "storage.h" + +/** + * An index for value pairs. + */ +class Index +{ +public: + class Error + { + public: + Error(const std::string &s, int c, const std::string &m) + : store(s), message(m), code(c) {} + std::string store; + std::string message; + int code; + }; + + Index(const QString &storageRoot, const QString &name, Akonadi2::Storage::AccessMode mode = Akonadi2::Storage::ReadOnly); + + void add(const QByteArray &key, const QByteArray &value); + // void remove(const QByteArray &key, const QByteArray &value); + + void lookup(const QByteArray &key, const std::function &resultHandler, + const std::function &errorHandler); + +private: + Q_DISABLE_COPY(Index); + Akonadi2::Storage mStorage; +}; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 183b1bf..7ef4113 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -19,6 +19,7 @@ manual_tests ( dummyresourcetest domainadaptortest messagequeuetest + indextest ) target_link_libraries(dummyresourcetest akonadi2_resource_dummy) diff --git a/tests/indextest.cpp b/tests/indextest.cpp new file mode 100644 index 0000000..24f90c8 --- /dev/null +++ b/tests/indextest.cpp @@ -0,0 +1,61 @@ +#include + +#include +#include + +#include "clientapi.h" +#include "storage.h" +#include "index.h" + +class IndexTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void initTestCase() + { + Akonadi2::Storage store(Akonadi2::Store::storageLocation(), "org.kde.dummy.testindex", Akonadi2::Storage::ReadWrite); + store.removeFromDisk(); + } + + void cleanup() + { + Akonadi2::Storage store(Akonadi2::Store::storageLocation(), "org.kde.dummy.testindex", Akonadi2::Storage::ReadWrite); + store.removeFromDisk(); + } + + void testIndex() + { + Index index(Akonadi2::Store::storageLocation(), "org.kde.dummy.testindex", Akonadi2::Storage::ReadWrite); + index.add("key1", "value1"); + index.add("key1", "value2"); + index.add("key2", "value3"); + + { + QList values; + index.lookup(QByteArray("key1"), [&values](const QByteArray &value) { + values << value; + }, + [](const Index::Error &error){ qWarning() << "Error: "; }); + QCOMPARE(values.size(), 2); + } + { + QList values; + index.lookup(QByteArray("key2"), [&values](const QByteArray &value) { + values << value; + }, + [](const Index::Error &error){ qWarning() << "Error: "; }); + QCOMPARE(values.size(), 1); + } + { + QList values; + index.lookup(QByteArray("key3"), [&values](const QByteArray &value) { + values << value; + }, + [](const Index::Error &error){ qWarning() << "Error: "; }); + QCOMPARE(values.size(), 0); + } + } +}; + +QTEST_MAIN(IndexTest) +#include "indextest.moc" -- cgit v1.2.3