MySQL 8.0.40
Source Code Documentation
system_registry.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 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__SYSTEM_REGISTRY_INCLUDED
25#define DD__SYSTEM_REGISTRY_INCLUDED
26
27#include <assert.h>
28#include <stdio.h>
29#include <map>
30#include <new>
31#include <string>
32#include <utility>
33#include <vector>
34
35#include "mysqld_error.h" // ER_NO_SYSTEM_TABLE_...
36#include "sql/dd/string_type.h" // dd::String_type
39#include "sql/plugin_table.h"
40
41namespace dd {
42
43class Object_table;
44
45/**
46 Class to wrap an entity object.
47
48 The Entity_element instances are intended used in registries to support
49 classification of meta data. The Entity_element template points to an
50 instance of some type, and also has a property member that is associated
51 with the entity, as well as a key with which the object is associated.
52
53 @note The entity object ownership is decided by the relevant template
54 parameter, see below.
55
56 @tparam K A key type, e.g. a pair of strings.
57 @tparam T An entity type, e.g. an Object_table instance.
58 @tparam P A property type, e.g. an enumeration.
59 @tparam F Function to map from property to name.
60 @tparam D Boolean to decide ownership of wrapped object.
61*/
62
63template <typename K, typename T, typename P, const char *F(P), bool D>
65 private:
66 const K m_key; //!< The key associated with the entity object.
67 const T *m_entity; //!< Entity object pointer, like an Object_table instance.
68 const P m_property; //!< Property of some kind, like an enumeration.
69
70 public:
71 Entity_element(const K &key, const T *entity, const P property)
73
75 // Delete the wrapped object depending on the template parameter.
76 if (D) delete m_entity;
77 }
78
79 const K &key() const { return m_key; }
80
81 const T *entity() const { return m_entity; }
82
83 const P property() const { return m_property; }
84
85 const P *property_ptr() const { return &m_property; }
86
87#ifndef NDEBUG
88 void dump() const {
89 fprintf(stderr, "Key= '%s.%s', property= '%s'\n", m_key.first.c_str(),
90 m_key.second.c_str(), F(m_property));
91 }
92#endif
93};
94
95/**
96 Class to represent collections of meta data for entities.
97
98 This class is used to represent relevant meta data and properties of
99 system related entities, e.g. all the dictionary tables. The meta data
100 is associated with a key instance, usually a std::pair containing the
101 schema- and entity names.
102
103 The class supports adding and retrieving objects, as well as iteration
104 based on the order of inserts.
105
106 @note There is no support for concurrency. We assume that the registry
107 is created and that the entities are inserted in a single threaded
108 situation, e.g. while the server is being started. Read only access
109 can happen from several threads simultaneously, though.
110
111 @tparam K Key type.
112 @tparam T Entity type.
113 @tparam P Property type.
114 @tparam F Function to map from property to name.
115 @tparam D Boolean flag to decide whether the wrapped objects
116 are owned by the registry.
117*/
118
119template <typename K, typename T, typename P, const char *F(P), bool D>
121 private:
123 typedef std::vector<Entity_element_type *> Entity_list_type;
124 typedef std::map<K, Entity_element_type *> Entity_map_type;
125
126 Entity_list_type m_entity_list; //!< List for ordered access.
127 Entity_map_type m_entity_map; //!< Map for direct key based lookup.
128
129 public:
130 // Externally available iteration is based on the order of inserts.
131 typedef typename Entity_list_type::const_iterator Const_iterator;
132
133 /**
134 Delete the heap memory owned by the entity registry.
135
136 Only the wrapper elements are owned by the registry.
137 The std::vector and std::map are not allocated
138 dynamically, and their destructors are called implicitly
139 when this destructor is called.
140 */
141
143 // Delete elements from the map. The objects that are wrapped
144 // will be handled by the wrapper destructor.
145 for (typename Entity_map_type::iterator it = m_entity_map.begin();
146 it != m_entity_map.end(); ++it)
147 delete it->second;
148 }
149
150 /**
151 Add a new entity to the registry.
152
153 This method will create a new key and a new entity wrapper element. The
154 wrapper will be added to the map, along with the key, and to the vector
155 to keep track of the order of inserts.
156
157 @param schema_name Schema name used to construct the key.
158 @param entity_name Entity name used to construct the key.
159 @param property Property used to classify the entity.
160 @param entity Entity which is classified and registered.
161 */
162
163 void add(const String_type &schema_name, const String_type &entity_name,
164 P property, T *entity) {
165 // Create a new key and make sure it does not already exist.
166 K key(schema_name, entity_name);
167 assert(m_entity_map.find(key) == m_entity_map.end());
168
169 // Create a new entity wrapper element. The wrapper will be owned by the
170 // entity map and deleted along with it.
171 Entity_element_type *element =
172 new (std::nothrow) Entity_element_type(key, entity, property);
173
174 // Add the key, value pair to the map.
175 m_entity_map.insert(typename Entity_map_type::value_type(key, element));
176
177 // Add the entity to the ordered list too.
178 m_entity_list.push_back(element);
179 }
180
181 /**
182 Find an element entity in the registry.
183
184 This method creates a key based on the submitted parameters, and
185 looks up in the member map. If the key is found, the object pointed
186 to by the wrapper is returned, otherwise, nullptr is returned.
187
188 @param schema_name Schema containing the entity searched for.
189 @param entity_name Name of the entity searched for.
190 @retval nullptr if not found, otherwise, the entity
191 object pointed to by the wrapper element.
192 */
193
194 const T *find_entity(const String_type &schema_name,
195 const String_type &entity_name) const {
196 // Create a new key. This is only used for lookup, so it is allocated
197 // on the stack.
198 K key(schema_name, entity_name);
199
200 // Lookup in the map based on the key.
201 typename Entity_map_type::const_iterator element_it =
202 m_entity_map.find(key);
203
204 // Return nullptr if not found, otherwise, return a pointer to the entity.
205 if (element_it == m_entity_map.end()) return nullptr;
206
207 return element_it->second->entity();
208 }
209
210 /**
211 Find the property of an element in the registry.
212
213 This method creates a key based on the submitted parameters, and
214 looks up in the member map. If the key is found, the property
215 associated with the key is returned, otherwise, nullptr is returned.
216
217 @param schema_name Schema containing the entity searched for.
218 @param entity_name Name of the entity searched for.
219 @retval nullptr if not found, otherwise, the a pointer to
220 the property associated with the key.
221 */
222
223 const P *find_property(const String_type &schema_name,
224 const String_type &entity_name) const {
225 // Create a new key. This is only used for lookup, so it is allocated
226 // on the stack.
227 K key(schema_name, entity_name);
228
229 // Lookup in the map based on the key.
230 typename Entity_map_type::const_iterator element_it =
231 m_entity_map.find(key);
232
233 // Return nullptr if not found, otherwise, return a pointer to the property.
234 if (element_it == m_entity_map.end()) return nullptr;
235
236 return element_it->second->property_ptr();
237 }
238
239 /**
240 Get the beginning of the vector of ordered inserts.
241
242 @retval Iterator referring to the first element in the vector,
243 or end() if the vector is empty.
244 */
245
246 Const_iterator begin() const { return m_entity_list.begin(); }
247
248 /**
249 Get the first element with a certain property.
250
251 This method retrieves the first element with a certain property, and
252 is used for iterating only over elements having this property.
253
254 @param property Property that the element should have.
255 @retval Iterator referring to the first element in the vector
256 with the submitted property, or end().
257 */
258
259 Const_iterator begin(P property) const {
260 Const_iterator it = begin();
261 while (it != end()) {
262 if ((*it)->property() == property) break;
263 ++it;
264 }
265
266 return it;
267 }
268
269 /**
270 Get the end of the vector of ordered inserts.
271
272 @retval Iterator referring to the special element after the last "real"
273 element in the vector.
274 */
275
276 Const_iterator end() const { return m_entity_list.end(); }
277
278 /**
279 Get the next element in the list of ordered inserts.
280
281 @param current The current iterator.
282 @retval Iterator referring to the next element in the vector.
283 */
284
286 if (current == end()) return current;
287
288 return ++current;
289 }
290
291 /**
292 Get the next element in the list of ordered inserts.
293
294 This method retrieves the next element with a certain property, and
295 is used for iterating only over elements having this property.
296
297 @param current The current iterator.
298 @param property Property that the next element should have.
299 @retval Iterator referring to the next element in the vector
300 with the submitted property.
301 */
302
303 Const_iterator next(Const_iterator current, P property) const {
304 if (current == end()) return current;
305
306 while (++current != end())
307 if ((*current)->property() == property) break;
308
309 return current;
310 }
311
312#ifndef NDEBUG
313 void dump() const {
314 // List the entities in the order they were inserted.
315 for (Const_iterator it = begin(); it != end(); ++it) (*it)->dump();
316 }
317#endif
318};
319
320/**
321 Class used to represent the dictionary tables.
322
323 This class is a singleton used to represent meta data of the dictionary
324 tables, i.e., the tables that store meta data about dictionary entities.
325 The meta data collected here are the Object_table instances, which are
326 used to e.g. get hold of the definition of the table.
327
328 The singleton contains an instance of the Entity_registry class, and
329 has methods that mostly delegate to this instance.
330 */
331
333 public:
334 /*
335 Classification of tables based on WL#6391.
336
337 - An INERT table can never change.
338 - The dd::Table objects representing the CORE tables must be present
339 to handle a cache miss for an arbitrary table.
340 - The dd::Table objects representing the SECOND order tables can be
341 fetched from the dd tables as long as the core table objects are
342 present.
343 - The DDSE tables are not needed by the data dictionary, but the
344 server manages these based on requests from the DD storage engine.
345 - The PFS tables are not needed by the data dictionary, but the
346 server manages these based on requests from the performance schema.
347 */
348 enum class Types {
349 INERT,
350 CORE,
351 SECOND,
354 PFS,
355 SYSTEM
356 };
357
358 // Map from system table type to string description, e.g. for debugging.
359 static const char *type_name(Types type) {
360 switch (type) {
361 case Types::INERT:
362 return "INERT";
363 case Types::CORE:
364 return "CORE";
365 case Types::SECOND:
366 return "SECOND";
368 return "DDSE_PRIVATE";
370 return "DDSE_PROTECTED";
371 case Types::PFS:
372 return "PFS";
373 case Types::SYSTEM:
374 return "SYSTEM";
375 default:
376 return "";
377 }
378 }
379
380 // Map from system table type to error code for localized error messages.
383 return ER_NO_SYSTEM_TABLE_ACCESS_FOR_DICTIONARY_TABLE;
384
387 return ER_NO_SYSTEM_TABLE_ACCESS_FOR_SYSTEM_TABLE;
388
389 assert(false);
390 return ER_NO_SYSTEM_TABLE_ACCESS_FOR_TABLE;
391 }
392
393 private:
394 // The actual registry is referred and delegated to rather than
395 // being inherited from.
397 const Object_table, Types, type_name, true>
400
401 public:
402 // The ordered iterator type must be public.
404
405 static System_tables *instance();
406
407 // Add predefined system tables.
408 void add_inert_dd_tables();
410
411 // Add a new system table by delegation to the wrapped registry.
412 void add(const String_type &schema_name, const String_type &table_name,
413 Types type, const Object_table *table) {
414 m_registry.add(schema_name, table_name, type, table);
415 }
416
417 // Find a system table by delegation to the wrapped registry.
418 const Object_table *find_table(const String_type &schema_name,
419 const String_type &table_name) const {
420 return m_registry.find_entity(schema_name, table_name);
421 }
422
423 // Find a system table type by delegation to the wrapped registry.
424 const Types *find_type(const String_type &schema_name,
425 const String_type &table_name) const {
426 return m_registry.find_property(schema_name, table_name);
427 }
428
429 Const_iterator begin() const { return m_registry.begin(); }
430
432
433 Const_iterator end() const { return m_registry.end(); }
434
436 return m_registry.next(current, type);
437 }
438
439#ifndef NDEBUG
440 void dump() const { m_registry.dump(); }
441#endif
442};
443
444/**
445 Class used to represent the system views.
446
447 This class is a singleton used to represent meta data of the system
448 views, i.e., the views that are available through the information schema.
449
450 @note The registry currently only stores the view names and their
451 (dummy) classification.
452
453 The singleton contains an instance of the Entity_registry class, and
454 has methods that mostly delegate to this instance.
455 */
456
458 public:
459 /*
460 Classification of system views.
461
462 Both of these types represent server INFORMATION_SCHEMA tables. The
463 difference is that NON_DD_BASED_INFORMATION_SCHEMA indicates that the
464 system view is based on ACL tables like role_edges, default_roles etc.
465 NON_DD_BASED_INFORMATION_SCHEMA are created after creation of ACL
466 tables defined in mysql_system_tables.sql and not with regular
467 INFORMATION_SCHEMA tables that are created during bootstrap.
468
469 */
471
472 // Map from system view type to string description, e.g. for debugging.
473 static const char *type_name(Types type) {
474 switch (type) {
476 return "INFORMATION_SCHEMA";
478 return "NON_DD_BASED_INFORMATION_SCHEMA";
479 default:
480 return "";
481 }
482 }
483
484 private:
485 // The actual registry is referred and delegated to rather than
486 // being inherited from.
489 true>
492
493 public:
494 // The ordered iterator type must be public.
496
497 static System_views *instance();
498
499 // Add predefined system views.
500 void init();
501
502 // Add a new system view by delegation to the wrapped registry.
503 void add(const String_type &schema_name, const String_type &view_name,
504 Types type, const system_views::System_view *view) {
505 m_registry.add(schema_name, view_name, type, view);
506 }
507
508 // Find a system view by delegation to the wrapped registry.
509 const system_views::System_view *find(const String_type &schema_name,
510 const String_type &view_name) const {
511 return m_registry.find_entity(schema_name, view_name);
512 }
513
514 Const_iterator begin() const { return m_registry.begin(); }
515
517
518 Const_iterator end() const { return m_registry.end(); }
519
521 return m_registry.next(current, type);
522 }
523
524#ifndef NDEBUG
525 void dump() const { m_registry.dump(); }
526#endif
527};
528
529/**
530 Class used to represent the system tablespaces.
531
532 This class is a singleton used to represent meta data of the system
533 tablespaces, i.e., the tablespaces that are predefined in the DDSE, or
534 needed by the SQL layer.
535
536 The singleton contains an instance of the Entity_registry class, and
537 has methods that mostly delegate to this instance.
538*/
539
541 public:
542 // Classification of system tablespaces.
543 enum class Types {
544 DD, // For storing the DD tables.
545 PREDEFINED_DDSE // Needed by the DDSE.
546 };
547
548 // Map from system tablespace type to string description, e.g. for debugging.
549 static const char *type_name(Types type) {
550 switch (type) {
551 case Types::DD:
552 return "DD";
554 return "PREDEFINED_DDSE";
555 default:
556 return "";
557 }
558 }
559
560 private:
561 // The actual registry is referred and delegated to rather than
562 // being inherited from.
564 const Plugin_tablespace, Types, type_name, false>
567
568 public:
569 // The ordered iterator type must be public.
571
573
574 // Add a new system tablespace by delegation to the wrapped registry.
575 void add(const String_type &tablespace_name, Types type,
576 const Plugin_tablespace *space) {
577 m_registry.add("", tablespace_name, type, space);
578 }
579
580 // Find a system tablespace by delegation to the wrapped registry.
581 const Plugin_tablespace *find(const String_type &tablespace_name) const {
582 return m_registry.find_entity("", tablespace_name);
583 }
584
585 Const_iterator begin() const { return m_registry.begin(); }
586
588
589 Const_iterator end() const { return m_registry.end(); }
590
592 return m_registry.next(current, type);
593 }
594
595#ifndef NDEBUG
596 void dump() const { m_registry.dump(); }
597#endif
598};
599} // namespace dd
600
601#endif // DD__SYSTEM_REGISTRY_INCLUDED
Class to hold information regarding a predefined tablespace created by a storage engine.
Definition: plugin_table.h:129
Class to wrap an entity object.
Definition: system_registry.h:64
Entity_element(const K &key, const T *entity, const P property)
Definition: system_registry.h:71
const P m_property
Property of some kind, like an enumeration.
Definition: system_registry.h:68
const P property() const
Definition: system_registry.h:83
const P * property_ptr() const
Definition: system_registry.h:85
~Entity_element()
Definition: system_registry.h:74
const K m_key
The key associated with the entity object.
Definition: system_registry.h:66
const T * m_entity
Entity object pointer, like an Object_table instance.
Definition: system_registry.h:67
const K & key() const
Definition: system_registry.h:79
const T * entity() const
Definition: system_registry.h:81
void dump() const
Definition: system_registry.h:88
Class to represent collections of meta data for entities.
Definition: system_registry.h:120
Entity_map_type m_entity_map
Map for direct key based lookup.
Definition: system_registry.h:127
std::map< K, Entity_element_type * > Entity_map_type
Definition: system_registry.h:124
const T * find_entity(const String_type &schema_name, const String_type &entity_name) const
Find an element entity in the registry.
Definition: system_registry.h:194
const P * find_property(const String_type &schema_name, const String_type &entity_name) const
Find the property of an element in the registry.
Definition: system_registry.h:223
Const_iterator end() const
Get the end of the vector of ordered inserts.
Definition: system_registry.h:276
std::vector< Entity_element_type * > Entity_list_type
Definition: system_registry.h:123
Entity_list_type m_entity_list
List for ordered access.
Definition: system_registry.h:126
Entity_list_type::const_iterator Const_iterator
Definition: system_registry.h:131
void dump() const
Definition: system_registry.h:313
Const_iterator next(Const_iterator current, P property) const
Get the next element in the list of ordered inserts.
Definition: system_registry.h:303
Entity_element< K, T, P, F, D > Entity_element_type
Definition: system_registry.h:122
Const_iterator begin(P property) const
Get the first element with a certain property.
Definition: system_registry.h:259
Const_iterator next(Const_iterator current) const
Get the next element in the list of ordered inserts.
Definition: system_registry.h:285
void add(const String_type &schema_name, const String_type &entity_name, P property, T *entity)
Add a new entity to the registry.
Definition: system_registry.h:163
Const_iterator begin() const
Get the beginning of the vector of ordered inserts.
Definition: system_registry.h:246
~Entity_registry()
Delete the heap memory owned by the entity registry.
Definition: system_registry.h:142
This class represents all data dictionary table like mysql.tables, mysql.columns and more.
Definition: object_table.h:72
Class used to represent the dictionary tables.
Definition: system_registry.h:332
void dump() const
Definition: system_registry.h:440
Const_iterator begin(Types type) const
Definition: system_registry.h:431
static int type_name_error_code(Types type)
Definition: system_registry.h:381
Entity_registry< std::pair< const String_type, const String_type >, const Object_table, Types, type_name, true > System_table_registry_type
Definition: system_registry.h:398
System_table_registry_type m_registry
Definition: system_registry.h:399
const Object_table * find_table(const String_type &schema_name, const String_type &table_name) const
Definition: system_registry.h:418
static System_tables * instance()
Definition: system_registry.cc:132
static const char * type_name(Types type)
Definition: system_registry.h:359
Const_iterator next(Const_iterator current, Types type) const
Definition: system_registry.h:435
System_table_registry_type::Const_iterator Const_iterator
Definition: system_registry.h:403
void add_remaining_dd_tables()
Definition: system_registry.cc:165
Const_iterator end() const
Definition: system_registry.h:433
const Types * find_type(const String_type &schema_name, const String_type &table_name) const
Definition: system_registry.h:424
void add(const String_type &schema_name, const String_type &table_name, Types type, const Object_table *table)
Definition: system_registry.h:412
void add_inert_dd_tables()
Definition: system_registry.cc:151
Const_iterator begin() const
Definition: system_registry.h:429
Types
Definition: system_registry.h:348
Class used to represent the system tablespaces.
Definition: system_registry.h:540
static const char * type_name(Types type)
Definition: system_registry.h:549
const Plugin_tablespace * find(const String_type &tablespace_name) const
Definition: system_registry.h:581
Types
Definition: system_registry.h:543
void add(const String_type &tablespace_name, Types type, const Plugin_tablespace *space)
Definition: system_registry.h:575
Const_iterator begin(Types type) const
Definition: system_registry.h:587
void dump() const
Definition: system_registry.h:596
Const_iterator begin() const
Definition: system_registry.h:585
System_tablespace_registry_type::Const_iterator Const_iterator
Definition: system_registry.h:570
Const_iterator next(Const_iterator current, Types type) const
Definition: system_registry.h:591
Const_iterator end() const
Definition: system_registry.h:589
System_tablespace_registry_type m_registry
Definition: system_registry.h:566
static System_tablespaces * instance()
Definition: system_registry.cc:142
Entity_registry< std::pair< const String_type, const String_type >, const Plugin_tablespace, Types, type_name, false > System_tablespace_registry_type
Definition: system_registry.h:565
Class used to represent the system views.
Definition: system_registry.h:457
Types
Definition: system_registry.h:470
Const_iterator begin() const
Definition: system_registry.h:514
void dump() const
Definition: system_registry.h:525
System_view_registry_type m_registry
Definition: system_registry.h:491
Const_iterator end() const
Definition: system_registry.h:518
Const_iterator begin(Types type) const
Definition: system_registry.h:516
Entity_registry< std::pair< const String_type, const String_type >, const system_views::System_view, Types, type_name, true > System_view_registry_type
Definition: system_registry.h:490
Const_iterator next(Const_iterator current, Types type) const
Definition: system_registry.h:520
static const char * type_name(Types type)
Definition: system_registry.h:473
void add(const String_type &schema_name, const String_type &view_name, Types type, const system_views::System_view *view)
Definition: system_registry.h:503
static System_views * instance()
Definition: system_registry.cc:137
void init()
Definition: system_registry.cc:260
const system_views::System_view * find(const String_type &schema_name, const String_type &view_name) const
Definition: system_registry.h:509
System_view_registry_type::Const_iterator Const_iterator
Definition: system_registry.h:495
This class represents base class for all INFORMATION_SCHEMA system views defined in sql/dd/impl/syste...
Definition: system_view.h:39
#define P
Definition: dtoa.cc:619
uint16_t value_type
Definition: vt100.h:184
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:51
const char * table_name
Definition: rules_table_service.cc:56
required string key
Definition: replication_asynchronous_connection_failover.proto:60
required string type
Definition: replication_group_member_actions.proto:34