summaryrefslogtreecommitdiffstats
path: root/common/index.cpp
blob: 7e3c09e6aa8a7dbb529b663737daabfe99ce0ca9 (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
#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.createTransaction(Akonadi2::Storage::ReadWrite).write(key, value);
}

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.createTransaction(Akonadi2::Storage::ReadOnly).scan(key, [this, resultHandler](const QByteArray &key, const QByteArray &value) -> bool {
        resultHandler(value);
        return true;
    },
    [errorHandler](const Akonadi2::Storage::Error &error) {
        qDebug() << "Error while retrieving value" << error.message;
        errorHandler(Error(error.store, error.code, error.message));
    }
    );
}