aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/cache
diff options
context:
space:
mode:
authornkmakarov <nkmakarov@yandex-team.ru>2022-02-10 16:49:06 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:49:06 +0300
commitf1803fa4ac9e2ee6cbfde317571ec330013392ff (patch)
tree5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/cache
parent324348a37ed08cf66897faefb0ec4bebfe7804e1 (diff)
downloadydb-f1803fa4ac9e2ee6cbfde317571ec330013392ff.tar.gz
Restoring authorship annotation for <nkmakarov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/cache')
-rw-r--r--library/cpp/cache/thread_safe_cache.h46
-rw-r--r--library/cpp/cache/ut/cache_ut.cpp68
2 files changed, 57 insertions, 57 deletions
diff --git a/library/cpp/cache/thread_safe_cache.h b/library/cpp/cache/thread_safe_cache.h
index 396ddec757..71e1442717 100644
--- a/library/cpp/cache/thread_safe_cache.h
+++ b/library/cpp/cache/thread_safe_cache.h
@@ -50,13 +50,13 @@ namespace NPrivate {
}
const TPtr Get(TArgs... args) const {
- return GetValue<true>(args...);
+ return GetValue<true>(args...);
+ }
+
+ const TPtr GetUnsafe(TArgs... args) const {
+ return GetValue<false>(args...);
}
- const TPtr GetUnsafe(TArgs... args) const {
- return GetValue<false>(args...);
- }
-
void Clear() {
TWriteGuard w(Mutex);
Cache.Clear();
@@ -107,9 +107,9 @@ namespace NPrivate {
}
private:
- template <bool AllowNullValues>
- const TPtr GetValue(TArgs... args) const {
- Key key = Callbacks.GetKey(args...);
+ template <bool AllowNullValues>
+ const TPtr GetValue(TArgs... args) const {
+ Key key = Callbacks.GetKey(args...);
switch (GettersPromotionPolicy) {
case EGettersPromotionPolicy::Promoted:
break;
@@ -120,21 +120,21 @@ namespace NPrivate {
return i.Value();
}
break;
- }
- }
- TWriteGuard w(Mutex);
- typename TInternalCache::TIterator i = Cache.Find(key);
- if (i != Cache.End()) {
- return i.Value();
- }
- TPtr value = Callbacks.CreateObject(args...);
- if (value || AllowNullValues) {
- Cache.Insert(key, value);
- }
- return value;
- }
-
- private:
+ }
+ }
+ TWriteGuard w(Mutex);
+ typename TInternalCache::TIterator i = Cache.Find(key);
+ if (i != Cache.End()) {
+ return i.Value();
+ }
+ TPtr value = Callbacks.CreateObject(args...);
+ if (value || AllowNullValues) {
+ Cache.Insert(key, value);
+ }
+ return value;
+ }
+
+ private:
using TInternalCache = TCache<Key, TPtr, List<Key, TPtr>, TNoopDelete>;
template <class TCallbacks>
diff --git a/library/cpp/cache/ut/cache_ut.cpp b/library/cpp/cache/ut/cache_ut.cpp
index 2a9af30d10..329872cfde 100644
--- a/library/cpp/cache/ut/cache_ut.cpp
+++ b/library/cpp/cache/ut/cache_ut.cpp
@@ -376,7 +376,7 @@ Y_UNIT_TEST_SUITE(TThreadSafeCacheTest) {
};
Y_UNIT_TEST(SimpleTest) {
- for (ui32 i = 0; i < Y_ARRAY_SIZE(VALS); ++i) {
+ for (ui32 i = 0; i < Y_ARRAY_SIZE(VALS); ++i) {
const TString data = *TCache::Get<TCallbacks>(i);
UNIT_ASSERT(data == VALS[i]);
}
@@ -405,39 +405,39 @@ Y_UNIT_TEST_SUITE(TThreadSafeCacheTest) {
UNIT_ASSERT(*item == "hjk");
}
}
-
-Y_UNIT_TEST_SUITE(TThreadSafeCacheUnsafeTest) {
- typedef TThreadSafeCache<ui32, TString, ui32> TCache;
-
- const char* VALS[] = {"abcd", "defg", "hjkl"};
- const ui32 FAILED_IDX = 1;
-
- class TCallbacks: public TCache::ICallbacks {
- public:
- TKey GetKey(ui32 i) const override {
- return i;
- }
- TValue* CreateObject(ui32 i) const override {
- if (i == FAILED_IDX) {
- return nullptr;
- }
- return new TString(VALS[i]);
- }
- };
-
- Y_UNIT_TEST(SimpleTest) {
- TCallbacks callbacks;
- TCache cache(callbacks, Y_ARRAY_SIZE(VALS));
- for (ui32 i = 0; i < Y_ARRAY_SIZE(VALS); ++i) {
- const TString* data = cache.GetUnsafe(i).Get();
- if (i == FAILED_IDX) {
- UNIT_ASSERT(data == nullptr);
- } else {
- UNIT_ASSERT(*data == VALS[i]);
- }
- }
- }
-}
+
+Y_UNIT_TEST_SUITE(TThreadSafeCacheUnsafeTest) {
+ typedef TThreadSafeCache<ui32, TString, ui32> TCache;
+
+ const char* VALS[] = {"abcd", "defg", "hjkl"};
+ const ui32 FAILED_IDX = 1;
+
+ class TCallbacks: public TCache::ICallbacks {
+ public:
+ TKey GetKey(ui32 i) const override {
+ return i;
+ }
+ TValue* CreateObject(ui32 i) const override {
+ if (i == FAILED_IDX) {
+ return nullptr;
+ }
+ return new TString(VALS[i]);
+ }
+ };
+
+ Y_UNIT_TEST(SimpleTest) {
+ TCallbacks callbacks;
+ TCache cache(callbacks, Y_ARRAY_SIZE(VALS));
+ for (ui32 i = 0; i < Y_ARRAY_SIZE(VALS); ++i) {
+ const TString* data = cache.GetUnsafe(i).Get();
+ if (i == FAILED_IDX) {
+ UNIT_ASSERT(data == nullptr);
+ } else {
+ UNIT_ASSERT(*data == VALS[i]);
+ }
+ }
+ }
+}
Y_UNIT_TEST_SUITE(TThreadSafeLRUCacheTest) {
typedef TThreadSafeLRUCache<size_t, TString, size_t> TCache;