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 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 common/index.cpp create mode 100644 common/index.h (limited to 'common') 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; +}; -- cgit v1.2.3