1 /*
2    Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
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 
25 #ifndef ALTER_TABLE_HPP
26 #define ALTER_TABLE_HPP
27 
28 #include "SignalData.hpp"
29 
30 #define JAM_FILE_ID 112
31 
32 
33 struct AlterTableReq {
34   STATIC_CONST( SignalLength = 8 );
35 
36   Uint32 clientRef;
37   Uint32 clientData;
38   Uint32 transId;
39   Uint32 transKey;
40   Uint32 requestInfo;
41   Uint32 tableId;
42   Uint32 tableVersion;
43   Uint32 changeMask;
44 
45   SECTION( DICT_TAB_INFO = 0 );
46 
47 /**
48  * ChangeMask
49  */
50 
51 /*
52   n = Changed name
53   f = Changed frm
54   d = Changed fragment data
55   r = Changed range or list array
56   t = Changed tablespace name array
57   s = Changed tablespace id array
58   a = Add attribute
59   f = Add fragment(s)
60   r = Reorg fragment(s)
61   c = Reorg commit flag
62   C = Reorg complete
63   u = Reorg Suma enable flag
64   U = Reorg Suma filter flag
65   F = Fragment count type flag
66   R = Changed Read Backup flag
67   m = Modified attribute
68            1111111111222222222233
69  01234567890123456789012345678901
70  nfdrtsafrcCuUFRm----------------
71 */
72 #define NAME_SHIFT        (0)
73 #define FRM_SHIFT         (1)
74 #define FRAG_DATA_SHIFT   (2)
75 #define RANGE_LIST_SHIFT  (3)
76 #define TS_NAME_SHIFT     (4)
77 #define TS_SHIFT          (5)
78 #define ADD_ATTR_SHIFT    (6)
79 #define ADD_FRAG_SHIFT    (7)
80 #define REORG_FRAG_SHIFT  (8)
81 #define REORG_COMMIT_SHIFT   (9)
82 #define REORG_COMPLETE_SHIFT (10)
83 #define REORG_SUMA_ENABLE (11)
84 #define REORG_SUMA_FILTER (12)
85 #define PARTITION_BALANCE_SHIFT (13)
86 #define READ_BACKUP_SHIFT (14)
87 #define MODIFY_ATTR_SHIFT (15)
88 
89  /**
90    * Getters and setters
91    */
92 
93   /**
94    * These are that flags that can be set from the NDB API
95    * as part of an online alter table (inplace).
96    * We can change the name of a table,
97    * we can change the frm file of a table,
98    * we can change the read backup flag of a table,
99    * we can change the name of an attribute of a table and
100    * we can add attributes to a table and
101    * we can change the partition balance of a table,
102    * we can add fragments to the table.
103    */
104   static Uint8 getNameFlag(const UintR & changeMask);
105   static void setNameFlag(UintR &  changeMask, Uint32 nameFlg);
106   static Uint8 getFrmFlag(const UintR & changeMask);
107   static void setFrmFlag(UintR &  changeMask, Uint32 frmFlg);
108   static Uint8 getFragDataFlag(const UintR & changeMask);
109   static void setFragDataFlag(UintR &  changeMask, Uint32 fragFlg);
110   static Uint8 getRangeListFlag(const UintR & changeMask);
111   static void setRangeListFlag(UintR &  changeMask, Uint32 rangeFlg);
112   static Uint8 getAddAttrFlag(const UintR & changeMask);
113   static void setAddAttrFlag(UintR &  changeMask, Uint32 tsFlg);
114   static Uint8 getModifyAttrFlag(const UintR & changeMask);
115   static void setModifyAttrFlag(UintR &  changeMask, Uint32 tsFlg);
116   static Uint8 getAddFragFlag(const UintR & changeMask);
117   static void setAddFragFlag(UintR &  changeMask, Uint32 tsFlg);
118   static void setReadBackupFlag(UintR & changeMask, Uint32 tsFlg);
119   static Uint8 getReadBackupFlag(const UintR & changeMask);
120 
121   /**
122    * These flags are never used.
123    */
124   static Uint8 getTsNameFlag(const UintR & changeMask);
125   static void setTsNameFlag(UintR &  changeMask, Uint32 tsNameFlg);
126   static Uint8 getTsFlag(const UintR & changeMask);
127   static void setTsFlag(UintR &  changeMask, Uint32 tsFlg);
128 
129   /**
130    * The getReorgFragFlag is set by DICT when the hashmap changes
131    * as part of reorganise of partitions. It should not be set
132    * by the NDB API, it is set by DICT.
133    */
134   static Uint8 getReorgFragFlag(const UintR & changeMask);
135   static void setReorgFragFlag(UintR &  changeMask, Uint32 tsFlg);
136 
137   /**
138    * The flags below are al defined as part of DICT subops. This means
139    * that they should not be set by the NDB API. They are set in the
140    * subops handling in DICT as part of executing the ALTER_TABLE_REQ
141    * signal from the NDB API.
142    */
143   static Uint8 getReorgCommitFlag(const UintR & changeMask);
144   static void setReorgCommitFlag(UintR &  changeMask, Uint32 tsFlg);
145   static Uint8 getReorgCompleteFlag(const UintR & changeMask);
146   static void setReorgCompleteFlag(UintR &  changeMask, Uint32 tsFlg);
147   static Uint8 getReorgSumaEnableFlag(const UintR & changeMask);
148   static void setReorgSumaEnableFlag(UintR &  changeMask, Uint32 tsFlg);
149   static Uint8 getReorgSumaFilterFlag(const UintR & changeMask);
150   static void setReorgSumaFilterFlag(UintR &  changeMask, Uint32 tsFlg);
151   static void setPartitionBalanceFlag(UintR & changeMask, Uint32 tsFlg);
152   static Uint8 getPartitionBalanceFlag(const UintR & changeMask);
153 
getSubOpAlterTableReq154   static bool getSubOp(const UintR & changeMask)
155   {
156     return
157       getReorgCommitFlag(changeMask) ||
158       getReorgCompleteFlag(changeMask) ||
159       getReorgSumaEnableFlag(changeMask) ||
160       getReorgSumaFilterFlag(changeMask);
161   }
162 
getReorgSubOpAlterTableReq163   static bool getReorgSubOp(const UintR & changeMask)
164   {
165     return
166       getReorgCommitFlag(changeMask) ||
167       getReorgCompleteFlag(changeMask) ||
168       getReorgSumaEnableFlag(changeMask) ||
169       getReorgSumaFilterFlag(changeMask);
170   }
171 };
172 
173 inline
174 Uint8
getTsFlag(const UintR & changeMask)175 AlterTableReq::getTsFlag(const UintR & changeMask){
176   return (Uint8)((changeMask >> TS_SHIFT) & 1);
177 }
178 
179 inline
180 void
setTsFlag(UintR & changeMask,Uint32 tsFlg)181 AlterTableReq::setTsFlag(UintR & changeMask, Uint32 tsFlg){
182   changeMask |= (tsFlg << TS_SHIFT);
183 }
184 
185 inline
186 Uint8
getNameFlag(const UintR & changeMask)187 AlterTableReq::getNameFlag(const UintR & changeMask){
188   return (Uint8)((changeMask >> NAME_SHIFT) & 1);
189 }
190 
191 inline
192 void
setNameFlag(UintR & changeMask,Uint32 nameFlg)193 AlterTableReq::setNameFlag(UintR & changeMask, Uint32 nameFlg){
194   changeMask |= (nameFlg << NAME_SHIFT);
195 }
196 
197 inline
198 Uint8
getFrmFlag(const UintR & changeMask)199 AlterTableReq::getFrmFlag(const UintR & changeMask){
200   return (Uint8)((changeMask >> FRM_SHIFT) & 1);
201 }
202 
203 inline
204 void
setFrmFlag(UintR & changeMask,Uint32 frmFlg)205 AlterTableReq::setFrmFlag(UintR & changeMask, Uint32 frmFlg){
206   changeMask |= (frmFlg << FRM_SHIFT);
207 }
208 
209 inline
210 Uint8
getFragDataFlag(const UintR & changeMask)211 AlterTableReq::getFragDataFlag(const UintR & changeMask){
212   return (Uint8)((changeMask >> FRAG_DATA_SHIFT) & 1);
213 }
214 
215 inline
216 void
setFragDataFlag(UintR & changeMask,Uint32 fragDataFlg)217 AlterTableReq::setFragDataFlag(UintR & changeMask, Uint32 fragDataFlg){
218   changeMask |= (fragDataFlg << FRAG_DATA_SHIFT);
219 }
220 
221 inline
222 Uint8
getRangeListFlag(const UintR & changeMask)223 AlterTableReq::getRangeListFlag(const UintR & changeMask){
224   return (Uint8)((changeMask >> RANGE_LIST_SHIFT) & 1);
225 }
226 
227 inline
228 void
setRangeListFlag(UintR & changeMask,Uint32 rangeFlg)229 AlterTableReq::setRangeListFlag(UintR & changeMask, Uint32 rangeFlg){
230   changeMask |= (rangeFlg << RANGE_LIST_SHIFT);
231 }
232 
233 inline
234 Uint8
getTsNameFlag(const UintR & changeMask)235 AlterTableReq::getTsNameFlag(const UintR & changeMask){
236   return (Uint8)((changeMask >> TS_NAME_SHIFT) & 1);
237 }
238 
239 inline
240 void
setTsNameFlag(UintR & changeMask,Uint32 tsNameFlg)241 AlterTableReq::setTsNameFlag(UintR & changeMask, Uint32 tsNameFlg){
242   changeMask |= (tsNameFlg << TS_NAME_SHIFT);
243 }
244 
245 inline
246 Uint8
getAddAttrFlag(const UintR & changeMask)247 AlterTableReq::getAddAttrFlag(const UintR & changeMask){
248   return (Uint8)((changeMask >> ADD_ATTR_SHIFT) & 1);
249 }
250 
251 inline
252 void
setAddAttrFlag(UintR & changeMask,Uint32 addAttrFlg)253 AlterTableReq::setAddAttrFlag(UintR & changeMask, Uint32 addAttrFlg){
254   changeMask |= (addAttrFlg << ADD_ATTR_SHIFT);
255 }
256 
257 inline
258 Uint8
getModifyAttrFlag(const UintR & changeMask)259 AlterTableReq::getModifyAttrFlag(const UintR & changeMask){
260   return (Uint8)((changeMask >> MODIFY_ATTR_SHIFT) & 1);
261 }
262 
263 inline
264 void
setModifyAttrFlag(UintR & changeMask,Uint32 addAttrFlg)265 AlterTableReq::setModifyAttrFlag(UintR & changeMask, Uint32 addAttrFlg){
266   changeMask |= (addAttrFlg << MODIFY_ATTR_SHIFT);
267 }
268 
269 inline
270 Uint8
getAddFragFlag(const UintR & changeMask)271 AlterTableReq::getAddFragFlag(const UintR & changeMask){
272   return (Uint8)((changeMask >> ADD_FRAG_SHIFT) & 1);
273 }
274 
275 inline
276 void
setAddFragFlag(UintR & changeMask,Uint32 addAttrFlg)277 AlterTableReq::setAddFragFlag(UintR & changeMask, Uint32 addAttrFlg){
278   changeMask |= (addAttrFlg << ADD_FRAG_SHIFT);
279 }
280 
281 inline
282 Uint8
getReorgFragFlag(const UintR & changeMask)283 AlterTableReq::getReorgFragFlag(const UintR & changeMask){
284   return (Uint8)((changeMask >> REORG_FRAG_SHIFT) & 1);
285 }
286 
287 inline
288 void
setReorgFragFlag(UintR & changeMask,Uint32 reorgAttrFlg)289 AlterTableReq::setReorgFragFlag(UintR & changeMask, Uint32 reorgAttrFlg){
290   changeMask |= (reorgAttrFlg << REORG_FRAG_SHIFT);
291 }
292 
293 inline
294 Uint8
getReorgCommitFlag(const UintR & changeMask)295 AlterTableReq::getReorgCommitFlag(const UintR & changeMask){
296   return (Uint8)((changeMask >> REORG_COMMIT_SHIFT) & 1);
297 }
298 
299 inline
300 void
setReorgCommitFlag(UintR & changeMask,Uint32 reorgAttrFlg)301 AlterTableReq::setReorgCommitFlag(UintR & changeMask, Uint32 reorgAttrFlg){
302   changeMask |= (reorgAttrFlg << REORG_COMMIT_SHIFT);
303 }
304 
305 
306 inline
307 Uint8
getReorgCompleteFlag(const UintR & changeMask)308 AlterTableReq::getReorgCompleteFlag(const UintR & changeMask){
309   return (Uint8)((changeMask >> REORG_COMPLETE_SHIFT) & 1);
310 }
311 
312 inline
313 void
setReorgCompleteFlag(UintR & changeMask,Uint32 reorgAttrFlg)314 AlterTableReq::setReorgCompleteFlag(UintR & changeMask, Uint32 reorgAttrFlg){
315   changeMask |= (reorgAttrFlg << REORG_COMPLETE_SHIFT);
316 }
317 
318 inline
319 Uint8
getReorgSumaEnableFlag(const UintR & changeMask)320 AlterTableReq::getReorgSumaEnableFlag(const UintR & changeMask){
321   return (Uint8)((changeMask >> REORG_SUMA_ENABLE) & 1);
322 }
323 
324 inline
325 void
setReorgSumaEnableFlag(UintR & changeMask,Uint32 reorgAttrFlg)326 AlterTableReq::setReorgSumaEnableFlag(UintR & changeMask, Uint32 reorgAttrFlg){
327   changeMask |= (reorgAttrFlg << REORG_SUMA_ENABLE);
328 }
329 
330 inline
331 Uint8
getReorgSumaFilterFlag(const UintR & changeMask)332 AlterTableReq::getReorgSumaFilterFlag(const UintR & changeMask){
333   return (Uint8)((changeMask >> REORG_SUMA_FILTER) & 1);
334 }
335 
336 inline
337 void
setReorgSumaFilterFlag(UintR & changeMask,Uint32 reorgAttrFlg)338 AlterTableReq::setReorgSumaFilterFlag(UintR & changeMask, Uint32 reorgAttrFlg){
339   changeMask |= (reorgAttrFlg << REORG_SUMA_FILTER);
340 }
341 
342 inline
343 Uint8
getPartitionBalanceFlag(const UintR & changeMask)344 AlterTableReq::getPartitionBalanceFlag(const UintR & changeMask){
345   return (Uint8)((changeMask >> PARTITION_BALANCE_SHIFT) & 1);
346 }
347 
348 inline
349 void
setPartitionBalanceFlag(UintR & changeMask,Uint32 fctFlag)350 AlterTableReq::setPartitionBalanceFlag(UintR & changeMask, Uint32 fctFlag){
351   changeMask |= (fctFlag << PARTITION_BALANCE_SHIFT);
352 }
353 
354 inline
355 Uint8
getReadBackupFlag(const UintR & changeMask)356 AlterTableReq::getReadBackupFlag(const UintR & changeMask){
357   return (Uint8)((changeMask >> READ_BACKUP_SHIFT) & 1);
358 }
359 
360 inline
361 void
setReadBackupFlag(UintR & changeMask,Uint32 rbFlag)362 AlterTableReq::setReadBackupFlag(UintR & changeMask, Uint32 rbFlag){
363   changeMask |= (rbFlag << READ_BACKUP_SHIFT);
364 }
365 
366 struct AlterTableConf {
367   STATIC_CONST( SignalLength = 6 );
368 
369   Uint32 senderRef;
370   union {
371     Uint32 clientData;
372     Uint32 senderData;
373   };
374   Uint32 transId;
375   Uint32 tableId;
376   Uint32 tableVersion;
377   Uint32 newTableVersion;
378 };
379 
380 struct AlterTableRef {
381   STATIC_CONST( SignalLength = 9 );
382 
383   enum ErrorCode {
384     NoError = 0,
385     InvalidTableVersion = 241,
386     DropInProgress      = 283,
387     Busy = 701,
388     BusyWithNR = 711,
389     NotMaster = 702,
390     InvalidFormat = 703,
391     AttributeNameTooLong = 704,
392     TableNameTooLong = 705,
393     Inconsistency = 706,
394     NoMoreTableRecords = 707,
395     NoMoreAttributeRecords = 708,
396     NoSuchTable = 709,
397     AttributeNameTwice = 720,
398     TableAlreadyExist = 721,
399     ArraySizeTooBig = 737,
400     RecordTooBig = 738,
401     InvalidPrimaryKeySize  = 739,
402     NullablePrimaryKey = 740,
403     UnsupportedChange = 741,
404     BackupInProgress = 762,
405     IncompatibleVersions = 763,
406     SingleUser = 299,
407     TableDefinitionTooBig = 793
408   };
409 
410   Uint32 senderRef;
411   union {
412     Uint32 clientData;
413     Uint32 senderData;
414   };
415   Uint32 transId;
416   Uint32 errorCode;
417   Uint32 errorLine;
418   Uint32 errorNodeId;
419   Uint32 masterNodeId;
420   Uint32 errorStatus;
421   Uint32 errorKey;
422 };
423 
424 /**
425  * Inform API about change of table definition
426  */
427 struct AlterTableRep
428 {
429   friend bool printALTER_TABLE_REP(FILE*, const Uint32*, Uint32, Uint16);
430 
431   STATIC_CONST( SignalLength = 3 );
432 
433   enum Change_type
434   {
435     CT_ALTERED = 0x1,
436     CT_DROPPED = 0x2
437   };
438 
439   Uint32 tableId;
440   Uint32 tableVersion;
441   Uint32 changeType;
442 
443   SECTION( TABLE_NAME = 0 );
444 };
445 
446 
447 #undef JAM_FILE_ID
448 
449 #endif
450