MySQL 9.0.0
Source Code Documentation
object_registry.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef DD_CACHE__OBJECT_REGISTRY_INCLUDED
25#define DD_CACHE__OBJECT_REGISTRY_INCLUDED
26
27#include <assert.h>
28#include <memory> // unique_ptr
29
30#include "local_multi_map.h" // Local_multi_map
31
32#include "sql/dd/types/abstract_table.h" // Abstract_table
33#include "sql/dd/types/charset.h" // Charset
34#include "sql/dd/types/collation.h" // Collation
35#include "sql/dd/types/column_statistics.h" // Column_statistics
36#include "sql/dd/types/event.h" // Event
37#include "sql/dd/types/resource_group.h" // Resource_group
38#include "sql/dd/types/routine.h" // Routine
39#include "sql/dd/types/schema.h" // Schema
40#include "sql/dd/types/spatial_reference_system.h" // Spatial_reference_system
41#include "sql/dd/types/tablespace.h" // Tablespace
42
43namespace dd {
44namespace cache {
45
46/**
47 Object registry containing several maps.
48
49 The registry is mainly a collection of maps for each type supported. The
50 functions dispatch to the appropriate map based on the key and object type
51 parameter. There is no support for locking or thread synchronization. The
52 object registry is kind of the single threaded version of the shared
53 dictionary cache.
54
55 The object registry is intended to be used as a thread local record of
56 which objects have been used.
57
58 The individual maps containing DD object pointers are allocated on demand
59 to avoid excessive performance overhead during object instantiation.
60*/
61
63 private:
64 template <typename T>
65 struct Type_selector {}; // Dummy type to use for
66 // selecting map instance.
67
68 std::unique_ptr<Local_multi_map<Abstract_table>> m_abstract_table_map;
69 std::unique_ptr<Local_multi_map<Charset>> m_charset_map;
70 std::unique_ptr<Local_multi_map<Collation>> m_collation_map;
71 std::unique_ptr<Local_multi_map<Column_statistics>> m_column_statistics_map;
72 std::unique_ptr<Local_multi_map<Event>> m_event_map;
73 std::unique_ptr<Local_multi_map<Resource_group>> m_resource_group_map;
74 std::unique_ptr<Local_multi_map<Routine>> m_routine_map;
75 std::unique_ptr<Local_multi_map<Schema>> m_schema_map;
76 std::unique_ptr<Local_multi_map<Spatial_reference_system>>
78 std::unique_ptr<Local_multi_map<Tablespace>> m_tablespace_map;
79
80 // Not inlined because it is big, and because it takes a lot of time
81 // for the compiler to instantiate. Defined in dd.cc, along the similar
82 // create_object() instantiations.
83 template <class T>
84 void create_map(std::unique_ptr<T> *map);
85
86 /**
87 Create a map instance unless it exists already.
88 */
89
90 template <typename T>
91 T *create_map_if_needed(std::unique_ptr<T> *map) {
92 if (*map == nullptr) create_map(map);
93 return map->get();
94 }
95
96 /**
97 Overloaded functions to use for selecting map instance based
98 on a key type. Const and non-const variants. The non-const variants
99 will create the map unless it exists.
100 */
101
104 }
105
108 return m_abstract_table_map.get();
109 }
110
113 }
114
116 return m_charset_map.get();
117 }
118
121 }
122
124 return m_collation_map.get();
125 }
126
129 }
130
133 return m_column_statistics_map.get();
134 }
135
138 }
139
141 return m_event_map.get();
142 }
143
146 }
147
150 return m_resource_group_map.get();
151 }
152
155 }
156
158 return m_routine_map.get();
159 }
160
163 }
164
166 return m_schema_map.get();
167 }
168
172 }
173
177 }
178
181 }
182
184 return m_tablespace_map.get();
185 }
186
187 /**
188 Template function to get a map instance.
189
190 To support generic code, the map instances are available through
191 template function instances. This allows looking up the appropriate
192 instance based on the key type. We must use overloading to accomplish
193 this (see above). Const and non-const variants.
194
195 @note The non-const variant will create a map unless it exists
196 already. The const variant will not create a map, and may
197 thus return a nullptr.
198
199 @tparam T Dictionary object type.
200
201 @return The map handling objects of type T.
202 */
203
204 template <typename T>
206 return m_map(Type_selector<T>());
207 }
208
209 template <typename T>
210 const Local_multi_map<T> *m_map() const {
211 return m_map(Type_selector<T>());
212 }
213
214 public:
215 /**
216 Get an iterator to the beginning of the local reverse map.
217
218 The reverse map is guaranteed to contain all elements, that why we
219 use it for iteration. The other maps may not contain all elements
220 since keys may be NULL.
221
222 @tparam T Dictionary object type.
223
224 @return Iterator to the beginning of the local reverse map.
225 */
226
227 template <typename T>
229 return m_map<T>()->begin();
230 }
231
232 /**
233 Get an iterator to one past the end of the local reverse map.
234
235 The reverse map is guaranteed to contain all elements, that why we
236 use it for iteration. The other maps may not contain all elements
237 since keys may be NULL.
238
239 @tparam T Dictionary object type.
240
241 @return Iterator to one past the end of the local reverse map.
242 */
243
244 template <typename T>
246 return m_map<T>()->end();
247 }
248
249 /**
250 Get the element corresponding to the given key.
251
252 @note If the map does not exist, we may as well just set the
253 object pointer to NULL right away since we know that the
254 object will not exist either.
255
256 @tparam K Key type.
257 @tparam T Dictionary object type.
258 @param key Key too lookup.
259 @param [out] element Element, if present, otherwise, NULL.
260 */
261
262 template <typename K, typename T>
263 void get(const K &key, Cache_element<T> **element) const {
264 const auto map = m_map<T>();
265 if (map)
266 map->get(key, element);
267 else
268 *element = nullptr;
269 }
270
271 /**
272 Add a new element to the registry.
273
274 @tparam T Dictionary object type.
275 @param element Element to be added.
276 */
277
278 template <typename T>
279 void put(Cache_element<T> *element) {
280 // If the map does not exist yet, the call to m_map() will
281 // create it.
282 m_map<T>()->put(element);
283 }
284
285 /**
286 Remove an element from the registry.
287
288 @tparam T Dictionary object type.
289 @param element Element to be removed.
290 */
291
292 template <typename T>
293 void remove(Cache_element<T> *element) {
294 // There should be no need to create a map to remove an element. At the
295 // same time, removing an element from a non-existing map means there
296 // is an error in our bookkeeping.
297 const auto map = m_map<T>();
298 assert(map != nullptr);
299 if (map) m_map<T>()->remove(element);
300 }
301
302 /**
303 Remove and delete all objects of a given type from the registry.
304
305 @tparam T Dictionary object type.
306 */
307
308 template <typename T>
309 void erase() {
310 // No need to create a map just to erase it. But in this case,
311 // it's not necessarily an error in our bookkeeping, since this
312 // is done as part of regular clean-up.
313 const auto map = m_map<T>();
314 if (map) m_map<T>()->erase();
315 }
316
317 /**
318 Remove and delete all objects from the registry.
319 */
320
321 void erase_all() {
322 erase<Abstract_table>();
323 erase<Charset>();
324 erase<Collation>();
325 erase<Column_statistics>();
326 erase<Event>();
327 erase<Resource_group>();
328 erase<Routine>();
329 erase<Schema>();
330 erase<Spatial_reference_system>();
331 erase<Tablespace>();
332 }
333
334 /**
335 Get the number of objects of a given type in the registry.
336
337 @tparam T Dictionary object type.
338 @return Number of objects.
339 */
340
341 template <typename T>
342 size_t size() const {
343 const auto map = m_map<T>();
344 if (map) return map->size();
345 return 0;
346 }
347
348 /**
349 Get the total number of objects in the registry.
350
351 @return Number of objects.
352 */
353
354 size_t size_all() const {
355 return size<Abstract_table>() + size<Charset>() + size<Collation>() +
356 size<Column_statistics>() + size<Event>() + size<Resource_group>() +
357 size<Routine>() + size<Schema>() + size<Spatial_reference_system>() +
358 size<Tablespace>();
359 }
360
361 /**
362 Debug dump of the object registry to stderr.
363
364 @tparam T Dictionary object type.
365 */
366
367 /* purecov: begin inspected */
368 template <typename T>
369 void dump() const {
370#ifndef NDEBUG
371 const auto map = m_map<T>();
372 if (map) map->dump();
373#endif
374 }
375 /* purecov: end */
376};
377
378} // namespace cache
379} // namespace dd
380
381#endif // DD_CACHE__OBJECT_REGISTRY_INCLUDED
Implementation of a dictionary client.
Definition: cache_element.h:69
Implementation of a local set of maps for a given object type.
Definition: local_multi_map.h:53
Element_map< constT *, Cache_element< T > >::Iterator Iterator
Definition: multi_map_base.h:128
Object registry containing several maps.
Definition: object_registry.h:62
Local_multi_map< Tablespace > * m_map(Type_selector< Tablespace >)
Definition: object_registry.h:179
void erase()
Remove and delete all objects of a given type from the registry.
Definition: object_registry.h:309
Local_multi_map< Event > * m_map(Type_selector< Event >)
Definition: object_registry.h:136
const Local_multi_map< Event > * m_map(Type_selector< Event >) const
Definition: object_registry.h:140
void put(Cache_element< T > *element)
Add a new element to the registry.
Definition: object_registry.h:279
std::unique_ptr< Local_multi_map< Event > > m_event_map
Definition: object_registry.h:72
const Local_multi_map< Resource_group > * m_map(Type_selector< Resource_group >) const
Definition: object_registry.h:148
T * create_map_if_needed(std::unique_ptr< T > *map)
Create a map instance unless it exists already.
Definition: object_registry.h:91
const Local_multi_map< Column_statistics > * m_map(Type_selector< Column_statistics >) const
Definition: object_registry.h:131
size_t size() const
Get the number of objects of a given type in the registry.
Definition: object_registry.h:342
void create_map(std::unique_ptr< T > *map)
Definition: dd.cc:112
const Local_multi_map< Routine > * m_map(Type_selector< Routine >) const
Definition: object_registry.h:157
std::unique_ptr< Local_multi_map< Tablespace > > m_tablespace_map
Definition: object_registry.h:78
Local_multi_map< T > * m_map()
Template function to get a map instance.
Definition: object_registry.h:205
const Local_multi_map< Collation > * m_map(Type_selector< Collation >) const
Definition: object_registry.h:123
void erase_all()
Remove and delete all objects from the registry.
Definition: object_registry.h:321
Local_multi_map< Resource_group > * m_map(Type_selector< Resource_group >)
Definition: object_registry.h:144
std::unique_ptr< Local_multi_map< Collation > > m_collation_map
Definition: object_registry.h:70
std::unique_ptr< Local_multi_map< Routine > > m_routine_map
Definition: object_registry.h:74
Multi_map_base< T >::Iterator end()
Get an iterator to one past the end of the local reverse map.
Definition: object_registry.h:245
void remove(Cache_element< T > *element)
Remove an element from the registry.
Definition: object_registry.h:293
std::unique_ptr< Local_multi_map< Abstract_table > > m_abstract_table_map
Definition: object_registry.h:68
const Local_multi_map< Schema > * m_map(Type_selector< Schema >) const
Definition: object_registry.h:165
void get(const K &key, Cache_element< T > **element) const
Get the element corresponding to the given key.
Definition: object_registry.h:263
size_t size_all() const
Get the total number of objects in the registry.
Definition: object_registry.h:354
Local_multi_map< Collation > * m_map(Type_selector< Collation >)
Definition: object_registry.h:119
Local_multi_map< Abstract_table > * m_map(Type_selector< Abstract_table >)
Overloaded functions to use for selecting map instance based on a key type.
Definition: object_registry.h:102
Local_multi_map< Column_statistics > * m_map(Type_selector< Column_statistics >)
Definition: object_registry.h:127
std::unique_ptr< Local_multi_map< Column_statistics > > m_column_statistics_map
Definition: object_registry.h:71
Multi_map_base< T >::Iterator begin()
Get an iterator to the beginning of the local reverse map.
Definition: object_registry.h:228
std::unique_ptr< Local_multi_map< Resource_group > > m_resource_group_map
Definition: object_registry.h:73
const Local_multi_map< Abstract_table > * m_map(Type_selector< Abstract_table >) const
Definition: object_registry.h:106
void dump() const
Debug dump of the object registry to stderr.
Definition: object_registry.h:369
const Local_multi_map< Charset > * m_map(Type_selector< Charset >) const
Definition: object_registry.h:115
std::unique_ptr< Local_multi_map< Charset > > m_charset_map
Definition: object_registry.h:69
Local_multi_map< Routine > * m_map(Type_selector< Routine >)
Definition: object_registry.h:153
std::unique_ptr< Local_multi_map< Schema > > m_schema_map
Definition: object_registry.h:75
const Local_multi_map< Tablespace > * m_map(Type_selector< Tablespace >) const
Definition: object_registry.h:183
const Local_multi_map< T > * m_map() const
Definition: object_registry.h:210
Local_multi_map< Spatial_reference_system > * m_map(Type_selector< Spatial_reference_system >)
Definition: object_registry.h:169
Local_multi_map< Charset > * m_map(Type_selector< Charset >)
Definition: object_registry.h:111
Local_multi_map< Schema > * m_map(Type_selector< Schema >)
Definition: object_registry.h:161
const Local_multi_map< Spatial_reference_system > * m_map(Type_selector< Spatial_reference_system >) const
Definition: object_registry.h:174
std::unique_ptr< Local_multi_map< Spatial_reference_system > > m_spatial_reference_system_map
Definition: object_registry.h:77
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2893
required string key
Definition: replication_asynchronous_connection_failover.proto:60
Definition: object_registry.h:65