hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
cache.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_CACHE_HPP
7 #define IROHA_CACHE_HPP
8 
10 
11 #include <unordered_map>
12 
13 namespace iroha {
14  namespace cache {
15 
22  template <typename KeyType,
23  typename ValueType,
24  typename KeyHash = std::hash<KeyType>>
25  class Cache : public AbstractCache<KeyType,
26  ValueType,
27  Cache<KeyType, ValueType, KeyHash>> {
28  public:
29  Cache(uint32_t max_handler_map_size_high = 20000,
30  uint32_t max_handler_map_size_low = 10000)
31  : max_handler_map_size_high_(max_handler_map_size_high),
32  max_handler_map_size_low_(max_handler_map_size_low) {}
33 
34  uint32_t getIndexSizeHighImpl() const {
35  return max_handler_map_size_high_;
36  }
37 
38  uint32_t getIndexSizeLowImpl() const {
39  return max_handler_map_size_low_;
40  }
41 
42  uint32_t getCacheItemCountImpl() const {
43  return (uint32_t)handler_map_.size();
44  }
45 
46  void addItemImpl(const KeyType &key, const ValueType &value) {
47  // elements with the same hash should be replaced
48  handler_map_[key] = value;
49  handler_map_index_.push_back(key);
50  if (handler_map_.size() > getIndexSizeHighImpl()) {
51  while (handler_map_.size() > getIndexSizeLowImpl()) {
52  handler_map_.erase(handler_map_index_.front());
53  handler_map_index_.pop_front();
54  }
55  }
56  }
57 
58  boost::optional<ValueType> findItemImpl(const KeyType &key) const {
59  auto found = handler_map_.find(key);
60  if (found == handler_map_.end()) {
61  return boost::none;
62  } else {
63  return handler_map_.at(key);
64  }
65  }
66 
67  private:
68  std::unordered_map<KeyType, ValueType, KeyHash> handler_map_;
69  std::list<KeyType> handler_map_index_;
70 
76  const uint32_t max_handler_map_size_high_;
77  const uint32_t max_handler_map_size_low_;
78  };
79  } // namespace cache
80 } // namespace iroha
81 
82 #endif // IROHA_CACHE_HPP
void addItemImpl(const KeyType &key, const ValueType &value)
Definition: cache.hpp:46
uint32_t getIndexSizeLowImpl() const
Definition: cache.hpp:38
boost::optional< ValueType > findItemImpl(const KeyType &key) const
Definition: cache.hpp:58
uint32_t getIndexSizeHighImpl() const
Definition: cache.hpp:34
Definition: block_query.hpp:16
Definition: abstract_cache.hpp:26
Cache(uint32_t max_handler_map_size_high=20000, uint32_t max_handler_map_size_low=10000)
Definition: cache.hpp:29
Definition: cache.hpp:25
uint32_t getCacheItemCountImpl() const
Definition: cache.hpp:42