From 0456cef80e909e545897ac9eb107fd832d4dfde8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Nicole?= Date: Thu, 24 May 2018 12:10:24 +0200 Subject: Add findAllInRange function in the storage layer Summary: In preparation of the support for ranged queries. Notes: Since they are pretty similar, it could be nice to refactor `scan` and `findAllInRange` to use common 3rd function Test Plan: This is tested in storagetest.cpp Reviewers: cmollekopf Tags: #sink Differential Revision: https://phabricator.kde.org/D13066 --- common/storage.h | 9 +++++++++ common/storage_lmdb.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) (limited to 'common') diff --git a/common/storage.h b/common/storage.h index bb8c1fa..a8c486c 100644 --- a/common/storage.h +++ b/common/storage.h @@ -108,6 +108,15 @@ 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 = + std::function()) const; + /** * Returns true if the database contains the substring key. */ diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index bf631a8..4ac5724 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp @@ -551,6 +551,60 @@ void DataStore::NamedDatabase::findLatest(const QByteArray &k, const std::functi return; } +int DataStore::NamedDatabase::findAllInRange(const QByteArray &lowerBound, const QByteArray &upperBound, + const std::function &resultHandler, + const std::function &errorHandler) const +{ + if (!d || !d->transaction) { + // Not an error. We rely on this to read nothing from non-existing databases. + return 0; + } + + MDB_cursor *cursor; + if (int rc = mdb_cursor_open(d->transaction, d->dbi, &cursor)) { + // Invalid arguments can mean that the transaction doesn't contain the db dbi + Error error(d->name.toLatin1() + d->db, getErrorCode(rc), + QByteArray("Error during mdb_cursor_open: ") + QByteArray(mdb_strerror(rc)) + + ". Lower bound: " + lowerBound + " Upper bound: " + upperBound); + errorHandler ? errorHandler(error) : d->defaultErrorHandler(error); + return 0; + } + + int numberOfRetrievedValues = 0; + + 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, &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; + } + + currentKey = firstKey; + + // 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 *)data.mv_data, data.mv_size); + resultHandler(currentBAKey, currentBAValue); + } 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() { if (!d || !d->transaction) { -- cgit v1.2.3