summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/index.cpp30
-rw-r--r--common/index.h35
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/indextest.cpp61
4 files changed, 127 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
4Index::Index(const QString &storageRoot, const QString &name, Akonadi2::Storage::AccessMode mode)
5 : mStorage(storageRoot, name, mode, true)
6{
7
8}
9
10void 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
17void 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 */
11class Index
12{
13public:
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
32private:
33 Q_DISABLE_COPY(Index);
34 Akonadi2::Storage mStorage;
35};
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 (
19 dummyresourcetest 19 dummyresourcetest
20 domainadaptortest 20 domainadaptortest
21 messagequeuetest 21 messagequeuetest
22 indextest
22) 23)
23 24
24target_link_libraries(dummyresourcetest akonadi2_resource_dummy) 25target_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 @@
1#include <QtTest>
2
3#include <QString>
4#include <QQueue>
5
6#include "clientapi.h"
7#include "storage.h"
8#include "index.h"
9
10class IndexTest : public QObject
11{
12 Q_OBJECT
13private Q_SLOTS:
14 void initTestCase()
15 {
16 Akonadi2::Storage store(Akonadi2::Store::storageLocation(), "org.kde.dummy.testindex", Akonadi2::Storage::ReadWrite);
17 store.removeFromDisk();
18 }
19
20 void cleanup()
21 {
22 Akonadi2::Storage store(Akonadi2::Store::storageLocation(), "org.kde.dummy.testindex", Akonadi2::Storage::ReadWrite);
23 store.removeFromDisk();
24 }
25
26 void testIndex()
27 {
28 Index index(Akonadi2::Store::storageLocation(), "org.kde.dummy.testindex", Akonadi2::Storage::ReadWrite);
29 index.add("key1", "value1");
30 index.add("key1", "value2");
31 index.add("key2", "value3");
32
33 {
34 QList<QByteArray> values;
35 index.lookup(QByteArray("key1"), [&values](const QByteArray &value) {
36 values << value;
37 },
38 [](const Index::Error &error){ qWarning() << "Error: "; });
39 QCOMPARE(values.size(), 2);
40 }
41 {
42 QList<QByteArray> values;
43 index.lookup(QByteArray("key2"), [&values](const QByteArray &value) {
44 values << value;
45 },
46 [](const Index::Error &error){ qWarning() << "Error: "; });
47 QCOMPARE(values.size(), 1);
48 }
49 {
50 QList<QByteArray> values;
51 index.lookup(QByteArray("key3"), [&values](const QByteArray &value) {
52 values << value;
53 },
54 [](const Index::Error &error){ qWarning() << "Error: "; });
55 QCOMPARE(values.size(), 0);
56 }
57 }
58};
59
60QTEST_MAIN(IndexTest)
61#include "indextest.moc"