MySQL 8.4.0
Source Code Documentation
collection.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__COLLECTION_IMPL_INCLUDED
25#define DD__COLLECTION_IMPL_INCLUDED
26
27#include <assert.h>
28#include <stddef.h>
29#include <sys/types.h>
30#include <algorithm>
31#include <iterator>
32#include <type_traits>
33#include <vector>
34
35namespace dd {
36
37///////////////////////////////////////////////////////////////////////////
38
39class Object_key;
40class Open_dictionary_tables_ctx;
41class Raw_table;
42
43template <typename T>
45 public:
46 // Pointer to abstract type. Template argument.
47 typedef T value_type;
48 // Abstract type.
50 // Implementation type. Pointer to this type is actually stored in the vector.
51 typedef typename abstract_type::Impl impl_type;
52 typedef std::vector<impl_type *> Array;
53
54 private:
57
58 void clear_all_items();
59
61 for (size_t i = 0; i < m_items.size(); ++i)
62 m_items[i]->set_ordinal_position(static_cast<unsigned int>(i + 1));
63 }
64
66 public:
67 using iterator_category = std::forward_iterator_tag;
68 using value_type = T;
69 using difference_type = std::ptrdiff_t;
72
74 : m_array(array), m_current(array->begin()), m_current_obj(nullptr) {}
75
76 Collection_iterator(Array *array, typename Array::iterator it)
77 : m_array(array), m_current(it), m_current_obj(nullptr) {}
78
79 bool operator==(const Collection_iterator &iter) const {
80 return (m_array == iter.m_array && m_current == iter.m_current);
81 }
82
83 bool operator!=(const Collection_iterator &iter) const {
84 return (m_array != iter.m_array || m_current != iter.m_current);
85 }
86
88 if (m_current != m_array->end()) ++m_current;
89 return *this;
90 }
91
92 T &operator*();
93
95 m_current = m_array->end();
96 return *this;
97 }
98
99 typename Array::iterator current() { return m_current; }
100
101 private:
103 typename Array::iterator m_current;
105 };
106
108 public:
109 using iterator_category = std::forward_iterator_tag;
110 using value_type = const abstract_type *;
111 using difference_type = std::ptrdiff_t;
114
116 : m_array(array), m_current(array->begin()), m_current_obj(nullptr) {}
117
119 typename Array::const_iterator it)
120 : m_array(array), m_current(it), m_current_obj(nullptr) {}
121
122 bool operator==(const Collection_const_iterator &iter) const {
123 return (m_array == iter.m_array && m_current == iter.m_current);
124 }
125
126 bool operator!=(const Collection_const_iterator &iter) const {
127 return (m_array != iter.m_array || m_current != iter.m_current);
128 }
129
131 if (m_current != m_array->end()) ++m_current;
132 return *this;
133 }
134
135 const abstract_type *&operator*();
136
138 m_current = m_array->end();
139 return *this;
140 }
141
142 typename Array::const_iterator current() const { return m_current; }
143
144 private:
146 typename Array::const_iterator m_current;
148 };
149
150 public:
151 Collection() = default;
152
154
155 /**
156 Remove elements from m_removed_items. This is used only in case of
157 dropping triggers for now. See comments in
158 Table_impl::store_children() for more details.
159 */
160 void clear_removed_items();
161
162 Collection(const Collection &) = delete;
163 void operator=(Collection &) = delete;
164
167
168 void push_back(impl_type *item) {
169 item->set_ordinal_position(static_cast<unsigned int>(m_items.size() + 1));
170 m_items.push_back(item);
171 }
172
173 void push_front(impl_type *item) {
174 m_items.insert(m_items.begin(), item);
176 }
177
178 void insert(iterator it, impl_type *item) {
179 m_items.insert(it.current(), item);
181 }
182
183 void remove(impl_type *item);
184
185 /**
186 Remove all items and move it to m_removed_items items.
187 */
188
189 void remove_all() { m_removed_items = std::move(m_items); }
190
191 /**
192 Find item and return the position.
193
194 @returns iterator pointing to found element.
195 */
196
197 iterator find(const impl_type *item);
198
200
202
204 iterator it(&m_items);
205 it.end();
206 return it;
207 }
208
211 it.end();
212 return it;
213 }
214
215 const_iterator cbegin() const { return begin(); }
216
217 const_iterator cend() const { return end(); }
218
219 bool empty() const { return m_items.empty() && m_removed_items.empty(); }
220
221 /**
222 Check if some of collection elements are removed.
223
224 @returns void.
225 */
226
227 bool has_removed_items() const { return !m_removed_items.empty(); }
228
229 size_t size() const { return m_items.size(); }
230
231 const abstract_type *at(size_t n) const;
232
233 T at(size_t n) {
234 assert(n < size());
235 return m_items[n];
236 }
237
238 const abstract_type *front() const { return at(0); }
239 T front() { return at(0); }
240
241 const abstract_type *back() const { return at(size() - 1); }
242 T back() { return at(size() - 1); }
243
244 const abstract_type *operator[](size_t n) const { return at(n); }
245 T operator[](size_t n) { return at(n); }
246
247 /**
248 @brief
249 Populate collection with items read from DD table.
250
251 @details
252 Iterate through DD tables to find rows that match the 'Object_key'
253 supplied. Create collection item for each row we find and populate
254 the item with data read from DD. Sort items in collection by their
255 ordinal position property.
256
257 @param parent - Object owning the restored object.
258 @param otx - Context with information about open tables.
259 @param table - The DD table from which read rows for items.
260 @param key - The search key to be used to find rows.
261
262 @return true - on failure and error is reported.
263 @return false - on success.
264 */
265 template <typename Parent_item>
266 bool restore_items(Parent_item *parent, Open_dictionary_tables_ctx *otx,
268
269 /**
270 Populate collection with items read from DD table.
271
272 @details
273 Iterate through DD tables to find rows that match the 'Object_key'
274 supplied. Create collection item for each row we find and populate
275 the item with data read from DD. Sort items in collection using
276 comparator provided.
277
278 @param parent Object owning the restored object.
279 @param otx Context with information about open tables.
280 @param table The DD table from which read rows for items.
281 @param key The search key to be used to find rows.
282 @param comp Comparator to be used for sorting items.
283
284 @retval True on failure and error is reported.
285 @retval False on success.
286 */
287 template <typename Parent_item, typename Compare>
288 bool restore_items(Parent_item *parent, Open_dictionary_tables_ctx *otx,
289 Raw_table *table, Object_key *key, Compare comp);
290
291 /**
292 @brief
293 store items in collection on to DD table.
294
295 @details
296 Iterate through collection and stores them in DD tables.
297
298 @param otx - Context with information about open tables.
299
300 @return true - on failure and error is reported.
301 @return false - on success.
302 */
304
305 /**
306 @brief
307 Remove all items details from DD table.
308
309 @details
310 Iterate through the collection and remove respective rows
311 from DD tables.
312
313 @param otx - Context with information about open tables.
314 @param table - The DD table from which rows are removed.
315 @param key - The search key to use to find rows.
316
317 @return true - on failure and error is reported.
318 @return false - on success.
319 */
321 Object_key *key) const;
322
323 /**
324 @brief
325 Do a deep copy of a given collection.
326
327 @details
328 Calls clone() on the items in the given collection and
329 stores the result in this collection.
330
331 @param src - Collection to do a deep copy of.
332 @param parent - Object "owning" the items in the collection.
333 E.g. Columns are owned by Table.
334 */
335 template <typename Parent_item>
336 void deep_copy(const Collection<T> &src, Parent_item *parent);
337};
338
339///////////////////////////////////////////////////////////////////////////
340
341} // namespace dd
342
343#endif // DD__COLLECTION_PARENT_INCLUDED
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Definition: collection.h:107
std::forward_iterator_tag iterator_category
Definition: collection.h:109
Collection_const_iterator & end()
Definition: collection.h:137
std::ptrdiff_t difference_type
Definition: collection.h:111
Collection_const_iterator(const Array *array)
Definition: collection.h:115
Collection_const_iterator(const Array *array, typename Array::const_iterator it)
Definition: collection.h:118
Array::const_iterator m_current
Definition: collection.h:146
const abstract_type * m_current_obj
Definition: collection.h:147
bool operator!=(const Collection_const_iterator &iter) const
Definition: collection.h:126
bool operator==(const Collection_const_iterator &iter) const
Definition: collection.h:122
const abstract_type *& operator*()
Definition: collection.cc:71
const Array * m_array
Definition: collection.h:145
const abstract_type * value_type
Definition: collection.h:110
value_type & reference
Definition: collection.h:113
value_type * pointer
Definition: collection.h:112
Collection_const_iterator & operator++()
Definition: collection.h:130
Array::const_iterator current() const
Definition: collection.h:142
Definition: collection.h:65
Collection_iterator & operator++()
Definition: collection.h:87
bool operator!=(const Collection_iterator &iter) const
Definition: collection.h:83
T & operator*()
Definition: collection.cc:60
Collection_iterator(Array *array, typename Array::iterator it)
Definition: collection.h:76
bool operator==(const Collection_iterator &iter) const
Definition: collection.h:79
Collection_iterator(Array *array)
Definition: collection.h:73
Array * m_array
Definition: collection.h:102
std::ptrdiff_t difference_type
Definition: collection.h:69
Array::iterator current()
Definition: collection.h:99
T value_type
Definition: collection.h:68
std::forward_iterator_tag iterator_category
Definition: collection.h:67
T m_current_obj
Definition: collection.h:104
Array::iterator m_current
Definition: collection.h:103
value_type * pointer
Definition: collection.h:70
Collection_iterator & end()
Definition: collection.h:94
value_type & reference
Definition: collection.h:71
Definition: collection.h:44
const abstract_type * front() const
Definition: collection.h:238
iterator end()
Definition: collection.h:203
bool drop_items(Open_dictionary_tables_ctx *otx, Raw_table *table, Object_key *key) const
Remove all items details from DD table.
Definition: collection.cc:226
void clear_removed_items()
Remove elements from m_removed_items.
Definition: collection.cc:87
void remove_all()
Remove all items and move it to m_removed_items items.
Definition: collection.h:189
const_iterator cbegin() const
Definition: collection.h:215
const_iterator begin() const
Definition: collection.h:201
Collection_const_iterator const_iterator
Definition: collection.h:166
void remove(impl_type *item)
const abstract_type * operator[](size_t n) const
Definition: collection.h:244
const_iterator cend() const
Definition: collection.h:217
const abstract_type * back() const
Definition: collection.h:241
T at(size_t n)
Definition: collection.h:233
void deep_copy(const Collection< T > &src, Parent_item *parent)
Do a deep copy of a given collection.
Definition: collection.cc:265
iterator begin()
Definition: collection.h:199
bool has_removed_items() const
Check if some of collection elements are removed.
Definition: collection.h:227
std::vector< impl_type * > Array
Definition: collection.h:52
Array m_items
Definition: collection.h:55
void push_front(impl_type *item)
Definition: collection.h:173
void insert(iterator it, impl_type *item)
Definition: collection.h:178
Collection_iterator iterator
Definition: collection.h:165
Collection()=default
T front()
Definition: collection.h:239
iterator find(const impl_type *item)
Find item and return the position.
const abstract_type * at(size_t n) const
Definition: collection.cc:258
Array m_removed_items
Definition: collection.h:56
void clear_all_items()
Definition: collection.cc:81
std::remove_pointer< T >::type abstract_type
Definition: collection.h:49
bool restore_items(Parent_item *parent, Open_dictionary_tables_ctx *otx, Raw_table *table, Object_key *key)
Populate collection with items read from DD table.
Definition: collection.cc:195
void push_back(impl_type *item)
Definition: collection.h:168
abstract_type::Impl impl_type
Definition: collection.h:51
const_iterator end() const
Definition: collection.h:209
T operator[](size_t n)
Definition: collection.h:245
bool empty() const
Definition: collection.h:219
void operator=(Collection &)=delete
size_t size() const
Definition: collection.h:229
T value_type
Definition: collection.h:47
void renumerate_items()
Definition: collection.h:60
bool store_items(Open_dictionary_tables_ctx *otx)
store items in collection on to DD table.
Definition: collection.cc:203
T back()
Definition: collection.h:242
Collection(const Collection &)=delete
~Collection()
Definition: collection.h:153
Definition: object_key.h:38
Auxiliary class for opening dictionary tables.
Definition: transaction_impl.h:76
Definition: raw_table.h:44
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
required string key
Definition: replication_asynchronous_connection_failover.proto:60
required string type
Definition: replication_group_member_actions.proto:34
int n
Definition: xcom_base.cc:509