From 6746247a49f09287ae4924c5c3df791f9bf61cbc Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sat, 22 Aug 2015 11:25:26 +0200 Subject: Use named databases in storage. This will allow us to create indexes in the same store. --- tests/indextest.cpp | 21 +++++---- tests/storagebenchmark.cpp | 8 +++- tests/storagetest.cpp | 114 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 12 deletions(-) (limited to 'tests') diff --git a/tests/indextest.cpp b/tests/indextest.cpp index 24f90c8..e3eabcc 100644 --- a/tests/indextest.cpp +++ b/tests/indextest.cpp @@ -13,38 +13,39 @@ class IndexTest : public QObject private Q_SLOTS: void initTestCase() { - Akonadi2::Storage store(Akonadi2::Store::storageLocation(), "org.kde.dummy.testindex", Akonadi2::Storage::ReadWrite); + Akonadi2::Storage store("./testindex", "org.kde.dummy.testindex", Akonadi2::Storage::ReadWrite); store.removeFromDisk(); } void cleanup() { - Akonadi2::Storage store(Akonadi2::Store::storageLocation(), "org.kde.dummy.testindex", Akonadi2::Storage::ReadWrite); + Akonadi2::Storage store("./testindex", "org.kde.dummy.testindex", Akonadi2::Storage::ReadWrite); store.removeFromDisk(); } void testIndex() { - Index index(Akonadi2::Store::storageLocation(), "org.kde.dummy.testindex", Akonadi2::Storage::ReadWrite); - index.add("key1", "value1"); - index.add("key1", "value2"); - index.add("key2", "value3"); + Index index("./testindex", "org.kde.dummy.testindex", Akonadi2::Storage::ReadWrite); + //The first key is specifically a substring of the second key + index.add("key", "value1"); + index.add("keyFoo", "value2"); + index.add("keyFoo", "value3"); { QList values; - index.lookup(QByteArray("key1"), [&values](const QByteArray &value) { + index.lookup(QByteArray("key"), [&values](const QByteArray &value) { values << value; }, [](const Index::Error &error){ qWarning() << "Error: "; }); - QCOMPARE(values.size(), 2); + QCOMPARE(values.size(), 1); } { QList values; - index.lookup(QByteArray("key2"), [&values](const QByteArray &value) { + index.lookup(QByteArray("keyFoo"), [&values](const QByteArray &value) { values << value; }, [](const Index::Error &error){ qWarning() << "Error: "; }); - QCOMPARE(values.size(), 1); + QCOMPARE(values.size(), 2); } { QList values; diff --git a/tests/storagebenchmark.cpp b/tests/storagebenchmark.cpp index ce1005d..f143c4d 100644 --- a/tests/storagebenchmark.cpp +++ b/tests/storagebenchmark.cpp @@ -97,9 +97,12 @@ private Q_SLOTS: auto event = createEvent(); if (store) { auto transaction = store->createTransaction(Akonadi2::Storage::ReadWrite); - transaction.setAutocommit(10000); for (int i = 0; i < count; i++) { transaction.write(keyPrefix + QByteArray::number(i), event); + if ((i % 10000) == 0) { + transaction.commit(); + transaction = store->createTransaction(Akonadi2::Storage::ReadWrite); + } } transaction.commit(); } else { @@ -116,8 +119,9 @@ private Q_SLOTS: { if (store) { auto transaction = store->createTransaction(Akonadi2::Storage::ReadOnly); + auto db = transaction.openDatabase(); for (int i = 0; i < count; i++) { - transaction.scan(keyPrefix + QByteArray::number(i), [](const QByteArray &key, const QByteArray &value) -> bool { return true; }); + db.scan(keyPrefix + QByteArray::number(i), [](const QByteArray &key, const QByteArray &value) -> bool { return true; }); } } } diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp index 55ec888..fe80bb7 100644 --- a/tests/storagetest.cpp +++ b/tests/storagetest.cpp @@ -211,6 +211,120 @@ private Q_SLOTS: storage2.removeFromDisk(); } } + + void testNoDuplicates() + { + bool gotResult = false; + bool gotError = false; + Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite, false); + auto transaction = store.createTransaction(Akonadi2::Storage::ReadWrite); + auto db = transaction.openDatabase(); + db.write("key","value"); + db.write("key","value"); + + int numValues = db.scan("", [&](const QByteArray &key, const QByteArray &value) -> bool { + gotResult = true; + return true; + }, + [&](const Akonadi2::Storage::Error &error) { + qDebug() << error.message; + gotError = true; + }); + + QCOMPARE(numValues, 1); + QVERIFY(!gotError); + } + + void testDuplicates() + { + bool gotResult = false; + bool gotError = false; + Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite, true); + auto transaction = store.createTransaction(Akonadi2::Storage::ReadWrite); + auto db = transaction.openDatabase(); + db.write("key","value1"); + db.write("key","value2"); + int numValues = db.scan("key", [&](const QByteArray &key, const QByteArray &value) -> bool { + gotResult = true; + return true; + }, + [&](const Akonadi2::Storage::Error &error) { + qDebug() << error.message; + gotError = true; + }); + + QCOMPARE(numValues, 2); + QVERIFY(!gotError); + } + + void testNonexitingNamedDb() + { + bool gotResult = false; + bool gotError = false; + Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadOnly); + int numValues = store.createTransaction(Akonadi2::Storage::ReadOnly).openDatabase("test").scan("", [&](const QByteArray &key, const QByteArray &value) -> bool { + gotResult = true; + return false; + }, + [&](const Akonadi2::Storage::Error &error) { + qDebug() << error.message; + gotError = true; + }); + QCOMPARE(numValues, 0); + QVERIFY(!gotResult); + QVERIFY(!gotError); + } + + void testWriteToNamedDb() + { + bool gotError = false; + Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite); + store.createTransaction(Akonadi2::Storage::ReadWrite).openDatabase("test").write("key1", "value1", [&](const Akonadi2::Storage::Error &error) { + qDebug() << error.message; + gotError = true; + }); + QVERIFY(!gotError); + } + + void testWriteDuplicatesToNamedDb() + { + bool gotError = false; + Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite, true); + store.createTransaction(Akonadi2::Storage::ReadWrite).openDatabase("test").write("key1", "value1", [&](const Akonadi2::Storage::Error &error) { + qDebug() << error.message; + gotError = true; + }); + QVERIFY(!gotError); + } + + //By default we want only exact matches + void testSubstringKeys() + { + Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite, true); + auto transaction = store.createTransaction(Akonadi2::Storage::ReadWrite); + auto db = transaction.openDatabase(); + db.write("sub","value1"); + db.write("subsub","value2"); + int numValues = db.scan("sub", [&](const QByteArray &key, const QByteArray &value) -> bool { + return true; + }); + + QCOMPARE(numValues, 1); + } + + //Ensure we don't retrieve a key that is greater than the current key. We only want equal keys. + void testKeyRange() + { + Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite, true); + auto transaction = store.createTransaction(Akonadi2::Storage::ReadWrite); + auto db = transaction.openDatabase(); + db.write("sub1","value1"); + int numValues = db.scan("sub", [&](const QByteArray &key, const QByteArray &value) -> bool { + return true; + }); + + QCOMPARE(numValues, 0); + } }; QTEST_MAIN(StorageTest) -- cgit v1.2.3