summaryrefslogtreecommitdiffstats
path: root/common/storage_kyoto.cpp
diff options
context:
space:
mode:
authorAaron Seigo <aseigo@kde.org>2014-12-05 10:05:19 +0100
committerAaron Seigo <aseigo@kde.org>2014-12-05 10:05:19 +0100
commit93d1c82ffd17df45a5cecd875a01ee3cb15d9983 (patch)
treecda5c31857362ce81f3a50959024f4270d149a07 /common/storage_kyoto.cpp
parent639fc60c100204c87b93112516cf3b3117cfff0d (diff)
parent351a66b5fb1c8659bff8ea20d60f5a6d2d3263ad (diff)
downloadsink-93d1c82ffd17df45a5cecd875a01ee3cb15d9983.tar.gz
sink-93d1c82ffd17df45a5cecd875a01ee3cb15d9983.zip
Merge branch 'kyoto'
Conflicts: common/storage.h common/storage_lmdb.cpp dummyresource/facade.cpp store/test/CMakeLists.txt tests/storagebenchmark.cpp
Diffstat (limited to 'common/storage_kyoto.cpp')
-rw-r--r--common/storage_kyoto.cpp170
1 files changed, 170 insertions, 0 deletions
diff --git a/common/storage_kyoto.cpp b/common/storage_kyoto.cpp
new file mode 100644
index 0000000..40bd3e6
--- /dev/null
+++ b/common/storage_kyoto.cpp
@@ -0,0 +1,170 @@
1#include "storage.h"
2
3#include <iostream>
4
5#include <QAtomicInt>
6#include <QDebug>
7#include <QDir>
8#include <QFileInfo>
9#include <QReadWriteLock>
10#include <QString>
11#include <QTime>
12
13#include <kchashdb.h>
14
15class Storage::Private
16{
17public:
18 Private(const QString &storageRoot, const QString &name);
19 ~Private();
20
21 kyotocabinet::TreeDB db;
22 bool dbOpen;
23 bool inTransaction;
24};
25
26Storage::Private::Private(const QString &storageRoot, const QString &name)
27 : inTransaction(false)
28{
29 QDir dir;
30 dir.mkdir(storageRoot);
31
32 //create file
33 dbOpen = db.open((storageRoot + "/" + name + ".kch").toStdString(), kyotocabinet::BasicDB::OWRITER | kyotocabinet::BasicDB::OCREATE);
34 if (!dbOpen) {
35 // TODO: handle error
36 }
37}
38
39Storage::Private::~Private()
40{
41 if (dbOpen && inTransaction) {
42 db.end_transaction(false);
43 }
44}
45
46Storage::Storage(const QString &storageRoot, const QString &name)
47 : d(new Private(storageRoot, name))
48{
49}
50
51Storage::~Storage()
52{
53 delete d;
54}
55
56bool Storage::isInTransaction() const
57{
58 return d->inTransaction;
59}
60
61bool Storage::startTransaction(TransactionType type)
62{
63 if (!d->dbOpen) {
64 return false;
65 }
66
67 if (d->inTransaction) {
68 return true;
69 }
70
71 //TODO handle errors
72 d->inTransaction = d->db.begin_transaction();
73 return d->inTransaction;
74}
75
76bool Storage::commitTransaction()
77{
78 if (!d->dbOpen) {
79 return false;
80 }
81
82 if (!d->inTransaction) {
83 return false;
84 }
85
86 bool success = d->db.end_transaction(true);
87 d->inTransaction = false;
88 return success;
89}
90
91void Storage::abortTransaction()
92{
93 if (!d->dbOpen || !d->inTransaction) {
94 return;
95 }
96
97 d->db.end_transaction(false);
98 d->inTransaction = false;
99}
100
101bool Storage::write(const char *key, size_t keySize, const char *value, size_t valueSize)
102{
103 if (!d->dbOpen) {
104 return false;
105 }
106
107 bool success = d->db.set(key, keySize, value, valueSize);
108 return success;
109}
110
111bool Storage::write(const std::string &sKey, const std::string &sValue)
112{
113 if (!d->dbOpen) {
114 return false;
115 }
116
117 bool success = d->db.set(sKey, sValue);
118 return success;
119}
120
121bool Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler)
122{
123 if (!d->dbOpen) {
124 return false;
125 }
126
127 std::string value;
128 if (d->db.get(sKey, &value)) {
129 resultHandler(value);
130 return true;
131 }
132
133 return false;
134}
135
136bool Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler)
137{
138 if (!d->dbOpen) {
139 return false;
140 }
141
142 size_t valueSize;
143 char *valueBuffer = d->db.get(sKey.data(), sKey.size(), &valueSize);
144 if (valueBuffer) {
145 resultHandler(valueBuffer, valueSize);
146 }
147 delete[] valueBuffer;
148 return valueBuffer != nullptr;
149}
150
151qint64 Storage::diskUsage() const
152{
153 if (!d->dbOpen) {
154 return 0;
155 }
156
157 QFileInfo info(QString::fromStdString(d->db.path()));
158 return info.size();
159}
160
161void Storage::removeFromDisk() const
162{
163 if (!d->dbOpen) {
164 return;
165 }
166
167 QFileInfo info(QString::fromStdString(d->db.path()));
168 QDir dir = info.dir();
169 dir.remove(info.fileName());
170}