~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/tools/restore/Restore.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
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 as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#ifndef RESTORE_H
 
17
#define RESTORE_H
 
18
 
 
19
#include <ndb_global.h>
 
20
#include <NdbOut.hpp>
 
21
#include "../src/kernel/blocks/backup/BackupFormat.hpp"
 
22
#include "../src/ndbapi/NdbDictionaryImpl.hpp"
 
23
#include <NdbApi.hpp>
 
24
 
 
25
#include <ndb_version.h>
 
26
#include <version.h>
 
27
 
 
28
const int FileNameLenC = 256;
 
29
const int TableNameLenC = 256;
 
30
const int AttrNameLenC = 256;
 
31
const Uint32 timeToWaitForNdbC = 10000;
 
32
const Uint32 opsDefaultC = 1000;
 
33
 
 
34
// Forward declarations
 
35
//class AttributeDesc;
 
36
struct AttributeDesc;
 
37
struct AttributeData;
 
38
struct AttributeS;
 
39
 
 
40
struct AttributeData {
 
41
  bool null;
 
42
  Uint32 size;
 
43
  union {
 
44
    Int8 * int8_value;
 
45
    Uint8 * u_int8_value;
 
46
    
 
47
    Int16 * int16_value;
 
48
    Uint16 * u_int16_value;
 
49
 
 
50
    Int32 * int32_value;
 
51
    Uint32 * u_int32_value;
 
52
    
 
53
    Int64 * int64_value;
 
54
    Uint64 * u_int64_value;
 
55
 
 
56
    char * string_value;
 
57
    
 
58
    void* void_value;
 
59
  };
 
60
};
 
61
 
 
62
struct AttributeDesc {
 
63
  //private:
 
64
  friend class TupleS;
 
65
  friend class TableS;
 
66
  friend class RestoreDataIterator;
 
67
  friend class RestoreMetaData;
 
68
  friend struct AttributeS;
 
69
  Uint32 size; // bits       
 
70
  Uint32 arraySize;
 
71
  Uint32 attrId;
 
72
  NdbDictionary::Column *m_column;
 
73
 
 
74
  Uint32 m_nullBitIndex;
 
75
public:
 
76
  
 
77
  AttributeDesc(NdbDictionary::Column *column);
 
78
  AttributeDesc();
 
79
 
 
80
  Uint32 getSizeInWords() const { return (size * arraySize + 31)/ 32;}
 
81
}; // AttributeDesc
 
82
 
 
83
struct AttributeS {
 
84
  const AttributeDesc * Desc;
 
85
  AttributeData Data;
 
86
};
 
87
 
 
88
class TupleS {
 
89
private:
 
90
  friend class RestoreDataIterator;
 
91
  
 
92
  class TableS *m_currentTable;
 
93
  AttributeData *allAttrData;
 
94
  bool prepareRecord(TableS &);
 
95
  
 
96
public:
 
97
  TupleS() {
 
98
    m_currentTable= 0;
 
99
    allAttrData= 0;
 
100
  };
 
101
  ~TupleS()
 
102
  {
 
103
    if (allAttrData)
 
104
      delete [] allAttrData;
 
105
  };
 
106
  TupleS(const TupleS& tuple); // disable copy constructor
 
107
  TupleS & operator=(const TupleS& tuple);
 
108
  int getNoOfAttributes() const;
 
109
  TableS * getTable() const;
 
110
  const AttributeDesc * getDesc(int i) const;
 
111
  AttributeData * getData(int i) const;
 
112
}; // class TupleS
 
113
 
 
114
struct FragmentInfo
 
115
{
 
116
  Uint32 fragmentNo;
 
117
  Uint64 noOfRecords;
 
118
  Uint32 filePosLow;
 
119
  Uint32 filePosHigh;
 
120
};
 
121
 
 
122
class TableS {
 
123
  
 
124
  friend class TupleS;
 
125
  friend class RestoreMetaData;
 
126
  friend class RestoreDataIterator;
 
127
  
 
128
  Uint32 schemaVersion;
 
129
  Uint32 backupVersion;
 
130
  Vector<AttributeDesc *> allAttributesDesc;
 
131
  Vector<AttributeDesc *> m_fixedKeys;
 
132
  //Vector<AttributeDesc *> m_variableKey; 
 
133
  Vector<AttributeDesc *> m_fixedAttribs;
 
134
  Vector<AttributeDesc *> m_variableAttribs;
 
135
  
 
136
  Uint32 m_noOfNullable;
 
137
  Uint32 m_nullBitmaskSize;
 
138
 
 
139
  Uint32 m_auto_val_id;
 
140
  Uint64 m_max_auto_val;
 
141
 
 
142
  bool isSysTable;
 
143
  TableS *m_main_table;
 
144
  Uint32 m_local_id;
 
145
 
 
146
  Uint64 m_noOfRecords;
 
147
  Vector<FragmentInfo *> m_fragmentInfo;
 
148
 
 
149
  void createAttr(NdbDictionary::Column *column);
 
150
 
 
151
public:
 
152
  class NdbDictionary::Table* m_dictTable;
 
153
  TableS (Uint32 version, class NdbTableImpl* dictTable);
 
154
  ~TableS();
 
155
 
 
156
  Uint32 getTableId() const { 
 
157
    return m_dictTable->getTableId(); 
 
158
  }
 
159
  Uint32 getLocalId() const { 
 
160
    return m_local_id; 
 
161
  }
 
162
  Uint32 getNoOfRecords() const { 
 
163
    return m_noOfRecords; 
 
164
  }
 
165
  /*
 
166
  void setMysqlTableName(char * tableName) {
 
167
    strpcpy(mysqlTableName, tableName);
 
168
  }
 
169
  
 
170
  char * 
 
171
  void setMysqlDatabaseName(char * databaseName) {
 
172
    strpcpy(mysqlDatabaseName, databaseName);
 
173
  }
 
174
 
 
175
  table.setMysqlDatabaseName(database);
 
176
  */
 
177
  void setBackupVersion(Uint32 version) { 
 
178
    backupVersion = version;
 
179
  }
 
180
  
 
181
  Uint32 getBackupVersion() const { 
 
182
    return backupVersion;
 
183
  }
 
184
  
 
185
  const char * getTableName() const { 
 
186
    return m_dictTable->getName();
 
187
  }
 
188
  
 
189
  int getNoOfAttributes() const { 
 
190
    return allAttributesDesc.size();
 
191
  };
 
192
  
 
193
  bool have_auto_inc() const {
 
194
    return m_auto_val_id != ~(Uint32)0;
 
195
  };
 
196
 
 
197
  bool have_auto_inc(Uint32 id) const {
 
198
    return m_auto_val_id == id;
 
199
  };
 
200
 
 
201
  Uint64 get_max_auto_val() const {
 
202
    return m_max_auto_val;
 
203
  };
 
204
 
 
205
  void update_max_auto_val(const char *data, int size) {
 
206
    union {
 
207
      Uint8  u8;
 
208
      Uint16 u16;
 
209
      Uint32 u32;
 
210
    } val;
 
211
    Uint64 v;
 
212
    switch(size){
 
213
    case 64:
 
214
      memcpy(&v,data,8);
 
215
      break;
 
216
    case 32:
 
217
      memcpy(&val.u32,data,4);
 
218
      v= val.u32;
 
219
      break;
 
220
    case 24:
 
221
      v= uint3korr((unsigned char*)data);
 
222
      break;
 
223
    case 16:
 
224
      memcpy(&val.u16,data,2);
 
225
      v= val.u16;
 
226
      break;
 
227
    case 8:
 
228
      memcpy(&val.u8,data,1);
 
229
      v= val.u8;
 
230
      break;
 
231
    default:
 
232
      return;
 
233
    };
 
234
    if(v > m_max_auto_val)
 
235
      m_max_auto_val= v;
 
236
  };
 
237
  /**
 
238
   * Get attribute descriptor
 
239
   */
 
240
  const AttributeDesc * operator[](int attributeId) const { 
 
241
    return allAttributesDesc[attributeId]; 
 
242
  }
 
243
 
 
244
  bool getSysTable() const {
 
245
    return isSysTable;
 
246
  }
 
247
 
 
248
  const TableS *getMainTable() const {
 
249
    return m_main_table;
 
250
  }
 
251
 
 
252
  TableS& operator=(TableS& org) ; 
 
253
}; // TableS;
 
254
 
 
255
class RestoreLogIterator;
 
256
 
 
257
class BackupFile {
 
258
protected:
 
259
  FILE * m_file;
 
260
  char m_path[PATH_MAX];
 
261
  char m_fileName[PATH_MAX];
 
262
  bool m_hostByteOrder;
 
263
  BackupFormat::FileHeader m_fileHeader;
 
264
  BackupFormat::FileHeader m_expectedFileHeader;
 
265
  
 
266
  Uint32 m_nodeId;
 
267
  
 
268
  void * m_buffer;
 
269
  void * m_buffer_ptr;
 
270
  Uint32 m_buffer_sz;
 
271
  Uint32 m_buffer_data_left;
 
272
  void (* free_data_callback)();
 
273
 
 
274
  bool openFile();
 
275
  void setCtlFile(Uint32 nodeId, Uint32 backupId, const char * path);
 
276
  void setDataFile(const BackupFile & bf, Uint32 no);
 
277
  void setLogFile(const BackupFile & bf, Uint32 no);
 
278
  
 
279
  Uint32 buffer_get_ptr(void **p_buf_ptr, Uint32 size, Uint32 nmemb);
 
280
  Uint32 buffer_read(void *ptr, Uint32 size, Uint32 nmemb);
 
281
  Uint32 buffer_get_ptr_ahead(void **p_buf_ptr, Uint32 size, Uint32 nmemb);
 
282
  Uint32 buffer_read_ahead(void *ptr, Uint32 size, Uint32 nmemb);
 
283
 
 
284
  void setName(const char * path, const char * name);
 
285
 
 
286
  BackupFile(void (* free_data_callback)() = 0);
 
287
  ~BackupFile();
 
288
public:
 
289
  bool readHeader();
 
290
  bool validateFooter();
 
291
 
 
292
  const char * getPath() const { return m_path;}
 
293
  const char * getFilename() const { return m_fileName;}
 
294
  Uint32 getNodeId() const { return m_nodeId;}
 
295
  const BackupFormat::FileHeader & getFileHeader() const { return m_fileHeader;}
 
296
  bool Twiddle(const AttributeDesc *  attr_desc, AttributeData * attr_data, Uint32 arraySize = 0);
 
297
};
 
298
 
 
299
struct DictObject {
 
300
  Uint32 m_objType;
 
301
  void * m_objPtr;
 
302
};
 
303
 
 
304
class RestoreMetaData : public BackupFile {
 
305
 
 
306
  Vector<TableS *> allTables;
 
307
  bool readMetaFileHeader();
 
308
  bool readMetaTableDesc();
 
309
  bool markSysTables();
 
310
                
 
311
  bool readGCPEntry();
 
312
  bool readFragmentInfo();
 
313
  Uint32 readMetaTableList();
 
314
 
 
315
  Uint32 m_startGCP;
 
316
  Uint32 m_stopGCP;
 
317
  
 
318
  bool parseTableDescriptor(const Uint32 * data, Uint32 len);
 
319
 
 
320
  Vector<DictObject> m_objects;
 
321
  
 
322
public:
 
323
  RestoreMetaData(const char * path, Uint32 nodeId, Uint32 bNo);
 
324
  virtual ~RestoreMetaData();
 
325
  
 
326
  int loadContent();
 
327
                  
 
328
  Uint32 getNoOfTables() const { return allTables.size();}
 
329
  
 
330
  const TableS * operator[](int i) const { return allTables[i];}
 
331
  TableS * getTable(Uint32 tableId) const;
 
332
 
 
333
  Uint32 getNoOfObjects() const { return m_objects.size();}
 
334
  Uint32 getObjType(Uint32 i) const { return m_objects[i].m_objType; }
 
335
  void* getObjPtr(Uint32 i) const { return m_objects[i].m_objPtr; }
 
336
  
 
337
  Uint32 getStopGCP() const;
 
338
}; // RestoreMetaData
 
339
 
 
340
 
 
341
class RestoreDataIterator : public BackupFile {
 
342
  const RestoreMetaData & m_metaData;
 
343
  Uint32 m_count;
 
344
  TableS* m_currentTable;
 
345
  TupleS m_tuple;
 
346
 
 
347
public:
 
348
 
 
349
  // Constructor
 
350
  RestoreDataIterator(const RestoreMetaData &, void (* free_data_callback)());
 
351
  ~RestoreDataIterator() {};
 
352
  
 
353
  // Read data file fragment header
 
354
  bool readFragmentHeader(int & res, Uint32 *fragmentId);
 
355
  bool validateFragmentFooter();
 
356
 
 
357
  const TupleS *getNextTuple(int & res);
 
358
 
 
359
private:
 
360
 
 
361
  int readTupleData(Uint32 *buf_ptr, Uint32 *ptr, Uint32 dataLength);
 
362
};
 
363
 
 
364
class LogEntry {
 
365
public:
 
366
  enum EntryType {
 
367
    LE_INSERT,
 
368
    LE_DELETE,
 
369
    LE_UPDATE
 
370
  };
 
371
  Uint32 m_frag_id;
 
372
  EntryType m_type;
 
373
  TableS * m_table;
 
374
  Vector<AttributeS*> m_values;
 
375
  Vector<AttributeS*> m_values_e;
 
376
  AttributeS *add_attr() {
 
377
    AttributeS * attr;
 
378
    if (m_values_e.size() > 0) {
 
379
      attr = m_values_e[m_values_e.size()-1];
 
380
      m_values_e.erase(m_values_e.size()-1);
 
381
    }
 
382
    else
 
383
    {
 
384
      attr = new AttributeS;
 
385
    }
 
386
    m_values.push_back(attr);
 
387
    return attr;
 
388
  }
 
389
  void clear() {
 
390
    for(Uint32 i= 0; i < m_values.size(); i++)
 
391
      m_values_e.push_back(m_values[i]);
 
392
    m_values.clear();
 
393
  }
 
394
  LogEntry() {}
 
395
  ~LogEntry()
 
396
  {
 
397
    Uint32 i;
 
398
    for(i= 0; i< m_values.size(); i++)
 
399
      delete m_values[i];
 
400
    for(i= 0; i< m_values_e.size(); i++)
 
401
      delete m_values_e[i];
 
402
  }
 
403
  Uint32 size() const { return m_values.size(); }
 
404
  const AttributeS * operator[](int i) const { return m_values[i];}
 
405
};
 
406
 
 
407
class RestoreLogIterator : public BackupFile {
 
408
private:
 
409
  const RestoreMetaData & m_metaData;
 
410
 
 
411
  Uint32 m_count;  
 
412
  Uint32 m_last_gci;
 
413
  LogEntry m_logEntry;
 
414
public:
 
415
  RestoreLogIterator(const RestoreMetaData &);
 
416
  virtual ~RestoreLogIterator() {};
 
417
 
 
418
  const LogEntry * getNextLogEntry(int & res);
 
419
};
 
420
 
 
421
NdbOut& operator<<(NdbOut& ndbout, const TableS&);
 
422
NdbOut& operator<<(NdbOut& ndbout, const TupleS&);
 
423
NdbOut& operator<<(NdbOut& ndbout, const LogEntry&);
 
424
NdbOut& operator<<(NdbOut& ndbout, const RestoreMetaData&);
 
425
 
 
426
#endif
 
427
 
 
428