1 /* Copyright (c) 2014, 2019, 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__PARTITION_IMPL_INCLUDED
24 #define DD__PARTITION_IMPL_INCLUDED
25 
26 #include <sys/types.h>
27 #include <memory>
28 #include <new>
29 
30 #include "sql/dd/collection.h"
31 #include "sql/dd/impl/properties_impl.h"
32 #include "sql/dd/impl/types/entity_object_impl.h"  // dd::Entity_object_impl
33 #include "sql/dd/impl/types/weak_object_impl.h"
34 #include "sql/dd/object_id.h"
35 #include "sql/dd/sdi_fwd.h"
36 #include "sql/dd/string_type.h"
37 #include "sql/dd/types/partition.h"        // dd::Partition
38 #include "sql/dd/types/partition_index.h"  // IWYU pragma: keep
39 #include "sql/dd/types/partition_value.h"  // IWYU pragma: keep
40 #include "sql/dd/types/table.h"
41 
42 namespace dd {
43 
44 ///////////////////////////////////////////////////////////////////////////
45 
46 class Index;
47 class Object_table;
48 class Open_dictionary_tables_ctx;
49 class Properties;
50 class Raw_record;
51 class Sdi_rcontext;
52 class Sdi_wcontext;
53 class Table;
54 class Table_impl;
55 class Weak_object;
56 
57 ///////////////////////////////////////////////////////////////////////////
58 
59 class Partition_impl : public Entity_object_impl, public Partition {
60  public:
61   Partition_impl();
62 
63   Partition_impl(Table_impl *table);
64 
65   Partition_impl(Table_impl *parent, Partition_impl *partition);
66 
67   Partition_impl(const Partition_impl &src, Table_impl *parent);
68 
69   Partition_impl(const Partition_impl &src, Partition_impl *partition);
70 
71   virtual ~Partition_impl();
72 
73  public:
74   virtual const Object_table &object_table() const;
75 
76   virtual bool validate() const;
77 
78   virtual bool restore_children(Open_dictionary_tables_ctx *otx);
79 
80   virtual bool store_children(Open_dictionary_tables_ctx *otx);
81 
82   virtual bool drop_children(Open_dictionary_tables_ctx *otx) const;
83 
84   virtual bool restore_attributes(const Raw_record &r);
85 
86   virtual bool store_attributes(Raw_record *r);
87 
88   void serialize(Sdi_wcontext *wctx, Sdi_writer *w) const;
89 
90   bool deserialize(Sdi_rcontext *rctx, const RJ_Value &val);
91 
92   void debug_print(String_type &outb) const;
93 
set_ordinal_position(uint)94   void set_ordinal_position(uint) {}
95 
ordinal_position()96   virtual uint ordinal_position() const { return -1; }
97 
98  public:
99   static void register_tables(Open_dictionary_tables_ctx *otx);
100 
101   /////////////////////////////////////////////////////////////////////////
102   // Table.
103   /////////////////////////////////////////////////////////////////////////
104 
105   virtual const Table &table() const;
106 
107   virtual Table &table();
108 
table_impl()109   /* non-virtual */ const Table_impl &table_impl() const { return *m_table; }
110 
table_impl()111   /* non-virtual */ Table_impl &table_impl() { return *m_table; }
112 
113   /////////////////////////////////////////////////////////////////////////
114   // Parent partition.
115   /////////////////////////////////////////////////////////////////////////
116 
parent_partition()117   virtual const Partition *parent_partition() const { return m_parent; }
118 
parent_partition()119   virtual Partition *parent_partition() {
120     return const_cast<dd::Partition *>(m_parent);
121   }
122 
123   /////////////////////////////////////////////////////////////////////////
124   // parent_partition_id
125   /////////////////////////////////////////////////////////////////////////
126 
parent_partition_id()127   virtual Object_id parent_partition_id() const {
128     return m_parent_partition_id;
129   }
130 
set_parent_partition_id(Object_id parent_partition_id)131   virtual void set_parent_partition_id(Object_id parent_partition_id) {
132     m_parent_partition_id = parent_partition_id;
133   }
134 
135   /////////////////////////////////////////////////////////////////////////
136   // number.
137   /////////////////////////////////////////////////////////////////////////
138 
number()139   virtual uint number() const { return m_number; }
140 
set_number(uint number)141   virtual void set_number(uint number) { m_number = number; }
142 
143   /////////////////////////////////////////////////////////////////////////
144   // description_utf8.
145   /////////////////////////////////////////////////////////////////////////
146 
description_utf8()147   virtual const String_type &description_utf8() const {
148     return m_description_utf8;
149   }
150 
set_description_utf8(const String_type & description_utf8)151   virtual void set_description_utf8(const String_type &description_utf8) {
152     m_description_utf8 = description_utf8;
153   }
154 
155   /////////////////////////////////////////////////////////////////////////
156   // engine.
157   /////////////////////////////////////////////////////////////////////////
158 
engine()159   virtual const String_type &engine() const { return m_engine; }
160 
set_engine(const String_type & engine)161   virtual void set_engine(const String_type &engine) { m_engine = engine; }
162 
163   /////////////////////////////////////////////////////////////////////////
164   // comment.
165   /////////////////////////////////////////////////////////////////////////
166 
comment()167   virtual const String_type &comment() const { return m_comment; }
168 
set_comment(const String_type & comment)169   virtual void set_comment(const String_type &comment) { m_comment = comment; }
170 
171   /////////////////////////////////////////////////////////////////////////
172   // Options.
173   /////////////////////////////////////////////////////////////////////////
174 
options()175   virtual const Properties &options() const { return m_options; }
176 
options()177   virtual Properties &options() { return m_options; }
178 
set_options(const Properties & options)179   virtual bool set_options(const Properties &options) {
180     return m_options.insert_values(options);
181   }
182 
set_options(const String_type & options_raw)183   virtual bool set_options(const String_type &options_raw) {
184     return m_options.insert_values(options_raw);
185   }
186 
187   /////////////////////////////////////////////////////////////////////////
188   // se_private_data.
189   /////////////////////////////////////////////////////////////////////////
190 
se_private_data()191   virtual const Properties &se_private_data() const {
192     return m_se_private_data;
193   }
194 
se_private_data()195   virtual Properties &se_private_data() { return m_se_private_data; }
196 
set_se_private_data(const String_type & se_private_data_raw)197   virtual bool set_se_private_data(const String_type &se_private_data_raw) {
198     return m_se_private_data.insert_values(se_private_data_raw);
199   }
200 
set_se_private_data(const Properties & se_private_data)201   virtual bool set_se_private_data(const Properties &se_private_data) {
202     return m_se_private_data.insert_values(se_private_data);
203   }
204 
205   /////////////////////////////////////////////////////////////////////////
206   // se_private_id.
207   /////////////////////////////////////////////////////////////////////////
208 
se_private_id()209   virtual Object_id se_private_id() const { return m_se_private_id; }
210 
set_se_private_id(Object_id se_private_id)211   virtual void set_se_private_id(Object_id se_private_id) {
212     m_se_private_id = se_private_id;
213   }
214 
215   /////////////////////////////////////////////////////////////////////////
216   // Tablespace.
217   /////////////////////////////////////////////////////////////////////////
218 
tablespace_id()219   virtual Object_id tablespace_id() const { return m_tablespace_id; }
220 
set_tablespace_id(Object_id tablespace_id)221   virtual void set_tablespace_id(Object_id tablespace_id) {
222     m_tablespace_id = tablespace_id;
223   }
224 
225   /////////////////////////////////////////////////////////////////////////
226   // Partition-value collection
227   /////////////////////////////////////////////////////////////////////////
228 
229   virtual Partition_value *add_value();
230 
values()231   virtual const Partition_values &values() const { return m_values; }
232 
233   /////////////////////////////////////////////////////////////////////////
234   // Partition-index collection
235   /////////////////////////////////////////////////////////////////////////
236 
237   virtual Partition_index *add_index(Index *idx);
238 
indexes()239   virtual const Partition_indexes &indexes() const { return m_indexes; }
240 
241   /* purecov: begin deadcode */
indexes()242   virtual Partition_indexes *indexes() { return &m_indexes; }
243   /* purecov: end */
244 
245   /////////////////////////////////////////////////////////////////////////
246   // Sub Partition collection.
247   /////////////////////////////////////////////////////////////////////////
248 
249   virtual Partition *add_subpartition();
250 
subpartitions()251   virtual const Table::Partition_collection &subpartitions() const {
252     return m_subpartitions;
253   }
254 
subpartitions()255   virtual Table::Partition_collection *subpartitions() {
256     return &m_subpartitions;
257   }
258 
parent()259   virtual const Partition *parent() const { return m_parent; }
set_parent(const Partition * parent)260   virtual void set_parent(const Partition *parent) { m_parent = parent; }
261 
262   // Fix "inherits ... via dominance" warnings
impl()263   virtual Entity_object_impl *impl() { return Entity_object_impl::impl(); }
impl()264   virtual const Entity_object_impl *impl() const {
265     return Entity_object_impl::impl();
266   }
id()267   virtual Object_id id() const { return Entity_object_impl::id(); }
is_persistent()268   virtual bool is_persistent() const {
269     return Entity_object_impl::is_persistent();
270   }
name()271   virtual const String_type &name() const { return Entity_object_impl::name(); }
set_name(const String_type & name)272   virtual void set_name(const String_type &name) {
273     Entity_object_impl::set_name(name);
274   }
275 
276  public:
restore_item(Table_impl * table)277   static Partition_impl *restore_item(Table_impl *table) {
278     return new (std::nothrow) Partition_impl(table);
279   }
280 
restore_item(Partition_impl * part)281   static Partition_impl *restore_item(Partition_impl *part) {
282     Partition_impl *p =
283         new (std::nothrow) Partition_impl(&part->table_impl(), part);
284     p->set_parent(part);
285 
286     return p;
287   }
288 
clone(const Partition_impl & other,Table_impl * table)289   static Partition_impl *clone(const Partition_impl &other, Table_impl *table) {
290     return new (std::nothrow) Partition_impl(other, table);
291   }
292 
clone(const Partition_impl & other,Partition_impl * part)293   static Partition_impl *clone(const Partition_impl &other,
294                                Partition_impl *part) {
295     return new (std::nothrow) Partition_impl(other, part);
296   }
297 
298  private:
299   // Fields.
300 
301   Object_id m_parent_partition_id;
302   uint m_number;
303   Object_id m_se_private_id;
304 
305   String_type m_description_utf8;
306   String_type m_engine;
307   String_type m_comment;
308   Properties_impl m_options;
309   Properties_impl m_se_private_data;
310 
311   // References to tightly-coupled objects.
312 
313   Table_impl *m_table;
314 
315   const Partition *m_parent;
316 
317   Partition_values m_values;
318   Partition_indexes m_indexes;
319   Table::Partition_collection m_subpartitions;
320 
321   // References to loosely-coupled objects.
322 
323   Object_id m_tablespace_id;
324 };
325 
326 ///////////////////////////////////////////////////////////////////////////
327 
328 /** Used to compare two partition elements. */
329 struct Partition_order_comparator {
330   // TODO : do we really need this ordering now ?
operatorPartition_order_comparator331   bool operator()(const dd::Partition *p1, const dd::Partition *p2) const {
332     if (p1->parent_partition_id() == p2->parent_partition_id())
333       return p1->number() < p2->number();
334     return p1->parent_partition_id() < p2->parent_partition_id();
335   }
336 };
337 
338 }  // namespace dd
339 
340 #endif  // DD__PARTITION_IMPL_INCLUDED
341