From d89c27c9a1cc4d2c4ea806d71653e32c2891077b Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sat, 20 Feb 2016 11:29:30 +0100 Subject: Get substring matches to work with sorted duplicates in store --- common/storage_lmdb.cpp | 7 ++++++- tests/indextest.cpp | 9 +++++++++ tests/storagetest.cpp | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index 8cb965f..2d8b187 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp @@ -224,7 +224,12 @@ int Storage::NamedDatabase::scan(const QByteArray &k, if (QByteArray::fromRawData((char*)key.mv_data, key.mv_size).startsWith(k)) { numberOfRetrievedValues++; if (resultHandler(QByteArray::fromRawData((char*)key.mv_data, key.mv_size), QByteArray::fromRawData((char*)data.mv_data, data.mv_size))) { - MDB_cursor_op nextOp = d->allowDuplicates ? MDB_NEXT_DUP : MDB_NEXT; + if (findSubstringKeys) { + //Reset the key to what we search for + key.mv_data = (void*)k.constData(); + key.mv_size = k.size(); + } + MDB_cursor_op nextOp = (d->allowDuplicates && !findSubstringKeys) ? MDB_NEXT_DUP : MDB_NEXT; while ((rc = mdb_cursor_get(cursor, &key, &data, nextOp)) == 0) { //Every consequent lookup simply iterates through the list if (QByteArray::fromRawData((char*)key.mv_data, key.mv_size).startsWith(k)) { diff --git a/tests/indextest.cpp b/tests/indextest.cpp index ce1fde7..1676ecb 100644 --- a/tests/indextest.cpp +++ b/tests/indextest.cpp @@ -58,6 +58,15 @@ private slots: [](const Index::Error &error){ qWarning() << "Error: "; }); QCOMPARE(values.size(), 0); } + { + QList values; + index.lookup(QByteArray("key"), [&values](const QByteArray &value) { + values << value; + }, + [](const Index::Error &error){ qWarning() << "Error: "; }, + true); + QCOMPARE(values.size(), 3); + } } }; diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp index 6341808..ebd9b8f 100644 --- a/tests/storagetest.cpp +++ b/tests/storagetest.cpp @@ -334,6 +334,21 @@ private slots: QCOMPARE(numValues, 2); } + void testFindSubstringKeysWithDuplicatesEnabled() + { + Sink::Storage store(testDataPath, dbName, Sink::Storage::ReadWrite); + auto transaction = store.createTransaction(Sink::Storage::ReadWrite); + auto db = transaction.openDatabase("test", nullptr, true); + db.write("sub","value1"); + db.write("subsub","value2"); + db.write("wubsub","value3"); + int numValues = db.scan("sub", [&](const QByteArray &key, const QByteArray &value) -> bool { + return true; + }, nullptr, true); + + QCOMPARE(numValues, 2); + } + void testKeySorting() { Sink::Storage store(testDataPath, dbName, Sink::Storage::ReadWrite); -- cgit v1.2.3