6 #ifndef IROHA_CACHE_HPP 7 #define IROHA_CACHE_HPP 11 #include <unordered_map> 24 template <
typename KeyType,
26 typename KeyHash = std::hash<KeyType>,
27 size_t Count = 20000ull>
31 Cache<KeyType, ValueType, KeyHash, Count>> {
33 decltype(std::declval<KeyHash>()(std::declval<KeyType>()));
39 KeyAndValue() =
delete;
40 KeyAndValue(KeyAndValue
const &) =
delete;
41 KeyAndValue(HashType h, ValueType
const &v) :
hash(h), value(v) {}
43 KeyAndValue &operator=(KeyAndValue
const &) =
delete;
48 using KeyValuesBuffer = std::unordered_map<HashType, ValueHandle>;
50 inline HashType toHash(KeyType
const &key)
const {
51 return KeyHash()(key);
62 return static_cast<uint32_t
>(keys_.size());
65 void addItemImpl(
const KeyType &key,
const ValueType &value) {
66 auto const hash = toHash(key);
67 auto it = keys_.find(
hash);
68 if (keys_.end() == it) {
70 [&](ValueHandle h, KeyAndValue
const & ) {
71 this->keys_[
hash] = h;
73 [&](ValueHandle h, KeyAndValue
const &stored_value) {
75 this->keys_.end() != this->keys_.find(stored_value.hash),
76 "keys_ must contain item, which we want to remove!");
77 this->keys_.erase(stored_value.hash);
82 auto &stored_value = values_.getItem(it->second);
83 stored_value.value = value;
87 boost::optional<ValueType>
findItemImpl(
const KeyType &key)
const {
88 auto const hash = toHash(key);
89 auto it = keys_.find(
hash);
91 if (keys_.end() == it) {
94 return values_.getItem(it->second).value;
99 KeyValuesBuffer keys_;
105 #endif // IROHA_CACHE_HPP Cache()
Definition: cache.hpp:55
boost::optional< ValueType > findItemImpl(const KeyType &key) const
Definition: cache.hpp:87
uint32_t getCacheItemCountImpl() const
Definition: cache.hpp:61
Definition: block_query.hpp:15
uint32_t getIndexSizeHighImpl() const
Definition: cache.hpp:57
hash256_t hash(const T &pb)
Definition: pb_common.hpp:43
Definition: abstract_cache.hpp:26
void addItemImpl(const KeyType &key, const ValueType &value)
Definition: cache.hpp:65
size_t Handle
Definition: ring_buffer.hpp:21