MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Restore.hpp
1 /*
2  Copyright (c) 2003, 2010, 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 as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 // Restore
19 
20 #ifndef RESTORE_H
21 #define RESTORE_H
22 
23 #include <ndb_global.h>
24 #include <NdbOut.hpp>
25 #include "../src/kernel/blocks/backup/BackupFormat.hpp"
26 #include <NdbApi.hpp>
27 #include <util/ndbzio.h>
28 #include <util/UtilBuffer.hpp>
29 
30 #include <ndb_version.h>
31 #include <version.h>
32 
33 enum TableChangesMask
34 {
38  TCM_ATTRIBUTE_PROMOTION = 0x1,
39 
43  TCM_EXCLUDE_MISSING_COLUMNS = 0x2,
44 
48  TCM_ATTRIBUTE_DEMOTION = 0x4
49 };
50 
51 inline
52 bool
53 isDrop6(Uint32 version)
54 {
55  return (getMajor(version) == 5 && getMinor(version) == 2);
56 }
57 
60 typedef void* (*AttrConvertFunc)(const void *old_data,
61  void *parameter,
62  bool &truncated);
63 
64 struct AttributeData {
65  bool null;
66  Uint32 size;
67  union {
68  Int8 * int8_value;
69  Uint8 * u_int8_value;
70 
71  Int16 * int16_value;
72  Uint16 * u_int16_value;
73 
74  Int32 * int32_value;
75  Uint32 * u_int32_value;
76 
77  Int64 * int64_value;
78  Uint64 * u_int64_value;
79 
80  char * string_value;
81 
82  void* void_value;
83  };
84 };
85 
86 struct AttributeDesc {
87  //private:
88  friend class TupleS;
89  friend class TableS;
90  friend class RestoreDataIterator;
91  friend class RestoreMetaData;
92  friend class AttributeS;
93  Uint32 size; // bits
94  Uint32 arraySize;
95  Uint32 attrId;
96  NdbDictionary::Column *m_column;
97 
98  bool m_exclude;
99  Uint32 m_nullBitIndex;
100  AttrConvertFunc convertFunc;
101  void *parameter;
102  bool truncation_detected;
103 
104 public:
105 
107  AttributeDesc();
108 
109  Uint32 getSizeInWords() const { return (size * arraySize + 31)/ 32;}
110  Uint32 getSizeInBytes() const {
111  assert(size >= 8);
112  return (size / 8) * arraySize;
113  }
114 }; // AttributeDesc
115 
116 class AttributeS {
117 public:
118  AttributeDesc * Desc;
120 };
121 
122 class TupleS {
123 private:
124  friend class RestoreDataIterator;
125 
126  class TableS *m_currentTable;
127  AttributeData *allAttrData;
128  bool prepareRecord(TableS &);
129 
130 public:
131  TupleS() {
132  m_currentTable= 0;
133  allAttrData= 0;
134  };
135  ~TupleS()
136  {
137  if (allAttrData)
138  delete [] allAttrData;
139  };
140  TupleS(const TupleS& tuple); // disable copy constructor
141  TupleS & operator=(const TupleS& tuple);
142  int getNoOfAttributes() const;
143  TableS * getTable() const;
144  AttributeDesc * getDesc(int i) const;
145  AttributeData * getData(int i) const;
146 }; // class TupleS
147 
149 {
150  Uint32 fragmentNo;
151  Uint64 noOfRecords;
152  Uint32 filePosLow;
153  Uint32 filePosHigh;
154 };
155 
156 class TableS {
157 
158  friend class TupleS;
159  friend class RestoreMetaData;
160  friend class RestoreDataIterator;
161 
162  Uint32 schemaVersion;
163  Uint32 backupVersion;
164  Vector<AttributeDesc *> allAttributesDesc;
165  Vector<AttributeDesc *> m_fixedKeys;
166  //Vector<AttributeDesc *> m_variableKey;
167  Vector<AttributeDesc *> m_fixedAttribs;
168  Vector<AttributeDesc *> m_variableAttribs;
169 
170  Uint32 m_noOfNullable;
171  Uint32 m_nullBitmaskSize;
172 
173  AttributeDesc * m_auto_val_attrib;
174  Uint64 m_max_auto_val;
175 
176  bool m_isSysTable;
177  bool m_isSYSTAB_0;
178  bool m_broken;
179 
180  TableS *m_main_table;
181  Uint32 m_main_column_id;
182  Uint32 m_local_id;
183 
184  Uint64 m_noOfRecords;
185  Vector<FragmentInfo *> m_fragmentInfo;
186 
187  void createAttr(NdbDictionary::Column *column);
188 
189 public:
190  class NdbDictionary::Table* m_dictTable;
191  TableS (Uint32 version, class NdbTableImpl* dictTable);
192  ~TableS();
193 
194  Uint32 getTableId() const {
195  return m_dictTable->getTableId();
196  }
197  Uint32 getLocalId() const {
198  return m_local_id;
199  }
200  Uint64 getNoOfRecords() const {
201  return m_noOfRecords;
202  }
203  /*
204  void setMysqlTableName(char * tableName) {
205  strpcpy(mysqlTableName, tableName);
206  }
207 
208  char *
209  void setMysqlDatabaseName(char * databaseName) {
210  strpcpy(mysqlDatabaseName, databaseName);
211  }
212 
213  table.setMysqlDatabaseName(database);
214  */
215  void setBackupVersion(Uint32 version) {
216  backupVersion = version;
217  }
218 
219  Uint32 getBackupVersion() const {
220  return backupVersion;
221  }
222 
223  const char * getTableName() const {
224  return m_dictTable->getName();
225  }
226 
227  int getNoOfAttributes() const {
228  return allAttributesDesc.size();
229  };
230 
231  bool have_auto_inc() const {
232  return m_auto_val_attrib != 0;
233  };
234 
235  bool have_auto_inc(Uint32 id) const {
236  return (m_auto_val_attrib ? m_auto_val_attrib->attrId == id : false);
237  };
238 
239  Uint64 get_max_auto_val() const {
240  return m_max_auto_val;
241  };
242 
243  void update_max_auto_val(const char *data, int size) {
244  union {
245  Uint8 u8;
246  Uint16 u16;
247  Uint32 u32;
248  } val;
249  Uint64 v;
250  switch(size){
251  case 64:
252  memcpy(&v,data,8);
253  break;
254  case 32:
255  memcpy(&val.u32,data,4);
256  v= val.u32;
257  break;
258  case 24:
259  v= uint3korr((unsigned char*)data);
260  break;
261  case 16:
262  memcpy(&val.u16,data,2);
263  v= val.u16;
264  break;
265  case 8:
266  memcpy(&val.u8,data,1);
267  v= val.u8;
268  break;
269  default:
270  return;
271  };
272  if(v > m_max_auto_val)
273  m_max_auto_val= v;
274  };
275 
276  bool get_auto_data(const TupleS & tuple, Uint32 * syskey, Uint64 * nextid) const;
277 
281  const AttributeDesc * operator[](int attributeId) const {
282  return allAttributesDesc[attributeId];
283  }
284 
285  AttributeDesc *getAttributeDesc(int attributeId) const {
286  return allAttributesDesc[attributeId];
287  }
288 
289  bool getSysTable() const {
290  return m_isSysTable;
291  }
292 
293  const TableS *getMainTable() const {
294  return m_main_table;
295  }
296 
297  TableS& operator=(TableS& org) ;
298 
299  bool isSYSTAB_0() const {
300  return m_isSYSTAB_0;
301  }
302 
303  inline
304  bool isBroken() const {
305  return m_broken || (m_main_table && m_main_table->isBroken());
306  }
307 
308 }; // TableS;
309 
310 class RestoreLogIterator;
311 
312 class BackupFile {
313 protected:
314  ndbzio_stream m_file;
315  char m_path[PATH_MAX];
316  char m_fileName[PATH_MAX];
317  bool m_hostByteOrder;
318  BackupFormat::FileHeader m_fileHeader;
319  BackupFormat::FileHeader m_expectedFileHeader;
320 
321  Uint32 m_nodeId;
322 
323  void * m_buffer;
324  void * m_buffer_ptr;
325  Uint32 m_buffer_sz;
326  Uint32 m_buffer_data_left;
327 
328  Uint64 m_file_size;
329  Uint64 m_file_pos;
330 
331  UtilBuffer m_twiddle_buffer;
332 
333  bool m_is_undolog;
334 
335  void (* free_data_callback)();
336  virtual void reset_buffers() {}
337 
338  bool openFile();
339  void setCtlFile(Uint32 nodeId, Uint32 backupId, const char * path);
340  void setDataFile(const BackupFile & bf, Uint32 no);
341  void setLogFile(const BackupFile & bf, Uint32 no);
342 
343  Uint32 buffer_get_ptr(void **p_buf_ptr, Uint32 size, Uint32 nmemb);
344  Uint32 buffer_read(void *ptr, Uint32 size, Uint32 nmemb);
345  Uint32 buffer_get_ptr_ahead(void **p_buf_ptr, Uint32 size, Uint32 nmemb);
346  Uint32 buffer_read_ahead(void *ptr, Uint32 size, Uint32 nmemb);
347 
348  void setName(const char * path, const char * name);
349 
350  BackupFile(void (* free_data_callback)() = 0);
351  virtual ~BackupFile();
352 
353 public:
354  bool readHeader();
355  bool validateFooter();
356 
357  const char * getPath() const { return m_path;}
358  const char * getFilename() const { return m_fileName;}
359  Uint32 getNodeId() const { return m_nodeId;}
360  const BackupFormat::FileHeader & getFileHeader() const { return m_fileHeader;}
361  bool Twiddle(const AttributeDesc * const attr_desc,
362  AttributeData * attr_data);
363 
364  Uint64 get_file_size() const { return m_file_size; }
365  Uint64 get_file_pos() const { return m_file_pos; }
366 
367 private:
368  void
369  twiddle_atribute(const AttributeDesc * const attr_desc,
370  AttributeData* attr_data);
371 };
372 
373 struct DictObject {
374  Uint32 m_objType;
375  void * m_objPtr;
376 };
377 
378 class RestoreMetaData : public BackupFile {
379 
380  Vector<TableS *> allTables;
381  bool readMetaFileHeader();
382  bool readMetaTableDesc();
383  bool markSysTables();
384  bool fixBlobs();
385 
386  bool readGCPEntry();
387  bool readFragmentInfo();
388  Uint32 readMetaTableList();
389 
390  Uint32 m_startGCP;
391  Uint32 m_stopGCP;
392 
393  bool parseTableDescriptor(const Uint32 * data, Uint32 len);
394 
395  Vector<DictObject> m_objects;
396 
397 public:
398  RestoreMetaData(const char * path, Uint32 nodeId, Uint32 bNo);
399  virtual ~RestoreMetaData();
400 
401  int loadContent();
402 
403  Uint32 getNoOfTables() const { return allTables.size();}
404 
405  const TableS * operator[](int i) const { return allTables[i];}
406  TableS * getTable(Uint32 tableId) const;
407 
408  Uint32 getNoOfObjects() const { return m_objects.size();}
409  Uint32 getObjType(Uint32 i) const { return m_objects[i].m_objType; }
410  void* getObjPtr(Uint32 i) const { return m_objects[i].m_objPtr; }
411 
412  Uint32 getStopGCP() const;
413  Uint32 getNdbVersion() const { return m_fileHeader.NdbVersion; };
414 }; // RestoreMetaData
415 
416 
418  const RestoreMetaData & m_metaData;
419  Uint32 m_count;
420  TableS* m_currentTable;
421  TupleS m_tuple;
422 
423 public:
424 
425  // Constructor
427  void (* free_data_callback)());
428  virtual ~RestoreDataIterator();
429 
430  // Read data file fragment header
431  bool readFragmentHeader(int & res, Uint32 *fragmentId);
432  bool validateFragmentFooter();
433 
434  const TupleS *getNextTuple(int & res);
435  TableS *getCurrentTable();
436 
437 private:
438  void init_bitfield_storage(const NdbDictionary::Table*);
439  void free_bitfield_storage();
440  void reset_bitfield_storage();
441  Uint32* get_bitfield_storage(Uint32 len);
442  Uint32 get_free_bitfield_storage() const;
443 
444  Uint32 m_row_bitfield_len; // in words
445  Uint32* m_bitfield_storage_ptr;
446  Uint32* m_bitfield_storage_curr_ptr;
447  Uint32 m_bitfield_storage_len; // In words
448 
449 protected:
450  virtual void reset_buffers() { reset_bitfield_storage();}
451 
452  int readTupleData_old(Uint32 *buf_ptr, Uint32 dataLength);
453  int readTupleData_packed(Uint32 *buf_ptr, Uint32 dataLength);
454 
455  int readVarData(Uint32 *buf_ptr, Uint32 *ptr, Uint32 dataLength);
456  int readVarData_drop6(Uint32 *buf_ptr, Uint32 *ptr, Uint32 dataLength);
457 };
458 
459 class LogEntry {
460 public:
461  enum EntryType {
462  LE_INSERT,
463  LE_DELETE,
464  LE_UPDATE
465  };
466  Uint32 m_frag_id;
467  EntryType m_type;
468  TableS * m_table;
469  Vector<AttributeS*> m_values;
470  Vector<AttributeS*> m_values_e;
471  AttributeS *add_attr() {
472  AttributeS * attr;
473  if (m_values_e.size() > 0) {
474  attr = m_values_e[m_values_e.size()-1];
475  m_values_e.erase(m_values_e.size()-1);
476  }
477  else
478  {
479  attr = new AttributeS;
480  }
481  m_values.push_back(attr);
482  return attr;
483  }
484  void clear() {
485  for(Uint32 i= 0; i < m_values.size(); i++)
486  m_values_e.push_back(m_values[i]);
487  m_values.clear();
488  }
489  LogEntry() {}
490  ~LogEntry()
491  {
492  Uint32 i;
493  for(i= 0; i< m_values.size(); i++)
494  delete m_values[i];
495  for(i= 0; i< m_values_e.size(); i++)
496  delete m_values_e[i];
497  }
498  Uint32 size() const { return m_values.size(); }
499  const AttributeS * operator[](int i) const { return m_values[i];}
500 };
501 
503 private:
504  const RestoreMetaData & m_metaData;
505 
506  Uint32 m_count;
507  Uint32 m_last_gci;
508  LogEntry m_logEntry;
509 public:
511  virtual ~RestoreLogIterator() {};
512 
513  const LogEntry * getNextLogEntry(int & res);
514 };
515 
516 NdbOut& operator<<(NdbOut& ndbout, const TableS&);
517 NdbOut& operator<<(NdbOut& ndbout, const TupleS&);
518 NdbOut& operator<<(NdbOut& ndbout, const LogEntry&);
519 NdbOut& operator<<(NdbOut& ndbout, const RestoreMetaData&);
520 
521 bool readSYSTAB_0(const TupleS & tup, Uint32 * syskey, Uint64 * nextid);
522 
523 #endif
524 
525