summaryrefslogtreecommitdiffstats
path: root/common/index.cpp
blob: a8de69730d5a9647fd6d9806e18151308d0d6a1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "index.h"
#include <QDebug>

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<void(const QByteArray &value)> &resultHandler,
                                          const std::function<void(const Error &error)> &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<char*>(valuePtr), valueSize));
        return true;
    },
    [errorHandler](const Akonadi2::Storage::Error &error) {
        qDebug() << "Error while retrieving value" << error.message;
        errorHandler(Error(error.store, error.code, error.message));
    }
    );
}