summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2018-05-24 11:54:08 +0200
committerMinijackson <minijackson@riseup.net>2018-05-24 11:54:08 +0200
commitee686653237a828008014e843653b9b8fdb5f11d (patch)
tree4b2c2f3bf1db3b9b86cfde668e6ef4a6c21c86af
parent427b1d38d870e6876e10349b1081b56ff3390dec (diff)
downloadsink-ee686653237a828008014e843653b9b8fdb5f11d.tar.gz
sink-ee686653237a828008014e843653b9b8fdb5f11d.zip
findAllInRange: Remove finding closest key to uppe bound ahead of time
-rw-r--r--common/storage_lmdb.cpp29
1 files 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
584 lastKey = idealLastKey, currentKey; 584 lastKey = idealLastKey, currentKey;
585 MDB_val reference; 585 MDB_val reference;
586 586
587 // Find the last key past the range
588 int rc = mdb_cursor_get(cursor, &lastKey, &reference, MDB_SET_RANGE);
589
590 // If only equal, move forward one spot
591 if (rc == MDB_SUCCESS && mdb_cmp(d->transaction, d->dbi, &lastKey, &idealLastKey) == 0) {
592 rc = mdb_cursor_get(cursor, &lastKey, &reference, MDB_NEXT);
593 }
594
595 bool untilEnd = false;
596 if (rc != MDB_SUCCESS) {
597 // Nothing is greater than the upper bound, meaning search until the end
598 untilEnd = true;
599 }
600
601 // Find the first key in the range 587 // Find the first key in the range
602 rc = mdb_cursor_get(cursor, &firstKey, &reference, MDB_SET_RANGE); 588 int rc = mdb_cursor_get(cursor, &firstKey, &reference, MDB_SET_RANGE);
603 589
604 if (rc != MDB_SUCCESS) { 590 if (rc != MDB_SUCCESS) {
605 // Nothing is greater or equal than the lower bound, meaning no result 591 // Nothing is greater or equal than the lower bound, meaning no result
@@ -608,16 +594,17 @@ int DataStore::NamedDatabase::findAllInRange(const QByteArray &lowerBound, const
608 594
609 currentKey = firstKey; 595 currentKey = firstKey;
610 596
611 do { 597 // If already bigger than the upper bound
612 // If we have a stopping point and this is the stopping point 598 if (mdb_cmp(d->transaction, d->dbi, &currentKey, &idealLastKey) > 0) {
613 if (!untilEnd && mdb_cmp(d->transaction, d->dbi, &currentKey, &lastKey) == 0) { 599 return 0;
614 break; 600 }
615 }
616 601
602 do {
617 const auto currentBAKey = QByteArray::fromRawData((char *)currentKey.mv_data, currentKey.mv_size); 603 const auto currentBAKey = QByteArray::fromRawData((char *)currentKey.mv_data, currentKey.mv_size);
618 const auto currentBAValue = QByteArray::fromRawData((char *)reference.mv_data, reference.mv_size); 604 const auto currentBAValue = QByteArray::fromRawData((char *)reference.mv_data, reference.mv_size);
619 resultHandler(currentBAKey, currentBAValue); 605 resultHandler(currentBAKey, currentBAValue);
620 } while (mdb_cursor_get(cursor, &currentKey, &reference, MDB_NEXT) == MDB_SUCCESS); 606 } while (mdb_cursor_get(cursor, &currentKey, &reference, MDB_NEXT) == MDB_SUCCESS &&
607 mdb_cmp(d->transaction, d->dbi, &currentKey, &idealLastKey) <= 0);
621} 608}
622 609
623qint64 DataStore::NamedDatabase::getSize() 610qint64 DataStore::NamedDatabase::getSize()