MySQL 9.7.0
Source Code Documentation
lru.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_UTILITY_CACHE_LRU_H_
27#define MYSQL_HARNESS_UTILITY_CACHE_LRU_H_
28
29#include <algorithm>
30
31#include <concepts>
32#include <utility>
34
35namespace mysql_harness {
36namespace utility {
37namespace cache {
38namespace policy {
39
40class Lru {
41 public:
42 template <typename Key, uint32_t size>
44 public:
46
47 void access(const Key &key) {
48 auto it = std::remove(buffer_.begin(), buffer_.end(), key);
49
50 if (it != buffer_.end()) *it = key;
51 }
52
53 void remove(const Key &key) {
56 }
57 }
58
59 void push(const Key &key, Key **out_key = nullptr) {
60 if (size == buffer_.size()) {
61 auto key = pop();
62 if (out_key) *out_key = key;
63 }
64
66 }
67
68 Key *pop() {
69 if (buffer_.empty()) return nullptr;
70
71 if constexpr (std::movable<Key>) {
72 key_ = std::move(buffer_.front());
73 } else {
74 key_ = buffer_.front();
75 }
77 return &key_;
78 }
79
80 const Buffer &get_container() const { return buffer_; }
81
82 bool is_full() const { return size == buffer_.size(); }
83
84 private:
87 };
88
89 template <typename Key>
91 public:
92 using Buffer = std::list<Key>;
93
94 explicit AlgorithmDynamic(size_t max_size) : max_size_{max_size} {}
95
96 void access(const Key &key) {
97 if (buffer_.empty()) {
98 return;
99 }
100
101 if (buffer_.front() != key) {
102 buffer_.remove(key);
103 buffer_.push_front(key);
104 }
105 }
106
107 void remove(const Key &key) { buffer_.remove(key); }
108
109 void push(const Key &key, Key **out_key = nullptr) {
110 if (max_size_ == buffer_.size()) {
111 auto key = pop();
112 if (out_key) *out_key = key;
113 }
114
115 buffer_.push_front(key);
116 }
117
118 Key *pop() {
119 if (buffer_.empty()) return nullptr;
120
121 if constexpr (std::movable<Key>) {
122 key_ = std::move(buffer_.back());
123 } else {
124 key_ = buffer_.back();
125 }
126
127 buffer_.pop_back();
128 return &key_;
129 }
130
131 const Buffer &get_container() const { return buffer_; }
132
133 bool is_full() const { return max_size_ == buffer_.size(); }
134
135 private:
138 size_t max_size_;
139 };
140};
141
142} // namespace policy
143} // namespace cache
144} // namespace utility
145} // namespace mysql_harness
146
147#endif // MYSQL_HARNESS_UTILITY_CACHE_LRU_H_
const Buffer & get_container() const
Definition: lru.h:131
AlgorithmDynamic(size_t max_size)
Definition: lru.h:94
void access(const Key &key)
Definition: lru.h:96
void push(const Key &key, Key **out_key=nullptr)
Definition: lru.h:109
void remove(const Key &key)
Definition: lru.h:107
void remove(const Key &key)
Definition: lru.h:53
const Buffer & get_container() const
Definition: lru.h:80
void push(const Key &key, Key **out_key=nullptr)
Definition: lru.h:59
void access(const Key &key)
Definition: lru.h:47
iterator begin()
Definition: cyclic_buffer.h:149
void push_back(V &&v)
Definition: cyclic_buffer.h:156
value_type & front()
Definition: cyclic_buffer.h:172
uint32_t size() const
Definition: cyclic_buffer.h:168
void pop_back()
Definition: cyclic_buffer.h:184
iterator end()
Definition: cyclic_buffer.h:150
bool empty() const
Definition: cyclic_buffer.h:170
void pop_front()
Definition: cyclic_buffer.h:176
std::string_view Key
The key type for the hash structure in HashJoinRowBuffer.
Definition: hash_join_buffer.h:108
Definition: common.h:44
size_t size(const char *const c)
Definition: base64.h:46
static mysql_service_status_t remove(reference_caching_channel channel, const char *implementation_name) noexcept
Definition: component.cc:137
required string key
Definition: replication_asynchronous_connection_failover.proto:60