summaryrefslogtreecommitdiffstats
path: root/common/storage_lmdb.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/storage_lmdb.cpp')
-rw-r--r--common/storage_lmdb.cpp38
1 files changed, 24 insertions, 14 deletions
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp
index 7e23fd3..b7102aa 100644
--- a/common/storage_lmdb.cpp
+++ b/common/storage_lmdb.cpp
@@ -195,20 +195,26 @@ bool Storage::write(const std::string &sKey, const std::string &sValue)
195 return !rc; 195 return !rc;
196} 196}
197 197
198bool Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler) 198void Storage::read(const std::string &sKey,
199 const std::function<bool(const std::string &value)> &resultHandler,
200 const std::function<void(const Storage::Error &error)> &errorHandler)
199{ 201{
200 return read(sKey, 202 read(sKey,
201 [&](void *ptr, int size) { 203 [&](void *ptr, int size) -> bool {
202 const std::string resultValue(static_cast<char*>(ptr), size); 204 const std::string resultValue(static_cast<char*>(ptr), size);
203 resultHandler(resultValue); 205 return resultHandler(resultValue);
204 }); 206 }, errorHandler);
205// std::cout << "key: " << resultKey << " data: " << resultValue << std::endl; 207// std::cout << "key: " << resultKey << " data: " << resultValue << std::endl;
206} 208}
207 209
208bool Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler) 210void Storage::read(const std::string &sKey,
211 const std::function<bool(void *ptr, int size)> &resultHandler,
212 const std::function<void(const Storage::Error &error)> &errorHandler)
209{ 213{
210 if (!d->env) { 214 if (!d->env) {
211 return false; 215 Error error(d->name.toStdString(), -1, "Not open");
216 errorHandler(error);
217 return;
212 } 218 }
213 219
214 int rc; 220 int rc;
@@ -223,21 +229,26 @@ bool Storage::read(const std::string &sKey, const std::function<void(void *ptr,
223 if (implicitTransaction) { 229 if (implicitTransaction) {
224 // TODO: if this fails, still try the write below? 230 // TODO: if this fails, still try the write below?
225 if (!startTransaction(ReadOnly)) { 231 if (!startTransaction(ReadOnly)) {
226 return false; 232 Error error(d->name.toStdString(), -2, "Could not start transaction");
233 errorHandler(error);
234 return;
227 } 235 }
228 } 236 }
229 237
230 rc = mdb_cursor_open(d->transaction, d->dbi, &cursor); 238 rc = mdb_cursor_open(d->transaction, d->dbi, &cursor);
231 if (rc) { 239 if (rc) {
232 std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; 240 Error error(d->name.toStdString(), rc, mdb_strerror(rc));
233 return false; 241 errorHandler(error);
242 return;
234 } 243 }
235 244
236 if (sKey.empty()) { 245 if (sKey.empty()) {
237 std::cout << "Iterating over all values of store!" << std::endl; 246 std::cout << "Iterating over all values of store!" << std::endl;
238 rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST); 247 rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST);
239 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { 248 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
240 resultHandler(key.mv_data, data.mv_size); 249 if (!resultHandler(key.mv_data, data.mv_size)) {
250 break;
251 }
241 } 252 }
242 253
243 //We never find the last value 254 //We never find the last value
@@ -255,8 +266,8 @@ bool Storage::read(const std::string &sKey, const std::function<void(void *ptr,
255 mdb_cursor_close(cursor); 266 mdb_cursor_close(cursor);
256 267
257 if (rc) { 268 if (rc) {
258 std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; 269 Error error(d->name.toStdString(), rc, mdb_strerror(rc));
259 return false; 270 errorHandler(error);
260 } 271 }
261 272
262 /** 273 /**
@@ -265,7 +276,6 @@ bool Storage::read(const std::string &sKey, const std::function<void(void *ptr,
265 abortTransaction(); 276 abortTransaction();
266 } 277 }
267 */ 278 */
268 return true;
269} 279}
270 280
271qint64 Storage::diskUsage() const 281qint64 Storage::diskUsage() const