|
| 1 | +#pragma once |
| 2 | + |
| 3 | +/// @file userver/multi-index-lru/container.hpp |
| 4 | +/// @brief @copybrief multi_index_lru::Container |
| 5 | + |
| 6 | +#include <boost/intrusive/link_mode.hpp> |
| 7 | +#include <boost/intrusive/list.hpp> |
| 8 | +#include <boost/intrusive/list_hook.hpp> |
| 9 | +#include <boost/multi_index/hashed_index.hpp> |
| 10 | +#include <boost/multi_index/mem_fun.hpp> |
| 11 | +#include <boost/multi_index/member.hpp> |
| 12 | +#include <boost/multi_index/ordered_index.hpp> |
| 13 | +#include <boost/multi_index/tag.hpp> |
| 14 | +#include <boost/multi_index_container.hpp> |
| 15 | + |
| 16 | +#include <cstddef> |
| 17 | +#include <utility> |
| 18 | + |
| 19 | +USERVER_NAMESPACE_BEGIN |
| 20 | + |
| 21 | +namespace multi_index_lru { |
| 22 | + |
| 23 | +namespace impl { |
| 24 | + |
| 25 | +template <typename Value> |
| 26 | +struct ValueWithHook { |
| 27 | + Value value; |
| 28 | + mutable boost::intrusive::list_member_hook<> list_hook; |
| 29 | + |
| 30 | + const ValueWithHook* GetPointerToSelf() const { return this; }; |
| 31 | + |
| 32 | + explicit ValueWithHook(const Value& val) : value(val) {} |
| 33 | + |
| 34 | + explicit ValueWithHook(Value&& val) : value(std::move(val)) {} |
| 35 | + |
| 36 | + ValueWithHook() = delete; |
| 37 | + ValueWithHook(const ValueWithHook&) = delete; |
| 38 | + ValueWithHook(ValueWithHook&&) = delete; |
| 39 | + |
| 40 | + ValueWithHook& operator=(const ValueWithHook&) = delete; |
| 41 | + ValueWithHook& operator=(ValueWithHook&&) = delete; |
| 42 | + |
| 43 | + operator Value&() { return value; } |
| 44 | + operator const Value&() const { return value; } |
| 45 | + |
| 46 | + Value* operator->() { return &value; } |
| 47 | + const Value* operator->() const { return &value; } |
| 48 | + |
| 49 | + Value& get() { return value; } |
| 50 | + const Value& get() const { return value; } |
| 51 | +}; |
| 52 | + |
| 53 | +template <class List, class Node> |
| 54 | +void PushBackToList(List& lst, const Node& node) { |
| 55 | + lst.push_back(const_cast<Node&>(node)); // TODO: |
| 56 | +} |
| 57 | + |
| 58 | +template <class List, class Node> |
| 59 | +void SpliceInList(List& lst, Node& node) { |
| 60 | + lst.splice(lst.end(), lst, lst.iterator_to(node)); |
| 61 | +} |
| 62 | + |
| 63 | +struct InternalPtrTag {}; |
| 64 | + |
| 65 | +} // namespace impl |
| 66 | + |
| 67 | +/// @ingroup userver_containers |
| 68 | +/// |
| 69 | +/// @brief MultiIndex LRU container |
| 70 | +template <typename Value, typename IndexSpecifierList, typename Allocator = std::allocator<impl::ValueWithHook<Value>>> |
| 71 | +class Container { |
| 72 | +public: |
| 73 | + explicit Container(size_t max_size) : max_size(max_size) {} |
| 74 | + |
| 75 | + template <typename... Args> |
| 76 | + bool emplace(Args&&... args) { |
| 77 | + if (container.size() >= max_size) { |
| 78 | + EvictLru(); |
| 79 | + } |
| 80 | + |
| 81 | + auto result = container.emplace(std::forward<Args>(args)...); |
| 82 | + |
| 83 | + auto& value = *result.first; |
| 84 | + if (result.second) { |
| 85 | + impl::PushBackToList(usage_list, value); |
| 86 | + } else { |
| 87 | + impl::SpliceInList(usage_list, value); |
| 88 | + } |
| 89 | + return result.second; |
| 90 | + } |
| 91 | + |
| 92 | + bool insert(const Value& value) { return emplace(value); } |
| 93 | + |
| 94 | + bool insert(Value&& value) { return emplace(std::move(value)); } |
| 95 | + |
| 96 | + template <typename Tag, typename Key> |
| 97 | + auto find(const Key& key) { |
| 98 | + auto& primary_index = container.template get<Tag>(); |
| 99 | + auto it = primary_index.find(key); |
| 100 | + |
| 101 | + if (it != primary_index.end()) { |
| 102 | + impl::SpliceInList(usage_list, *it); |
| 103 | + } |
| 104 | + |
| 105 | + return it; |
| 106 | + } |
| 107 | + |
| 108 | + template <typename Tag> |
| 109 | + auto end() { |
| 110 | + return container.template get<Tag>().end(); |
| 111 | + } |
| 112 | + |
| 113 | + template <typename Tag, typename Key> |
| 114 | + bool contains(const Key& key) { |
| 115 | + return this->template find<Tag, Key>(key) != container.template get<Tag>().end(); |
| 116 | + } |
| 117 | + |
| 118 | + template <typename Tag, typename Key> |
| 119 | + bool erase(const Key& key) { |
| 120 | + auto& primary_index = container.template get<Tag>(); |
| 121 | + auto it = primary_index.find(key); |
| 122 | + if (it != primary_index.end()) { |
| 123 | + usage_list.erase(usage_list.iterator_to(*it)); |
| 124 | + } |
| 125 | + return container.template get<Tag>().erase(key) > 0; |
| 126 | + } |
| 127 | + |
| 128 | + std::size_t size() const { return container.size(); } |
| 129 | + bool empty() const { return container.empty(); } |
| 130 | + std::size_t capacity() const { return max_size; } |
| 131 | + |
| 132 | + void set_capacity(size_t new_capacity) { |
| 133 | + max_size = new_capacity; |
| 134 | + while (container.size() > max_size) { |
| 135 | + EvictLru(); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + void clear() { container.clear(); } |
| 140 | + |
| 141 | +private: |
| 142 | + using CacheItem = impl::ValueWithHook<Value>; |
| 143 | + using List = boost::intrusive::list< |
| 144 | + CacheItem, |
| 145 | + boost::intrusive::member_hook<CacheItem, boost::intrusive::list_member_hook<>, &CacheItem::list_hook>>; |
| 146 | + |
| 147 | + using ExtendedIndexSpecifierList = typename boost::mpl::push_back< |
| 148 | + IndexSpecifierList, |
| 149 | + boost::multi_index::hashed_unique< |
| 150 | + boost::multi_index::tag<impl::InternalPtrTag>, |
| 151 | + boost::multi_index::const_mem_fun<CacheItem, const CacheItem*, &CacheItem::GetPointerToSelf>>>::type; |
| 152 | + |
| 153 | + using BoostContainer = boost::multi_index::multi_index_container<CacheItem, ExtendedIndexSpecifierList, Allocator>; |
| 154 | + |
| 155 | + void EvictLru() { |
| 156 | + if (!usage_list.empty()) { |
| 157 | + CacheItem* ptr_to_erase = &*usage_list.begin(); |
| 158 | + usage_list.erase(usage_list.begin()); |
| 159 | + container.template get<impl::InternalPtrTag>().erase(ptr_to_erase); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + BoostContainer container; |
| 164 | + std::size_t max_size; |
| 165 | + List usage_list; |
| 166 | +}; |
| 167 | +} // namespace multi_index_lru |
| 168 | + |
| 169 | +USERVER_NAMESPACE_END |
0 commit comments