From 064277fa6e39e2f2b6bb32231a3a91f901105648 Mon Sep 17 00:00:00 2001 From: blue Date: Fri, 7 Apr 2023 02:51:45 +0300 Subject: [PATCH] two more methods for getting all records from cache, one more test for reading all --- src/cache.h | 2 ++ src/cache.hpp | 63 +++++++++++++++++++++++++++++++------------------- test/basic.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 24 deletions(-) diff --git a/src/cache.h b/src/cache.h index 2810f85..b7571d5 100644 --- a/src/cache.h +++ b/src/cache.h @@ -93,6 +93,8 @@ public: virtual std::map readAll() const override; virtual std::map readAll(TransactionID txn) const override; + virtual void readAll(std::map& out) const override; + virtual void readAll(std::map& out, TransactionID txn) const override; virtual void replaceAll(const std::map& data) override; virtual void replaceAll(const std::map& data, TransactionID txn) override; virtual SizeType addRecords(const std::map& data, bool overwrite = false) override; diff --git a/src/cache.hpp b/src/cache.hpp index a28f363..93f19dd 100644 --- a/src/cache.hpp +++ b/src/cache.hpp @@ -229,7 +229,6 @@ void LMDBAL::Cache::getRecord(const K& key, V& out) const { } } - template V LMDBAL::Cache::getRecord(const K& key, TransactionID txn) const { iStorage::ensureOpened(iStorage::getRecordMethodName); @@ -449,41 +448,64 @@ std::map LMDBAL::Cache::readAll() const { return *cache; } +template +void LMDBAL::Cache::readAll(std::map& out) const { + iStorage::ensureOpened(iStorage::readAllMethodName); + + if (*mode != Mode::full) { //there is a room for optimization + *mode = Mode::full; //I can read and deserialize only those values + Storage::readAll(out); //that are missing in the cache + *cache = out; + abscent->clear(); + *sizeDifference = 0; + } +} + template std::map LMDBAL::Cache::readAll(TransactionID txn) const { iStorage::ensureOpened(iStorage::readAllMethodName); + std::map out; + readAll(out, txn); + + return out; +} + +template +void LMDBAL::Cache::readAll(std::map& out, TransactionID txn) const { + iStorage::ensureOpened(iStorage::readAllMethodName); + typename TransactionCache::iterator tc = transactionCache->find(txn); if (tc != transactionCache->end()) { Queue& queue = tc->second; if (*mode != Mode::full) { - std::map result = *cache; + out = *cache; for (typename Queue::const_iterator i = queue.begin(), end = queue.end(); i != end; ++i) { const Entry& entry = *i; switch (entry.first) { case Operation::add: - result.insert(*static_cast*>(entry.second)); + out.insert(*static_cast*>(entry.second)); break; case Operation::remove: - result.erase(*static_cast(entry.second)); + out.erase(*static_cast(entry.second)); break; case Operation::change: { std::pair* pair = static_cast*>(entry.second); - result.at(pair->first) = pair->second; + out.at(pair->first) = pair->second; } break; case Operation::force:{ const std::tuple& tuple = *static_cast*>(entry.second); - result[std::get<1>(tuple)] = std::get<2>(tuple); + out[std::get<1>(tuple)] = std::get<2>(tuple); } break; case Operation::drop: - result.clear(); + out.clear(); break; case Operation::replace: - result = *static_cast*>(entry.second); + out = *static_cast*>(entry.second); break; case Operation::addMany: { const std::tuple>& t = @@ -492,35 +514,28 @@ std::map LMDBAL::Cache::readAll(TransactionID txn) const { bool overwrite = std::get<0>(t); for (const std::pair& pair : added) { if (overwrite) - result[pair.first] = pair.second; + out[pair.first] = pair.second; else - result.insert(pair); + out.insert(pair); } } break; } } - - return result; } else { - std::map* result = new std::map(); - Storage::readAll(*result, txn); + Storage::readAll(out, txn); - //queue.clear(); //since I'm getting a complete state of the database - queue.emplace_back(Operation::replace, result); //I can as well erase all previous cache entries - - return *result; + //queue.clear(); //since I'm getting a complete state of the database + queue.emplace_back(Operation::replace, new std::map(out)); //I can as well erase all previous cache entries } - } else { - if (*mode != Mode::full) { //there is a room for optimization - *mode = Mode::full; //I can read and deserialize only those values - *cache = Storage::readAll(txn); //that are missing in the cache + if (*mode != Mode::full) { //there is a room for optimization + *mode = Mode::full; //I can read and deserialize only those values + Storage::readAll(out); //that are missing in the cache + *cache = out; abscent->clear(); *sizeDifference = 0; } - - return *cache; } } diff --git a/test/basic.cpp b/test/basic.cpp index fdef128..baf48cf 100644 --- a/test/basic.cpp +++ b/test/basic.cpp @@ -301,3 +301,64 @@ TEST_F(BaseTest, ReadAll) { EXPECT_EQ(m2.size(), 3); EXPECT_EQ(m3.size(), 4); } + + + +TEST_F(BaseTest, ReplaceAll) { + EXPECT_EQ(db->ready(), true); + + t1->replaceAll({ + {7, 48}, + {194, 582}, + {857, 39}, + {9717, 8} + }); + t2->replaceAll({ + {"bringin", "keyboard"}, + {"cluster", "throttle"}, + {"ronin", "cheese"} + }); + c1->replaceAll({}); + + EXPECT_EQ(t1->count(), 4); + EXPECT_EQ(t2->count(), 3); + EXPECT_EQ(c1->count(), 0); + + EXPECT_FALSE(t1->checkRecord(2)); + EXPECT_FALSE(t1->checkRecord(58)); + EXPECT_FALSE(t1->checkRecord(68)); + + EXPECT_FALSE(t2->checkRecord("sdfhga")); + EXPECT_FALSE(t2->checkRecord("prophecy")); + EXPECT_FALSE(t2->checkRecord("lawfirm")); + + EXPECT_FALSE(c1->checkRecord(15)); + EXPECT_FALSE(c1->checkRecord(12)); + EXPECT_FALSE(c1->checkRecord(89)); + EXPECT_FALSE(c1->checkRecord(98)); + + EXPECT_EQ(t1->getRecord(7), 48); + EXPECT_EQ(t1->getRecord(194), 582); + EXPECT_EQ(t1->getRecord(857), 39); + EXPECT_EQ(t1->getRecord(9717), 8); + + EXPECT_EQ(t2->getRecord("bringin"), "keyboard"); + EXPECT_EQ(t2->getRecord("cluster"), "throttle"); + EXPECT_EQ(t2->getRecord("ronin"), "cheese"); + + + c1->replaceAll({ + {68, "quality"}, + {31, "ridgid body"}, + {16, "fermentation on your kind"}, + {22, "pseudo"}, + {-117, "lance of Michael"}, + }); + EXPECT_EQ(c1->count(), 5); + + EXPECT_EQ(c1->getRecord(68), "quality"); + EXPECT_EQ(c1->getRecord(31), "ridgid body"); + EXPECT_EQ(c1->getRecord(16), "fermentation on your kind"); + EXPECT_EQ(c1->getRecord(22), "pseudo"); + EXPECT_EQ(c1->getRecord(-117), "lance of Michael"); +}