diff options
author | heios <heios@yandex-team.ru> | 2022-02-10 16:50:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:15 +0300 |
commit | 58861ee1428f6e29c13edd982e662c38dd44c105 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 | |
parent | e0ff5d1ed8ec4db4bba3153fc3c3acb85052ada9 (diff) | |
download | ydb-58861ee1428f6e29c13edd982e662c38dd44c105.tar.gz |
Restoring authorship annotation for <heios@yandex-team.ru>. Commit 2 of 2.
-rw-r--r-- | library/cpp/cache/thread_safe_cache.h | 82 | ||||
-rw-r--r-- | library/cpp/cache/ut/cache_ut.cpp | 356 |
2 files changed, 219 insertions, 219 deletions
diff --git a/library/cpp/cache/thread_safe_cache.h b/library/cpp/cache/thread_safe_cache.h index 6c3305af2e..71e1442717 100644 --- a/library/cpp/cache/thread_safe_cache.h +++ b/library/cpp/cache/thread_safe_cache.h @@ -6,13 +6,13 @@ #include <util/system/rwlock.h> namespace NPrivate { - // We are interested in getters promotion policy _here_ because of Read-Write-Lock optimizations. - enum class EGettersPromotionPolicy { - Promoted, // LRU, TLRU, MRU, etc. - Unpromoted // FIFO, LIFO, LW, etc. - }; - - template <class Key, class Value, template <class, class> class List, EGettersPromotionPolicy GettersPromotionPolicy, class... TArgs> + // We are interested in getters promotion policy _here_ because of Read-Write-Lock optimizations. + enum class EGettersPromotionPolicy { + Promoted, // LRU, TLRU, MRU, etc. + Unpromoted // FIFO, LIFO, LW, etc. + }; + + template <class Key, class Value, template <class, class> class List, EGettersPromotionPolicy GettersPromotionPolicy, class... TArgs> class TThreadSafeCache { public: using TPtr = TAtomicSharedPtr<Value>; @@ -21,7 +21,7 @@ namespace NPrivate { public: using TKey = Key; using TValue = Value; - using TOwner = TThreadSafeCache<Key, Value, List, GettersPromotionPolicy, TArgs...>; + using TOwner = TThreadSafeCache<Key, Value, List, GettersPromotionPolicy, TArgs...>; public: virtual ~ICallbacks() = default; @@ -96,30 +96,30 @@ namespace NPrivate { return TThreadSafeCacheSingleton<TCallbacks>::Clear(); } - size_t GetMaxSize() const { - TReadGuard w(Mutex); - return Cache.GetMaxSize(); - } - - void SetMaxSize(size_t newSize) { - TWriteGuard w(Mutex); - Cache.SetMaxSize(newSize); - } - + size_t GetMaxSize() const { + TReadGuard w(Mutex); + return Cache.GetMaxSize(); + } + + void SetMaxSize(size_t newSize) { + TWriteGuard w(Mutex); + Cache.SetMaxSize(newSize); + } + private: template <bool AllowNullValues> const TPtr GetValue(TArgs... args) const { Key key = Callbacks.GetKey(args...); - switch (GettersPromotionPolicy) { - case EGettersPromotionPolicy::Promoted: - break; - case EGettersPromotionPolicy::Unpromoted: { - TReadGuard r(Mutex); - typename TInternalCache::TIterator i = Cache.FindWithoutPromote(key); - if (i != Cache.End()) { - return i.Value(); - } - break; + switch (GettersPromotionPolicy) { + case EGettersPromotionPolicy::Promoted: + break; + case EGettersPromotionPolicy::Unpromoted: { + TReadGuard r(Mutex); + typename TInternalCache::TIterator i = Cache.FindWithoutPromote(key); + if (i != Cache.End()) { + return i.Value(); + } + break; } } TWriteGuard w(Mutex); @@ -180,22 +180,22 @@ namespace NPrivate { using TListType = TLWList<TKey, TValue, int, TConstWeighter<TValue>>; template <class TKey, class TValue, class... TArgs> - using TCache = TThreadSafeCache<TKey, TValue, TListType, EGettersPromotionPolicy::Unpromoted, TArgs...>; + using TCache = TThreadSafeCache<TKey, TValue, TListType, EGettersPromotionPolicy::Unpromoted, TArgs...>; + }; + + struct TLRUHelper { + template <class TKey, class TValue> + using TListType = TLRUList<TKey, TValue>; + + template <class TKey, class TValue, class... TArgs> + using TCache = TThreadSafeCache<TKey, TValue, TListType, EGettersPromotionPolicy::Promoted, TArgs...>; }; - struct TLRUHelper { - template <class TKey, class TValue> - using TListType = TLRUList<TKey, TValue>; - - template <class TKey, class TValue, class... TArgs> - using TCache = TThreadSafeCache<TKey, TValue, TListType, EGettersPromotionPolicy::Promoted, TArgs...>; - }; - } template <class TKey, class TValue, class... TArgs> using TThreadSafeCache = typename NPrivate::TLWHelper::template TCache<TKey, TValue, TArgs...>; - -template <class TKey, class TValue, class... TArgs> -using TThreadSafeLRUCache = typename NPrivate::TLRUHelper::template TCache<TKey, TValue, TArgs...>; - + +template <class TKey, class TValue, class... TArgs> +using TThreadSafeLRUCache = typename NPrivate::TLRUHelper::template TCache<TKey, TValue, TArgs...>; + diff --git a/library/cpp/cache/ut/cache_ut.cpp b/library/cpp/cache/ut/cache_ut.cpp index e1e37eddd7..329872cfde 100644 --- a/library/cpp/cache/ut/cache_ut.cpp +++ b/library/cpp/cache/ut/cache_ut.cpp @@ -438,181 +438,181 @@ Y_UNIT_TEST_SUITE(TThreadSafeCacheUnsafeTest) { } } } - -Y_UNIT_TEST_SUITE(TThreadSafeLRUCacheTest) { - typedef TThreadSafeLRUCache<size_t, TString, size_t> TCache; - - TVector<TString> Values = {"zero", "one", "two", "three", "four"}; - - class TCallbacks: public TCache::ICallbacks { - public: - TKey GetKey(size_t i) const override { - return i; - } - TValue* CreateObject(size_t i) const override { - UNIT_ASSERT(i < Values.size()); - Creations++; - return new TString(Values[i]); - } - - mutable size_t Creations = 0; - }; - - Y_UNIT_TEST(SimpleTest) { - for (size_t i = 0; i < Values.size(); ++i) { - const TString data = *TCache::Get<TCallbacks>(i); - UNIT_ASSERT(data == Values[i]); - } - } - - Y_UNIT_TEST(InsertUpdateTest) { - TCallbacks callbacks; - TCache cache(callbacks, 10); - - cache.Insert(2, MakeAtomicShared<TString>("hj")); - TAtomicSharedPtr<TString> item = cache.Get(2); - - UNIT_ASSERT(callbacks.Creations == 0); - UNIT_ASSERT(*item == "hj"); - - cache.Insert(2, MakeAtomicShared<TString>("hjk")); - item = cache.Get(2); - - UNIT_ASSERT(callbacks.Creations == 0); - UNIT_ASSERT(*item == "hj"); - - cache.Update(2, MakeAtomicShared<TString>("hjk")); - item = cache.Get(2); - - UNIT_ASSERT(callbacks.Creations == 0); - UNIT_ASSERT(*item == "hjk"); - } - - Y_UNIT_TEST(LRUTest) { - TCallbacks callbacks; - TCache cache(callbacks, 3); - - UNIT_ASSERT_EQUAL(cache.GetMaxSize(), 3); - - for (size_t i = 0; i < Values.size(); ++i) { - TAtomicSharedPtr<TString> item = cache.Get(i); - UNIT_ASSERT(*item == Values[i]); - } - UNIT_ASSERT(callbacks.Creations == Values.size()); - - size_t expectedCreations = Values.size(); - TAtomicSharedPtr<TString> item; - - - item = cache.Get(4); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "four"); - - item = cache.Get(2); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "two"); - - item = cache.Get(0); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "zero"); - - UNIT_ASSERT(cache.Contains(1) == false); - UNIT_ASSERT(cache.Contains(3) == false); - UNIT_ASSERT(cache.Contains(4)); - UNIT_ASSERT(cache.Contains(2)); - UNIT_ASSERT(cache.Contains(0)); - - item = cache.Get(3); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "three"); - - item = cache.Get(2); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "two"); - - item = cache.Get(0); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "zero"); - - item = cache.Get(1); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "one"); - - item = cache.Get(2); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "two"); - - item = cache.Get(4); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "four"); - } - - Y_UNIT_TEST(ChangeMaxSizeTest) { - TCallbacks callbacks; - TCache cache(callbacks, 3); - - UNIT_ASSERT_EQUAL(cache.GetMaxSize(), 3); - - for (size_t i = 0; i < Values.size(); ++i) { - TAtomicSharedPtr<TString> item = cache.Get(i); - UNIT_ASSERT(*item == Values[i]); - } - - size_t expectedCreations = Values.size(); - TAtomicSharedPtr<TString> item; - - item = cache.Get(4); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "four"); - - item = cache.Get(3); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "three"); - - item = cache.Get(2); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "two"); - - item = cache.Get(1); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "one"); - - cache.SetMaxSize(4); - - item = cache.Get(0); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "zero"); - - item = cache.Get(4); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "four"); - - item = cache.Get(3); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "three"); - UNIT_ASSERT(cache.Contains(2) == false); - - cache.SetMaxSize(2); - UNIT_ASSERT(cache.Contains(3)); - UNIT_ASSERT(cache.Contains(4)); - UNIT_ASSERT(cache.Contains(2) == false); - UNIT_ASSERT(cache.Contains(1) == false); - UNIT_ASSERT(cache.Contains(0) == false); - - item = cache.Get(0); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "zero"); - UNIT_ASSERT(cache.Contains(4) == false); - UNIT_ASSERT(cache.Contains(3)); - UNIT_ASSERT(cache.Contains(0)); - } -} + +Y_UNIT_TEST_SUITE(TThreadSafeLRUCacheTest) { + typedef TThreadSafeLRUCache<size_t, TString, size_t> TCache; + + TVector<TString> Values = {"zero", "one", "two", "three", "four"}; + + class TCallbacks: public TCache::ICallbacks { + public: + TKey GetKey(size_t i) const override { + return i; + } + TValue* CreateObject(size_t i) const override { + UNIT_ASSERT(i < Values.size()); + Creations++; + return new TString(Values[i]); + } + + mutable size_t Creations = 0; + }; + + Y_UNIT_TEST(SimpleTest) { + for (size_t i = 0; i < Values.size(); ++i) { + const TString data = *TCache::Get<TCallbacks>(i); + UNIT_ASSERT(data == Values[i]); + } + } + + Y_UNIT_TEST(InsertUpdateTest) { + TCallbacks callbacks; + TCache cache(callbacks, 10); + + cache.Insert(2, MakeAtomicShared<TString>("hj")); + TAtomicSharedPtr<TString> item = cache.Get(2); + + UNIT_ASSERT(callbacks.Creations == 0); + UNIT_ASSERT(*item == "hj"); + + cache.Insert(2, MakeAtomicShared<TString>("hjk")); + item = cache.Get(2); + + UNIT_ASSERT(callbacks.Creations == 0); + UNIT_ASSERT(*item == "hj"); + + cache.Update(2, MakeAtomicShared<TString>("hjk")); + item = cache.Get(2); + + UNIT_ASSERT(callbacks.Creations == 0); + UNIT_ASSERT(*item == "hjk"); + } + + Y_UNIT_TEST(LRUTest) { + TCallbacks callbacks; + TCache cache(callbacks, 3); + + UNIT_ASSERT_EQUAL(cache.GetMaxSize(), 3); + + for (size_t i = 0; i < Values.size(); ++i) { + TAtomicSharedPtr<TString> item = cache.Get(i); + UNIT_ASSERT(*item == Values[i]); + } + UNIT_ASSERT(callbacks.Creations == Values.size()); + + size_t expectedCreations = Values.size(); + TAtomicSharedPtr<TString> item; + + + item = cache.Get(4); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "four"); + + item = cache.Get(2); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "two"); + + item = cache.Get(0); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "zero"); + + UNIT_ASSERT(cache.Contains(1) == false); + UNIT_ASSERT(cache.Contains(3) == false); + UNIT_ASSERT(cache.Contains(4)); + UNIT_ASSERT(cache.Contains(2)); + UNIT_ASSERT(cache.Contains(0)); + + item = cache.Get(3); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "three"); + + item = cache.Get(2); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "two"); + + item = cache.Get(0); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "zero"); + + item = cache.Get(1); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "one"); + + item = cache.Get(2); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "two"); + + item = cache.Get(4); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "four"); + } + + Y_UNIT_TEST(ChangeMaxSizeTest) { + TCallbacks callbacks; + TCache cache(callbacks, 3); + + UNIT_ASSERT_EQUAL(cache.GetMaxSize(), 3); + + for (size_t i = 0; i < Values.size(); ++i) { + TAtomicSharedPtr<TString> item = cache.Get(i); + UNIT_ASSERT(*item == Values[i]); + } + + size_t expectedCreations = Values.size(); + TAtomicSharedPtr<TString> item; + + item = cache.Get(4); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "four"); + + item = cache.Get(3); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "three"); + + item = cache.Get(2); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "two"); + + item = cache.Get(1); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "one"); + + cache.SetMaxSize(4); + + item = cache.Get(0); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "zero"); + + item = cache.Get(4); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "four"); + + item = cache.Get(3); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "three"); + UNIT_ASSERT(cache.Contains(2) == false); + + cache.SetMaxSize(2); + UNIT_ASSERT(cache.Contains(3)); + UNIT_ASSERT(cache.Contains(4)); + UNIT_ASSERT(cache.Contains(2) == false); + UNIT_ASSERT(cache.Contains(1) == false); + UNIT_ASSERT(cache.Contains(0) == false); + + item = cache.Get(0); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "zero"); + UNIT_ASSERT(cache.Contains(4) == false); + UNIT_ASSERT(cache.Contains(3)); + UNIT_ASSERT(cache.Contains(0)); + } +} |