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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
#include "storage.h"
#include <iostream>
#include <QAtomicInt>
#include <QDebug>
#include <QDir>
#include <QReadWriteLock>
#include <QString>
#include <QTime>
#include <lmdb.h>
class Storage::Private
{
public:
Private(const QString &s, const QString &name, AccessMode m);
~Private();
QString storageRoot;
QString name;
MDB_dbi dbi;
MDB_env *env;
MDB_txn *transaction;
AccessMode mode;
bool readTransaction;
bool firstOpen;
};
Storage::Private::Private(const QString &s, const QString &n, AccessMode m)
: storageRoot(s),
name(n),
transaction(0),
mode(m),
readTransaction(false),
firstOpen(true)
{
QDir dir;
dir.mkdir(storageRoot);
//create file
if (mdb_env_create(&env)) {
// TODO: handle error
} else {
int rc = mdb_env_open(env, storageRoot.toStdString().data(), 0, 0664);
if (rc) {
std::cerr << "mdb_env_open: " << rc << " " << mdb_strerror(rc) << std::endl;
mdb_env_close(env);
env = 0;
} else {
const size_t dbSize = 10485760 * 100; //10MB * 100
mdb_env_set_mapsize(env, dbSize);
}
}
}
Storage::Private::~Private()
{
if (transaction) {
mdb_txn_abort(transaction);
}
// it is still there and still unused, so we can shut it down
mdb_dbi_close(env, dbi);
mdb_env_close(env);
}
Storage::Storage(const QString &storageRoot, const QString &name, AccessMode mode)
: d(new Private(storageRoot, name, mode))
{
}
Storage::~Storage()
{
delete d;
}
bool Storage::isInTransaction() const
{
return d->transaction;
}
bool Storage::startTransaction(AccessMode type)
{
if (!d->env) {
return false;
}
bool requestedRead = type == ReadOnly;
if (d->mode == ReadOnly && !requestedRead) {
return false;
}
if (d->transaction && (!d->readTransaction || requestedRead)) {
return true;
}
if (d->transaction) {
// we are about to turn a read transaction into a writable one
abortTransaction();
}
if (d->firstOpen && requestedRead) {
//A write transaction is at least required the first time
mdb_txn_begin(d->env, nullptr, 0, &d->transaction);
//Open the database
//With this we could open multiple named databases if we wanted to
mdb_dbi_open(d->transaction, nullptr, 0, &d->dbi);
mdb_txn_abort(d->transaction);
}
int rc;
rc = mdb_txn_begin(d->env, NULL, requestedRead ? MDB_RDONLY : 0, &d->transaction);
if (!rc) {
rc = mdb_dbi_open(d->transaction, NULL, 0, &d->dbi);
}
d->firstOpen = false;
return !rc;
}
bool Storage::commitTransaction()
{
if (!d->env) {
return false;
}
if (!d->transaction) {
return false;
}
int rc;
rc = mdb_txn_commit(d->transaction);
d->transaction = 0;
if (rc) {
std::cerr << "mdb_txn_commit: " << rc << " " << mdb_strerror(rc) << std::endl;
}
return !rc;
}
void Storage::abortTransaction()
{
if (!d->env || !d->transaction) {
return;
}
mdb_txn_abort(d->transaction);
d->transaction = 0;
}
bool Storage::write(const char *key, size_t keySize, const char *value, size_t valueSize)
{
return write(std::string(key, keySize), std::string(value, valueSize));
}
bool Storage::write(const std::string &sKey, const std::string &sValue)
{
if (!d->env) {
return false;
}
const bool implicitTransaction = !d->transaction || d->readTransaction;
if (implicitTransaction) {
// TODO: if this fails, still try the write below?
if (!startTransaction()) {
return false;
}
}
int rc;
MDB_val key, data;
key.mv_size = sKey.size();
key.mv_data = (void*)sKey.data();
data.mv_size = sValue.size();
data.mv_data = (void*)sValue.data();
rc = mdb_put(d->transaction, d->dbi, &key, &data, 0);
if (rc) {
std::cerr << "mdb_put: " << rc << " " << mdb_strerror(rc) << std::endl;
}
if (implicitTransaction) {
if (rc) {
abortTransaction();
} else {
rc = commitTransaction();
}
}
return !rc;
}
void Storage::read(const std::string &sKey,
const std::function<bool(const std::string &value)> &resultHandler,
const std::function<void(const Storage::Error &error)> &errorHandler)
{
read(sKey,
[&](void *ptr, int size) -> bool {
const std::string resultValue(static_cast<char*>(ptr), size);
return resultHandler(resultValue);
}, errorHandler);
// std::cout << "key: " << resultKey << " data: " << resultValue << std::endl;
}
void Storage::read(const std::string &sKey,
const std::function<bool(void *ptr, int size)> &resultHandler,
const std::function<void(const Storage::Error &error)> &errorHandler)
{
scan(sKey, [resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) {
return resultHandler(valuePtr, valueSize);
}, errorHandler);
}
void Storage::scan(const std::string &sKey,
const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler,
const std::function<void(const Storage::Error &error)> &errorHandler)
{
if (!d->env) {
Error error(d->name.toStdString(), -1, "Not open");
errorHandler(error);
return;
}
int rc;
MDB_val key;
MDB_val data;
MDB_cursor *cursor;
key.mv_size = sKey.size();
key.mv_data = (void*)sKey.data();
const bool implicitTransaction = !d->transaction;
if (implicitTransaction) {
// TODO: if this fails, still try the write below?
if (!startTransaction(ReadOnly)) {
Error error(d->name.toStdString(), -2, "Could not start transaction");
errorHandler(error);
return;
}
}
rc = mdb_cursor_open(d->transaction, d->dbi, &cursor);
if (rc) {
Error error(d->name.toStdString(), rc, mdb_strerror(rc));
errorHandler(error);
return;
}
if (sKey.empty()) {
rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST);
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
if (!resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) {
break;
}
}
//We never find the last value
if (rc == MDB_NOTFOUND) {
rc = 0;
}
} else {
if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_SET)) == 0) {
resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size);
} else {
std::cout << "couldn't find value " << sKey << " " << std::endl;
}
}
mdb_cursor_close(cursor);
if (rc) {
Error error(d->name.toStdString(), rc, mdb_strerror(rc));
errorHandler(error);
}
/**
we don't abort the transaction since we need it for reading the values
if (implicitTransaction) {
abortTransaction();
}
*/
}
qint64 Storage::diskUsage() const
{
QFileInfo info(d->storageRoot + "/data.mdb");
return info.size();
}
void Storage::removeFromDisk() const
{
QDir dir(d->storageRoot);
dir.remove("data.mdb");
dir.remove("lock.mdb");
}
|