LCOV - code coverage report
Current view: top level - libs/cache - cache.hpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 18 18 100.0 %
Date: 2019-03-07 14:46:43 Functions: 46 50 92.0 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #ifndef IROHA_CACHE_HPP
       7             : #define IROHA_CACHE_HPP
       8             : 
       9             : #include "cache/abstract_cache.hpp"
      10             : 
      11             : #include <unordered_map>
      12             : 
      13             : namespace iroha {
      14             :   namespace cache {
      15             : 
      16             :     /**
      17             :      * Cache for arbitrary types
      18             :      * @tparam KeyType type of key objects
      19             :      * @tparam ValueType type of value objects
      20             :      * @tparam KeyHash hasher for keys
      21             :      */
      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         771 :           : max_handler_map_size_high_(max_handler_map_size_high),
      32         771 :             max_handler_map_size_low_(max_handler_map_size_low) {}
      33             : 
      34             :       uint32_t getIndexSizeHighImpl() const {
      35       83657 :         return max_handler_map_size_high_;
      36             :       }
      37             : 
      38             :       uint32_t getIndexSizeLowImpl() const {
      39       20005 :         return max_handler_map_size_low_;
      40             :       }
      41             : 
      42             :       uint32_t getCacheItemCountImpl() const {
      43           6 :         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       43654 :         handler_map_[key] = value;
      49       43654 :         handler_map_index_.push_back(key);
      50       43654 :         if (handler_map_.size() > getIndexSizeHighImpl()) {
      51       20004 :           while (handler_map_.size() > getIndexSizeLowImpl()) {
      52       20002 :             handler_map_.erase(handler_map_index_.front());
      53       20002 :             handler_map_index_.pop_front();
      54             :           }
      55           2 :         }
      56       43654 :       }
      57             : 
      58             :       boost::optional<ValueType> findItemImpl(const KeyType &key) const {
      59       10117 :         auto found = handler_map_.find(key);
      60       10117 :         if (found == handler_map_.end()) {
      61        7119 :           return boost::none;
      62             :         } else {
      63        2997 :           return handler_map_.at(key);
      64             :         }
      65       10113 :       }
      66             : 
      67             :      private:
      68             :       std::unordered_map<KeyType, ValueType, KeyHash> handler_map_;
      69             :       std::list<KeyType> handler_map_index_;
      70             : 
      71             :       /**
      72             :        * Protection from handler map overflow.
      73             :        * TODO 27/10/2017 luckychess Values are quite random and should be tuned
      74             :        * for better performance and may be even move to config IR-579
      75             :        */
      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

Generated by: LCOV version 1.13