summaryrefslogtreecommitdiffstats
path: root/common/index.h
blob: cfcc7a0848055609697996f403051ec0a577de70 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once

#include "sink_export.h"
#include <string>
#include <functional>
#include <QString>
#include "storage.h"
#include "log.h"

/**
 * An index for value pairs.
 */
class SINK_EXPORT Index
{
public:
    enum ErrorCodes
    {
        IndexNotAvailable = -1
    };

    class Error
    {
    public:
        Error(const QByteArray &s, int c, const QByteArray &m) : store(s), message(m), code(c)
        {
        }
        QByteArray store;
        QByteArray message;
        int code;
    };

    Index(const QString &storageRoot, const QString &name, Sink::Storage::DataStore::AccessMode mode = Sink::Storage::DataStore::ReadOnly);
    Index(const QByteArray &name, Sink::Storage::DataStore::Transaction &);

    void add(const QByteArray &key, const QByteArray &value);
    void remove(const QByteArray &key, const QByteArray &value);

    void lookup(const QByteArray &key, const std::function<void(const QByteArray &value)> &resultHandler, const std::function<void(const Error &error)> &errorHandler,
        bool matchSubStringKeys = false);
    QByteArray lookup(const QByteArray &key);

private:
    Q_DISABLE_COPY(Index);
    Sink::Storage::DataStore::Transaction mTransaction;
    Sink::Storage::DataStore::NamedDatabase mDb;
    QString mName;
    SINK_DEBUG_COMPONENT(mName.toLatin1())
};