forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_registry.h
601 lines (470 loc) · 19.8 KB
/
system_registry.h
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/* Copyright (c) 2014, 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 */
#ifndef DD__SYSTEM_REGISTRY_INCLUDED
#define DD__SYSTEM_REGISTRY_INCLUDED
#include <assert.h>
#include <stdio.h>
#include <map>
#include <new>
#include <string>
#include <utility>
#include <vector>
#include "mysqld_error.h" // ER_NO_SYSTEM_TABLE_...
#include "sql/dd/string_type.h" // dd::String_type
#include "sql/dd/types/object_table.h"
#include "sql/dd/types/system_view.h"
#include "sql/plugin_table.h"
namespace dd {
class Object_table;
/**
Class to wrap an entity object.
The Entity_element instances are intended used in registries to support
classification of meta data. The Entity_element template points to an
instance of some type, and also has a property member that is associated
with the entity, as well as a key with which the object is associated.
@note The entity object ownership is decided by the relevant template
parameter, see below.
@tparam K A key type, e.g. a pair of strings.
@tparam T An entity type, e.g. an Object_table instance.
@tparam P A property type, e.g. an enumeration.
@tparam F Function to map from property to name.
@tparam D Boolean to decide ownership of wrapped object.
*/
template <typename K, typename T, typename P, const char *F(P), bool D>
class Entity_element {
private:
const K m_key; //!< The key associated with the entity object.
const T *m_entity; //!< Entity object pointer, like an Object_table instance.
const P m_property; //!< Property of some kind, like an enumeration.
public:
Entity_element(const K &key, const T *entity, const P property)
: m_key(key), m_entity(entity), m_property(property) {}
~Entity_element() {
// Delete the wrapped object depending on the template parameter.
if (D) delete m_entity;
}
const K &key() const { return m_key; }
const T *entity() const { return m_entity; }
const P property() const { return m_property; }
const P *property_ptr() const { return &m_property; }
#ifndef NDEBUG
void dump() const {
fprintf(stderr, "Key= '%s.%s', property= '%s'\n", m_key.first.c_str(),
m_key.second.c_str(), F(m_property));
}
#endif
};
/**
Class to represent collections of meta data for entities.
This class is used to represent relevant meta data and properties of
system related entities, e.g. all the dictionary tables. The meta data
is associated with a key instance, usually a std::pair containing the
schema- and entity names.
The class supports adding and retrieving objects, as well as iteration
based on the order of inserts.
@note There is no support for concurrency. We assume that the registry
is created and that the entities are inserted in a single threaded
situation, e.g. while the server is being started. Read only access
can happen from several threads simultaneously, though.
@tparam K Key type.
@tparam T Entity type.
@tparam P Property type.
@tparam F Function to map from property to name.
@tparam D Boolean flag to decide whether the wrapped objects
are owned by the registry.
*/
template <typename K, typename T, typename P, const char *F(P), bool D>
class Entity_registry {
private:
typedef Entity_element<K, T, P, F, D> Entity_element_type;
typedef std::vector<Entity_element_type *> Entity_list_type;
typedef std::map<K, Entity_element_type *> Entity_map_type;
Entity_list_type m_entity_list; //!< List for ordered access.
Entity_map_type m_entity_map; //!< Map for direct key based lookup.
public:
// Externally available iteration is based on the order of inserts.
typedef typename Entity_list_type::const_iterator Const_iterator;
/**
Delete the heap memory owned by the entity registry.
Only the wrapper elements are owned by the registry.
The std::vector and std::map are not allocated
dynamically, and their destructors are called implicitly
when this destructor is called.
*/
~Entity_registry() {
// Delete elements from the map. The objects that are wrapped
// will be handled by the wrapper destructor.
for (typename Entity_map_type::iterator it = m_entity_map.begin();
it != m_entity_map.end(); ++it)
delete it->second;
}
/**
Add a new entity to the registry.
This method will create a new key and a new entity wrapper element. The
wrapper will be added to the map, along with the key, and to the vector
to keep track of the order of inserts.
@param schema_name Schema name used to construct the key.
@param entity_name Entity name used to construct the key.
@param property Property used to classify the entity.
@param entity Entity which is classified and registered.
*/
void add(const String_type &schema_name, const String_type &entity_name,
P property, T *entity) {
// Create a new key and make sure it does not already exist.
K key(schema_name, entity_name);
assert(m_entity_map.find(key) == m_entity_map.end());
// Create a new entity wrapper element. The wrapper will be owned by the
// entity map and deleted along with it.
Entity_element_type *element =
new (std::nothrow) Entity_element_type(key, entity, property);
// Add the key, value pair to the map.
m_entity_map.insert(typename Entity_map_type::value_type(key, element));
// Add the entity to the ordered list too.
m_entity_list.push_back(element);
}
/**
Find an element entity in the registry.
This method creates a key based on the submitted parameters, and
looks up in the member map. If the key is found, the object pointed
to by the wrapper is returned, otherwise, nullptr is returned.
@param schema_name Schema containing the entity searched for.
@param entity_name Name of the entity searched for.
@retval nullptr if not found, otherwise, the entity
object pointed to by the wrapper element.
*/
const T *find_entity(const String_type &schema_name,
const String_type &entity_name) const {
// Create a new key. This is only used for lookup, so it is allocated
// on the stack.
K key(schema_name, entity_name);
// Lookup in the map based on the key.
typename Entity_map_type::const_iterator element_it =
m_entity_map.find(key);
// Return nullptr if not found, otherwise, return a pointer to the entity.
if (element_it == m_entity_map.end()) return nullptr;
return element_it->second->entity();
}
/**
Find the property of an element in the registry.
This method creates a key based on the submitted parameters, and
looks up in the member map. If the key is found, the property
associated with the key is returned, otherwise, nullptr is returned.
@param schema_name Schema containing the entity searched for.
@param entity_name Name of the entity searched for.
@retval nullptr if not found, otherwise, the a pointer to
the property associated with the key.
*/
const P *find_property(const String_type &schema_name,
const String_type &entity_name) const {
// Create a new key. This is only used for lookup, so it is allocated
// on the stack.
K key(schema_name, entity_name);
// Lookup in the map based on the key.
typename Entity_map_type::const_iterator element_it =
m_entity_map.find(key);
// Return nullptr if not found, otherwise, return a pointer to the property.
if (element_it == m_entity_map.end()) return nullptr;
return element_it->second->property_ptr();
}
/**
Get the beginning of the vector of ordered inserts.
@retval Iterator referring to the first element in the vector,
or end() if the vector is empty.
*/
Const_iterator begin() const { return m_entity_list.begin(); }
/**
Get the first element with a certain property.
This method retrieves the first element with a certain property, and
is used for iterating only over elements having this property.
@param property Property that the element should have.
@retval Iterator referring to the first element in the vector
with the submitted property, or end().
*/
Const_iterator begin(P property) const {
Const_iterator it = begin();
while (it != end()) {
if ((*it)->property() == property) break;
++it;
}
return it;
}
/**
Get the end of the vector of ordered inserts.
@retval Iterator referring to the special element after the last "real"
element in the vector.
*/
Const_iterator end() const { return m_entity_list.end(); }
/**
Get the next element in the list of ordered inserts.
@param current The current iterator.
@retval Iterator referring to the next element in the vector.
*/
Const_iterator next(Const_iterator current) const {
if (current == end()) return current;
return ++current;
}
/**
Get the next element in the list of ordered inserts.
This method retrieves the next element with a certain property, and
is used for iterating only over elements having this property.
@param current The current iterator.
@param property Property that the next element should have.
@retval Iterator referring to the next element in the vector
with the submitted property.
*/
Const_iterator next(Const_iterator current, P property) const {
if (current == end()) return current;
while (++current != end())
if ((*current)->property() == property) break;
return current;
}
#ifndef NDEBUG
void dump() const {
// List the entities in the order they were inserted.
for (Const_iterator it = begin(); it != end(); ++it) (*it)->dump();
}
#endif
};
/**
Class used to represent the dictionary tables.
This class is a singleton used to represent meta data of the dictionary
tables, i.e., the tables that store meta data about dictionary entities.
The meta data collected here are the Object_table instances, which are
used to e.g. get hold of the definition of the table.
The singleton contains an instance of the Entity_registry class, and
has methods that mostly delegate to this instance.
*/
class System_tables {
public:
/*
Classification of tables based on WL#6391.
- An INERT table can never change.
- The dd::Table objects representing the CORE tables must be present
to handle a cache miss for an arbitrary table.
- The dd::Table objects representing the SECOND order tables can be
fetched from the dd tables as long as the core table objects are
present.
- The DDSE tables are not needed by the data dictionary, but the
server manages these based on requests from the DD storage engine.
- The PFS tables are not needed by the data dictionary, but the
server manages these based on requests from the performance schema.
*/
enum class Types {
INERT,
CORE,
SECOND,
DDSE_PRIVATE,
DDSE_PROTECTED,
PFS,
SYSTEM
};
// Map from system table type to string description, e.g. for debugging.
static const char *type_name(Types type) {
switch (type) {
case Types::INERT:
return "INERT";
case Types::CORE:
return "CORE";
case Types::SECOND:
return "SECOND";
case Types::DDSE_PRIVATE:
return "DDSE_PRIVATE";
case Types::DDSE_PROTECTED:
return "DDSE_PROTECTED";
case Types::PFS:
return "PFS";
case Types::SYSTEM:
return "SYSTEM";
default:
return "";
}
}
// Map from system table type to error code for localized error messages.
static int type_name_error_code(Types type) {
if (type == Types::INERT || type == Types::CORE || type == Types::SECOND)
return ER_NO_SYSTEM_TABLE_ACCESS_FOR_DICTIONARY_TABLE;
if (type == Types::DDSE_PRIVATE || type == Types::DDSE_PROTECTED ||
type == Types::PFS || type == Types::SYSTEM)
return ER_NO_SYSTEM_TABLE_ACCESS_FOR_SYSTEM_TABLE;
assert(false);
return ER_NO_SYSTEM_TABLE_ACCESS_FOR_TABLE;
}
private:
// The actual registry is referred and delegated to rather than
// being inherited from.
typedef Entity_registry<std::pair<const String_type, const String_type>,
const Object_table, Types, type_name, true>
System_table_registry_type;
System_table_registry_type m_registry;
public:
// The ordered iterator type must be public.
typedef System_table_registry_type::Const_iterator Const_iterator;
static System_tables *instance();
// Add predefined system tables.
void add_inert_dd_tables();
void add_remaining_dd_tables();
// Add a new system table by delegation to the wrapped registry.
void add(const String_type &schema_name, const String_type &table_name,
Types type, const Object_table *table) {
m_registry.add(schema_name, table_name, type, table);
}
// Find a system table by delegation to the wrapped registry.
const Object_table *find_table(const String_type &schema_name,
const String_type &table_name) const {
return m_registry.find_entity(schema_name, table_name);
}
// Find a system table type by delegation to the wrapped registry.
const Types *find_type(const String_type &schema_name,
const String_type &table_name) const {
return m_registry.find_property(schema_name, table_name);
}
Const_iterator begin() const { return m_registry.begin(); }
Const_iterator begin(Types type) const { return m_registry.begin(type); }
Const_iterator end() const { return m_registry.end(); }
Const_iterator next(Const_iterator current, Types type) const {
return m_registry.next(current, type);
}
#ifndef NDEBUG
void dump() const { m_registry.dump(); }
#endif
};
/**
Class used to represent the system views.
This class is a singleton used to represent meta data of the system
views, i.e., the views that are available through the information schema.
@note The registry currently only stores the view names and their
(dummy) classification.
The singleton contains an instance of the Entity_registry class, and
has methods that mostly delegate to this instance.
*/
class System_views {
public:
/*
Classification of system views.
Both of these types represent server INFORMATION_SCHEMA tables. The
difference is that NON_DD_BASED_INFORMATION_SCHEMA indicates that the
system view is based on ACL tables like role_edges, default_roles etc.
NON_DD_BASED_INFORMATION_SCHEMA are created after creation of ACL
tables defined in mysql_system_tables.sql and not with regular
INFORMATION_SCHEMA tables that are created during bootstrap.
*/
enum class Types { INFORMATION_SCHEMA, NON_DD_BASED_INFORMATION_SCHEMA };
// Map from system view type to string description, e.g. for debugging.
static const char *type_name(Types type) {
switch (type) {
case Types::INFORMATION_SCHEMA:
return "INFORMATION_SCHEMA";
case Types::NON_DD_BASED_INFORMATION_SCHEMA:
return "NON_DD_BASED_INFORMATION_SCHEMA";
default:
return "";
}
}
private:
// The actual registry is referred and delegated to rather than
// being inherited from.
typedef Entity_registry<std::pair<const String_type, const String_type>,
const system_views::System_view, Types, type_name,
true>
System_view_registry_type;
System_view_registry_type m_registry;
public:
// The ordered iterator type must be public.
typedef System_view_registry_type::Const_iterator Const_iterator;
static System_views *instance();
// Add predefined system views.
void init();
// Add a new system view by delegation to the wrapped registry.
void add(const String_type &schema_name, const String_type &view_name,
Types type, const system_views::System_view *view) {
m_registry.add(schema_name, view_name, type, view);
}
// Find a system view by delegation to the wrapped registry.
const system_views::System_view *find(const String_type &schema_name,
const String_type &view_name) const {
return m_registry.find_entity(schema_name, view_name);
}
Const_iterator begin() const { return m_registry.begin(); }
Const_iterator begin(Types type) const { return m_registry.begin(type); }
Const_iterator end() const { return m_registry.end(); }
Const_iterator next(Const_iterator current, Types type) const {
return m_registry.next(current, type);
}
#ifndef NDEBUG
void dump() const { m_registry.dump(); }
#endif
};
/**
Class used to represent the system tablespaces.
This class is a singleton used to represent meta data of the system
tablespaces, i.e., the tablespaces that are predefined in the DDSE, or
needed by the SQL layer.
The singleton contains an instance of the Entity_registry class, and
has methods that mostly delegate to this instance.
*/
class System_tablespaces {
public:
// Classification of system tablespaces.
enum class Types {
DD, // For storing the DD tables.
PREDEFINED_DDSE // Needed by the DDSE.
};
// Map from system tablespace type to string description, e.g. for debugging.
static const char *type_name(Types type) {
switch (type) {
case Types::DD:
return "DD";
case Types::PREDEFINED_DDSE:
return "PREDEFINED_DDSE";
default:
return "";
}
}
private:
// The actual registry is referred and delegated to rather than
// being inherited from.
typedef Entity_registry<std::pair<const String_type, const String_type>,
const Plugin_tablespace, Types, type_name, false>
System_tablespace_registry_type;
System_tablespace_registry_type m_registry;
public:
// The ordered iterator type must be public.
typedef System_tablespace_registry_type::Const_iterator Const_iterator;
static System_tablespaces *instance();
// Add a new system tablespace by delegation to the wrapped registry.
void add(const String_type &tablespace_name, Types type,
const Plugin_tablespace *space) {
m_registry.add("", tablespace_name, type, space);
}
// Find a system tablespace by delegation to the wrapped registry.
const Plugin_tablespace *find(const String_type &tablespace_name) const {
return m_registry.find_entity("", tablespace_name);
}
Const_iterator begin() const { return m_registry.begin(); }
Const_iterator begin(Types type) const { return m_registry.begin(type); }
Const_iterator end() const { return m_registry.end(); }
Const_iterator next(Const_iterator current, Types type) const {
return m_registry.next(current, type);
}
#ifndef NDEBUG
void dump() const { m_registry.dump(); }
#endif
};
} // namespace dd
#endif // DD__SYSTEM_REGISTRY_INCLUDED