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