summaryrefslogtreecommitdiffstats
path: root/store/database.cpp
blob: 542667a499f845afa515da00927bf5e67d3c99ce (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "database.h"

#include <iostream>

#include <QAtomicInt>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QReadWriteLock>
#include <QString>
#include <QTime>

#include <kchashdb.h>

class Database::Private
{
public:
    Private(const QString &storageRoot, const QString &name);
    ~Private();

    kyotocabinet::TreeDB db;
    bool dbOpen;
    bool inTransaction;
};

Database::Private::Private(const QString &storageRoot, const QString &name)
    : inTransaction(false)
{
    QDir dir;
    dir.mkdir(storageRoot);

    //create file
    dbOpen = db.open((storageRoot + "/" + name + ".kch").toStdString(), kyotocabinet::BasicDB::OWRITER | kyotocabinet::BasicDB::OCREATE);
    if (!dbOpen) {
        // TODO: handle error
    }
}

Database::Private::~Private()
{
    if (dbOpen && inTransaction) {
        db.end_transaction(false);
    }
}

Database::Database(const QString &storageRoot, const QString &name)
    : d(new Private(storageRoot, name))
{
}

Database::~Database()
{
    delete d;
}

bool Database::isInTransaction() const
{
    return d->inTransaction;
}

bool Database::startTransaction(TransactionType type)
{
    if (!d->dbOpen) {
        return false;
    }

    if (d->inTransaction) {
        return true;
    }

    //TODO handle errors
    d->inTransaction = d->db.begin_transaction();
    return d->inTransaction;
}

bool Database::commitTransaction()
{
    if (!d->dbOpen) {
        return false;
    }

    if (!d->inTransaction) {
        return false;
    }

    bool success = d->db.end_transaction(true);
    d->inTransaction = false;
    return success;
}

void Database::abortTransaction()
{
    if (!d->dbOpen || !d->inTransaction) {
        return;
    }

    d->db.end_transaction(false);
    d->inTransaction = false;
}

bool Database::write(const char *key, size_t keySize, const char *value, size_t valueSize)
{
    if (!d->dbOpen) {
        return false;
    }

    bool success = d->db.set(key, keySize, value, valueSize);
    return success; 
}

bool Database::write(const std::string &sKey, const std::string &sValue)
{
    if (!d->dbOpen) {
        return false;
    }

    bool success = d->db.set(sKey, sValue);
    return success; 
}

void Database::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler)
{
    if (!d->dbOpen) {
        return;
    }

    std::string value;
    if (d->db.get(sKey, &value)) {
        resultHandler(value);
    }
}

void Database::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler)
{
    if (!d->dbOpen) {
        return;
    }

    size_t valueSize;
    char *valueBuffer = d->db.get(sKey.data(), sKey.size(), &valueSize);
    resultHandler(valueBuffer, valueSize);
    delete[] valueBuffer;
}

qint64 Database::diskUsage() const
{
    if (!d->dbOpen) {
        return 0;
    }

    QFileInfo info(QString::fromStdString(d->db.path()));
    return info.size();
}

void Database::removeFromDisk() const
{
    if (!d->dbOpen) {
        return;
    }

   QFileInfo info(QString::fromStdString(d->db.path()));
   QDir dir = info.dir();
   dir.remove(info.fileName());
}