diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-06-05 17:45:22 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-06-05 17:45:22 +0200 |
commit | b135813110db7de8625bd2902e49afd971af9756 (patch) | |
tree | 2eb1329ea0458db8207e469bc3a3d496b72076e6 | |
parent | 245dc57adedb0c8225aee0066c39f5ea38a39610 (diff) | |
download | sink-b135813110db7de8625bd2902e49afd971af9756.tar.gz sink-b135813110db7de8625bd2902e49afd971af9756.zip |
Skip internal keys by default while scanning.
-rw-r--r-- | common/storage.h | 2 | ||||
-rw-r--r-- | common/storage_lmdb.cpp | 26 | ||||
-rw-r--r-- | tests/storagetest.cpp | 1 |
3 files changed, 19 insertions, 10 deletions
diff --git a/common/storage.h b/common/storage.h index 2661439..a04969b 100644 --- a/common/storage.h +++ b/common/storage.h | |||
@@ -87,7 +87,7 @@ public: | |||
87 | * @return The number of values retrieved. | 87 | * @return The number of values retrieved. |
88 | */ | 88 | */ |
89 | int scan(const QByteArray &key, const std::function<bool(const QByteArray &key, const QByteArray &value)> &resultHandler, | 89 | int scan(const QByteArray &key, const std::function<bool(const QByteArray &key, const QByteArray &value)> &resultHandler, |
90 | const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>(), bool findSubstringKeys = false) const; | 90 | const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>(), bool findSubstringKeys = false, bool skipInternalKeys = true) const; |
91 | 91 | ||
92 | /** | 92 | /** |
93 | * Finds the last value in a series matched by prefix. | 93 | * Finds the last value in a series matched by prefix. |
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index b39f7df..aeb4dd6 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp | |||
@@ -179,7 +179,7 @@ void Storage::NamedDatabase::remove(const QByteArray &k, const QByteArray &value | |||
179 | } | 179 | } |
180 | 180 | ||
181 | int Storage::NamedDatabase::scan(const QByteArray &k, const std::function<bool(const QByteArray &key, const QByteArray &value)> &resultHandler, | 181 | int Storage::NamedDatabase::scan(const QByteArray &k, const std::function<bool(const QByteArray &key, const QByteArray &value)> &resultHandler, |
182 | const std::function<void(const Storage::Error &error)> &errorHandler, bool findSubstringKeys) const | 182 | const std::function<void(const Storage::Error &error)> &errorHandler, bool findSubstringKeys, bool skipInternalKeys) const |
183 | { | 183 | { |
184 | if (!d || !d->transaction) { | 184 | if (!d || !d->transaction) { |
185 | // Not an error. We rely on this to read nothing from non-existing databases. | 185 | // Not an error. We rely on this to read nothing from non-existing databases. |
@@ -209,10 +209,14 @@ int Storage::NamedDatabase::scan(const QByteArray &k, const std::function<bool(c | |||
209 | op = MDB_SET_RANGE; | 209 | op = MDB_SET_RANGE; |
210 | } | 210 | } |
211 | if ((rc = mdb_cursor_get(cursor, &key, &data, op)) == 0) { | 211 | if ((rc = mdb_cursor_get(cursor, &key, &data, op)) == 0) { |
212 | const auto current = QByteArray::fromRawData((char *)key.mv_data, key.mv_size); | ||
212 | // The first lookup will find a key that is equal or greather than our key | 213 | // The first lookup will find a key that is equal or greather than our key |
213 | if (QByteArray::fromRawData((char *)key.mv_data, key.mv_size).startsWith(k)) { | 214 | if (current.startsWith(k)) { |
214 | numberOfRetrievedValues++; | 215 | const bool callResultHandler = !(skipInternalKeys && isInternalKey(current)); |
215 | if (resultHandler(QByteArray::fromRawData((char *)key.mv_data, key.mv_size), QByteArray::fromRawData((char *)data.mv_data, data.mv_size))) { | 216 | if (callResultHandler) { |
217 | numberOfRetrievedValues++; | ||
218 | } | ||
219 | if (!callResultHandler || resultHandler(current, QByteArray::fromRawData((char *)data.mv_data, data.mv_size))) { | ||
216 | if (findSubstringKeys) { | 220 | if (findSubstringKeys) { |
217 | // Reset the key to what we search for | 221 | // Reset the key to what we search for |
218 | key.mv_data = (void *)k.constData(); | 222 | key.mv_data = (void *)k.constData(); |
@@ -220,11 +224,15 @@ int Storage::NamedDatabase::scan(const QByteArray &k, const std::function<bool(c | |||
220 | } | 224 | } |
221 | MDB_cursor_op nextOp = (d->allowDuplicates && !findSubstringKeys) ? MDB_NEXT_DUP : MDB_NEXT; | 225 | MDB_cursor_op nextOp = (d->allowDuplicates && !findSubstringKeys) ? MDB_NEXT_DUP : MDB_NEXT; |
222 | while ((rc = mdb_cursor_get(cursor, &key, &data, nextOp)) == 0) { | 226 | while ((rc = mdb_cursor_get(cursor, &key, &data, nextOp)) == 0) { |
223 | // Every consequent lookup simply iterates through the list | 227 | const auto current = QByteArray::fromRawData((char *)key.mv_data, key.mv_size); |
224 | if (QByteArray::fromRawData((char *)key.mv_data, key.mv_size).startsWith(k)) { | 228 | // Every consequitive lookup simply iterates through the list |
225 | numberOfRetrievedValues++; | 229 | if (current.startsWith(k)) { |
226 | if (!resultHandler(QByteArray::fromRawData((char *)key.mv_data, key.mv_size), QByteArray::fromRawData((char *)data.mv_data, data.mv_size))) { | 230 | const bool callResultHandler = !(skipInternalKeys && isInternalKey(current)); |
227 | break; | 231 | if (callResultHandler) { |
232 | numberOfRetrievedValues++; | ||
233 | if (!resultHandler(current, QByteArray::fromRawData((char *)data.mv_data, data.mv_size))) { | ||
234 | break; | ||
235 | } | ||
228 | } | 236 | } |
229 | } | 237 | } |
230 | } | 238 | } |
diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp index ee6900d..e199faf 100644 --- a/tests/storagetest.cpp +++ b/tests/storagetest.cpp | |||
@@ -245,6 +245,7 @@ private slots: | |||
245 | 245 | ||
246 | QCOMPARE(numValues, 1); | 246 | QCOMPARE(numValues, 1); |
247 | QVERIFY(!gotError); | 247 | QVERIFY(!gotError); |
248 | QVERIFY(gotResult); | ||
248 | } | 249 | } |
249 | 250 | ||
250 | void testDuplicates() | 251 | void testDuplicates() |