diff --git a/src/cachemap.h b/src/cachemap.h index c99ad917983c..c68608fb2bab 100644 --- a/src/cachemap.h +++ b/src/cachemap.h @@ -89,6 +89,9 @@ class CacheMap void SetMaxSize(size_type nMaxSizeIn) { nMaxSize = nMaxSizeIn; + while (listItems.size() > nMaxSize) { + PruneLast(); + } } size_type GetMaxSize() const { @@ -101,10 +104,13 @@ class CacheMap bool Insert(const K& key, const V& value) { + if (nMaxSize == 0) { + return false; + } if(mapIndex.find(key) != mapIndex.end()) { return false; } - if(listItems.size() == nMaxSize) { + while (listItems.size() >= nMaxSize) { PruneLast(); } listItems.push_front(item_t(key, value)); diff --git a/src/cachemultimap.h b/src/cachemultimap.h index 88f3f12c2bea..22d77691722d 100644 --- a/src/cachemultimap.h +++ b/src/cachemultimap.h @@ -57,7 +57,7 @@ class CacheMultiMap mapIndex() {} - explicit CacheMultiMap(const CacheMap& other) + CacheMultiMap(const CacheMultiMap& other) : nMaxSize(other.nMaxSize), listItems(other.listItems), mapIndex() @@ -74,6 +74,9 @@ class CacheMultiMap void SetMaxSize(size_type nMaxSizeIn) { nMaxSize = nMaxSizeIn; + while (listItems.size() > nMaxSize) { + PruneLast(); + } } size_type GetMaxSize() const { @@ -86,22 +89,20 @@ class CacheMultiMap bool Insert(const K& key, const V& value) { - map_it mit = mapIndex.find(key); - if(mit == mapIndex.end()) { - mit = mapIndex.emplace(key, it_map_t()).first; + if (nMaxSize == 0) { + return false; } - it_map_t& mapIt = mit->second; - - if(mapIt.count(value) > 0) { + if (map_cit mit = mapIndex.find(key); mit != mapIndex.end() && mit->second.count(value) > 0) { // Don't insert duplicates return false; } - if(listItems.size() == nMaxSize) { + // Pruning may erase this key's entry in mapIndex, so look it up only afterwards + while (listItems.size() >= nMaxSize) { PruneLast(); } listItems.push_front(item_t(key, value)); - mapIt.emplace(value, listItems.begin()); + mapIndex[key].emplace(value, listItems.begin()); return true; } @@ -184,8 +185,11 @@ class CacheMultiMap return listItems; } - CacheMap& operator=(const CacheMap& other) + CacheMultiMap& operator=(const CacheMultiMap& other) { + if (this == &other) { + return *this; + } nMaxSize = other.nMaxSize; listItems = other.listItems; RebuildIndex(); diff --git a/src/test/cachemap_tests.cpp b/src/test/cachemap_tests.cpp index 70b67069a8b3..dd84cd09c00a 100644 --- a/src/test/cachemap_tests.cpp +++ b/src/test/cachemap_tests.cpp @@ -135,4 +135,33 @@ BOOST_AUTO_TEST_CASE(cachemap_test) BOOST_CHECK(Compare(cmapTest1, mapTest4)); } +BOOST_AUTO_TEST_CASE(capacity_limits) +{ + CacheMap cache(0); + BOOST_CHECK(!cache.Insert(1, 1)); + BOOST_CHECK_EQUAL(cache.GetSize(), 0); + BOOST_CHECK(!cache.HasKey(1)); + + cache.Clear(); + cache.SetMaxSize(10); + for (int i = 0; i < 10; ++i) { + BOOST_REQUIRE(cache.Insert(i, i)); + } + cache.SetMaxSize(5); + BOOST_CHECK_EQUAL(cache.GetSize(), 5); + for (int i = 0; i < 10; ++i) { + int value = -1; + if (i < 5) { + BOOST_CHECK(!cache.Get(i, value)); + } else { + BOOST_CHECK(cache.Get(i, value)); + BOOST_CHECK_EQUAL(value, i); + } + } + cache.SetMaxSize(0); + BOOST_CHECK_EQUAL(cache.GetSize(), 0); + BOOST_CHECK(!cache.HasKey(9)); + BOOST_CHECK(!cache.Insert(1, 1)); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/cachemultimap_tests.cpp b/src/test/cachemultimap_tests.cpp index 46712b831b98..31ad94a94dab 100644 --- a/src/test/cachemultimap_tests.cpp +++ b/src/test/cachemultimap_tests.cpp @@ -164,6 +164,59 @@ BOOST_AUTO_TEST_CASE(cachemultimap_test) CacheMultiMap mapTest4; mapTest4 = cmmapTest1; BOOST_CHECK(Compare(cmmapTest1, mapTest4)); + + // copies must keep working after the source's storage is gone + cmmapTest1.Clear(); + for (auto* copy : {&cmmapTest3, &mapTest4}) { + std::vector vals; + BOOST_CHECK(copy->GetAll(5, vals)); + BOOST_CHECK(vals == std::vector({1, 2, 4})); + copy->Erase(5); + BOOST_CHECK(!copy->HasKey(5)); + } +} + +BOOST_AUTO_TEST_CASE(same_key_eviction_at_capacity) +{ + CacheMultiMap cache(1); + BOOST_REQUIRE(cache.Insert(42, 1)); + BOOST_CHECK(!cache.Insert(42, 1)); + BOOST_REQUIRE(cache.Insert(42, 2)); + BOOST_CHECK_EQUAL(cache.GetSize(), 1); + int value = 0; + BOOST_CHECK(cache.Get(42, value)); + BOOST_CHECK_EQUAL(value, 2); +} + +BOOST_AUTO_TEST_CASE(capacity_limits) +{ + CacheMultiMap cache(0); + BOOST_CHECK(!cache.Insert(1, 1)); + BOOST_CHECK_EQUAL(cache.GetSize(), 0); + BOOST_CHECK(!cache.HasKey(1)); + + cache.Clear(); + cache.SetMaxSize(10); + for (int i = 0; i < 10; ++i) { + BOOST_REQUIRE(cache.Insert(i / 2, i)); + } + cache.SetMaxSize(5); + BOOST_CHECK_EQUAL(cache.GetSize(), 5); + for (int i = 0; i < 5; ++i) { + std::vector values; + const bool found = cache.GetAll(i, values); + if (i < 2) { + BOOST_CHECK(!found); + } else { + BOOST_CHECK(found); + const std::vector expected = i == 2 ? std::vector{5} : std::vector{i * 2, i * 2 + 1}; + BOOST_CHECK_EQUAL_COLLECTIONS(values.begin(), values.end(), expected.begin(), expected.end()); + } + } + cache.SetMaxSize(0); + BOOST_CHECK_EQUAL(cache.GetSize(), 0); + BOOST_CHECK(!cache.HasKey(4)); + BOOST_CHECK(!cache.Insert(1, 1)); } BOOST_AUTO_TEST_SUITE_END()