MySQL 8.1.0
Source Code Documentation
weak_object_impl.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 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__WEAK_OBJECT_IMPL_INCLUDED
24#define DD__WEAK_OBJECT_IMPL_INCLUDED
25
26#include "my_sys.h" // MY_WME
27#include "mysql/service_mysql_alloc.h" // my_malloc
28#include "sql/dd/object_id.h" // Object_id
29#include "sql/dd/types/weak_object.h" // dd::Weak_object
30#include "sql/psi_memory_key.h" // key_memory_DD_objects
31
32namespace dd {
33
34///////////////////////////////////////////////////////////////////////////
35
36class Entity_object;
37class Entity_object_impl;
38class Object_key;
39class Object_table;
40class Open_dictionary_tables_ctx;
41class Raw_new_record;
42class Raw_record;
43
44///////////////////////////////////////////////////////////////////////////
45
46template <bool use_pfs>
47class Weak_object_impl_ : virtual public Weak_object {
48 public:
49 Weak_object_impl_() = default;
50
51 ~Weak_object_impl_() override = default;
52
53 void *operator new(size_t size, const std::nothrow_t &nt) noexcept {
54 /*
55 Call my_malloc() with the MY_WME flag to make sure that it will
56 write an error message if the memory could not be allocated.
57 */
58 if (use_pfs) return my_malloc(key_memory_DD_objects, size, MYF(MY_WME));
59 return ::operator new(size, nt);
60 }
61
62 void operator delete(void *ptr, const std::nothrow_t &nt) noexcept {
63 if (use_pfs)
64 my_free(ptr);
65 else
66 ::operator delete(ptr, nt);
67 }
68
69 void *operator new(size_t size) noexcept {
70 /*
71 Call my_malloc() with the MY_WME flag to make sure that it will
72 write an error message if the memory could not be allocated.
73 */
74 if (use_pfs) return my_malloc(key_memory_DD_objects, size, MYF(MY_WME));
75 return ::operator new(size);
76 }
77
78 void operator delete(void *ptr) noexcept {
79 if (use_pfs)
80 my_free(ptr);
81 else
82 ::operator delete(ptr);
83 }
84
85 public:
86 virtual const Object_table &object_table() const = 0;
87
88 virtual bool validate() const = 0;
89
90 // NOTE: the store() operation can not be made constant as
91 // the object state is modified when storing a newly created object
92 // (object id is assigned using auto-increment).
93 virtual bool store(Open_dictionary_tables_ctx *otx);
94
95 bool drop(Open_dictionary_tables_ctx *otx) const;
96
97 public:
98 virtual bool restore_attributes(const Raw_record &r) = 0;
99
100 virtual bool store_attributes(Raw_record *r) = 0;
101
102 public:
103 // Restore's all the related collections.
104 // There are 2 scenarios when a collection is filled.
105 // 1) Parent object is retrieved using restore()
106 // and then restore collections.
107 // Eg: Tablespace (Parent object) invoked restore()
108 // and then call restore_children() to fetch
109 // Tablespace_file objects.
110 //
111 // 2) Parent object is fetched using Raw_record_set->next()
112 // and then restore collections is called for each
113 // parent object fetched.
114 // Eg. Indexes (Parent object) that belong to a Table object
115 // is fetched and then Index_element collections per
116 // index is restored using restore_children().
117 //
118 virtual bool restore_children(Open_dictionary_tables_ctx *) { return false; }
119
120 virtual bool store_children(Open_dictionary_tables_ctx *) { return false; }
121
123 return false;
124 }
125
126 /**
127 Indicates that object is guaranteed to have primary key value which
128 doesn't exist in database (e.g. because it only will be generated
129 using auto-increment at store() time). So it is ok for store() method
130 to skip lookup of existing object with the same primary key and simply
131 try to insert new object into the table.
132 */
133 virtual bool has_new_primary_key() const = 0;
134
135 protected:
136 virtual Object_key *create_primary_key() const = 0;
137
138 // set_primary_key_value() is called after new object has been inserted into
139 // the table, giving the chance to get inserted values of AUTO_INCREMENT
140 // columns. It gives a chance for Entity_object to override it.
141 virtual void set_primary_key_value(const Raw_new_record &) {}
142
143 /*
144 Called by store() method to allow resetting of has_new_primary_key()
145 property after we completed loading of object and its children.
146 */
147 virtual void fix_has_new_primary_key() {}
148
149 protected:
150 // Check if the parent object id matches with this object.
152 Object_id parent_id) const;
153};
154
155/*
156 Provide a type alias to be used elsewhere in the server source code.
157 The template instance without PFS instrumentation is only used in
158 unit tests to compare allocation performance.
159*/
161
162///////////////////////////////////////////////////////////////////////////
163
164} // namespace dd
165
166#endif // DD__WEAK_OBJECT_IMPL_INCLUDED
Definition: entity_object_impl.h:43
Definition: object_key.h:37
This class represents all data dictionary table like mysql.tables, mysql.columns and more.
Definition: object_table.h:71
Auxiliary class for opening dictionary tables.
Definition: transaction_impl.h:75
Definition: raw_record.h:140
Definition: raw_record.h:45
Definition: weak_object_impl.h:47
virtual void fix_has_new_primary_key()
Definition: weak_object_impl.h:147
virtual void set_primary_key_value(const Raw_new_record &)
Definition: weak_object_impl.h:141
virtual bool restore_children(Open_dictionary_tables_ctx *)
Definition: weak_object_impl.h:118
~Weak_object_impl_() override=default
bool check_parent_consistency(Entity_object_impl *parent, Object_id parent_id) const
Definition: weak_object_impl.cc:224
virtual bool restore_attributes(const Raw_record &r)=0
virtual bool store_children(Open_dictionary_tables_ctx *)
Definition: weak_object_impl.h:120
Weak_object_impl_()=default
virtual const Object_table & object_table() const =0
virtual bool has_new_primary_key() const =0
Indicates that object is guaranteed to have primary key value which doesn't exist in database (e....
virtual bool validate() const =0
virtual bool store_attributes(Raw_record *r)=0
virtual Object_key * create_primary_key() const =0
virtual bool store(Open_dictionary_tables_ctx *otx)
Store the DD object into DD table.
Definition: weak_object_impl.cc:58
virtual bool drop_children(Open_dictionary_tables_ctx *) const
Definition: weak_object_impl.h:122
bool drop(Open_dictionary_tables_ctx *otx) const
Drop the DD object from DD table.
Definition: weak_object_impl.cc:177
Base class for all data dictionary objects.
Definition: weak_object.h:41
#define MY_WME
Definition: my_sys.h:125
#define MYF(v)
Definition: my_inttypes.h:96
void * my_malloc(PSI_memory_key key, size_t size, int flags)
Allocates size bytes of memory.
Definition: my_memory.cc:56
void my_free(void *ptr)
Frees the memory pointed by the ptr.
Definition: my_memory.cc:80
Common header for many mysys elements.
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:42
unsigned long long Object_id
Definition: object_id.h:30
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:85
PSI_memory_key key_memory_DD_objects
Definition: psi_memory_key.cc:40