MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 parent_ = other.parent_;
64 wait_ = other.wait_;
65 object_ = other.object_;
66 other.parent_ = nullptr;
67 other.object_ = {};
68
69 return *this;
70 }
71
72 bool empty() const { return object_ == nullptr; }
73
74 bool operator==(const Object &obj) const { return obj == object_; }
75
78 if (object_ == nullptr && parent_) *this = parent_->get_instance(wait_);
79
80 return object_;
81 }
82
83 /**
84 * Mark that the object is dirty.
85 *
86 * Dirty object means that it is release by the manager
87 * without trying to cache it. This functionality is useful,
88 * when the user code, can't rollback changed done at instance
89 * of `Obj` thus releasing is the best option.
90 */
91 void set_dirty() { dirty_ = true; }
92
93 /**
94 * Mark the the object is clean.
95 *
96 * After marking object dirty, this method removes the dirty flag.
97 * It is useful after successful processing `Obj`, and there is
98 * no need of rollback its state.
99 */
100 void set_clean() { dirty_ = false; }
101
102 bool is_dirty() const { return dirty_; }
103
104 CacheManager *get_parent() const { return parent_; }
105
107 bool wait_{false};
109
110 private:
111 bool dirty_{false};
112 };
113
114 class Callbacks {
115 public:
117 virtual ~Callbacks() = default;
118
119 virtual bool object_before_cache(Object, bool dirty) = 0;
121 virtual void object_remove(Object) = 0;
122 virtual Object object_allocate(bool wait) = 0;
123 };
124
125 public:
126 CacheManager(Callbacks *callbacks, uint32_t limit = 20)
127 : objects_limit_{limit}, callbacks_{callbacks} {}
128
129 virtual ~CacheManager() {
130 while (!objects_.empty()) {
132 objects_.pop_front();
133 }
134 }
135
137 auto result = pop(wait);
138
139 return CachedObject{this, wait, result};
140 }
141
143 object.parent_ = nullptr;
144 {
145 std::unique_lock<std::mutex> lock(object_container_mutex_);
146 if (objects_.size() < objects_limit_) {
147 if (callbacks_->object_before_cache(object.object_,
148 object.is_dirty())) {
149 objects_.push_back(std::move(object.object_));
150 return;
151 }
152 }
153 }
154 callbacks_->object_remove(object.object_);
155 }
156
157 void change_cache_object_limit(uint32_t limit) {
158 // Just set the new limit, still in the cache
159 // there might be more objects than new limit,
160 // lest leave. Those object will be removed at runtime.
161 objects_limit_ = limit;
162 }
163
164 Callbacks *get_callbacks() const { return callbacks_; }
165
166 private:
168 {
169 std::unique_lock<std::mutex> lock(object_container_mutex_);
170 while (objects_.size()) {
171 auto result = std::move(objects_.front());
172 objects_.pop_front();
173
175 return result;
176 }
178 }
179 }
181 }
182
183 uint32_t objects_limit_{20};
185 std::list<Object> objects_;
187};
188
189} // namespace collector
190
191#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:74
Object get()
Definition: cache_manager.h:77
bool wait_
Definition: cache_manager.h:107
CacheManager * parent_
Definition: cache_manager.h:106
bool is_dirty() const
Definition: cache_manager.h:102
bool empty() const
Definition: cache_manager.h:72
CachedObject & operator=(CachedObject &&other)
Definition: cache_manager.h:62
void set_dirty()
Mark that the object is dirty.
Definition: cache_manager.h:91
Object object_
Definition: cache_manager.h:108
CacheManager * get_parent() const
Definition: cache_manager.h:104
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:111
void set_clean()
Mark the the object is clean.
Definition: cache_manager.h:100
Object operator->()
Definition: cache_manager.h:76
CachedObject(CachedObject &&other)
Definition: cache_manager.h:46
Definition: cache_manager.h:114
CacheManager::Object Object
Definition: cache_manager.h:116
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:136
Obj Object
Definition: cache_manager.h:38
Callbacks * get_callbacks() const
Definition: cache_manager.h:164
std::mutex object_container_mutex_
Definition: cache_manager.h:184
void change_cache_object_limit(uint32_t limit)
Definition: cache_manager.h:157
std::list< Object > objects_
Definition: cache_manager.h:185
void return_instance(CachedObject &object)
Definition: cache_manager.h:142
virtual ~CacheManager()
Definition: cache_manager.h:129
uint32_t objects_limit_
Definition: cache_manager.h:183
Object pop(bool wait)
Definition: cache_manager.h:167
Callbacks * callbacks_
Definition: cache_manager.h:186
CacheManager(Callbacks *callbacks, uint32_t limit=20)
Definition: cache_manager.h:126
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