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