1
0
Fork 0
forked from blue/lmdbal

Sessions to manage db open state

This commit is contained in:
Blue 2025-05-06 00:22:03 +03:00
parent 3701fb92a1
commit f9902bc0b1
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
13 changed files with 342 additions and 124 deletions

View file

@ -3,6 +3,7 @@
#include "base.h"
#include "storage.h"
#include "cache.h"
#include "session.h"
#include <QString>
#include <QVariant>
@ -34,13 +35,14 @@ protected:
}
static void TearDownTestSuite() {
db->close();
session.close();
db->removeDirectory();
delete db;
db = nullptr;
}
static LMDBAL::Base* db;
static LMDBAL::Session session;
LMDBAL::Storage<uint32_t, uint32_t>* t1;
LMDBAL::Storage<QString, QString>* t2;
@ -50,19 +52,20 @@ protected:
LMDBAL::Base* BaseTest::db = nullptr;
LMDBAL::Session BaseTest::session;
TEST_F(BaseTest, RemovingDirectory) {
EXPECT_EQ(db->removeDirectory(), true);
}
TEST_F(BaseTest, OpeningClosingDatabase) {
EXPECT_EQ(db->ready(), false);
db->open();
EXPECT_EQ(db->ready(), true);
db->close();
EXPECT_EQ(db->ready(), false);
db->open();
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), false);
session = db->open();
EXPECT_EQ(db->opened(), true);
session.close();
EXPECT_EQ(db->opened(), false);
session = db->open();
EXPECT_EQ(db->opened(), true);
}
TEST_F(BaseTest, Flags) {
@ -93,7 +96,7 @@ TEST_F(BaseTest, Flags) {
}
TEST_F(BaseTest, AddingIntegerKey) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
t1->addRecord(1, 2);
t1->addRecord(2, 2);
t1->addRecord(3, 15);
@ -101,7 +104,7 @@ TEST_F(BaseTest, AddingIntegerKey) {
}
TEST_F(BaseTest, AddingQStringKey) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
t2->addRecord("hello", "world");
t2->addRecord("aaa", "gagdfsdf");
t2->addRecord("sdfhga", "DSFFDG");
@ -110,7 +113,7 @@ TEST_F(BaseTest, AddingQStringKey) {
}
TEST_F(BaseTest, AddingKeysToCache) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
c1->addRecord(2, "blah balah");
c1->addRecord(-4, "testing goes brrr");
c1->addRecord(40, "whatever");
@ -119,7 +122,7 @@ TEST_F(BaseTest, AddingKeysToCache) {
}
TEST_F(BaseTest, AddingKeysToVariableCache) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
c2->addRecord("regrets", "blah balah");
c2->addRecord("fossil fingers", 842);
c2->addRecord("preloaded cut", 539.75);
@ -132,7 +135,7 @@ TEST_F(BaseTest, AddingKeysToVariableCache) {
}
TEST_F(BaseTest, AddingRepeatingKey) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
EXPECT_THROW(t1->addRecord(3, 24), LMDBAL::Exist);
EXPECT_EQ(t1->getRecord(3), 15);
@ -148,7 +151,7 @@ TEST_F(BaseTest, AddingRepeatingKey) {
}
TEST_F(BaseTest, GettingNotExistingKeys) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
EXPECT_THROW(t2->getRecord("almonds"), LMDBAL::NotFound);
EXPECT_THROW(t1->getRecord(64), LMDBAL::NotFound);
@ -157,21 +160,21 @@ TEST_F(BaseTest, GettingNotExistingKeys) {
}
TEST_F(BaseTest, Persistence) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
uint32_t t1Size = t1->count();
uint32_t t2Size = t2->count();
uint32_t c1Size = c1->count();
uint32_t c2Size = c2->count();
db->close();
delete db;
EXPECT_EQ(session.opened(), false);
db = new LMDBAL::Base("testBase");
t1 = db->addStorage<uint32_t, uint32_t>("table1");
t2 = db->addStorage<QString, QString>("table2");
c1 = db->addCache<int8_t, std::string>("cache1");
c2 = db->addCache<std::string, QVariant>("cache2");
db->open();
session = db->open();
EXPECT_EQ(t1->count(), t1Size);
EXPECT_EQ(t1->getRecord(3), 15);
@ -212,7 +215,7 @@ TEST_F(BaseTest, Persistence) {
}
TEST_F(BaseTest, CountAndDrop) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
EXPECT_EQ(t1->count(), 3);
EXPECT_EQ(t2->count(), 4);
EXPECT_EQ(c1->count(), 4);
@ -237,7 +240,7 @@ TEST_F(BaseTest, CountAndDrop) {
}
TEST_F(BaseTest, Change) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
EXPECT_EQ(t1->count(), 1);
EXPECT_EQ(t2->count(), 1);
EXPECT_EQ(c1->count(), 2);
@ -283,7 +286,7 @@ TEST_F(BaseTest, Change) {
}
TEST_F(BaseTest, Force) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
EXPECT_EQ(t1->forceRecord(58, 35), false); //changing
EXPECT_EQ(t1->forceRecord(68, 36), true); //adding
@ -318,7 +321,7 @@ TEST_F(BaseTest, Force) {
}
TEST_F(BaseTest, ReadAll) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
std::map<uint32_t, uint32_t> m1 = t1->readAll();
std::map<QString, QString> m2 = t2->readAll();
@ -349,7 +352,7 @@ TEST_F(BaseTest, ReadAll) {
}
TEST_F(BaseTest, ReplaceAll) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
t1->replaceAll({
{7, 48},
@ -417,7 +420,7 @@ TEST_F(BaseTest, ReplaceAll) {
}
TEST_F(BaseTest, AddRecords) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
LMDBAL::SizeType s1 = t1->addRecords({
{5, 3},

View file

@ -4,6 +4,7 @@
#include "storage.h"
#include "cache.h"
#include "cursor.h"
#include "session.h"
class CacheCursorTest : public ::testing::Test {
protected:
@ -19,7 +20,7 @@ protected:
db = new LMDBAL::Base("testBase");
db->addCache<uint64_t, std::string>("table1");
db->addCache<uint64_t, std::string>("empty");
db->open();
session = db->open();
}
}
@ -30,7 +31,7 @@ protected:
static void TearDownTestSuite() {
cursor.drop();
transaction.terminate();
db->close();
session.close();
db->removeDirectory();
delete db;
db = nullptr;
@ -39,6 +40,7 @@ protected:
static LMDBAL::Base* db;
static LMDBAL::Cursor<uint64_t, std::string> cursor;
static LMDBAL::Transaction transaction;
static LMDBAL::Session session;
LMDBAL::Cache<uint64_t, std::string>* cache;
LMDBAL::Cache<uint64_t, std::string>* emptyCache;
@ -47,6 +49,7 @@ protected:
LMDBAL::Base* CacheCursorTest::db = nullptr;
LMDBAL::Cursor<uint64_t, std::string> CacheCursorTest::cursor;
LMDBAL::Transaction CacheCursorTest::transaction;
LMDBAL::Session CacheCursorTest::session;
static const std::map<uint64_t, std::string> data({
{245665783, "bothering nerds"},

View file

@ -4,6 +4,7 @@
#include "base.h"
#include "cache.h"
#include "session.h"
class CacheTransactionsTest : public testing::Test {
protected:
@ -39,18 +40,19 @@ protected:
db->addStorage<std::string, float>("cache2");
}
db->open();
session = db->open();
db->drop();
}
static void TearDownTestSuite() {
db->close();
session.close();
db->removeDirectory();
delete db;
db = nullptr;
}
static LMDBAL::Base* db;
static LMDBAL::Session session;
LMDBAL::Cache<int16_t, int64_t>* c1;
LMDBAL::Cache<std::string, float>* c2;
@ -58,9 +60,10 @@ protected:
LMDBAL::Base* CacheTransactionsTest::db = nullptr;
LMDBAL::Session CacheTransactionsTest::session;
TEST_F(CacheTransactionsTest, Adding) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
EXPECT_EQ(c1->count(), 0);
EXPECT_EQ(c2->count(), 0);
@ -90,7 +93,7 @@ TEST_F(CacheTransactionsTest, Adding) {
}
TEST_F(CacheTransactionsTest, Aborting) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
LMDBAL::SizeType s1 = c1->count();
LMDBAL::SizeType s2 = c2->count();
@ -114,7 +117,7 @@ TEST_F(CacheTransactionsTest, Aborting) {
}
TEST_F(CacheTransactionsTest, Reading) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
LMDBAL::Transaction txn = db->beginReadOnlyTransaction();
@ -132,7 +135,7 @@ TEST_F(CacheTransactionsTest, Reading) {
}
TEST_F(CacheTransactionsTest, ConcurentReading) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
LMDBAL::SizeType size = c1->count();
LMDBAL::WriteTransaction txn = db->beginTransaction();
@ -168,7 +171,7 @@ TEST_F(CacheTransactionsTest, ConcurentReading) {
TEST_F(CacheTransactionsTest, ConcurentModification) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
//if you start one writable transaction after another
//in a single thread like so:
@ -231,7 +234,7 @@ TEST_F(CacheTransactionsTest, ConcurentModification) {
}
TEST_F(CacheTransactionsTest, RAIIResourceFree) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
int pid = fork();
if (pid == 0) { // I am the child
@ -287,8 +290,8 @@ TEST_F(CacheTransactionsTest, TransactionTerminationOnClose) {
EXPECT_EQ(c1->getRecord(578, txn), 4552);
EXPECT_EQ(c1->checkRecord(578), false);
db->close();
db->open();
session.close();
session = db->open();
EXPECT_EQ(txn.isActive(), false);
EXPECT_THROW(c1->getRecord(578, txn), LMDBAL::TransactionTerminated);

View file

@ -7,6 +7,7 @@
#include "base.h"
#include "storage.h"
#include "cursor.h"
#include "session.h"
class DuplicatesTest : public ::testing::Test {
protected:
@ -35,18 +36,19 @@ protected:
db->addStorage<uint16_t, double>("intDouble", true);
db->addStorage<float, int64_t>("floatLong", true);
db->open();
session = db->open();
}
}
static void TearDownTestSuite() {
db->close();
session.close();
db->removeDirectory();
delete db;
db = nullptr;
}
static LMDBAL::Base* db;
static LMDBAL::Session session;
LMDBAL::Storage<int16_t, uint16_t>* tu1;
LMDBAL::Storage<std::string, int8_t>* tu2;
@ -56,6 +58,7 @@ protected:
};
LMDBAL::Base* DuplicatesTest::db = nullptr;
LMDBAL::Session DuplicatesTest::session;
TEST_F(DuplicatesTest, Flags) {
uint32_t tu1Flags = getTU1Flags();

View file

@ -3,6 +3,7 @@
#include "base.h"
#include "storage.h"
#include "cursor.h"
#include "session.h"
class StorageCursorTest : public ::testing::Test {
protected:
@ -18,7 +19,7 @@ protected:
db = new LMDBAL::Base("testBase");
db->addStorage<uint64_t, std::string>("table1");
db->addStorage<uint64_t, std::string>("empty");
db->open();
session = db->open();
}
}
@ -29,7 +30,7 @@ protected:
static void TearDownTestSuite() {
cursor.drop();
transaction.terminate();
db->close();
session.close();
db->removeDirectory();
delete db;
db = nullptr;
@ -38,6 +39,7 @@ protected:
static LMDBAL::Base* db;
static LMDBAL::Cursor<uint64_t, std::string> cursor;
static LMDBAL::Transaction transaction;
static LMDBAL::Session session;
LMDBAL::Storage<uint64_t, std::string>* table;
LMDBAL::Storage<uint64_t, std::string>* emptyTable;
@ -46,6 +48,7 @@ protected:
LMDBAL::Base* StorageCursorTest::db = nullptr;
LMDBAL::Cursor<uint64_t, std::string> StorageCursorTest::cursor;
LMDBAL::Transaction StorageCursorTest::transaction = LMDBAL::Transaction();
LMDBAL::Session StorageCursorTest::session = LMDBAL::Session();
static const std::map<uint64_t, std::string> data({
{245665783, "bothering nerds"},

View file

@ -4,6 +4,7 @@
#include "base.h"
#include "storage.h"
#include "session.h"
class StorageTransactionsTest : public testing::Test {
protected:
@ -39,18 +40,19 @@ protected:
db->addStorage<std::string, float>("table2");
}
db->open();
session = db->open();
db->drop();
}
static void TearDownTestSuite() {
db->close();
session.close();
db->removeDirectory();
delete db;
db = nullptr;
}
static LMDBAL::Base* db;
static LMDBAL::Session session;
LMDBAL::Storage<int16_t, int64_t>* t1;
LMDBAL::Storage<std::string, float>* t2;
@ -58,9 +60,10 @@ protected:
LMDBAL::Base* StorageTransactionsTest::db = nullptr;
LMDBAL::Session StorageTransactionsTest::session;
TEST_F(StorageTransactionsTest, Adding) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
EXPECT_EQ(t1->count(), 0);
EXPECT_EQ(t2->count(), 0);
@ -90,7 +93,7 @@ TEST_F(StorageTransactionsTest, Adding) {
}
TEST_F(StorageTransactionsTest, Aborting) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
LMDBAL::SizeType s1 = t1->count();
LMDBAL::SizeType s2 = t2->count();
@ -114,7 +117,7 @@ TEST_F(StorageTransactionsTest, Aborting) {
}
TEST_F(StorageTransactionsTest, Reading) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
LMDBAL::Transaction txn = db->beginReadOnlyTransaction();
@ -132,7 +135,7 @@ TEST_F(StorageTransactionsTest, Reading) {
}
TEST_F(StorageTransactionsTest, ConcurentReading) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
LMDBAL::SizeType size = t1->count();
LMDBAL::WriteTransaction txn = db->beginTransaction();
@ -167,7 +170,7 @@ TEST_F(StorageTransactionsTest, ConcurentReading) {
}
TEST_F(StorageTransactionsTest, ConcurentModification) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
//if you start one writable transaction after another
//in a single thread like so:
@ -230,7 +233,7 @@ TEST_F(StorageTransactionsTest, ConcurentModification) {
}
TEST_F(StorageTransactionsTest, RAIIResourceFree) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(db->opened(), true);
int pid = fork();
if (pid == 0) { // I am the child
@ -286,8 +289,8 @@ TEST_F(StorageTransactionsTest, TransactionTerminationOnClose) {
EXPECT_EQ(t1->getRecord(543, txn), 229);
EXPECT_EQ(t1->checkRecord(543), false);
db->close();
db->open();
session.close();
session = db->open();
EXPECT_EQ(txn.isActive(), false);
EXPECT_THROW(t1->getRecord(543, txn), LMDBAL::TransactionTerminated);