summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2014-12-04 22:51:05 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2014-12-04 22:51:05 +0100
commit838bc745bbce58ec5892c453971d5f7bd48f74ff (patch)
treec8c9bb456eaabe01ae82f0458c5cc051620bcf08
parent4626fdf480ccd989854015b53f656fe66fa02a66 (diff)
downloadsink-838bc745bbce58ec5892c453971d5f7bd48f74ff.tar.gz
sink-838bc745bbce58ec5892c453971d5f7bd48f74ff.zip
Added kyotodatabase code.
We should probably turn Database into an interface.
-rw-r--r--store/kyotodatabase.cpp164
-rw-r--r--store/kyotodatabase.h28
2 files changed, 192 insertions, 0 deletions
diff --git a/store/kyotodatabase.cpp b/store/kyotodatabase.cpp
new file mode 100644
index 0000000..542667a
--- /dev/null
+++ b/store/kyotodatabase.cpp
@@ -0,0 +1,164 @@
1#include "database.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 Database::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
26Database::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
39Database::Private::~Private()
40{
41 if (dbOpen && inTransaction) {
42 db.end_transaction(false);
43 }
44}
45
46Database::Database(const QString &storageRoot, const QString &name)
47 : d(new Private(storageRoot, name))
48{
49}
50
51Database::~Database()
52{
53 delete d;
54}
55
56bool Database::isInTransaction() const
57{
58 return d->inTransaction;
59}
60
61bool Database::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 Database::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 Database::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 Database::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 Database::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
121void Database::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler)
122{
123 if (!d->dbOpen) {
124 return;
125 }
126
127 std::string value;
128 if (d->db.get(sKey, &value)) {
129 resultHandler(value);
130 }
131}
132
133void Database::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler)
134{
135 if (!d->dbOpen) {
136 return;
137 }
138
139 size_t valueSize;
140 char *valueBuffer = d->db.get(sKey.data(), sKey.size(), &valueSize);
141 resultHandler(valueBuffer, valueSize);
142 delete[] valueBuffer;
143}
144
145qint64 Database::diskUsage() const
146{
147 if (!d->dbOpen) {
148 return 0;
149 }
150
151 QFileInfo info(QString::fromStdString(d->db.path()));
152 return info.size();
153}
154
155void Database::removeFromDisk() const
156{
157 if (!d->dbOpen) {
158 return;
159 }
160
161 QFileInfo info(QString::fromStdString(d->db.path()));
162 QDir dir = info.dir();
163 dir.remove(info.fileName());
164}
diff --git a/store/kyotodatabase.h b/store/kyotodatabase.h
new file mode 100644
index 0000000..e752ff5
--- /dev/null
+++ b/store/kyotodatabase.h
@@ -0,0 +1,28 @@
1#pragma once
2
3#include <string>
4#include <QString>
5
6class Database {
7public:
8 enum TransactionType { ReadOnly, ReadWrite };
9
10 Database(const QString &storageRoot, const QString &name);
11 ~Database();
12 bool isInTransaction() const;
13 bool startTransaction(TransactionType type = ReadWrite);
14 bool commitTransaction();
15 void abortTransaction();
16 bool write(const char *key, size_t keySize, const char *value, size_t valueSize);
17 bool write(const std::string &sKey, const std::string &sValue);
18 //Perhaps prefer iterators (assuming we need to be able to match multiple values
19 void read(const std::string &sKey, const std::function<void(const std::string &value)> &);
20 void read(const std::string &sKey, const std::function<void(void *ptr, int size)> &);
21
22 qint64 diskUsage() const;
23 void removeFromDisk() const;
24private:
25 class Private;
26 Private * const d;
27};
28