aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrobot-piglet <robot-piglet@yandex-team.com>2023-08-13 22:19:11 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2023-08-13 23:06:58 +0300
commitbb198010d2fd09f174a7b57a7b4232e31c626d5f (patch)
treefba47dc1a6422fdb07cdd307b729fb5574dd580f
parentffeb778d515beddbd1c6df7e28466a494f110bc2 (diff)
downloadydb-bb198010d2fd09f174a7b57a7b4232e31c626d5f.tar.gz
Intermediate changes
-rw-r--r--yt/yt/core/misc/sync_expiring_cache-inl.h39
-rw-r--r--yt/yt/core/misc/sync_expiring_cache.h11
2 files changed, 27 insertions, 23 deletions
diff --git a/yt/yt/core/misc/sync_expiring_cache-inl.h b/yt/yt/core/misc/sync_expiring_cache-inl.h
index d259667be4..98957baf84 100644
--- a/yt/yt/core/misc/sync_expiring_cache-inl.h
+++ b/yt/yt/core/misc/sync_expiring_cache-inl.h
@@ -4,6 +4,8 @@
#include "sync_expiring_cache.h"
#endif
+#include "collection_helpers.h"
+
#include <yt/yt/core/concurrency/periodic_executor.h>
namespace NYT {
@@ -41,10 +43,10 @@ TSyncExpiringCache<TKey, TValue>::TEntry::operator=(typename TSyncExpiringCache<
template <class TKey, class TValue>
TSyncExpiringCache<TKey, TValue>::TSyncExpiringCache(
- TCallback<TValue(const TKey&)> calculateValueAction,
+ TValueCalculator valueCalculator,
std::optional<TDuration> expirationTimeout,
IInvokerPtr invoker)
- : CalculateValueAction_(std::move(calculateValueAction))
+ : ValueCalculator_(std::move(valueCalculator))
, EvictionExecutor_(New<NConcurrency::TPeriodicExecutor>(
invoker,
BIND(&TSyncExpiringCache::DeleteExpiredItems, MakeWeak(this))))
@@ -92,7 +94,7 @@ TValue TSyncExpiringCache<TKey, TValue>::Get(const TKey& key)
}
}
- auto result = CalculateValueAction_(key);
+ auto result = ValueCalculator_(key);
{
auto guard = WriterGuard(MapLock_);
@@ -143,18 +145,18 @@ std::vector<TValue> TSyncExpiringCache<TKey, TValue>::Get(const std::vector<TKey
return foundValues;
}
- std::vector<TValue> results;
- results.reserve(keys.size());
+ std::vector<TValue> values;
+ values.reserve(keys.size());
int missingIndex = 0;
for (int keyIndex = 0; keyIndex < std::ssize(keys); ++keyIndex) {
if (missingIndex < std::ssize(missingValueIndexes) &&
missingValueIndexes[missingIndex] == keyIndex)
{
- results.push_back(CalculateValueAction_(keys[keyIndex]));
+ values.push_back(ValueCalculator_(keys[keyIndex]));
++missingIndex;
} else {
- results.push_back(std::move(foundValues[keyIndex - missingIndex]));
+ values.push_back(std::move(foundValues[keyIndex - missingIndex]));
}
}
@@ -162,22 +164,20 @@ std::vector<TValue> TSyncExpiringCache<TKey, TValue>::Get(const std::vector<TKey
auto guard = WriterGuard(MapLock_);
for (auto index : missingValueIndexes) {
+ const auto& value = values[index];
if (auto it = Map_.find(keys[index]); it != Map_.end()) {
- it->second = {now, now, results[index]};
+ it->second = {now, now, value};
} else {
- YT_VERIFY(Map_.emplace(
- keys[index],
- TEntry(now, now, results[index]))
- .second);
+ EmplaceOrCrash(Map_, keys[index], TEntry(now, now, value));
}
}
}
- return results;
+ return values;
}
template <class TKey, class TValue>
-void TSyncExpiringCache<TKey, TValue>::Set(const TKey& key, TValue value)
+std::optional<TValue> TSyncExpiringCache<TKey, TValue>::Set(const TKey& key, TValue value)
{
auto now = NProfiling::GetCpuInstant();
@@ -186,13 +186,12 @@ void TSyncExpiringCache<TKey, TValue>::Set(const TKey& key, TValue value)
if (auto it = Map_.find(key);
it != Map_.end())
{
+ auto oldValue = std::move(it->second.Value);
it->second = {now, now, std::move(value)};
- } else
- {
- YT_VERIFY(Map_.emplace(
- key,
- TEntry(now, now, std::move(value)))
- .second);
+ return oldValue;
+ } else {
+ EmplaceOrCrash(Map_, key, TEntry(now, now, std::move(value)));
+ return {};
}
}
diff --git a/yt/yt/core/misc/sync_expiring_cache.h b/yt/yt/core/misc/sync_expiring_cache.h
index 1bc7cc5874..5a4d3cf5be 100644
--- a/yt/yt/core/misc/sync_expiring_cache.h
+++ b/yt/yt/core/misc/sync_expiring_cache.h
@@ -4,6 +4,8 @@
#include <library/cpp/yt/threading/rw_spin_lock.h>
+#include <optional>
+
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
@@ -13,8 +15,10 @@ class TSyncExpiringCache
: public TRefCounted
{
public:
+ using TValueCalculator = TCallback<TValue(const TKey&)>;
+
TSyncExpiringCache(
- TCallback<TValue(const TKey&)> calculateValueAction,
+ TValueCalculator valueCalculator,
std::optional<TDuration> expirationTimeout,
IInvokerPtr invoker);
@@ -23,7 +27,8 @@ public:
TValue Get(const TKey& key);
std::vector<TValue> Get(const std::vector<TKey>& keys);
- void Set(const TKey& key, TValue value);
+ //! Returns the previous value, if any.
+ std::optional<TValue> Set(const TKey& key, TValue value);
void Invalidate(const TKey& key);
void Clear();
@@ -45,7 +50,7 @@ private:
TValue Value;
};
- const TCallback<TValue(const TKey&)> CalculateValueAction_;
+ const TValueCalculator ValueCalculator_;
const NConcurrency::TPeriodicExecutorPtr EvictionExecutor_;
YT_DECLARE_SPIN_LOCK(NThreading::TReaderWriterSpinLock, MapLock_);