LCOV - code coverage report
Current view: top level - irohad/ordering/impl/ordering_gate_cache - on_demand_cache.cpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 25 25 100.0 %
Date: 2019-03-07 14:46:43 Functions: 6 6 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             : #include "ordering/impl/ordering_gate_cache/on_demand_cache.hpp"
       7             : 
       8             : #include "interfaces/iroha_internal/transaction_batch.hpp"
       9             : #include "interfaces/transaction.hpp"
      10             : 
      11             : using namespace iroha::ordering::cache;
      12             : 
      13             : // TODO: IR-1864 13.11.18 kamilsa use nvi to separate business logic and locking
      14             : // logic
      15             : 
      16             : void OnDemandCache::addToBack(
      17             :     const OrderingGateCache::BatchesSetType &batches) {
      18        4099 :   std::unique_lock<std::shared_timed_mutex> lock(mutex_);
      19        4102 :   circ_buffer.back().insert(batches.begin(), batches.end());
      20        4102 : }
      21             : 
      22             : void OnDemandCache::remove(const OrderingGateCache::HashesSetType &hashes) {
      23         965 :   std::unique_lock<std::shared_timed_mutex> lock(mutex_);
      24        3860 :   for (auto &batches : circ_buffer) {
      25        3957 :     for (auto it = batches.begin(); it != batches.end();) {
      26        1062 :       if (std::any_of(it->get()->transactions().begin(),
      27        1062 :                       it->get()->transactions().end(),
      28             :                       [&hashes](const auto &tx) {
      29        1062 :                         return hashes.find(tx->hash()) != hashes.end();
      30             :                       })) {
      31             :         // returns iterator following the last removed element
      32             :         // hence there is no increment in loop iteration_expression
      33         718 :         it = batches.erase(it);
      34         718 :       } else {
      35         344 :         ++it;
      36             :       }
      37             :     }
      38             :   }
      39         965 : }
      40             : 
      41             : OrderingGateCache::BatchesSetType OnDemandCache::pop() {
      42        3381 :   std::unique_lock<std::shared_timed_mutex> lock(mutex_);
      43        3381 :   BatchesSetType res;
      44        3381 :   std::swap(res, circ_buffer.front());
      45             :   // push empty set to remove front element
      46        3381 :   circ_buffer.push_back(BatchesSetType{});
      47        3381 :   return res;
      48        3381 : }
      49             : 
      50             : const OrderingGateCache::BatchesSetType &OnDemandCache::head() const {
      51           2 :   std::shared_lock<std::shared_timed_mutex> lock(mutex_);
      52           2 :   return circ_buffer.front();
      53           2 : }
      54             : 
      55             : const OrderingGateCache::BatchesSetType &OnDemandCache::tail() const {
      56           1 :   std::shared_lock<std::shared_timed_mutex> lock(mutex_);
      57           1 :   return circ_buffer.back();
      58           1 : }

Generated by: LCOV version 1.13