From ee686653237a828008014e843653b9b8fdb5f11d Mon Sep 17 00:00:00 2001 From: Minijackson Date: Thu, 24 May 2018 11:54:08 +0200 Subject: findAllInRange: Remove finding closest key to uppe bound ahead of time --- common/storage_lmdb.cpp | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index bd9bdce..851fe36 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp @@ -584,22 +584,8 @@ int DataStore::NamedDatabase::findAllInRange(const QByteArray &lowerBound, const lastKey = idealLastKey, currentKey; MDB_val reference; - // Find the last key past the range - int rc = mdb_cursor_get(cursor, &lastKey, &reference, MDB_SET_RANGE); - - // If only equal, move forward one spot - if (rc == MDB_SUCCESS && mdb_cmp(d->transaction, d->dbi, &lastKey, &idealLastKey) == 0) { - rc = mdb_cursor_get(cursor, &lastKey, &reference, MDB_NEXT); - } - - bool untilEnd = false; - if (rc != MDB_SUCCESS) { - // Nothing is greater than the upper bound, meaning search until the end - untilEnd = true; - } - // Find the first key in the range - rc = mdb_cursor_get(cursor, &firstKey, &reference, MDB_SET_RANGE); + int rc = mdb_cursor_get(cursor, &firstKey, &reference, MDB_SET_RANGE); if (rc != MDB_SUCCESS) { // Nothing is greater or equal than the lower bound, meaning no result @@ -608,16 +594,17 @@ int DataStore::NamedDatabase::findAllInRange(const QByteArray &lowerBound, const currentKey = firstKey; - do { - // If we have a stopping point and this is the stopping point - if (!untilEnd && mdb_cmp(d->transaction, d->dbi, ¤tKey, &lastKey) == 0) { - break; - } + // If already bigger than the upper bound + if (mdb_cmp(d->transaction, d->dbi, ¤tKey, &idealLastKey) > 0) { + 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); resultHandler(currentBAKey, currentBAValue); - } while (mdb_cursor_get(cursor, ¤tKey, &reference, MDB_NEXT) == MDB_SUCCESS); + } while (mdb_cursor_get(cursor, ¤tKey, &reference, MDB_NEXT) == MDB_SUCCESS && + mdb_cmp(d->transaction, d->dbi, ¤tKey, &idealLastKey) <= 0); } qint64 DataStore::NamedDatabase::getSize() -- cgit v1.2.3