summaryrefslogtreecommitdiffstats
path: root/store/database.h
blob: ab398a4d47b7243b564414f958989ae778221c56 (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
#pragma once

#include <lmdb.h>
#include <string>
#include <QString>

class Database {
public:
    Database(const QString &path);
    ~Database();
    MDB_txn *startTransaction();
    void endTransaction(MDB_txn *transaction);
    void write(const std::string &sKey, const std::string &sValue, MDB_txn *transaction);
    //Perhaps prefer iterators (assuming we need to be able to match multiple values
    void read(const std::string &sKey, const std::function<void(const std::string)> &);

private:
    MDB_env *env;
    MDB_dbi dbi;
};

/*
 * This opens the db for a single read transaction.
 *
 * The lifetime of all read values is tied to this transaction.
 */
class ReadTransaction {
public:
    ReadTransaction(const QString &path);
    ~ReadTransaction();

    void read(const std::string &sKey, const std::function<void(void *ptr, int size)> &);

private:
    MDB_env *env;
    MDB_dbi dbi;
    MDB_txn *txn;
};