aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/cache
diff options
context:
space:
mode:
authorAnton Samokhvalov <pg83@yandex.ru>2022-02-10 16:45:15 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:45:15 +0300
commit72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch)
treeda2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /library/cpp/cache
parent778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff)
downloadydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/cache')
-rw-r--r--library/cpp/cache/cache.h54
-rw-r--r--library/cpp/cache/thread_safe_cache.h14
-rw-r--r--library/cpp/cache/ut/cache_ut.cpp168
3 files changed, 118 insertions, 118 deletions
diff --git a/library/cpp/cache/cache.h b/library/cpp/cache/cache.h
index 6dc997076d..9662601209 100644
--- a/library/cpp/cache/cache.h
+++ b/library/cpp/cache/cache.h
@@ -1,7 +1,7 @@
#pragma once
#include <util/generic/algorithm.h>
-#include <util/generic/ptr.h>
+#include <util/generic/ptr.h>
#include <util/generic/intrlist.h>
#include <util/generic/hash_set.h>
#include <util/generic/vector.h>
@@ -28,7 +28,7 @@ public:
}
public:
- struct TItem: public TIntrusiveListItem<TItem> {
+ struct TItem: public TIntrusiveListItem<TItem> {
typedef TIntrusiveListItem<TItem> TBase;
TItem(const TKey& key, const TValue& value = TValue())
: TBase()
@@ -44,11 +44,11 @@ public:
{
}
- bool operator<(const TItem& rhs) const {
+ bool operator<(const TItem& rhs) const {
return Key < rhs.Key;
}
- bool operator==(const TItem& rhs) const {
+ bool operator==(const TItem& rhs) const {
return Key == rhs.Key;
}
@@ -56,7 +56,7 @@ public:
TValue Value;
struct THash {
- size_t operator()(const TItem& item) const {
+ size_t operator()(const TItem& item) const {
return ::THash<TKey>()(item.Key);
}
};
@@ -100,7 +100,7 @@ public:
size_t GetSize() const {
return ItemsAmount;
}
-
+
size_t GetTotalSize() const {
return TotalSize;
}
@@ -134,7 +134,7 @@ public:
{
}
- struct TItem: public TIntrusiveListItem<TItem> {
+ struct TItem: public TIntrusiveListItem<TItem> {
typedef TIntrusiveListItem<TItem> TBase;
TItem(const TKey& key, const TValue& value = TValue())
: TBase()
@@ -152,11 +152,11 @@ public:
{
}
- bool operator<(const TItem& rhs) const {
+ bool operator<(const TItem& rhs) const {
return Key < rhs.Key;
}
- bool operator==(const TItem& rhs) const {
+ bool operator==(const TItem& rhs) const {
return Key == rhs.Key;
}
@@ -165,7 +165,7 @@ public:
size_t Counter;
struct THash {
- size_t operator()(const TItem& item) const {
+ size_t operator()(const TItem& item) const {
return ::THash<TKey>()(item.Key);
}
};
@@ -256,11 +256,11 @@ public:
{
}
- bool operator<(const TItem& rhs) const {
+ bool operator<(const TItem& rhs) const {
return Key < rhs.Key;
}
- bool operator==(const TItem& rhs) const {
+ bool operator==(const TItem& rhs) const {
return Key == rhs.Key;
}
@@ -269,7 +269,7 @@ public:
TWeight Weight;
struct THash {
- size_t operator()(const TItem& item) const {
+ size_t operator()(const TItem& item) const {
return ::THash<TKey>()(item.Key);
}
};
@@ -380,7 +380,7 @@ class TCache {
typedef THashMultiSet<TItem, THash> TIndex;
typedef typename TIndex::iterator TIndexIterator;
typedef typename TIndex::const_iterator TIndexConstIterator;
-
+
public:
class TIterator {
public:
@@ -389,23 +389,23 @@ public:
{
}
- TValue& operator*() {
+ TValue& operator*() {
return const_cast<TValue&>(Iter->Value);
}
- TValue* operator->() {
+ TValue* operator->() {
return const_cast<TValue*>(&Iter->Value);
}
- bool operator==(const TIterator& rhs) const {
+ bool operator==(const TIterator& rhs) const {
return Iter == rhs.Iter;
}
- bool operator!=(const TIterator& rhs) const {
+ bool operator!=(const TIterator& rhs) const {
return Iter != rhs.Iter;
}
- TIterator& operator++() {
+ TIterator& operator++() {
++Iter;
return *this;
}
@@ -419,7 +419,7 @@ public:
}
friend class TCache<TKey, TValue, TListType, TDeleter>;
-
+
private:
TIndexConstIterator Iter;
};
@@ -459,7 +459,7 @@ public:
}
// note: it shouldn't touch 'value' if it returns false.
- bool PickOut(const TKey& key, TValue* value) {
+ bool PickOut(const TKey& key, TValue* value) {
Y_ASSERT(value);
TIndexIterator it = Index.find(TItem(key));
if (it == Index.end())
@@ -475,7 +475,7 @@ public:
return Insert(p.first, p.second);
}
- bool Insert(const TKey& key, const TValue& value) {
+ bool Insert(const TKey& key, const TValue& value) {
TItem tmpItem(key, value);
if (!MultiValue && Index.find(tmpItem) != Index.end())
return false;
@@ -550,7 +550,7 @@ protected:
TListType List;
bool MultiValue;
- TIterator FindByItem(TItem* item) {
+ TIterator FindByItem(TItem* item) {
std::pair<TIndexIterator, TIndexIterator> p = Index.equal_range(*item);
// we have to delete the exact unlinked item (there may be multiple items for one key)
TIndexIterator it;
@@ -593,7 +593,7 @@ public:
}
TIterator FindOldest() {
- return TBase::Empty() ? TBase::End() : this->FindByItem(TBase::List.GetOldest());
+ return TBase::Empty() ? TBase::End() : this->FindByItem(TBase::List.GetOldest());
}
size_t TotalSize() const {
@@ -602,8 +602,8 @@ public:
};
template <typename TKey, typename TValue, typename TDeleter = TNoopDelete>
-class TLFUCache: public TCache<TKey, TValue, TLFUList<TKey, TValue>, TDeleter> {
- typedef TCache<TKey, TValue, TLFUList<TKey, TValue>, TDeleter> TBase;
+class TLFUCache: public TCache<TKey, TValue, TLFUList<TKey, TValue>, TDeleter> {
+ typedef TCache<TKey, TValue, TLFUList<TKey, TValue>, TDeleter> TBase;
using TListType = TLFUList<TKey, TValue>;
public:
@@ -627,7 +627,7 @@ public:
// discards the least weighted items first
// doesn't support promotion
template <typename TKey, typename TValue, typename TWeight, typename TWeighter, typename TDeleter = TNoopDelete>
-class TLWCache: public TCache<TKey, TValue, TLWList<TKey, TValue, TWeight, TWeighter>, TDeleter> {
+class TLWCache: public TCache<TKey, TValue, TLWList<TKey, TValue, TWeight, TWeighter>, TDeleter> {
typedef TCache<TKey, TValue, TLWList<TKey, TValue, TWeight, TWeighter>, TDeleter> TBase;
using TListType = TLWList<TKey, TValue, TWeight, TWeighter>;
diff --git a/library/cpp/cache/thread_safe_cache.h b/library/cpp/cache/thread_safe_cache.h
index 71e1442717..276ac28bc6 100644
--- a/library/cpp/cache/thread_safe_cache.h
+++ b/library/cpp/cache/thread_safe_cache.h
@@ -33,8 +33,8 @@ namespace NPrivate {
TThreadSafeCache(const ICallbacks& callbacks, size_t maxSize = Max<size_t>())
: Callbacks(callbacks)
, Cache(maxSize)
- {
- }
+ {
+ }
bool Insert(const Key& key, const TPtr& value) {
if (!Contains(key)) {
@@ -81,12 +81,12 @@ namespace NPrivate {
return iter != Cache.End();
}
- template <class TCallbacks>
+ template <class TCallbacks>
static const TPtr Get(TArgs... args) {
return TThreadSafeCacheSingleton<TCallbacks>::Get(args...);
}
- template <class TCallbacks>
+ template <class TCallbacks>
static const TPtr Erase(TArgs... args) {
return TThreadSafeCacheSingleton<TCallbacks>::Erase(args...);
}
@@ -154,8 +154,8 @@ namespace NPrivate {
TThreadSafeCacheSingleton()
: Cache(Callbacks)
- {
- }
+ {
+ }
private:
TCallbacks Callbacks;
@@ -177,7 +177,7 @@ namespace NPrivate {
};
template <class TKey, class TValue>
- using TListType = TLWList<TKey, TValue, int, TConstWeighter<TValue>>;
+ using TListType = TLWList<TKey, TValue, int, TConstWeighter<TValue>>;
template <class TKey, class TValue, class... TArgs>
using TCache = TThreadSafeCache<TKey, TValue, TListType, EGettersPromotionPolicy::Unpromoted, TArgs...>;
diff --git a/library/cpp/cache/ut/cache_ut.cpp b/library/cpp/cache/ut/cache_ut.cpp
index 329872cfde..d7fabfffee 100644
--- a/library/cpp/cache/ut/cache_ut.cpp
+++ b/library/cpp/cache/ut/cache_ut.cpp
@@ -1,7 +1,7 @@
#include <library/cpp/cache/cache.h>
#include <library/cpp/cache/thread_safe_cache.h>
#include <library/cpp/testing/unittest/registar.h>
-
+
struct TStrokaWeighter {
static size_t Weight(const TString& s) {
return s.size();
@@ -10,23 +10,23 @@ struct TStrokaWeighter {
Y_UNIT_TEST_SUITE(TCacheTest) {
Y_UNIT_TEST(LRUListTest) {
- typedef TLRUList<int, TString> TListType;
- TListType list(2);
+ typedef TLRUList<int, TString> TListType;
+ TListType list(2);
- TListType::TItem x1(1, "ttt");
- list.Insert(&x1);
- UNIT_ASSERT_EQUAL(list.GetOldest()->Key, 1);
+ TListType::TItem x1(1, "ttt");
+ list.Insert(&x1);
+ UNIT_ASSERT_EQUAL(list.GetOldest()->Key, 1);
- TListType::TItem x2(2, "yyy");
- list.Insert(&x2);
- UNIT_ASSERT_EQUAL(list.GetOldest()->Key, 1);
+ TListType::TItem x2(2, "yyy");
+ list.Insert(&x2);
+ UNIT_ASSERT_EQUAL(list.GetOldest()->Key, 1);
- list.Promote(list.GetOldest());
- UNIT_ASSERT_EQUAL(list.GetOldest()->Key, 2);
+ list.Promote(list.GetOldest());
+ UNIT_ASSERT_EQUAL(list.GetOldest()->Key, 2);
- TListType::TItem x3(3, "zzz");
- list.Insert(&x3);
- UNIT_ASSERT_EQUAL(list.GetOldest()->Key, 1);
+ TListType::TItem x3(3, "zzz");
+ list.Insert(&x3);
+ UNIT_ASSERT_EQUAL(list.GetOldest()->Key, 1);
}
Y_UNIT_TEST(LRUListWeightedTest) {
@@ -66,52 +66,52 @@ Y_UNIT_TEST_SUITE(TCacheTest) {
}
Y_UNIT_TEST(LFUListTest) {
- typedef TLFUList<int, TString> TListType;
- TListType list(2);
+ typedef TLFUList<int, TString> TListType;
+ TListType list(2);
- TListType::TItem x1(1, "ttt");
- list.Insert(&x1);
- UNIT_ASSERT_EQUAL(list.GetLeastFrequentlyUsed()->Key, 1);
+ TListType::TItem x1(1, "ttt");
+ list.Insert(&x1);
+ UNIT_ASSERT_EQUAL(list.GetLeastFrequentlyUsed()->Key, 1);
- TListType::TItem x2(2, "yyy");
- list.Insert(&x2);
- UNIT_ASSERT_EQUAL(list.GetLeastFrequentlyUsed()->Key, 1);
+ TListType::TItem x2(2, "yyy");
+ list.Insert(&x2);
+ UNIT_ASSERT_EQUAL(list.GetLeastFrequentlyUsed()->Key, 1);
- list.Promote(list.GetLeastFrequentlyUsed());
- UNIT_ASSERT_EQUAL(list.GetLeastFrequentlyUsed()->Key, 2);
+ list.Promote(list.GetLeastFrequentlyUsed());
+ UNIT_ASSERT_EQUAL(list.GetLeastFrequentlyUsed()->Key, 2);
- TListType::TItem x3(3, "zzz");
- list.Insert(&x3);
- UNIT_ASSERT_EQUAL(list.GetLeastFrequentlyUsed()->Key, 1);
+ TListType::TItem x3(3, "zzz");
+ list.Insert(&x3);
+ UNIT_ASSERT_EQUAL(list.GetLeastFrequentlyUsed()->Key, 1);
}
Y_UNIT_TEST(LWListTest) {
- typedef TLWList<int, TString, size_t, TStrokaWeighter> TListType;
- TListType list(2);
-
- TListType::TItem x1(1, "tt");
- list.Insert(&x1);
- UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 1);
- UNIT_ASSERT_EQUAL(list.GetSize(), 1);
-
- TListType::TItem x2(2, "yyyy");
- list.Insert(&x2);
- UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 1);
- UNIT_ASSERT_EQUAL(list.GetSize(), 2);
-
- TListType::TItem x3(3, "z");
- list.Insert(&x3);
- UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 1);
- UNIT_ASSERT_EQUAL(list.GetSize(), 2);
-
- TListType::TItem x4(4, "xxxxxx");
- list.Insert(&x4);
- UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 2);
- UNIT_ASSERT_EQUAL(list.GetSize(), 2);
-
- list.Erase(&x2);
- UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 4);
- UNIT_ASSERT_EQUAL(list.GetSize(), 1);
+ typedef TLWList<int, TString, size_t, TStrokaWeighter> TListType;
+ TListType list(2);
+
+ TListType::TItem x1(1, "tt");
+ list.Insert(&x1);
+ UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 1);
+ UNIT_ASSERT_EQUAL(list.GetSize(), 1);
+
+ TListType::TItem x2(2, "yyyy");
+ list.Insert(&x2);
+ UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 1);
+ UNIT_ASSERT_EQUAL(list.GetSize(), 2);
+
+ TListType::TItem x3(3, "z");
+ list.Insert(&x3);
+ UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 1);
+ UNIT_ASSERT_EQUAL(list.GetSize(), 2);
+
+ TListType::TItem x4(4, "xxxxxx");
+ list.Insert(&x4);
+ UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 2);
+ UNIT_ASSERT_EQUAL(list.GetSize(), 2);
+
+ list.Erase(&x2);
+ UNIT_ASSERT_EQUAL(list.GetLightest()->Key, 4);
+ UNIT_ASSERT_EQUAL(list.GetSize(), 1);
}
Y_UNIT_TEST(SimpleTest) {
@@ -139,7 +139,7 @@ Y_UNIT_TEST_SUITE(TCacheTest) {
TCache::TIterator it = s.Find(3);
s.Erase(it);
UNIT_ASSERT(s.Find(3) == s.End());
- }
+ }
Y_UNIT_TEST(LRUWithCustomSizeProviderTest) {
typedef TLRUCache<int, TString, TNoopDelete, size_t(*)(const TString&)> TCache;
@@ -322,39 +322,39 @@ Y_UNIT_TEST_SUITE(TCacheTest) {
UNIT_ASSERT(*s.Find(1) == "bcde");
// (1, "bcde") will be promoted
UNIT_ASSERT(*s.FindOldest() == "fghi");
- }
+ }
- struct TMyDelete {
- static int count;
- template <typename T>
- static void Destroy(const T&) {
- ++count;
- }
- };
- int TMyDelete::count = 0;
+ struct TMyDelete {
+ static int count;
+ template <typename T>
+ static void Destroy(const T&) {
+ ++count;
+ }
+ };
+ int TMyDelete::count = 0;
Y_UNIT_TEST(DeleterTest) {
- typedef TLRUCache<int, TString, TMyDelete> TCache;
- TCache s(2);
- s.Insert(1, "123");
- s.Insert(2, "456");
- s.Insert(3, "789");
- UNIT_ASSERT(TMyDelete::count == 1);
- TCache::TIterator it = s.Find(2);
- UNIT_ASSERT(it != s.End());
- s.Erase(it);
- UNIT_ASSERT(TMyDelete::count == 2);
- }
+ typedef TLRUCache<int, TString, TMyDelete> TCache;
+ TCache s(2);
+ s.Insert(1, "123");
+ s.Insert(2, "456");
+ s.Insert(3, "789");
+ UNIT_ASSERT(TMyDelete::count == 1);
+ TCache::TIterator it = s.Find(2);
+ UNIT_ASSERT(it != s.End());
+ s.Erase(it);
+ UNIT_ASSERT(TMyDelete::count == 2);
+ }
Y_UNIT_TEST(PromoteOnFind) {
- typedef TLRUCache<int, TString> TCache;
- TCache s(2);
- s.Insert(1, "123");
- s.Insert(2, "456");
- UNIT_ASSERT(s.Find(1) != s.End());
- s.Insert(3, "789");
- UNIT_ASSERT(s.Find(1) != s.End()); // Key 2 should have been deleted
- }
+ typedef TLRUCache<int, TString> TCache;
+ TCache s(2);
+ s.Insert(1, "123");
+ s.Insert(2, "456");
+ UNIT_ASSERT(s.Find(1) != s.End());
+ s.Insert(3, "789");
+ UNIT_ASSERT(s.Find(1) != s.End()); // Key 2 should have been deleted
+ }
}
Y_UNIT_TEST_SUITE(TThreadSafeCacheTest) {
@@ -362,7 +362,7 @@ Y_UNIT_TEST_SUITE(TThreadSafeCacheTest) {
const char* VALS[] = {"abcd", "defg", "hjkl"};
- class TCallbacks: public TCache::ICallbacks {
+ class TCallbacks: public TCache::ICallbacks {
public:
TKey GetKey(ui32 i) const override {
return i;
@@ -380,7 +380,7 @@ Y_UNIT_TEST_SUITE(TThreadSafeCacheTest) {
const TString data = *TCache::Get<TCallbacks>(i);
UNIT_ASSERT(data == VALS[i]);
}
- }
+ }
Y_UNIT_TEST(InsertUpdateTest) {
TCallbacks callbacks;