From 52607c5d331bb997acb0c96c6a68ded5e679b071 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 10 Dec 2014 08:48:41 +0100 Subject: add a read that gets keys and values (this really really really needs a cleanup now) --- common/storage.h | 2 ++ common/storage_lmdb.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) (limited to 'common') 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: void scan(const char *keyData, uint keySize, const std::function &resultHandler, const std::function &errorHandler); + void readAll(const std::function &resultHandler, + const std::function &errorHandler); static std::function basicErrorHandler(); 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, } */ } +void Storage::readAll(const std::function &resultHandler, + const std::function &errorHandler) +{ + int rc; + MDB_val key; + MDB_val data; + MDB_cursor *cursor = 0; + + const bool implicitTransaction = !d->transaction; + if (implicitTransaction) { + // TODO: if this fails, still try the write below? + if (!startTransaction(ReadOnly)) { + Error error(d->name.toStdString(), -2, "Could not start transaction"); + errorHandler(error); + return; + } + } + + rc = mdb_cursor_open(d->transaction, d->dbi, &cursor); + if (rc) { + Error error(d->name.toStdString(), rc, mdb_strerror(rc)); + errorHandler(error); + return; + } + + rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST); + while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { + if (!resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) { + break; + } + } + + //We never find the last value + if (rc == MDB_NOTFOUND) { + rc = 0; + } + + mdb_cursor_close(cursor); + + if (rc) { + Error error(d->name.toStdString(), rc, mdb_strerror(rc)); + errorHandler(error); + } +} qint64 Storage::diskUsage() const { -- cgit v1.2.3