YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/lru_cache.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#ifndef YB_UTIL_LRU_CACHE_H
15
#define YB_UTIL_LRU_CACHE_H
16
17
#include <boost/multi_index_container.hpp>
18
#include <boost/multi_index/hashed_index.hpp>
19
#include <boost/multi_index/sequenced_index.hpp>
20
21
namespace yb {
22
23
// The cache that stores at most specified number of recently added entries.
24
template <class Value>
25
class LRUCache {
26
 private:
27
  class IdTag;
28
29
  using Impl = boost::multi_index_container<
30
      Value,
31
      boost::multi_index::indexed_by<
32
        boost::multi_index::sequenced<>,
33
        boost::multi_index::hashed_unique<
34
          boost::multi_index::tag<IdTag>,
35
          boost::multi_index::identity<Value>
36
        >
37
      >
38
  >;
39
40
 public:
41
  typedef typename Impl::const_iterator const_iterator;
42
43
25.7k
  explicit LRUCache(size_t capacity) : capacity_(capacity) {}
_ZN2yb8LRUCacheIiEC2Em
Line
Count
Source
43
1
  explicit LRUCache(size_t capacity) : capacity_(capacity) {}
_ZN2yb8LRUCacheINS_17StronglyTypedUuidINS_17TransactionId_TagEEEEC2Em
Line
Count
Source
43
25.7k
  explicit LRUCache(size_t capacity) : capacity_(capacity) {}
44
45
  // Insert entry in cache.
46
318k
  void insert(const Value& value) {
47
318k
    auto p = impl_.push_front(value);
48
49
318k
    if (!p.second) {
50
0
      impl_.relocate(impl_.begin(), p.first);
51
318k
    } else if (impl_.size() > capacity_) {
52
225k
      impl_.pop_back();
53
225k
    }
54
318k
  }
_ZN2yb8LRUCacheIiE6insertERKi
Line
Count
Source
46
3
  void insert(const Value& value) {
47
3
    auto p = impl_.push_front(value);
48
49
3
    if (!p.second) {
50
0
      impl_.relocate(impl_.begin(), p.first);
51
3
    } else if (impl_.size() > capacity_) {
52
1
      impl_.pop_back();
53
1
    }
54
3
  }
_ZN2yb8LRUCacheINS_17StronglyTypedUuidINS_17TransactionId_TagEEEE6insertERKS3_
Line
Count
Source
46
318k
  void insert(const Value& value) {
47
318k
    auto p = impl_.push_front(value);
48
49
318k
    if (!p.second) {
50
0
      impl_.relocate(impl_.begin(), p.first);
51
318k
    } else if (impl_.size() > capacity_) {
52
225k
      impl_.pop_back();
53
225k
    }
54
318k
  }
55
56
318k
  void Insert(const Value& value) {
57
318k
    insert(value);
58
318k
  }
59
60
  // Erase entry from cache. Returns number of removed entries.
61
  template <class Key>
62
1.02M
  size_t erase(const Key& key) {
63
1.02M
    return impl_.template get<IdTag>().erase(key);
64
1.02M
  }
_ZN2yb8LRUCacheIiE5eraseIiEEmRKT_
Line
Count
Source
62
2
  size_t erase(const Key& key) {
63
2
    return impl_.template get<IdTag>().erase(key);
64
2
  }
_ZN2yb8LRUCacheINS_17StronglyTypedUuidINS_17TransactionId_TagEEEE5eraseIS3_EEmRKT_
Line
Count
Source
62
1.02M
  size_t erase(const Key& key) {
63
1.02M
    return impl_.template get<IdTag>().erase(key);
64
1.02M
  }
65
66
  // Erase entry from cache. Returns true if entry was removed.
67
  template <class Key>
68
1.02M
  size_t Erase(const Key& key) {
69
1.02M
    return erase(key);
70
1.02M
  }
71
72
2
  const_iterator begin() const {
73
2
    return impl_.begin();
74
2
  }
75
76
2
  const_iterator end() const {
77
2
    return impl_.end();
78
2
  }
79
80
 private:
81
  const size_t capacity_;
82
  Impl impl_;
83
};
84
85
} // namespace yb
86
87
#endif // YB_UTIL_LRU_CACHE_H