MySQL 9.5.0
Source Code Documentation
cache_manager.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2025, 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 ROUTER_SRC_REST_MRS_SRC_CACHE_MANAGER_H_
27#define ROUTER_SRC_REST_MRS_SRC_CACHE_MANAGER_H_
28
29#include <list>
30#include <mutex>
31#include <utility>
32
33namespace collector {
34
35template <typename Obj>
37 public:
38 using Object = Obj;
39 class Callbacks;
40
42 public:
43 CachedObject(CacheManager *parent = nullptr)
44 : parent_{parent}, object_{nullptr} {}
45
47 : parent_{other.parent_},
48 wait_{other.wait_},
49 object_{std::move(other.object_)} {
50 other.parent_ = nullptr;
51 other.object_ = nullptr;
52 }
53
54 template <typename... Args>
55 explicit CachedObject(CacheManager *parent, bool wait, Args &&...args)
56 : parent_{parent}, wait_{wait}, object_{std::forward<Args>(args)...} {}
57
59 if (parent_ && object_) parent_->return_instance(*this);
60 }
61
63 if (parent_ && object_) parent_->return_instance(*this);
64
65 parent_ = other.parent_;
66 wait_ = other.wait_;
67 object_ = other.object_;
68 other.parent_ = nullptr;
69 other.object_ = {};
70
71 return *this;
72 }
73
74 bool empty() const { return object_ == nullptr; }
75
76 bool operator==(const Object &obj) const { return obj == object_; }
77
80 if (object_ == nullptr && parent_) *this = parent_->get_instance(wait_);
81
82 return object_;
83 }
84
85 /**
86 * Mark that the object is dirty.
87 *
88 * Dirty object means that it is release by the manager
89 * without trying to cache it. This functionality is useful,
90 * when the user code, can't rollback changed done at instance
91 * of `Obj` thus releasing is the best option.
92 */
93 void set_dirty() { dirty_ = true; }
94
95 /**
96 * Mark the the object is clean.
97 *
98 * After marking object dirty, this method removes the dirty flag.
99 * It is useful after successful processing `Obj`, and there is
100 * no need of rollback its state.
101 */
102 void set_clean() { dirty_ = false; }
103
104 bool is_dirty() const { return dirty_; }
105
106 CacheManager *get_parent() const { return parent_; }
107
109 bool wait_{false};
111
112 private:
113 bool dirty_{false};
114 };
115
116 class Callbacks {
117 public:
119 virtual ~Callbacks() = default;
120
121 virtual bool object_before_cache(Object, bool dirty) = 0;
123 virtual void object_remove(Object) = 0;
124 virtual Object object_allocate(bool wait) = 0;
125 };
126
127 public:
128 CacheManager(Callbacks *callbacks, uint32_t limit = 20)
129 : objects_limit_{limit}, callbacks_{callbacks} {}
130
131 virtual ~CacheManager() {
132 while (!objects_.empty()) {
134 objects_.pop_front();
135 }
136 }
137
139 auto result = pop(wait);
140
141 return CachedObject{this, wait, result};
142 }
143
145 object.parent_ = nullptr;
146 {
147 bool maybe_returnable = false;
148 {
149 std::unique_lock lock(object_container_mutex_);
150 if (objects_.size() < objects_limit_) maybe_returnable = true;
151 }
152
153 if (maybe_returnable &&
154 callbacks_->object_before_cache(object.object_, object.is_dirty())) {
155 std::unique_lock lock(object_container_mutex_);
156 if (objects_.size() < objects_limit_) {
157 objects_.push_back(std::move(object.object_));
158 return;
159 }
160 }
161 }
162 callbacks_->object_remove(object.object_);
163 }
164
165 void change_cache_object_limit(uint32_t limit) {
166 // Just set the new limit, still in the cache
167 // there might be more objects than new limit,
168 // lest leave. Those object will be removed at runtime.
169 objects_limit_ = limit;
170 }
171
172 Callbacks *get_callbacks() const { return callbacks_; }
173
174 private:
175 inline Object pop_one() {
176 std::unique_lock<std::mutex> lock(object_container_mutex_);
177 if (!objects_.empty()) {
178 auto result = std::move(objects_.front());
179 objects_.pop_front();
180
181 return result;
182 }
183 return Object();
184 }
185
187 for (;;) {
189 if (!result) break;
190
192 return result;
193 }
195 }
196
198 }
199
200 uint32_t objects_limit_{20};
202 std::list<Object> objects_;
204};
205
206} // namespace collector
207
208#endif // ROUTER_SRC_REST_MRS_SRC_CACHE_MANAGER_H_
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
Definition: cache_manager.h:41
~CachedObject()
Definition: cache_manager.h:58
bool operator==(const Object &obj) const
Definition: cache_manager.h:76
Object get()
Definition: cache_manager.h:79
bool wait_
Definition: cache_manager.h:109
CacheManager * parent_
Definition: cache_manager.h:108
bool is_dirty() const
Definition: cache_manager.h:104
bool empty() const
Definition: cache_manager.h:74
CachedObject & operator=(CachedObject &&other)
Definition: cache_manager.h:62
void set_dirty()
Mark that the object is dirty.
Definition: cache_manager.h:93
Object object_
Definition: cache_manager.h:110
CacheManager * get_parent() const
Definition: cache_manager.h:106
CachedObject(CacheManager *parent=nullptr)
Definition: cache_manager.h:43
CachedObject(CacheManager *parent, bool wait, Args &&...args)
Definition: cache_manager.h:55
bool dirty_
Definition: cache_manager.h:113
void set_clean()
Mark the the object is clean.
Definition: cache_manager.h:102
Object operator->()
Definition: cache_manager.h:78
CachedObject(CachedObject &&other)
Definition: cache_manager.h:46
Definition: cache_manager.h:116
CacheManager::Object Object
Definition: cache_manager.h:118
virtual void object_remove(Object)=0
virtual Object object_allocate(bool wait)=0
virtual bool object_before_cache(Object, bool dirty)=0
virtual bool object_retrived_from_cache(Object)=0
Definition: cache_manager.h:36
CachedObject get_instance(bool wait)
Definition: cache_manager.h:138
Object pop_one()
Definition: cache_manager.h:175
Obj Object
Definition: cache_manager.h:38
Callbacks * get_callbacks() const
Definition: cache_manager.h:172
std::mutex object_container_mutex_
Definition: cache_manager.h:201
void change_cache_object_limit(uint32_t limit)
Definition: cache_manager.h:165
std::list< Object > objects_
Definition: cache_manager.h:202
void return_instance(CachedObject &object)
Definition: cache_manager.h:144
virtual ~CacheManager()
Definition: cache_manager.h:131
uint32_t objects_limit_
Definition: cache_manager.h:200
Object pop(bool wait)
Definition: cache_manager.h:186
Callbacks * callbacks_
Definition: cache_manager.h:203
CacheManager(Callbacks *callbacks, uint32_t limit=20)
Definition: cache_manager.h:128
Definition: cache_manager.h:33
static int wait(mysql_cond_t *that, mysql_mutex_t *mutex_arg, const char *, unsigned int)
Definition: mysql_cond_v1_native.cc:62
Definition: gcs_xcom_synode.h:64
static std::mutex lock
Definition: net_ns.cc:56
struct result result
Definition: result.h:34
Definition: result.h:30