diff options
-rw-r--r-- | common/storage.h | 2 | ||||
-rw-r--r-- | common/storage_lmdb.cpp | 44 |
2 files changed, 46 insertions, 0 deletions
diff --git a/common/storage.h b/common/storage.h index a051043..64a632d 100644 --- a/common/storage.h +++ b/common/storage.h | |||
@@ -41,6 +41,8 @@ public: | |||
41 | void scan(const char *keyData, uint keySize, | 41 | void scan(const char *keyData, uint keySize, |
42 | const std::function<bool(void *keyPtr, int keySize, void *ptr, int size)> &resultHandler, | 42 | const std::function<bool(void *keyPtr, int keySize, void *ptr, int size)> &resultHandler, |
43 | const std::function<void(const Storage::Error &error)> &errorHandler); | 43 | const std::function<void(const Storage::Error &error)> &errorHandler); |
44 | void readAll(const std::function<bool(void *key, int keySize, void *data, int dataSize)> &resultHandler, | ||
45 | const std::function<void(const Storage::Error &error)> &errorHandler); | ||
44 | 46 | ||
45 | static std::function<void(const Storage::Error &error)> basicErrorHandler(); | 47 | static std::function<void(const Storage::Error &error)> basicErrorHandler(); |
46 | qint64 diskUsage() const; | 48 | qint64 diskUsage() const; |
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index 2bffcd7..0070888 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp | |||
@@ -286,6 +286,50 @@ void Storage::scan(const char *keyData, uint keySize, | |||
286 | } | 286 | } |
287 | */ | 287 | */ |
288 | } | 288 | } |
289 | void Storage::readAll(const std::function<bool(void *key, int keySize, void *data, int dataSize)> &resultHandler, | ||
290 | const std::function<void(const Storage::Error &error)> &errorHandler) | ||
291 | { | ||
292 | int rc; | ||
293 | MDB_val key; | ||
294 | MDB_val data; | ||
295 | MDB_cursor *cursor = 0; | ||
296 | |||
297 | const bool implicitTransaction = !d->transaction; | ||
298 | if (implicitTransaction) { | ||
299 | // TODO: if this fails, still try the write below? | ||
300 | if (!startTransaction(ReadOnly)) { | ||
301 | Error error(d->name.toStdString(), -2, "Could not start transaction"); | ||
302 | errorHandler(error); | ||
303 | return; | ||
304 | } | ||
305 | } | ||
306 | |||
307 | rc = mdb_cursor_open(d->transaction, d->dbi, &cursor); | ||
308 | if (rc) { | ||
309 | Error error(d->name.toStdString(), rc, mdb_strerror(rc)); | ||
310 | errorHandler(error); | ||
311 | return; | ||
312 | } | ||
313 | |||
314 | rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST); | ||
315 | while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { | ||
316 | if (!resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) { | ||
317 | break; | ||
318 | } | ||
319 | } | ||
320 | |||
321 | //We never find the last value | ||
322 | if (rc == MDB_NOTFOUND) { | ||
323 | rc = 0; | ||
324 | } | ||
325 | |||
326 | mdb_cursor_close(cursor); | ||
327 | |||
328 | if (rc) { | ||
329 | Error error(d->name.toStdString(), rc, mdb_strerror(rc)); | ||
330 | errorHandler(error); | ||
331 | } | ||
332 | } | ||
289 | 333 | ||
290 | qint64 Storage::diskUsage() const | 334 | qint64 Storage::diskUsage() const |
291 | { | 335 | { |