From 7f9634bcdde4dd498da9794d34bf1b2d9a9fed27 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Thu, 24 May 2018 12:02:10 +0200 Subject: Nitpicks --- common/storage.h | 4 ++++ common/storage_lmdb.cpp | 19 ++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/common/storage.h b/common/storage.h index 1fde8e6..a8c486c 100644 --- a/common/storage.h +++ b/common/storage.h @@ -108,6 +108,10 @@ public: void findLatest(const QByteArray &uid, const std::function &resultHandler, const std::function &errorHandler = std::function()) const; + /** + * Finds all the keys and values whose keys are in a given range + * (inclusive). + */ int findAllInRange(const QByteArray &lowerBound, const QByteArray &upperBound, const std::function &resultHandler, const std::function &errorHandler = diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index 851fe36..655f858 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp @@ -579,16 +579,18 @@ int DataStore::NamedDatabase::findAllInRange(const QByteArray &lowerBound, const int numberOfRetrievedValues = 0; - MDB_val firstKey = { .mv_size = (size_t)lowerBound.size(), .mv_data = (void *)lowerBound.constData() }, - idealLastKey = { .mv_size = (size_t)upperBound.size(), .mv_data = (void *)upperBound.constData() }, - lastKey = idealLastKey, currentKey; - MDB_val reference; + MDB_val firstKey = { .mv_size = (size_t)lowerBound.size(), .mv_data = (void *)lowerBound.constData() }; + MDB_val idealLastKey = { .mv_size = (size_t)upperBound.size(), .mv_data = (void *)upperBound.constData() }; + MDB_val lastKey = idealLastKey; + MDB_val currentKey; + MDB_val data; // Find the first key in the range - int rc = mdb_cursor_get(cursor, &firstKey, &reference, MDB_SET_RANGE); + int rc = mdb_cursor_get(cursor, &firstKey, &data, MDB_SET_RANGE); if (rc != MDB_SUCCESS) { // Nothing is greater or equal than the lower bound, meaning no result + mdb_cursor_close(cursor); return 0; } @@ -596,15 +598,18 @@ int DataStore::NamedDatabase::findAllInRange(const QByteArray &lowerBound, const // If already bigger than the upper bound if (mdb_cmp(d->transaction, d->dbi, ¤tKey, &idealLastKey) > 0) { + mdb_cursor_close(cursor); return 0; } do { const auto currentBAKey = QByteArray::fromRawData((char *)currentKey.mv_data, currentKey.mv_size); - const auto currentBAValue = QByteArray::fromRawData((char *)reference.mv_data, reference.mv_size); + const auto currentBAValue = QByteArray::fromRawData((char *)data.mv_data, data.mv_size); resultHandler(currentBAKey, currentBAValue); - } while (mdb_cursor_get(cursor, ¤tKey, &reference, MDB_NEXT) == MDB_SUCCESS && + } while (mdb_cursor_get(cursor, ¤tKey, &data, MDB_NEXT) == MDB_SUCCESS && mdb_cmp(d->transaction, d->dbi, ¤tKey, &idealLastKey) <= 0); + + mdb_cursor_close(cursor); } qint64 DataStore::NamedDatabase::getSize() -- cgit v1.2.3