From 1b31818b3cd48be19e2e29eefe91ecec2cbb69e2 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 27 Feb 2017 22:19:21 +0100 Subject: Make opening dbis non-racy Dbis can only be opened by one thread and should then be shared accross all threads after committing the transaction to create the dbi. This requires us to initially open all db's, which in turn requires us to know the correct flags. This patch stores the flags to open each db in a separate db, and then opens up all databases on initial start. If a new database is created that dbi is as well shared as soon as the transaction is committed (before the dbi is private to the transaction). --- tests/storagetest.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'tests') diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp index 5a517c7..7202628 100644 --- a/tests/storagetest.cpp +++ b/tests/storagetest.cpp @@ -451,6 +451,42 @@ private slots: db.findLatest(uid, [&](const QByteArray &key, const QByteArray &value) { result = value; }); QCOMPARE(result, QByteArray("value2")); } + + void testTransactionVisibility() + { + auto readValue = [](const Sink::Storage::DataStore::NamedDatabase &db, const QByteArray) { + QByteArray result; + db.scan("key1", [&](const QByteArray &, const QByteArray &value) { + result = value; + return true; + }); + return result; + }; + { + Sink::Storage::DataStore store(testDataPath, dbName, Sink::Storage::DataStore::ReadWrite); + auto transaction = store.createTransaction(Sink::Storage::DataStore::ReadWrite); + + auto db = transaction.openDatabase("testTransactionVisibility", nullptr, false); + db.write("key1", "foo"); + QCOMPARE(readValue(db, "key1"), QByteArray("foo")); + + { + auto transaction2 = store.createTransaction(Sink::Storage::DataStore::ReadOnly); + auto db2 = transaction2 + .openDatabase("testTransactionVisibility", nullptr, false); + QCOMPARE(readValue(db2, "key1"), QByteArray()); + } + transaction.commit(); + { + auto transaction2 = store.createTransaction(Sink::Storage::DataStore::ReadOnly); + auto db2 = transaction2 + .openDatabase("testTransactionVisibility", nullptr, false); + QCOMPARE(readValue(db2, "key1"), QByteArray("foo")); + } + + } + } + }; QTEST_MAIN(StorageTest) -- cgit v1.2.3