forked from blue/lmdbal
some more tests, one subtle but important bugfix with cache
This commit is contained in:
parent
8ff5672655
commit
d57d27f952
@ -1,7 +1,7 @@
|
|||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
project(LMDBAL
|
project(LMDBAL
|
||||||
VERSION 0.3.1
|
VERSION 0.4.0
|
||||||
DESCRIPTION "LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer"
|
DESCRIPTION "LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer"
|
||||||
LANGUAGES CXX
|
LANGUAGES CXX
|
||||||
)
|
)
|
||||||
|
@ -57,6 +57,9 @@ protected:
|
|||||||
virtual void transactionStarted(TransactionID txn, bool readOnly) const override;
|
virtual void transactionStarted(TransactionID txn, bool readOnly) const override;
|
||||||
virtual void transactionCommited(TransactionID txn) override;
|
virtual void transactionCommited(TransactionID txn) override;
|
||||||
virtual void transactionAborted(TransactionID txn) const override;
|
virtual void transactionAborted(TransactionID txn) const override;
|
||||||
|
|
||||||
|
virtual void discoveredRecord(const K& key, const V& value) const override;
|
||||||
|
virtual void discoveredRecord(const K& key, const V& value, TransactionID txn) const override;
|
||||||
private:
|
private:
|
||||||
void handleMode() const;
|
void handleMode() const;
|
||||||
|
|
||||||
@ -70,6 +73,7 @@ private:
|
|||||||
void handleReplaceAll(std::map<K, V>* data);
|
void handleReplaceAll(std::map<K, V>* data);
|
||||||
void handleAddRecords(const std::map<K, V>& data, bool overwrite, SizeType newSize);
|
void handleAddRecords(const std::map<K, V>& data, bool overwrite, SizeType newSize);
|
||||||
void handleDrop();
|
void handleDrop();
|
||||||
|
void appendToCache(const K& key, const V& value) const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using Storage<K, V>::drop;
|
using Storage<K, V>::drop;
|
||||||
|
@ -235,8 +235,7 @@ void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out) const {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Storage<K, V>::getRecord(key, out);
|
Storage<K, V>::getRecord(key, out);
|
||||||
cache->insert(std::make_pair(key, out));
|
appendToCache(key, out);
|
||||||
handleMode();
|
|
||||||
return;
|
return;
|
||||||
} catch (const NotFound& error) {
|
} catch (const NotFound& error) {
|
||||||
if (mode != Mode::full)
|
if (mode != Mode::full)
|
||||||
@ -337,10 +336,9 @@ void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out, TransactionID txn) con
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Storage<K, V>::getRecord(key, out, txn);
|
Storage<K, V>::getRecord(key, out, txn);
|
||||||
if (!currentTransaction) {
|
if (!currentTransaction)
|
||||||
cache->insert(std::make_pair(key, out));
|
appendToCache(key, out);
|
||||||
handleMode();
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
} catch (const NotFound& error) {
|
} catch (const NotFound& error) {
|
||||||
if (!currentTransaction && mode != Mode::full)
|
if (!currentTransaction && mode != Mode::full)
|
||||||
@ -350,6 +348,18 @@ void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out, TransactionID txn) con
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class K, class V>
|
||||||
|
void LMDBAL::Cache<K, V>::discoveredRecord(const K& key, const V& value) const {
|
||||||
|
appendToCache(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class K, class V>
|
||||||
|
void LMDBAL::Cache<K, V>::discoveredRecord(const K& key, const V& value, TransactionID txn) const {
|
||||||
|
typename TransactionCache::const_iterator tc = transactionCache->find(txn);
|
||||||
|
if (tc == transactionCache->end()) //there is a way to look though all the records in transaction log and cache the new pair
|
||||||
|
discoveredRecord(key, value); //if there is nothing in transaction log about it, but it seems like too much for a small gain
|
||||||
|
}
|
||||||
|
|
||||||
template<class K, class V>
|
template<class K, class V>
|
||||||
bool LMDBAL::Cache<K, V>::checkRecord(const K& key) const {
|
bool LMDBAL::Cache<K, V>::checkRecord(const K& key) const {
|
||||||
iStorage::ensureOpened(iStorage::checkRecordMethodName);
|
iStorage::ensureOpened(iStorage::checkRecordMethodName);
|
||||||
@ -363,8 +373,8 @@ bool LMDBAL::Cache<K, V>::checkRecord(const K& key) const {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
V value = Storage<K, V>::getRecord(key);
|
V value = Storage<K, V>::getRecord(key);
|
||||||
cache->insert(std::make_pair(key, value));
|
appendToCache(key, value);
|
||||||
handleMode();
|
|
||||||
return true;
|
return true;
|
||||||
} catch (const NotFound& error) {
|
} catch (const NotFound& error) {
|
||||||
if (mode != Mode::full)
|
if (mode != Mode::full)
|
||||||
@ -438,10 +448,9 @@ bool LMDBAL::Cache<K, V>::checkRecord(const K& key, TransactionID txn) const {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
V value = Storage<K, V>::getRecord(key, txn);
|
V value = Storage<K, V>::getRecord(key, txn);
|
||||||
if (!currentTransaction) {
|
if (!currentTransaction)
|
||||||
cache->insert(std::make_pair(key, value));
|
appendToCache(key, value);
|
||||||
handleMode();
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
} catch (const NotFound& error) {
|
} catch (const NotFound& error) {
|
||||||
if (!currentTransaction && mode != Mode::full)
|
if (!currentTransaction && mode != Mode::full)
|
||||||
@ -451,6 +460,13 @@ bool LMDBAL::Cache<K, V>::checkRecord(const K& key, TransactionID txn) const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class K, class V>
|
||||||
|
void LMDBAL::Cache<K, V>::appendToCache(const K& key, const V& value) const {
|
||||||
|
typename std::pair<typename std::map<K, V>::const_iterator, bool> pair = cache->insert(std::make_pair(key, value));
|
||||||
|
if (pair.second)
|
||||||
|
handleMode();
|
||||||
|
}
|
||||||
|
|
||||||
template<class K, class V>
|
template<class K, class V>
|
||||||
std::map<K, V> LMDBAL::Cache<K, V>::readAll() const {
|
std::map<K, V> LMDBAL::Cache<K, V>::readAll() const {
|
||||||
iStorage::ensureOpened(iStorage::readAllMethodName);
|
iStorage::ensureOpened(iStorage::readAllMethodName);
|
||||||
|
@ -34,6 +34,7 @@ protected:
|
|||||||
static LMDBAL::Base* db;
|
static LMDBAL::Base* db;
|
||||||
static LMDBAL::Cursor<uint64_t, std::string>* cursor;
|
static LMDBAL::Cursor<uint64_t, std::string>* cursor;
|
||||||
static LMDBAL::Cursor<uint64_t, std::string>* emptyCursor;
|
static LMDBAL::Cursor<uint64_t, std::string>* emptyCursor;
|
||||||
|
static LMDBAL::TransactionID transaction;
|
||||||
|
|
||||||
LMDBAL::Storage<uint64_t, std::string>* table;
|
LMDBAL::Storage<uint64_t, std::string>* table;
|
||||||
LMDBAL::Storage<uint64_t, std::string>* emptyTable;
|
LMDBAL::Storage<uint64_t, std::string>* emptyTable;
|
||||||
@ -42,6 +43,7 @@ protected:
|
|||||||
LMDBAL::Base* StorageCursorTest::db = nullptr;
|
LMDBAL::Base* StorageCursorTest::db = nullptr;
|
||||||
LMDBAL::Cursor<uint64_t, std::string>* StorageCursorTest::cursor = nullptr;
|
LMDBAL::Cursor<uint64_t, std::string>* StorageCursorTest::cursor = nullptr;
|
||||||
LMDBAL::Cursor<uint64_t, std::string>* StorageCursorTest::emptyCursor = nullptr;
|
LMDBAL::Cursor<uint64_t, std::string>* StorageCursorTest::emptyCursor = nullptr;
|
||||||
|
LMDBAL::TransactionID StorageCursorTest::transaction = nullptr;
|
||||||
|
|
||||||
static const std::map<uint64_t, std::string> data({
|
static const std::map<uint64_t, std::string> data({
|
||||||
{245665783, "bothering nerds"},
|
{245665783, "bothering nerds"},
|
||||||
@ -167,9 +169,106 @@ TEST_F(StorageCursorTest, Destruction) {
|
|||||||
EXPECT_THROW(cursor->next(), LMDBAL::CursorNotReady);
|
EXPECT_THROW(cursor->next(), LMDBAL::CursorNotReady);
|
||||||
EXPECT_THROW(cursor->prev(), LMDBAL::CursorNotReady);
|
EXPECT_THROW(cursor->prev(), LMDBAL::CursorNotReady);
|
||||||
EXPECT_THROW(cursor->current(), LMDBAL::CursorNotReady);
|
EXPECT_THROW(cursor->current(), LMDBAL::CursorNotReady);
|
||||||
|
|
||||||
|
EXPECT_THROW(emptyTable->destroyCursor(cursor), LMDBAL::Unknown);
|
||||||
|
table->destroyCursor(cursor);
|
||||||
|
|
||||||
|
cursor = table->createCursor();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StorageCursorTest, FirstPublic) {
|
||||||
|
transaction = db->beginReadOnlyTransaction();
|
||||||
|
|
||||||
|
cursor->open(transaction);
|
||||||
|
std::pair<uint64_t, std::string> element = cursor->first();
|
||||||
|
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
|
||||||
|
|
||||||
|
EXPECT_EQ(element.first, reference->first);
|
||||||
|
EXPECT_EQ(element.second, reference->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StorageCursorTest, NextPublic) {
|
||||||
|
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
|
||||||
|
|
||||||
|
reference++;
|
||||||
|
for (; reference != data.end(); ++reference) {
|
||||||
|
std::pair<uint64_t, std::string> element = cursor->next();
|
||||||
|
EXPECT_EQ(element.first, reference->first);
|
||||||
|
EXPECT_EQ(element.second, reference->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
EXPECT_THROW(cursor->next(), LMDBAL::NotFound);
|
||||||
|
|
||||||
|
std::pair<uint64_t, std::string> element = cursor->first();
|
||||||
|
reference = data.begin();
|
||||||
|
|
||||||
|
EXPECT_EQ(element.first, reference->first);
|
||||||
|
EXPECT_EQ(element.second, reference->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StorageCursorTest, LastPublic) {
|
||||||
|
std::pair<uint64_t, std::string> element = cursor->last();
|
||||||
|
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
|
||||||
|
|
||||||
|
EXPECT_EQ(element.first, reference->first);
|
||||||
|
EXPECT_EQ(element.second, reference->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StorageCursorTest, PrevPublic) {
|
||||||
|
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
|
||||||
|
|
||||||
|
reference++;
|
||||||
|
for (; reference != data.rend(); ++reference) {
|
||||||
|
std::pair<uint64_t, std::string> element = cursor->prev();
|
||||||
|
EXPECT_EQ(element.first, reference->first);
|
||||||
|
EXPECT_EQ(element.second, reference->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound);
|
||||||
|
|
||||||
|
std::pair<uint64_t, std::string> element = cursor->last();
|
||||||
|
reference = data.rbegin();
|
||||||
|
|
||||||
|
EXPECT_EQ(element.first, reference->first);
|
||||||
|
EXPECT_EQ(element.second, reference->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StorageCursorTest, CurrentPublic) {
|
||||||
|
std::pair<uint64_t, std::string> element = cursor->first();
|
||||||
|
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
|
||||||
|
|
||||||
|
EXPECT_EQ(element.first, reference->first);
|
||||||
|
EXPECT_EQ(element.second, reference->second);
|
||||||
|
|
||||||
|
element = cursor->current();
|
||||||
|
|
||||||
|
EXPECT_EQ(element.first, reference->first);
|
||||||
|
EXPECT_EQ(element.second, reference->second);
|
||||||
|
|
||||||
|
cursor->next();
|
||||||
|
element = cursor->current();
|
||||||
|
++reference;
|
||||||
|
|
||||||
|
EXPECT_EQ(element.first, reference->first);
|
||||||
|
EXPECT_EQ(element.second, reference->second);
|
||||||
|
|
||||||
|
cursor->next();
|
||||||
|
cursor->next();
|
||||||
|
cursor->prev();
|
||||||
|
element = cursor->current();
|
||||||
|
++reference;
|
||||||
|
++reference;
|
||||||
|
--reference;
|
||||||
|
|
||||||
|
EXPECT_EQ(element.first, reference->first);
|
||||||
|
EXPECT_EQ(element.second, reference->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(StorageCursorTest, CornerCases) {
|
TEST_F(StorageCursorTest, CornerCases) {
|
||||||
|
db->abortTransaction(transaction);
|
||||||
|
EXPECT_THROW(cursor->current(), LMDBAL::Unknown);
|
||||||
|
cursor->close();
|
||||||
|
|
||||||
emptyCursor->open();
|
emptyCursor->open();
|
||||||
EXPECT_THROW(emptyCursor->first(), LMDBAL::NotFound);
|
EXPECT_THROW(emptyCursor->first(), LMDBAL::NotFound);
|
||||||
EXPECT_THROW(emptyCursor->last(), LMDBAL::NotFound);
|
EXPECT_THROW(emptyCursor->last(), LMDBAL::NotFound);
|
||||||
|
Loading…
Reference in New Issue
Block a user