diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/storage_lmdb.cpp | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index 18a5aa6..283205f 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp | |||
@@ -14,10 +14,12 @@ | |||
14 | class Storage::Private | 14 | class Storage::Private |
15 | { | 15 | { |
16 | public: | 16 | public: |
17 | Private(const QString &p); | 17 | Private(const QString &s, const QString &name); |
18 | ~Private(); | 18 | ~Private(); |
19 | 19 | ||
20 | QString path; | 20 | QString storageRoot; |
21 | QString name; | ||
22 | |||
21 | MDB_dbi dbi; | 23 | MDB_dbi dbi; |
22 | MDB_env *env; | 24 | MDB_env *env; |
23 | MDB_txn *transaction; | 25 | MDB_txn *transaction; |
@@ -25,20 +27,21 @@ public: | |||
25 | bool firstOpen; | 27 | bool firstOpen; |
26 | }; | 28 | }; |
27 | 29 | ||
28 | Storage::Private::Private(const QString &p) | 30 | Storage::Private::Private(const QString &s, const QString &n) |
29 | : path(p), | 31 | : transaction(0), |
30 | transaction(0), | ||
31 | readTransaction(false), | 32 | readTransaction(false), |
32 | firstOpen(true) | 33 | firstOpen(true), |
34 | storageRoot(s), | ||
35 | name(n) | ||
33 | { | 36 | { |
34 | QDir dir; | 37 | QDir dir; |
35 | dir.mkdir(path); | 38 | dir.mkdir(storageRoot); |
36 | 39 | ||
37 | //create file | 40 | //create file |
38 | if (mdb_env_create(&env)) { | 41 | if (mdb_env_create(&env)) { |
39 | // TODO: handle error | 42 | // TODO: handle error |
40 | } else { | 43 | } else { |
41 | int rc = mdb_env_open(env, path.toStdString().data(), 0, 0664); | 44 | int rc = mdb_env_open(env, storageRoot.toStdString().data(), 0, 0664); |
42 | 45 | ||
43 | if (rc) { | 46 | if (rc) { |
44 | std::cerr << "mdb_env_open: " << rc << " " << mdb_strerror(rc) << std::endl; | 47 | std::cerr << "mdb_env_open: " << rc << " " << mdb_strerror(rc) << std::endl; |
@@ -63,7 +66,7 @@ Storage::Private::~Private() | |||
63 | } | 66 | } |
64 | 67 | ||
65 | Storage::Storage(const QString &storageRoot, const QString &name) | 68 | Storage::Storage(const QString &storageRoot, const QString &name) |
66 | : d(new Private(storageRoot + '/' + name)) | 69 | : d(new Private(storageRoot, name)) |
67 | { | 70 | { |
68 | } | 71 | } |
69 | 72 | ||
@@ -143,6 +146,11 @@ void Storage::abortTransaction() | |||
143 | d->transaction = 0; | 146 | d->transaction = 0; |
144 | } | 147 | } |
145 | 148 | ||
149 | bool Storage::write(const char *key, size_t keySize, const char *value, size_t valueSize) | ||
150 | { | ||
151 | write(std::string(key, keySize), std::string(value, valueSize)); | ||
152 | } | ||
153 | |||
146 | bool Storage::write(const std::string &sKey, const std::string &sValue) | 154 | bool Storage::write(const std::string &sKey, const std::string &sValue) |
147 | { | 155 | { |
148 | if (!d->env) { | 156 | if (!d->env) { |
@@ -241,7 +249,7 @@ bool Storage::read(const std::string &sKey, const std::function<void(void *ptr, | |||
241 | 249 | ||
242 | if (rc) { | 250 | if (rc) { |
243 | std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; | 251 | std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; |
244 | return false | 252 | return false; |
245 | } | 253 | } |
246 | 254 | ||
247 | /** | 255 | /** |
@@ -255,7 +263,7 @@ bool Storage::read(const std::string &sKey, const std::function<void(void *ptr, | |||
255 | 263 | ||
256 | qint64 Storage::diskUsage() const | 264 | qint64 Storage::diskUsage() const |
257 | { | 265 | { |
258 | QFileInfo info(d->path, "data.mdb"); | 266 | QFileInfo info(d->storageRoot + "/data.mdb"); |
259 | return info.size(); | 267 | return info.size(); |
260 | } | 268 | } |
261 | 269 | ||
@@ -266,3 +274,9 @@ void Storage::removeFromDisk() const | |||
266 | dir.remove("lock.mdb"); | 274 | dir.remove("lock.mdb"); |
267 | } | 275 | } |
268 | 276 | ||
277 | void Storage::removeFromDisk() const | ||
278 | { | ||
279 | QDir dir(d->storageRoot); | ||
280 | dir.remove("data.mdb"); | ||
281 | dir.remove("lock.mdb"); | ||
282 | } | ||