forked from blue/lmdbal
a method to get the size of the storage with opened transaction, tests for readAll, and some test for transactions
This commit is contained in:
parent
17fb37075c
commit
a4bb7e6269
@ -59,25 +59,31 @@ void LMDBAL::iStorage::ensureOpened(const std::string& methodName) const {
|
|||||||
throw Closed(methodName, db->name, name);
|
throw Closed(methodName, db->name, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t LMDBAL::iStorage::count() const {
|
LMDBAL::SizeType LMDBAL::iStorage::count() const {
|
||||||
ensureOpened(countMethodName);
|
ensureOpened(countMethodName);
|
||||||
|
|
||||||
MDB_txn *txn;
|
TransactionID txn = beginReadOnlyTransaction();
|
||||||
|
SizeType amount;
|
||||||
|
try {
|
||||||
|
amount = count(txn);
|
||||||
|
} catch (...) {
|
||||||
|
abortTransaction(txn);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
abortTransaction(txn);
|
||||||
|
|
||||||
|
return amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
LMDBAL::SizeType LMDBAL::iStorage::count(TransactionID txn) const {
|
||||||
MDB_stat stat;
|
MDB_stat stat;
|
||||||
int rc = mdb_txn_begin(db->environment, NULL, MDB_RDONLY, &txn);
|
int rc = mdb_stat(txn, dbi, &stat);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
mdb_txn_abort(txn);
|
mdb_txn_abort(txn);
|
||||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = mdb_stat(txn, dbi, &stat);
|
return stat.ms_entries;
|
||||||
if (rc) {
|
|
||||||
mdb_txn_abort(txn);
|
|
||||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
|
||||||
}
|
|
||||||
uint32_t amount = stat.ms_entries;
|
|
||||||
mdb_txn_abort(txn);
|
|
||||||
return amount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LMDBAL::iStorage::throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const {
|
void LMDBAL::iStorage::throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const {
|
||||||
|
@ -24,8 +24,12 @@
|
|||||||
|
|
||||||
namespace LMDBAL {
|
namespace LMDBAL {
|
||||||
|
|
||||||
|
typedef uint32_t SizeType;
|
||||||
|
|
||||||
class iStorage {
|
class iStorage {
|
||||||
friend class Base;
|
friend class Base;
|
||||||
|
public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
iStorage(const std::string& name, Base* parent);
|
iStorage(const std::string& name, Base* parent);
|
||||||
virtual ~iStorage();
|
virtual ~iStorage();
|
||||||
@ -48,7 +52,8 @@ protected:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
virtual void drop();
|
virtual void drop();
|
||||||
virtual uint32_t count() const;
|
virtual SizeType count() const;
|
||||||
|
virtual SizeType count(TransactionID txn) const;
|
||||||
|
|
||||||
TransactionID beginReadOnlyTransaction() const;
|
TransactionID beginReadOnlyTransaction() const;
|
||||||
TransactionID beginTransaction() const;
|
TransactionID beginTransaction() const;
|
||||||
|
@ -5,6 +5,7 @@ include_directories(${GTEST_INCLUDE_DIR})
|
|||||||
add_executable(runUnitTests
|
add_executable(runUnitTests
|
||||||
basic.cpp
|
basic.cpp
|
||||||
serialization.cpp
|
serialization.cpp
|
||||||
|
storagetransaction.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_compile_options(runUnitTests PRIVATE -fPIC)
|
target_compile_options(runUnitTests PRIVATE -fPIC)
|
||||||
|
@ -256,12 +256,12 @@ TEST_F(BaseTest, Change) {
|
|||||||
TEST_F(BaseTest, Force) {
|
TEST_F(BaseTest, Force) {
|
||||||
EXPECT_EQ(db->ready(), true);
|
EXPECT_EQ(db->ready(), true);
|
||||||
|
|
||||||
t1->forceRecord(58, 35); //changing
|
EXPECT_EQ(t1->forceRecord(58, 35), false); //changing
|
||||||
t1->forceRecord(68, 36); //adding
|
EXPECT_EQ(t1->forceRecord(68, 36), true); //adding
|
||||||
t2->forceRecord("prophecy", "dumpling"); //adding
|
EXPECT_EQ(t2->forceRecord("prophecy", "dumpling"), true); //adding
|
||||||
t2->forceRecord("lawfirm", "paracetamol"); //changing
|
EXPECT_EQ(t2->forceRecord("lawfirm", "paracetamol"), false); //changing
|
||||||
c1->forceRecord(89, "canine"); //changing
|
EXPECT_EQ(c1->forceRecord(89, "canine"), false); //changing
|
||||||
c1->forceRecord(98, "duration"); //adding
|
EXPECT_EQ(c1->forceRecord(98, "duration"), true); //adding
|
||||||
|
|
||||||
EXPECT_EQ(t1->getRecord(2), 49);
|
EXPECT_EQ(t1->getRecord(2), 49);
|
||||||
EXPECT_EQ(t1->getRecord(58), 35);
|
EXPECT_EQ(t1->getRecord(58), 35);
|
||||||
@ -278,3 +278,26 @@ TEST_F(BaseTest, Force) {
|
|||||||
EXPECT_EQ(t2->count(), 3);
|
EXPECT_EQ(t2->count(), 3);
|
||||||
EXPECT_EQ(c1->count(), 4);
|
EXPECT_EQ(c1->count(), 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(BaseTest, ReadAll) {
|
||||||
|
EXPECT_EQ(db->ready(), true);
|
||||||
|
|
||||||
|
std::map<uint32_t, uint32_t> m1 = t1->readAll();
|
||||||
|
std::map<QString, QString> m2 = t2->readAll();
|
||||||
|
std::map<int8_t, std::string> m3 = c1->readAll();
|
||||||
|
|
||||||
|
EXPECT_EQ(m1.at(2), 49);
|
||||||
|
EXPECT_EQ(m1.at(58), 35);
|
||||||
|
EXPECT_EQ(m1.at(68), 36);
|
||||||
|
EXPECT_EQ(m2.at("sdfhga"), "void");
|
||||||
|
EXPECT_EQ(m2.at("prophecy"), "dumpling");
|
||||||
|
EXPECT_EQ(m2.at("lawfirm"), "paracetamol");
|
||||||
|
EXPECT_EQ(m3.at(15), "recording");
|
||||||
|
EXPECT_EQ(m3.at(12), "thermal");
|
||||||
|
EXPECT_EQ(m3.at(89), "canine");
|
||||||
|
EXPECT_EQ(m3.at(98), "duration");
|
||||||
|
|
||||||
|
EXPECT_EQ(m1.size(), 3);
|
||||||
|
EXPECT_EQ(m2.size(), 3);
|
||||||
|
EXPECT_EQ(m3.size(), 4);
|
||||||
|
}
|
||||||
|
@ -19,11 +19,11 @@ TEST(Serialization, Double) {
|
|||||||
double destination;
|
double destination;
|
||||||
serializer.deserialize(data, destination);
|
serializer.deserialize(data, destination);
|
||||||
|
|
||||||
EXPECT_EQ(source, destination);
|
EXPECT_DOUBLE_EQ(source, destination);
|
||||||
|
|
||||||
double dest2 = serializer.deserialize(data);
|
double dest2 = serializer.deserialize(data);
|
||||||
|
|
||||||
EXPECT_EQ(source, dest2);
|
EXPECT_DOUBLE_EQ(source, dest2);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Serialization, Float) {
|
TEST(Serialization, Float) {
|
||||||
@ -42,11 +42,11 @@ TEST(Serialization, Float) {
|
|||||||
float destination;
|
float destination;
|
||||||
serializer.deserialize(data, destination);
|
serializer.deserialize(data, destination);
|
||||||
|
|
||||||
EXPECT_EQ(source, destination);
|
EXPECT_FLOAT_EQ(source, destination);
|
||||||
|
|
||||||
float dest2 = serializer.deserialize(data);
|
float dest2 = serializer.deserialize(data);
|
||||||
|
|
||||||
EXPECT_EQ(source, dest2);
|
EXPECT_FLOAT_EQ(source, dest2);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Serialization, Int8) {
|
TEST(Serialization, Int8) {
|
||||||
|
111
test/storagetransaction.cpp
Normal file
111
test/storagetransaction.cpp
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include "base.h"
|
||||||
|
#include "storage.h"
|
||||||
|
|
||||||
|
class StorageTransactionsTest : public testing::Test {
|
||||||
|
protected:
|
||||||
|
StorageTransactionsTest():
|
||||||
|
testing::Test(),
|
||||||
|
t1(db->getStorage<int16_t, int64_t>("table1")),
|
||||||
|
t2(db->getStorage<std::string, float>("table2")) {}
|
||||||
|
|
||||||
|
~StorageTransactionsTest() {}
|
||||||
|
|
||||||
|
static void SetUpTestSuite() {
|
||||||
|
if (db == nullptr) {
|
||||||
|
db = new LMDBAL::Base("storageTrnansactionsTestBase");
|
||||||
|
db->addStorage<int16_t, int64_t>("table1");
|
||||||
|
db->addStorage<std::string, float>("table2");
|
||||||
|
}
|
||||||
|
|
||||||
|
db->open();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void TearDownTestSuite() {
|
||||||
|
db->close();
|
||||||
|
db->removeDirectory();
|
||||||
|
delete db;
|
||||||
|
db = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static LMDBAL::Base* db;
|
||||||
|
|
||||||
|
LMDBAL::Storage<int16_t, int64_t>* t1;
|
||||||
|
LMDBAL::Storage<std::string, float>* t2;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
LMDBAL::Base* StorageTransactionsTest::db = nullptr;
|
||||||
|
|
||||||
|
TEST_F(StorageTransactionsTest, Adding) {
|
||||||
|
EXPECT_EQ(db->ready(), true);
|
||||||
|
EXPECT_EQ(t1->count(), 0);
|
||||||
|
EXPECT_EQ(t2->count(), 0);
|
||||||
|
|
||||||
|
LMDBAL::TransactionID txn = db->beginTransaction();
|
||||||
|
t1->addRecord(5, 13, txn);
|
||||||
|
t1->addRecord(-53, 782, txn);
|
||||||
|
t1->addRecord(58392, -37829, txn);
|
||||||
|
|
||||||
|
t2->addRecord("lorem", 481, txn);
|
||||||
|
t2->addRecord("decallence", 8532.48, txn);
|
||||||
|
t2->addRecord("prevent recovery", -64.64, txn);
|
||||||
|
|
||||||
|
EXPECT_EQ(t1->count(), 0);
|
||||||
|
EXPECT_EQ(t2->count(), 0);
|
||||||
|
|
||||||
|
db->commitTransaction(txn);
|
||||||
|
|
||||||
|
EXPECT_EQ(t1->count(), 3);
|
||||||
|
EXPECT_EQ(t1->getRecord(5), 13);
|
||||||
|
EXPECT_EQ(t1->getRecord(-53), 782);
|
||||||
|
EXPECT_EQ(t1->getRecord(58392), -37829);
|
||||||
|
|
||||||
|
EXPECT_EQ(t2->count(), 3);
|
||||||
|
EXPECT_FLOAT_EQ(t2->getRecord("lorem"), 481);
|
||||||
|
EXPECT_FLOAT_EQ(t2->getRecord("decallence"), 8532.48);
|
||||||
|
EXPECT_FLOAT_EQ(t2->getRecord("prevent recovery"), -64.64);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StorageTransactionsTest, Aborting) {
|
||||||
|
EXPECT_EQ(db->ready(), true);
|
||||||
|
|
||||||
|
LMDBAL::SizeType s1 = t1->count();
|
||||||
|
LMDBAL::SizeType s2 = t2->count();
|
||||||
|
|
||||||
|
LMDBAL::TransactionID txn = t1->beginTransaction();
|
||||||
|
t1->addRecord(18, 40, txn);
|
||||||
|
t1->addRecord(85, -4, txn);
|
||||||
|
t1->addRecord(-5, -3, txn);
|
||||||
|
|
||||||
|
t2->addRecord("tapestry", .053, txn);
|
||||||
|
t2->addRecord("pepper plants are beautifull", -7, txn);
|
||||||
|
t2->addRecord("horrots", -23.976, txn);
|
||||||
|
|
||||||
|
EXPECT_EQ(t1->count(), s1);
|
||||||
|
EXPECT_EQ(t2->count(), s2);
|
||||||
|
|
||||||
|
t1->abortTransaction(txn);
|
||||||
|
|
||||||
|
EXPECT_EQ(t1->count(), s1);
|
||||||
|
EXPECT_EQ(t2->count(), s2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StorageTransactionsTest, Reading) {
|
||||||
|
EXPECT_EQ(db->ready(), true);
|
||||||
|
|
||||||
|
LMDBAL::TransactionID txn = db->beginReadOnlyTransaction();
|
||||||
|
|
||||||
|
EXPECT_EQ(t1->count(txn), 3);
|
||||||
|
EXPECT_EQ(t1->getRecord(5, txn), 13);
|
||||||
|
EXPECT_EQ(t1->getRecord(-53, txn), 782);
|
||||||
|
EXPECT_EQ(t1->getRecord(58392, txn), -37829);
|
||||||
|
|
||||||
|
EXPECT_EQ(t2->count(txn), 3);
|
||||||
|
EXPECT_FLOAT_EQ(t2->getRecord("lorem", txn), 481);
|
||||||
|
EXPECT_FLOAT_EQ(t2->getRecord("decallence", txn), 8532.48);
|
||||||
|
EXPECT_FLOAT_EQ(t2->getRecord("prevent recovery", txn), -64.64);
|
||||||
|
|
||||||
|
db->abortTransaction(txn);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user