Fix review

This commit is contained in:
alexX512 2022-08-08 20:53:02 +00:00
parent c1a8c615f3
commit c9c26d4898
6 changed files with 39 additions and 35 deletions

View File

@ -37,14 +37,12 @@ public:
using Mapped = TMapped;
using MappedPtr = std::shared_ptr<Mapped>;
CacheBase(size_t max_size, size_t max_elements_size = 0) : CacheBase(default_cache_policy_name, max_size, max_elements_size) {}
/// TODO: Rewrite to custom struct with fields for all cache policies.
CacheBase(String cache_policy_name, size_t max_size, size_t max_elements_size = 0, double size_ratio = 0.5)
CacheBase(size_t max_size, size_t max_elements_size = 0, String cache_policy_name = "", double size_ratio = 0.5)
{
auto on_weight_loss_function = [&](size_t weight_loss) { onRemoveOverflowWeightLoss(weight_loss); };
if (cache_policy_name == "")
if (cache_policy_name.empty())
{
cache_policy_name = default_cache_policy_name;
}

View File

@ -183,21 +183,27 @@ protected:
size_t current_weight_lost = 0;
size_t queue_size = queue.size();
auto need_remove = [&]() -> bool
std::function<bool()> need_remove;
if (is_protected)
{
if (is_protected)
/// Check if after remove all elements from probationary part there will be no more than max elements
/// in protected queue and weight of all protected elements will be less then max protected weight.
/// It's not possible to check only cells.size() > max_elements_size
/// because protected elements move to probationary part and still remain in cache.
need_remove = [&]()
{
/// Check if after remove all elements from probationary part there will be no more than max elements
/// in protected queue and weight of all protected elements will be less then max protected weight.
/// It's not possible to check only cells.size() > max_elements_size
/// because protected elements move to probationary part and still remain in cache.
return ((max_elements_size != 0 && cells.size() - probationary_queue.size() > max_elements_size)
|| (current_weight_size > max_weight_size))
&& (queue_size > 0);
}
return ((max_elements_size != 0 && cells.size() > max_elements_size) || (current_weight_size > max_weight_size))
&& (queue_size > 0);
};
|| (current_weight_size > max_weight_size)) && (queue_size > 0);
};
}
else
{
need_remove = [&]()
{
return ((max_elements_size != 0 && cells.size() > max_elements_size)
|| (current_weight_size > max_weight_size)) && (queue_size > 0);
};
}
while (need_remove())
{

View File

@ -6,7 +6,7 @@
TEST(LRUCache, set)
{
using SimpleCacheBase = DB::CacheBase<int, int>;
auto lru_cache = SimpleCacheBase("LRU", /*max_size*/ 10, /*max_elements_size*/ 10);
auto lru_cache = SimpleCacheBase(/*max_size*/ 10, /*max_elements_size*/ 10, "LRU");
lru_cache.set(1, std::make_shared<int>(2));
lru_cache.set(2, std::make_shared<int>(3));
@ -19,7 +19,7 @@ TEST(LRUCache, set)
TEST(LRUCache, update)
{
using SimpleCacheBase = DB::CacheBase<int, int>;
auto lru_cache = SimpleCacheBase("LRU", /*max_size*/ 10, /*max_elements_size*/ 10);
auto lru_cache = SimpleCacheBase(/*max_size*/ 10, /*max_elements_size*/ 10, "LRU");
lru_cache.set(1, std::make_shared<int>(2));
lru_cache.set(1, std::make_shared<int>(3));
auto val = lru_cache.get(1);
@ -30,7 +30,7 @@ TEST(LRUCache, update)
TEST(LRUCache, get)
{
using SimpleCacheBase = DB::CacheBase<int, int>;
auto lru_cache = SimpleCacheBase("LRU", /*max_size*/ 10, /*max_elements_size*/ 10);
auto lru_cache = SimpleCacheBase(/*max_size*/ 10, /*max_elements_size*/ 10, "LRU");
lru_cache.set(1, std::make_shared<int>(2));
lru_cache.set(2, std::make_shared<int>(3));
SimpleCacheBase::MappedPtr value = lru_cache.get(1);
@ -50,7 +50,7 @@ struct ValueWeight
TEST(LRUCache, evictOnSize)
{
using SimpleCacheBase = DB::CacheBase<int, size_t>;
auto lru_cache = SimpleCacheBase("LRU", /*max_size*/ 20, /*max_elements_size*/ 3);
auto lru_cache = SimpleCacheBase(/*max_size*/ 20, /*max_elements_size*/ 3, "LRU");
lru_cache.set(1, std::make_shared<size_t>(2));
lru_cache.set(2, std::make_shared<size_t>(3));
lru_cache.set(3, std::make_shared<size_t>(4));
@ -66,7 +66,7 @@ TEST(LRUCache, evictOnSize)
TEST(LRUCache, evictOnWeight)
{
using SimpleCacheBase = DB::CacheBase<int, size_t, std::hash<int>, ValueWeight>;
auto lru_cache = SimpleCacheBase("LRU", /*max_size*/ 10, /*max_elements_size*/ 10);
auto lru_cache = SimpleCacheBase(/*max_size*/ 10, /*max_elements_size*/ 10, "LRU");
lru_cache.set(1, std::make_shared<size_t>(2));
lru_cache.set(2, std::make_shared<size_t>(3));
lru_cache.set(3, std::make_shared<size_t>(4));
@ -87,7 +87,7 @@ TEST(LRUCache, evictOnWeight)
TEST(LRUCache, getOrSet)
{
using SimpleCacheBase = DB::CacheBase<int, size_t, std::hash<int>, ValueWeight>;
auto lru_cache = SimpleCacheBase("LRU", /*max_size*/ 10, /*max_elements_size*/ 10);
auto lru_cache = SimpleCacheBase(/*max_size*/ 10, /*max_elements_size*/ 10, "LRU");
size_t x = 10;
auto load_func = [&] { return std::make_shared<size_t>(x); };
auto [value, loaded] = lru_cache.getOrSet(1, load_func);

View File

@ -6,7 +6,7 @@
TEST(SLRUCache, set)
{
using SimpleCacheBase = DB::CacheBase<int, int>;
auto slru_cache = SimpleCacheBase("SLRU", /*max_total_size=*/10, /*max_elements_size=*/0, /*size_ratio*/0.5);
auto slru_cache = SimpleCacheBase(/*max_total_size=*/10, /*max_elements_size=*/0, "SLRU", /*size_ratio*/0.5);
slru_cache.set(1, std::make_shared<int>(2));
slru_cache.set(2, std::make_shared<int>(3));
@ -19,7 +19,7 @@ TEST(SLRUCache, set)
TEST(SLRUCache, update)
{
using SimpleCacheBase = DB::CacheBase<int, int>;
auto slru_cache = SimpleCacheBase("SLRU", /*max_total_size=*/10, /*max_elements_size=*/0, /*size_ratio*/0.5);
auto slru_cache = SimpleCacheBase(/*max_total_size=*/10, /*max_elements_size=*/0, "SLRU", /*size_ratio*/0.5);
slru_cache.set(1, std::make_shared<int>(2));
slru_cache.set(1, std::make_shared<int>(3));
@ -31,7 +31,7 @@ TEST(SLRUCache, update)
TEST(SLRUCache, get)
{
using SimpleCacheBase = DB::CacheBase<int, int>;
auto slru_cache = SimpleCacheBase("SLRU", /*max_total_size=*/10, /*max_elements_size=*/0, /*size_ratio*/0.5);
auto slru_cache = SimpleCacheBase(/*max_total_size=*/10, /*max_elements_size=*/0, "SLRU", /*size_ratio*/0.5);
slru_cache.set(1, std::make_shared<int>(2));
slru_cache.set(2, std::make_shared<int>(3));
@ -47,7 +47,7 @@ TEST(SLRUCache, get)
TEST(SLRUCache, remove)
{
using SimpleCacheBase = DB::CacheBase<int, int>;
auto slru_cache = SimpleCacheBase("SLRU", /*max_total_size=*/10, /*max_elements_size=*/0, /*size_ratio*/0.5);
auto slru_cache = SimpleCacheBase(/*max_total_size=*/10, /*max_elements_size=*/0, "SLRU", /*size_ratio*/0.5);
slru_cache.set(1, std::make_shared<int>(2));
slru_cache.set(2, std::make_shared<int>(3));
@ -63,7 +63,7 @@ TEST(SLRUCache, remove)
TEST(SLRUCache, removeFromProtected)
{
using SimpleCacheBase = DB::CacheBase<int, int>;
auto slru_cache = SimpleCacheBase("SLRU", /*max_total_size=*/2, /*max_elements_size=*/0, /*size_ratio*/0.5);
auto slru_cache = SimpleCacheBase(/*max_total_size=*/2, /*max_elements_size=*/0, "SLRU", /*size_ratio*/0.5);
slru_cache.set(1, std::make_shared<int>(2));
slru_cache.set(1, std::make_shared<int>(3));
@ -96,7 +96,7 @@ TEST(SLRUCache, removeFromProtected)
TEST(SLRUCache, reset)
{
using SimpleCacheBase = DB::CacheBase<int, int>;
auto slru_cache = SimpleCacheBase("SLRU", /*max_total_size=*/10, /*max_elements_size=*/0, /*size_ratio*/0.5);
auto slru_cache = SimpleCacheBase(/*max_total_size=*/10, /*max_elements_size=*/0, "SLRU", /*size_ratio*/0.5);
slru_cache.set(1, std::make_shared<int>(2));
slru_cache.set(2, std::make_shared<int>(3));
@ -119,7 +119,7 @@ struct ValueWeight
TEST(SLRUCache, evictOnElements)
{
using SimpleCacheBase = DB::CacheBase<int, size_t, std::hash<int>, ValueWeight>;
auto slru_cache = SimpleCacheBase("SLRU", /*max_total_size=*/10, /*max_elements_size=*/1);
auto slru_cache = SimpleCacheBase(/*max_total_size=*/10, /*max_elements_size=*/1, "SLRU", /*size_ratio*/0.5);
slru_cache.set(1, std::make_shared<size_t>(2));
slru_cache.set(2, std::make_shared<size_t>(3));
@ -140,7 +140,7 @@ TEST(SLRUCache, evictOnElements)
TEST(SLRUCache, evictOnWeight)
{
using SimpleCacheBase = DB::CacheBase<int, size_t, std::hash<int>, ValueWeight>;
auto slru_cache = SimpleCacheBase("SLRU", /*max_total_size=*/10, /*max_elements_size=*/0, /*size_ratio*/0.5);
auto slru_cache = SimpleCacheBase(/*max_total_size=*/10, /*max_elements_size=*/0, "SLRU", /*size_ratio*/0.5);
slru_cache.set(1, std::make_shared<size_t>(2));
slru_cache.set(2, std::make_shared<size_t>(3));
slru_cache.set(3, std::make_shared<size_t>(4));
@ -161,7 +161,7 @@ TEST(SLRUCache, evictOnWeight)
TEST(SLRUCache, evictFromProtectedPart)
{
using SimpleCacheBase = DB::CacheBase<int, size_t, std::hash<int>, ValueWeight>;
auto slru_cache = SimpleCacheBase("SLRU", /*max_total_size=*/10, /*max_elements_size=*/0, /*size_ratio*/0.5);
auto slru_cache = SimpleCacheBase(/*max_total_size=*/10, /*max_elements_size=*/0, "SLRU", /*size_ratio*/0.5);
slru_cache.set(1, std::make_shared<size_t>(2));
slru_cache.set(1, std::make_shared<size_t>(2));
@ -177,7 +177,7 @@ TEST(SLRUCache, evictFromProtectedPart)
TEST(SLRUCache, evictStreamProtected)
{
using SimpleCacheBase = DB::CacheBase<int, size_t, std::hash<int>, ValueWeight>;
auto slru_cache = SimpleCacheBase("SLRU", /*max_total_size=*/10, /*max_elements_size=*/0, /*size_ratio*/0.5);
auto slru_cache = SimpleCacheBase(/*max_total_size=*/10, /*max_elements_size=*/0, "SLRU", /*size_ratio*/0.5);
slru_cache.set(1, std::make_shared<size_t>(2));
slru_cache.set(1, std::make_shared<size_t>(2));
@ -201,7 +201,7 @@ TEST(SLRUCache, evictStreamProtected)
TEST(SLRUCache, getOrSet)
{
using SimpleCacheBase = DB::CacheBase<int, size_t, std::hash<int>, ValueWeight>;
auto slru_cache = SimpleCacheBase("SLRU", /*max_total_size=*/10, /*max_elements_size=*/0, /*size_ratio*/0.5);
auto slru_cache = SimpleCacheBase(/*max_total_size=*/10, /*max_elements_size=*/0, "SLRU", /*size_ratio*/0.5);
size_t x = 5;
auto load_func = [&] { return std::make_shared<size_t>(x); };
auto [value, loaded] = slru_cache.getOrSet(1, load_func);

View File

@ -43,7 +43,7 @@ private:
public:
explicit UncompressedCache(size_t max_size_in_bytes, const String & uncompressed_cache_policy = "")
: Base(uncompressed_cache_policy, max_size_in_bytes) {}
: Base(max_size_in_bytes, 0, uncompressed_cache_policy) {}
/// Calculate key from path to file and offset.
static UInt128 hash(const String & path_to_file, size_t offset)

View File

@ -41,7 +41,7 @@ private:
public:
explicit MarkCache(size_t max_size_in_bytes, const String & mark_cache_policy = "")
: Base(mark_cache_policy, max_size_in_bytes) {}
: Base(max_size_in_bytes, 0, mark_cache_policy) {}
/// Calculate key from path to file and offset.
static UInt128 hash(const String & path_to_file)