1
0
forked from blue/lmdbal

some test cases for cache, found some bugs, fixed

This commit is contained in:
Blue 2022-10-09 17:03:30 +03:00
parent c23dae2a25
commit ceb6df6eca
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
3 changed files with 55 additions and 4 deletions

View File

@ -43,7 +43,7 @@ public:
virtual void removeRecord(const K& key) override; virtual void removeRecord(const K& key) override;
virtual V getRecord(const K& key) const override; virtual V getRecord(const K& key) const override;
virtual uint32_t count() const override; virtual uint32_t count() const override;
virtual void drop() override; virtual int drop(MDB_txn * transaction) override;
protected: protected:
Mode* mode; Mode* mode;

View File

@ -100,7 +100,7 @@ V DataBase::Cache<K, V>::getRecord(const K& key) const {
return itr->second; return itr->second;
} }
if (*mode == Mode::full || abscent->count(key) == 0) { if (*mode == Mode::full || abscent->count(key) != 0) {
throw NotFound(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name); throw NotFound(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
} }
@ -169,12 +169,13 @@ void DataBase::Cache<K, V>::handleMode() const {
} }
template<class K, class V> template<class K, class V>
void DataBase::Cache<K, V>::drop() { int DataBase::Cache<K, V>::drop(MDB_txn * transaction) {
DataBase::Table<K, V>::drop(); int res = DataBase::Table<K, V>::drop(transaction);
cache->clear(); cache->clear();
abscent->clear(); abscent->clear();
*mode = Mode::full; *mode = Mode::full;
*sizeDifference = 0; *sizeDifference = 0;
return res;
} }
#endif //DATABASE_CACHE_HPP #endif //DATABASE_CACHE_HPP

View File

@ -73,6 +73,16 @@ TEST_F(DataBaseTest, AddingQStringKey) {
EXPECT_EQ(t2->getRecord("hello"), "world"); EXPECT_EQ(t2->getRecord("hello"), "world");
} }
TEST_F(DataBaseTest, AddingKeysToCache) {
EXPECT_EQ(db->ready(), true);
c1->addRecord(2, "blah balah");
c1->addRecord(-4, "testing goes brrr");
c1->addRecord(140, "whatever");
c1->addRecord(-37, "aaaaa tss tsss tsss tsss aaaaaaa");
EXPECT_EQ(c1->getRecord(140), "whatever");
EXPECT_EQ(c1->getRecord(-116), "whatever");
}
TEST_F(DataBaseTest, AddingRepeatingIntegerKey) { TEST_F(DataBaseTest, AddingRepeatingIntegerKey) {
EXPECT_EQ(db->ready(), true); EXPECT_EQ(db->ready(), true);
bool thrown = false; bool thrown = false;
@ -97,6 +107,18 @@ TEST_F(DataBaseTest, AddingRepeatingStringKey) {
EXPECT_EQ(t2->getRecord("sdfhga"), "DSFFDG"); EXPECT_EQ(t2->getRecord("sdfhga"), "DSFFDG");
} }
TEST_F(DataBaseTest, AddingRepeatingCacheKey) {
EXPECT_EQ(db->ready(), true);
bool thrown = false;
try {
c1->addRecord(-4, "world");
} catch (const DataBase::Exist e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
}
TEST_F(DataBaseTest, GettingNotExistingKeys) { TEST_F(DataBaseTest, GettingNotExistingKeys) {
EXPECT_EQ(db->ready(), true); EXPECT_EQ(db->ready(), true);
bool thrown = false; bool thrown = false;
@ -114,6 +136,14 @@ TEST_F(DataBaseTest, GettingNotExistingKeys) {
thrown = true; thrown = true;
} }
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened"; ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
thrown = false;
try {
std::string wrong = c1->getRecord(21);
} catch (const DataBase::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
} }
TEST_F(DataBaseTest, Persistence) { TEST_F(DataBaseTest, Persistence) {
@ -130,11 +160,18 @@ TEST_F(DataBaseTest, Persistence) {
EXPECT_EQ(t1->getRecord(3), 15); EXPECT_EQ(t1->getRecord(3), 15);
EXPECT_EQ(t1->getRecord(1), 2); EXPECT_EQ(t1->getRecord(1), 2);
EXPECT_EQ(t1->getRecord(2), 2); EXPECT_EQ(t1->getRecord(2), 2);
EXPECT_EQ(t2->getRecord("hello"), "world"); EXPECT_EQ(t2->getRecord("hello"), "world");
EXPECT_EQ(t2->getRecord("aaa"), "gagdfsdf"); EXPECT_EQ(t2->getRecord("aaa"), "gagdfsdf");
EXPECT_EQ(t2->getRecord("sdfhga"), "DSFFDG"); EXPECT_EQ(t2->getRecord("sdfhga"), "DSFFDG");
EXPECT_EQ(t2->getRecord("sdfsda"), "shgsdgfa"); EXPECT_EQ(t2->getRecord("sdfsda"), "shgsdgfa");
EXPECT_EQ(c1->getRecord(-116), "whatever");
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
EXPECT_EQ(c1->getRecord(-37), "aaaaa tss tsss tsss tsss aaaaaaa");
EXPECT_EQ(c1->getRecord(2), "blah balah");
bool thrown = false; bool thrown = false;
try { try {
QString wrong = t2->getRecord("cats"); QString wrong = t2->getRecord("cats");
@ -150,22 +187,35 @@ TEST_F(DataBaseTest, Persistence) {
thrown = true; thrown = true;
} }
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened"; ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
thrown = false;
try {
std::string wrong = c1->getRecord(89);
} catch (const DataBase::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
} }
TEST_F(DataBaseTest, CountAndDrop) { TEST_F(DataBaseTest, CountAndDrop) {
EXPECT_EQ(db->ready(), true); EXPECT_EQ(db->ready(), true);
EXPECT_EQ(t1->count(), 3); EXPECT_EQ(t1->count(), 3);
EXPECT_EQ(t2->count(), 4); EXPECT_EQ(t2->count(), 4);
EXPECT_EQ(c1->count(), 4);
db->drop(); db->drop();
EXPECT_EQ(t1->count(), 0); EXPECT_EQ(t1->count(), 0);
EXPECT_EQ(t2->count(), 0); EXPECT_EQ(t2->count(), 0);
EXPECT_EQ(c1->count(), 0);
t1->addRecord(2, 2); t1->addRecord(2, 2);
t2->addRecord("sdfhga", "world"); t2->addRecord("sdfhga", "world");
c1->addRecord(15, "world");
c1->addRecord(12, "grr grr");
EXPECT_EQ(t1->count(), 1); EXPECT_EQ(t1->count(), 1);
EXPECT_EQ(t2->count(), 1); EXPECT_EQ(t2->count(), 1);
EXPECT_EQ(c1->count(), 2);
} }