LCOV - code coverage report
Current view: top level - libs/cache - abstract_cache.hpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 17 17 100.0 %
Date: 2018-12-05 17:11:35 Functions: 42 42 100.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_ABSTRACT_CACHE_HPP
       7             : #define IROHA_ABSTRACT_CACHE_HPP
       8             : 
       9             : #include <boost/optional.hpp>
      10             : #include <list>
      11             : #include <shared_mutex>
      12             : #include <string>
      13             : 
      14             : namespace iroha {
      15             :   namespace cache {
      16             :     /**
      17             :      * Cache for any key-value types.
      18             :      * Internally it uses a map to cache ItemTypes and linked list
      19             :      * based index of KeyTypes to remove oldest items when getIndexSizeHigh() is
      20             :      * reached. Implemented as a CRTP pattern.
      21             :      * @tparam KeyType - type of cache keys
      22             :      * @tparam ValueType - type of cache values
      23             :      * @tparam T - type of implementation
      24             :      */
      25             :     template <typename KeyType, typename ValueType, typename T>
      26             :     class AbstractCache {
      27             :      public:
      28             :       /**
      29             :        * @return high border of cache limit (@see AbstractCache#addItem)
      30             :        */
      31             :       uint32_t getIndexSizeHigh() const {
      32             :         // shared lock
      33       40003 :         std::shared_lock<std::shared_timed_mutex> lock(access_mutex_);
      34       40003 :         return constUnderlying().getIndexSizeHighImpl();
      35       40003 :       }
      36             : 
      37             :       /**
      38             :        * @return low border of cache limit (@see AbstractCache#addItem)
      39             :        */
      40             :       uint32_t getIndexSizeLow() const {
      41             :         // shared lock
      42           1 :         std::shared_lock<std::shared_timed_mutex> lock(access_mutex_);
      43           1 :         return constUnderlying().getIndexSizeLowImpl();
      44           1 :       }
      45             : 
      46             :       /**
      47             :        * @return amount of items in cache
      48             :        */
      49             :       uint32_t getCacheItemCount() const {
      50             :         // shared lock
      51           6 :         std::shared_lock<std::shared_timed_mutex> lock(access_mutex_);
      52           6 :         return constUnderlying().getCacheItemCountImpl();
      53           6 :       }
      54             : 
      55             :       /**
      56             :        * Adds new item to cache. When amount of cache records reaches
      57             :        * getIndexSizeHigh() a procedure of clean starts until getIndexSizeLow()
      58             :        * records left. Note: cache does not have a remove method, deletion
      59             :        * performs automatically.
      60             :        * Since every add operation can potentially lead to deletion,
      61             :        * it should be protected by mutex.
      62             :        * @param key - key to insert
      63             :        * @param value - value to insert
      64             :        */
      65             :       void addItem(const KeyType &key, const ValueType &value) {
      66             :         // exclusive lock
      67       42363 :         std::lock_guard<std::shared_timed_mutex> lock(access_mutex_);
      68       42364 :         underlying().addItemImpl(key, value);
      69       42364 :       }
      70             : 
      71             :       /**
      72             :        * Performs a search for an item with a specific key.
      73             :        * @param hash - key to find
      74             :        * @return Optional of ValueType
      75             :        */
      76             :       boost::optional<ValueType> findItem(const KeyType &key) const {
      77        4685 :         std::shared_lock<std::shared_timed_mutex> lock(access_mutex_);
      78        4686 :         return constUnderlying().findItemImpl(key);
      79        4685 :       }
      80             : 
      81             :      private:
      82             :       const T &constUnderlying() const {
      83       44696 :         return static_cast<const T &>(*this);
      84             :       }
      85             :       T &underlying() {
      86       42364 :         return static_cast<T &>(*this);
      87             :       }
      88             : 
      89             :       mutable std::shared_timed_mutex access_mutex_;
      90             :     };
      91             :   }  // namespace cache
      92             : }  // namespace iroha
      93             : 
      94             : #endif  // IROHA_ABSTRACT_CACHE_HPP

Generated by: LCOV version 1.13