6 #include <shared_mutex>
7 #include <unordered_map>
17 is_empty(
const T &v) : v_(v) {}
18 operator bool()
const {
return !v_; }
22 struct is_empty<
std::vector<T>> {
23 is_empty(
const std::vector<T> &v) : v_(v) {}
24 operator bool()
const {
return v_.empty(); }
25 const std::vector<T> &v_;
29 template <
class Key,
class Value>
32 using GetterFn = std::function<Value(uint64_t,
const Key &)>;
33 using ValueMap = std::unordered_map<Key, Value>;
34 using DataMap = std::map<uint64_t, ValueMap>;
44 void append(uint64_t block_num,
const Key &key,
const Value &value)
const {
45 std::unique_lock lock(
mutex_);
51 blk_entry->second.emplace(key, value);
59 Value
get(uint64_t blk_num,
const Key &key)
const {
61 std::shared_lock lock(
mutex_);
64 auto e = blk_entry->second.find(key);
65 if (e != blk_entry->second.end()) {
72 if (is_empty(value)) {
77 if (last_num < kBlocksToKeep || blk_num >= last_num -
kBlocksToKeep) {
78 append(blk_num, key, value);
84 std::shared_lock lock(
mutex_);
100 template <
class Value>
114 void append(uint64_t block_num, Value value)
const {
115 std::unique_lock lock(
mutex_);
124 Value
get(uint64_t block_num)
const {
126 std::shared_lock lock(
mutex_);
129 return blk_entry->second;
134 if (is_empty(value)) {
139 if (last_num < kBlocksToKeep || block_num >= last_num -
kBlocksToKeep) {
146 std::shared_lock lock(
mutex_);
154 std::shared_lock lock(
mutex_);
Value get(uint64_t blk_num, const Key &key) const
Definition: cache.hpp:59
const uint64_t kBlocksToKeep
Definition: cache.hpp:92
std::map< uint64_t, ValueMap > DataMap
Definition: cache.hpp:34
DataMap data_by_block_
Definition: cache.hpp:97
MapByBlockCache(uint64_t blocks_to_save, GetterFn &&getter_fn)
Definition: cache.hpp:41
std::unordered_map< Key, Value > ValueMap
Definition: cache.hpp:33
std::shared_mutex mutex_
Definition: cache.hpp:96
std::function< Value(uint64_t, const Key &)> GetterFn
Definition: cache.hpp:32
void append(uint64_t block_num, const Key &key, const Value &value) const
Definition: cache.hpp:44
MapByBlockCache(MapByBlockCache &&)=delete
MapByBlockCache & operator=(MapByBlockCache &&)=delete
uint64_t lastBlockNum() const
Definition: cache.hpp:83
GetterFn getter_fn_
Definition: cache.hpp:93
MapByBlockCache & operator=(const MapByBlockCache &)=delete
MapByBlockCache(const MapByBlockCache &)=delete
Definition: cache.hpp:101
Value get(uint64_t block_num) const
Definition: cache.hpp:124
ValueByBlockCache & operator=(const ValueByBlockCache &)=delete
std::shared_mutex mutex_
Definition: cache.hpp:166
ValueByBlockCache(uint64_t blocks_to_save, GetterFn &&getter_fn)
Definition: cache.hpp:111
const uint64_t kBlocksToKeep
Definition: cache.hpp:162
std::map< uint64_t, Value > DataMap
Definition: cache.hpp:104
ValueByBlockCache(const ValueByBlockCache &)=delete
uint64_t lastBlockNum() const
Definition: cache.hpp:153
void append(uint64_t block_num, Value value) const
Definition: cache.hpp:114
Value last() const
Definition: cache.hpp:145
std::function< Value(uint64_t)> GetterFn
Definition: cache.hpp:103
ValueByBlockCache(ValueByBlockCache &&)=delete
ValueByBlockCache & operator=(ValueByBlockCache &&)=delete
GetterFn getter_fn_
Definition: cache.hpp:163
DataMap data_by_block_
Definition: cache.hpp:167
std::hash for asio::adress
Definition: FixedHash.h:483