summaryrefslogtreecommitdiffstats
path: root/common/storage_kyoto.cpp
blob: d61c6c42c688943f1da4c0c61c8396d92917f818 (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
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
#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, AccessMode m);
    ~Private();

    QString name;
    kyotocabinet::TreeDB db;
    AccessMode mode;
    bool dbOpen;
    bool inTransaction;
};

Storage::Private::Private(const QString &storageRoot, const QString &n, AccessMode m)
    : name(n),
      mode(m),
      dbOpen(false),
      inTransaction(false)
{
    QDir dir;
    dir.mkdir(storageRoot);

    //create file
    uint32_t openMode = kyotocabinet::BasicDB::OCREATE |
                       (mode == ReadOnly ? kyotocabinet::BasicDB::OREADER
                                         : kyotocabinet::BasicDB::OWRITER);
    dbOpen = db.open((storageRoot + "/" + name + ".kch").toStdString(), openMode);
    if (!dbOpen) {
        std::cerr << "Could not open database: " << db.error().codename(db.error().code())  << " " << db.error().message() << std::endl;
        // TODO: handle error
    }
}

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

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->inTransaction;
}

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

    if (type == ReadWrite && d->mode != ReadWrite) {
        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; 
}

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)
{
    if (!d->dbOpen) {
        Error error(d->name.toStdString(), -1, "Not open");
        errorHandler(error);
        return;
    }

    std::string value;
    if (sKey.empty()) {
        kyotocabinet::DB::Cursor *cursor = d->db.cursor();
        cursor->jump();

        std::string key, value;
        while (cursor->get_value(&value, true) && resultHandler(value)) {}

        delete cursor;
        return;
    } else {
        if (d->db.get(sKey, &value)) {
            resultHandler(value);
            return;
        }
    }

    Error error(d->name.toStdString(), d->db.error().code(), d->db.error().message());
    errorHandler(error);
}

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)
{
    if (!d->dbOpen) {
        Error error(d->name.toStdString(), -1, "Not open");
        errorHandler(error);
        return;
    }

    size_t valueSize;
    char *valueBuffer;
    if (sKey.empty()) {
        kyotocabinet::DB::Cursor *cursor = d->db.cursor();
        cursor->jump();

        while ((valueBuffer = cursor->get_value(&valueSize, true))) {
            bool ok = resultHandler(valueBuffer, valueSize);
            delete[] valueBuffer;
            if (!ok) {
                break;
            }
        }

        delete cursor;
    } else {
        valueBuffer = d->db.get(sKey.data(), sKey.size(), &valueSize);
        if (valueBuffer) {
            resultHandler(valueBuffer, valueSize);
        } else {
            Error error(d->name.toStdString(), d->db.error().code(), d->db.error().message());
            errorHandler(error);
        }
        delete[] valueBuffer;
    }
}

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());
}