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
|
#include "storage.h"
#include <iostream>
#include <QAtomicInt>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QReadWriteLock>
#include <QString>
#include <QTime>
#include <kchashdb.h>
class Storage::Private
{
public:
Private(const QString &storageRoot, const QString &name);
~Private();
kyotocabinet::TreeDB db;
bool dbOpen;
bool inTransaction;
};
Storage::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
}
}
Storage::Private::~Private()
{
if (dbOpen && inTransaction) {
db.end_transaction(false);
}
}
Storage::Storage(const QString &storageRoot, const QString &name)
: d(new Private(storageRoot, name))
{
}
Storage::~Storage()
{
delete d;
}
bool Storage::isInTransaction() const
{
return d->inTransaction;
}
bool Storage::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 Storage::commitTransaction()
{
if (!d->dbOpen) {
return false;
}
if (!d->inTransaction) {
return false;
}
bool success = d->db.end_transaction(true);
d->inTransaction = false;
return success;
}
void Storage::abortTransaction()
{
if (!d->dbOpen || !d->inTransaction) {
return;
}
d->db.end_transaction(false);
d->inTransaction = false;
}
bool Storage::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 Storage::write(const std::string &sKey, const std::string &sValue)
{
if (!d->dbOpen) {
return false;
}
bool success = d->db.set(sKey, sValue);
return success;
}
bool Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler)
{
if (!d->dbOpen) {
return false;
}
std::string value;
if (d->db.get(sKey, &value)) {
resultHandler(value);
return true;
}
return false;
}
bool Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler)
{
if (!d->dbOpen) {
return false;
}
size_t valueSize;
char *valueBuffer = d->db.get(sKey.data(), sKey.size(), &valueSize);
if (valueBuffer) {
resultHandler(valueBuffer, valueSize);
}
delete[] valueBuffer;
return valueBuffer != nullptr;
}
qint64 Storage::diskUsage() const
{
if (!d->dbOpen) {
return 0;
}
QFileInfo info(QString::fromStdString(d->db.path()));
return info.size();
}
void Storage::removeFromDisk() const
{
if (!d->dbOpen) {
return;
}
QFileInfo info(QString::fromStdString(d->db.path()));
QDir dir = info.dir();
dir.remove(info.fileName());
}
|