diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/index.cpp | 30 | ||||
-rw-r--r-- | common/index.h | 35 |
2 files changed, 65 insertions, 0 deletions
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 @@ | |||
1 | #include "index.h" | ||
2 | #include <QDebug> | ||
3 | |||
4 | Index::Index(const QString &storageRoot, const QString &name, Akonadi2::Storage::AccessMode mode) | ||
5 | : mStorage(storageRoot, name, mode, true) | ||
6 | { | ||
7 | |||
8 | } | ||
9 | |||
10 | void Index::add(const QByteArray &key, const QByteArray &value) | ||
11 | { | ||
12 | mStorage.startTransaction(Akonadi2::Storage::ReadWrite); | ||
13 | mStorage.write(key.data(), key.size(), value.data(), value.size()); | ||
14 | mStorage.commitTransaction(); | ||
15 | } | ||
16 | |||
17 | void Index::lookup(const QByteArray &key, const std::function<void(const QByteArray &value)> &resultHandler, | ||
18 | const std::function<void(const Error &error)> &errorHandler) | ||
19 | { | ||
20 | mStorage.scan(key.data(), key.size(), [this, resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) -> bool { | ||
21 | resultHandler(QByteArray(static_cast<char*>(valuePtr), valueSize)); | ||
22 | return true; | ||
23 | }, | ||
24 | [errorHandler](const Akonadi2::Storage::Error &error) { | ||
25 | qDebug() << "Error while retrieving value" << QString::fromStdString(error.message); | ||
26 | errorHandler(Error(error.store, error.code, error.message)); | ||
27 | } | ||
28 | ); | ||
29 | } | ||
30 | |||
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 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include <string> | ||
4 | #include <functional> | ||
5 | #include <QString> | ||
6 | #include "storage.h" | ||
7 | |||
8 | /** | ||
9 | * An index for value pairs. | ||
10 | */ | ||
11 | class Index | ||
12 | { | ||
13 | public: | ||
14 | class Error | ||
15 | { | ||
16 | public: | ||
17 | Error(const std::string &s, int c, const std::string &m) | ||
18 | : store(s), message(m), code(c) {} | ||
19 | std::string store; | ||
20 | std::string message; | ||
21 | int code; | ||
22 | }; | ||
23 | |||
24 | Index(const QString &storageRoot, const QString &name, Akonadi2::Storage::AccessMode mode = Akonadi2::Storage::ReadOnly); | ||
25 | |||
26 | void add(const QByteArray &key, const QByteArray &value); | ||
27 | // void remove(const QByteArray &key, const QByteArray &value); | ||
28 | |||
29 | void lookup(const QByteArray &key, const std::function<void(const QByteArray &value)> &resultHandler, | ||
30 | const std::function<void(const Error &error)> &errorHandler); | ||
31 | |||
32 | private: | ||
33 | Q_DISABLE_COPY(Index); | ||
34 | Akonadi2::Storage mStorage; | ||
35 | }; | ||