diff options
author | Minijackson <minijackson@riseup.net> | 2018-05-24 12:02:10 +0200 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2018-05-24 12:02:10 +0200 |
commit | 7f9634bcdde4dd498da9794d34bf1b2d9a9fed27 (patch) | |
tree | be54a4ff74edd1c00dd70bf21c3ddb3281054689 | |
parent | ee686653237a828008014e843653b9b8fdb5f11d (diff) | |
download | sink-range-query/storage.tar.gz sink-range-query/storage.zip |
Nitpicksrange-query/storage
-rw-r--r-- | common/storage.h | 4 | ||||
-rw-r--r-- | 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: | |||
108 | void findLatest(const QByteArray &uid, const std::function<void(const QByteArray &key, const QByteArray &value)> &resultHandler, | 108 | void findLatest(const QByteArray &uid, const std::function<void(const QByteArray &key, const QByteArray &value)> &resultHandler, |
109 | const std::function<void(const DataStore::Error &error)> &errorHandler = std::function<void(const DataStore::Error &error)>()) const; | 109 | const std::function<void(const DataStore::Error &error)> &errorHandler = std::function<void(const DataStore::Error &error)>()) const; |
110 | 110 | ||
111 | /** | ||
112 | * Finds all the keys and values whose keys are in a given range | ||
113 | * (inclusive). | ||
114 | */ | ||
111 | int findAllInRange(const QByteArray &lowerBound, const QByteArray &upperBound, | 115 | int findAllInRange(const QByteArray &lowerBound, const QByteArray &upperBound, |
112 | const std::function<void(const QByteArray &key, const QByteArray &value)> &resultHandler, | 116 | const std::function<void(const QByteArray &key, const QByteArray &value)> &resultHandler, |
113 | const std::function<void(const DataStore::Error &error)> &errorHandler = | 117 | const std::function<void(const DataStore::Error &error)> &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 | |||
579 | 579 | ||
580 | int numberOfRetrievedValues = 0; | 580 | int numberOfRetrievedValues = 0; |
581 | 581 | ||
582 | MDB_val firstKey = { .mv_size = (size_t)lowerBound.size(), .mv_data = (void *)lowerBound.constData() }, | 582 | MDB_val firstKey = { .mv_size = (size_t)lowerBound.size(), .mv_data = (void *)lowerBound.constData() }; |
583 | idealLastKey = { .mv_size = (size_t)upperBound.size(), .mv_data = (void *)upperBound.constData() }, | 583 | MDB_val idealLastKey = { .mv_size = (size_t)upperBound.size(), .mv_data = (void *)upperBound.constData() }; |
584 | lastKey = idealLastKey, currentKey; | 584 | MDB_val lastKey = idealLastKey; |
585 | MDB_val reference; | 585 | MDB_val currentKey; |
586 | MDB_val data; | ||
586 | 587 | ||
587 | // Find the first key in the range | 588 | // Find the first key in the range |
588 | int rc = mdb_cursor_get(cursor, &firstKey, &reference, MDB_SET_RANGE); | 589 | int rc = mdb_cursor_get(cursor, &firstKey, &data, MDB_SET_RANGE); |
589 | 590 | ||
590 | if (rc != MDB_SUCCESS) { | 591 | if (rc != MDB_SUCCESS) { |
591 | // Nothing is greater or equal than the lower bound, meaning no result | 592 | // Nothing is greater or equal than the lower bound, meaning no result |
593 | mdb_cursor_close(cursor); | ||
592 | return 0; | 594 | return 0; |
593 | } | 595 | } |
594 | 596 | ||
@@ -596,15 +598,18 @@ int DataStore::NamedDatabase::findAllInRange(const QByteArray &lowerBound, const | |||
596 | 598 | ||
597 | // If already bigger than the upper bound | 599 | // If already bigger than the upper bound |
598 | if (mdb_cmp(d->transaction, d->dbi, ¤tKey, &idealLastKey) > 0) { | 600 | if (mdb_cmp(d->transaction, d->dbi, ¤tKey, &idealLastKey) > 0) { |
601 | mdb_cursor_close(cursor); | ||
599 | return 0; | 602 | return 0; |
600 | } | 603 | } |
601 | 604 | ||
602 | do { | 605 | do { |
603 | const auto currentBAKey = QByteArray::fromRawData((char *)currentKey.mv_data, currentKey.mv_size); | 606 | const auto currentBAKey = QByteArray::fromRawData((char *)currentKey.mv_data, currentKey.mv_size); |
604 | const auto currentBAValue = QByteArray::fromRawData((char *)reference.mv_data, reference.mv_size); | 607 | const auto currentBAValue = QByteArray::fromRawData((char *)data.mv_data, data.mv_size); |
605 | resultHandler(currentBAKey, currentBAValue); | 608 | resultHandler(currentBAKey, currentBAValue); |
606 | } while (mdb_cursor_get(cursor, ¤tKey, &reference, MDB_NEXT) == MDB_SUCCESS && | 609 | } while (mdb_cursor_get(cursor, ¤tKey, &data, MDB_NEXT) == MDB_SUCCESS && |
607 | mdb_cmp(d->transaction, d->dbi, ¤tKey, &idealLastKey) <= 0); | 610 | mdb_cmp(d->transaction, d->dbi, ¤tKey, &idealLastKey) <= 0); |
611 | |||
612 | mdb_cursor_close(cursor); | ||
608 | } | 613 | } |
609 | 614 | ||
610 | qint64 DataStore::NamedDatabase::getSize() | 615 | qint64 DataStore::NamedDatabase::getSize() |