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