summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/storage.h4
-rw-r--r--common/storage_kyoto.cpp16
-rw-r--r--common/storage_lmdb.cpp18
-rw-r--r--tests/storagetest.cpp23
4 files changed, 35 insertions, 26 deletions
diff --git a/common/storage.h b/common/storage.h
index f7dbd89..0b548fb 100644
--- a/common/storage.h
+++ b/common/storage.h
@@ -16,8 +16,8 @@ public:
16 bool write(const char *key, size_t keySize, const char *value, size_t valueSize); 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); 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 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)> &); 19 bool 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)> &); 20 bool read(const std::string &sKey, const std::function<void(void *ptr, int size)> &);
21 21
22 qint64 diskUsage() const; 22 qint64 diskUsage() const;
23 void removeFromDisk() const; 23 void removeFromDisk() const;
diff --git a/common/storage_kyoto.cpp b/common/storage_kyoto.cpp
index 05942c2..40bd3e6 100644
--- a/common/storage_kyoto.cpp
+++ b/common/storage_kyoto.cpp
@@ -118,28 +118,34 @@ bool Storage::write(const std::string &sKey, const std::string &sValue)
118 return success; 118 return success;
119} 119}
120 120
121void Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler) 121bool Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler)
122{ 122{
123 if (!d->dbOpen) { 123 if (!d->dbOpen) {
124 return; 124 return false;
125 } 125 }
126 126
127 std::string value; 127 std::string value;
128 if (d->db.get(sKey, &value)) { 128 if (d->db.get(sKey, &value)) {
129 resultHandler(value); 129 resultHandler(value);
130 return true;
130 } 131 }
132
133 return false;
131} 134}
132 135
133void Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler) 136bool Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler)
134{ 137{
135 if (!d->dbOpen) { 138 if (!d->dbOpen) {
136 return; 139 return false;
137 } 140 }
138 141
139 size_t valueSize; 142 size_t valueSize;
140 char *valueBuffer = d->db.get(sKey.data(), sKey.size(), &valueSize); 143 char *valueBuffer = d->db.get(sKey.data(), sKey.size(), &valueSize);
141 resultHandler(valueBuffer, valueSize); 144 if (valueBuffer) {
145 resultHandler(valueBuffer, valueSize);
146 }
142 delete[] valueBuffer; 147 delete[] valueBuffer;
148 return valueBuffer != nullptr;
143} 149}
144 150
145qint64 Storage::diskUsage() const 151qint64 Storage::diskUsage() const
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp
index 6c25448..18a5aa6 100644
--- a/common/storage_lmdb.cpp
+++ b/common/storage_lmdb.cpp
@@ -180,9 +180,9 @@ bool Storage::write(const std::string &sKey, const std::string &sValue)
180 return !rc; 180 return !rc;
181} 181}
182 182
183void Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler) 183bool Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler)
184{ 184{
185 read(sKey, 185 return read(sKey,
186 [&](void *ptr, int size) { 186 [&](void *ptr, int size) {
187 const std::string resultValue(static_cast<char*>(ptr), size); 187 const std::string resultValue(static_cast<char*>(ptr), size);
188 resultHandler(resultValue); 188 resultHandler(resultValue);
@@ -190,10 +190,10 @@ void Storage::read(const std::string &sKey, const std::function<void(const std::
190// std::cout << "key: " << resultKey << " data: " << resultValue << std::endl; 190// std::cout << "key: " << resultKey << " data: " << resultValue << std::endl;
191} 191}
192 192
193void Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler) 193bool Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler)
194{ 194{
195 if (!d->env) { 195 if (!d->env) {
196 return; 196 return false;
197 } 197 }
198 198
199 int rc; 199 int rc;
@@ -208,14 +208,14 @@ void Storage::read(const std::string &sKey, const std::function<void(void *ptr,
208 if (implicitTransaction) { 208 if (implicitTransaction) {
209 // TODO: if this fails, still try the write below? 209 // TODO: if this fails, still try the write below?
210 if (!startTransaction(ReadOnly)) { 210 if (!startTransaction(ReadOnly)) {
211 return; 211 return false;
212 } 212 }
213 } 213 }
214 214
215 rc = mdb_cursor_open(d->transaction, d->dbi, &cursor); 215 rc = mdb_cursor_open(d->transaction, d->dbi, &cursor);
216 if (rc) { 216 if (rc) {
217 std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; 217 std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl;
218 return; 218 return false;
219 } 219 }
220 220
221 if (sKey.empty()) { 221 if (sKey.empty()) {
@@ -237,18 +237,20 @@ void Storage::read(const std::string &sKey, const std::function<void(void *ptr,
237 } 237 }
238 } 238 }
239 239
240 mdb_cursor_close(cursor);
241
240 if (rc) { 242 if (rc) {
241 std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; 243 std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl;
244 return false
242 } 245 }
243 246
244 mdb_cursor_close(cursor);
245
246 /** 247 /**
247 we don't abort the transaction since we need it for reading the values 248 we don't abort the transaction since we need it for reading the values
248 if (implicitTransaction) { 249 if (implicitTransaction) {
249 abortTransaction(); 250 abortTransaction();
250 } 251 }
251 */ 252 */
253 return true;
252} 254}
253 255
254qint64 Storage::diskUsage() const 256qint64 Storage::diskUsage() const
diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp
index 1b105af..2b2805d 100644
--- a/tests/storagetest.cpp
+++ b/tests/storagetest.cpp
@@ -6,7 +6,7 @@
6#include <QString> 6#include <QString>
7#include <QtConcurrent/QtConcurrentRun> 7#include <QtConcurrent/QtConcurrentRun>
8 8
9#include "store/database.h" 9#include "common/storage.h"
10 10
11class StorageTest : public QObject 11class StorageTest : public QObject
12{ 12{
@@ -19,31 +19,32 @@ private:
19 19
20 void populate(int count) 20 void populate(int count)
21 { 21 {
22 Database db(testDataPath, dbName); 22 Storage storage(testDataPath, dbName);
23 for (int i = 0; i < count; i++) { 23 for (int i = 0; i < count; i++) {
24 //This should perhaps become an implementation detail of the db? 24 //This should perhaps become an implementation detail of the db?
25 if (i % 10000 == 0) { 25 if (i % 10000 == 0) {
26 if (i > 0) { 26 if (i > 0) {
27 db.commitTransaction(); 27 storage.commitTransaction();
28 } 28 }
29 db.startTransaction(); 29 storage.startTransaction();
30 } 30 }
31 db.write(keyPrefix + std::to_string(i), keyPrefix + std::to_string(i)); 31 storage.write(keyPrefix + std::to_string(i), keyPrefix + std::to_string(i));
32 } 32 }
33 db.commitTransaction(); 33 storage.commitTransaction();
34 } 34 }
35 35
36 bool verify(Database &db, int i) 36 bool verify(Storage &storage, int i)
37 { 37 {
38 bool error = false; 38 bool success = true;
39 bool keyMatch = true;
39 const auto reference = keyPrefix + std::to_string(i); 40 const auto reference = keyPrefix + std::to_string(i);
40 db.read(keyPrefix + std::to_string(i), [&error, &reference](const std::string &value) { 41 success = storage.read(keyPrefix + std::to_string(i), [&error, &reference](const std::string &value) {
41 if (value != reference) { 42 if (value != reference) {
42 qDebug() << "Mismatch while reading"; 43 qDebug() << "Mismatch while reading";
43 error = true; 44 keyMatch = false;
44 } 45 }
45 }); 46 });
46 return !error; 47 return succes && keyMatch;
47 } 48 }
48 49
49private Q_SLOTS: 50private Q_SLOTS: