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