-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomponent.cc
247 lines (205 loc) · 7.79 KB
/
component.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <mysql/components/component_implementation.h>
#include <mysql/components/services/mysql_mutex.h>
#include <mysql/components/services/psi_memory.h>
#include <mysql/components/services/psi_mutex.h>
#include <mysql/components/services/reference_caching.h>
#include "cache.h"
#include "channel.h"
namespace reference_caching {
namespace channel {
static DEFINE_BOOL_METHOD(create, (const char *service_names[],
reference_caching_channel *out_channel)) {
try {
service_names_set<> refs;
for (unsigned idx = 0; service_names[idx]; idx++)
refs.insert(service_names[idx]);
*out_channel =
reinterpret_cast<reference_caching_channel>(channel_imp::create(refs));
return *out_channel ? false : true;
} catch (...) {
return true;
}
}
static DEFINE_BOOL_METHOD(destroy, (reference_caching_channel channel)) {
try {
return channel_imp::destroy(reinterpret_cast<channel_imp *>(channel));
} catch (...) {
return true;
}
}
static DEFINE_BOOL_METHOD(invalidate, (reference_caching_channel channel)) {
try {
reinterpret_cast<channel_imp *>(channel)->set_valid(false);
return false;
} catch (...) {
return true;
}
}
static DEFINE_BOOL_METHOD(validate, (reference_caching_channel channel)) {
try {
reinterpret_cast<channel_imp *>(channel)->set_valid(true);
return false;
} catch (...) {
return true;
}
}
static DEFINE_BOOL_METHOD(fetch, (const char *service_name,
reference_caching_channel *out_channel)) {
try {
*out_channel = reinterpret_cast<reference_caching_channel>(
channel_imp::channel_by_name(service_name));
return *out_channel ? false : true;
} catch (...) {
return true;
}
}
} // namespace channel
namespace cache {
static DEFINE_BOOL_METHOD(create, (reference_caching_channel channel,
SERVICE_TYPE(registry) * registry,
reference_caching_cache *out_cache)) {
try {
cache_imp *ptr =
cache_imp::create(reinterpret_cast<channel_imp *>(channel), registry);
*out_cache = reinterpret_cast<reference_caching_cache>(ptr);
return false;
} catch (...) {
return true;
}
}
static DEFINE_BOOL_METHOD(destroy, (reference_caching_cache cache)) {
try {
return cache_imp::destroy(reinterpret_cast<cache_imp *>(cache));
} catch (...) {
return true;
}
}
static DEFINE_BOOL_METHOD(get, (reference_caching_cache cache,
unsigned service_name_index,
const my_h_service **refs)) {
try {
return reinterpret_cast<cache_imp *>(cache)->get(service_name_index, refs);
} catch (...) {
return true;
}
}
static DEFINE_BOOL_METHOD(flush, (reference_caching_cache cache)) {
try {
return reinterpret_cast<cache_imp *>(cache)->flush();
} catch (...) {
return true;
}
}
} // namespace cache
namespace channel_ignore_list {
static DEFINE_BOOL_METHOD(add, (reference_caching_channel channel,
const char *implementation_name)) {
try {
return reinterpret_cast<channel_imp *>(channel)->ignore_list_add(
implementation_name);
} catch (...) {
return true;
}
}
static DEFINE_BOOL_METHOD(remove, (reference_caching_channel channel,
const char *implementation_name)) {
try {
return reinterpret_cast<channel_imp *>(channel)->ignore_list_remove(
implementation_name);
} catch (...) {
return true;
}
}
static DEFINE_BOOL_METHOD(clear, (reference_caching_channel channel)) {
try {
return reinterpret_cast<channel_imp *>(channel)->ignore_list_clear();
} catch (...) {
return true;
}
}
} // namespace channel_ignore_list
PSI_memory_key KEY_mem_reference_cache;
void *Cache_malloced::operator new(std::size_t sz) {
return my_malloc(KEY_mem_reference_cache, sz, 0);
}
void Cache_malloced::operator delete(void *ptr, std::size_t) { my_free(ptr); }
static void register_instruments() {
static PSI_memory_info all_memory[] = {
{&KEY_mem_reference_cache, "reference_cache_mem", 0, 0,
"All the memory allocations for the reference cache component"},
};
int count = static_cast<int>(sizeof(all_memory) / sizeof(PSI_memory_info));
PSI_MEMORY_CALL(register_memory)(PSI_category, all_memory, count);
}
static mysql_service_status_t init() {
register_instruments();
try {
return channel_imp::factory_init() ? 1 : 0;
} catch (...) {
return 1;
}
}
static mysql_service_status_t deinit() {
try {
return channel_imp::factory_deinit() ? 1 : 0;
} catch (...) {
return 1;
}
}
} // namespace reference_caching
// component definition
BEGIN_SERVICE_IMPLEMENTATION(reference_caching, reference_caching_channel)
reference_caching::channel::create, reference_caching::channel::destroy,
reference_caching::channel::invalidate,
reference_caching::channel::validate,
reference_caching::channel::fetch END_SERVICE_IMPLEMENTATION();
BEGIN_SERVICE_IMPLEMENTATION(reference_caching, reference_caching_cache)
reference_caching::cache::create, reference_caching::cache::destroy,
reference_caching::cache::get,
reference_caching::cache::flush END_SERVICE_IMPLEMENTATION();
BEGIN_SERVICE_IMPLEMENTATION(reference_caching,
reference_caching_channel_ignore_list)
reference_caching::channel_ignore_list::add,
reference_caching::channel_ignore_list::remove,
reference_caching::channel_ignore_list::clear END_SERVICE_IMPLEMENTATION();
BEGIN_COMPONENT_PROVIDES(reference_caching)
PROVIDES_SERVICE(reference_caching, reference_caching_channel),
PROVIDES_SERVICE(reference_caching, reference_caching_cache),
PROVIDES_SERVICE(reference_caching, reference_caching_channel_ignore_list),
END_COMPONENT_PROVIDES();
REQUIRES_MYSQL_MUTEX_SERVICE_PLACEHOLDER;
REQUIRES_PSI_MUTEX_SERVICE_PLACEHOLDER;
REQUIRES_PSI_MEMORY_SERVICE_PLACEHOLDER;
/* A list of required services. */
BEGIN_COMPONENT_REQUIRES(reference_caching)
REQUIRES_MYSQL_MUTEX_SERVICE, REQUIRES_PSI_MUTEX_SERVICE,
REQUIRES_PSI_MEMORY_SERVICE, END_COMPONENT_REQUIRES();
/* A list of metadata to describe the Component. */
BEGIN_COMPONENT_METADATA(reference_caching)
METADATA("mysql.author", "Oracle Corporation"),
METADATA("mysql.license", "GPL"), END_COMPONENT_METADATA();
/* Declaration of the Component. */
DECLARE_COMPONENT(reference_caching, "mysql:reference_caching")
reference_caching::init, reference_caching::deinit END_DECLARE_COMPONENT();
/* Defines list of Components contained in this library. Note that for now
we assume that library will have exactly one Component. */
DECLARE_LIBRARY_COMPONENTS &COMPONENT_REF(reference_caching)
END_DECLARE_LIBRARY_COMPONENTS