~vadim-tk/percona-server/flushing-algo

« back to all changes in this revision

Viewing changes to storage/ndb/test/include/NdbSchemaOp.hpp

  • Committer: root
  • Date: 2011-10-29 01:34:40 UTC
  • Revision ID: root@hppro1.office.percona.com-20111029013440-qhnf4jk8kdjcf4e0
Initial import

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 NdbSchemaOp_H
 
17
#define NdbSchemaOp_H
 
18
 
 
19
#include <NdbDictionary.hpp>
 
20
 
 
21
#ifndef DOXYGEN_SHOULD_SKIP_DEPRECATED
 
22
 
 
23
  /**
 
24
   * Type of attribute
 
25
   *
 
26
   * NOTE! AttrType is deprecated, use NdbDictionary::Column::Type instead!
 
27
   */
 
28
  enum AttrType { 
 
29
    Signed,           ///< Attributes of this type can be read with:
 
30
                      ///< NdbRecAttr::int64_value, 
 
31
                      ///< NdbRecAttr::int32_value,
 
32
                      ///< NdbRecAttr::short_value, 
 
33
                      ///< NdbRecAttr::char_value
 
34
    UnSigned,         ///< Attributes of this type can be read with:
 
35
                      ///< NdbRecAttr::u_64_value, 
 
36
                      ///< NdbRecAttr::u_32_value,
 
37
                      ///< NdbRecAttr::u_short_value, 
 
38
                      ///< NdbRecAttr::u_char_value
 
39
    Float,            ///< Attributes of this type can be read with:
 
40
                      ///< NdbRecAttr::float_value and 
 
41
                      ///< NdbRecAttr::double_value
 
42
    String,           ///< Attributes of this type can be read with:
 
43
                      ///< NdbRecAttr::aRef, 
 
44
                      ///< NdbRecAttr::getAttributeObject
 
45
    NoAttrTypeDef     ///< Used for debugging only
 
46
  };
 
47
 
 
48
 
 
49
  /**
 
50
   * @deprecated
 
51
   */
 
52
  enum NullAttributeType { 
 
53
    NoNullTypeDefined = -1,
 
54
    NotNullAttribute, 
 
55
    NullAttribute,
 
56
    AttributeDefined 
 
57
  };
 
58
  /**
 
59
   * Indicates whether the attribute is part of a primary key or not
 
60
   */
 
61
  enum KeyType { 
 
62
    Undefined = -1,               ///< Used for debugging only
 
63
    NoKey,                        ///< Attribute is not part of primary key 
 
64
                                  ///< or tuple identity
 
65
    TupleKey,                     ///< Attribute is part of primary key
 
66
    TupleId                       ///< Attribute is part of tuple identity 
 
67
                                  ///< (This type of attribute is created 
 
68
                                  ///< internally, and should not be 
 
69
                                  ///< manually created.)
 
70
  };
 
71
  /**
 
72
   * Indicate whether the attribute should be stored on disk or not
 
73
   * Only for legacy createAttribute().
 
74
   */
 
75
  enum StorageMode { 
 
76
    MMBased = NDB_STORAGETYPE_MEMORY,
 
77
    DiskBased = NDB_STORAGETYPE_DISK
 
78
  };
 
79
  
 
80
  /**
 
81
   *  Type of fragmentation used for a table
 
82
   */
 
83
  enum FragmentType { 
 
84
    Default = 0,                  ///<  (All is default!)
 
85
    Single = 1,                   ///< Only one fragment
 
86
    All = 2,                      ///< Default value.  One fragment per node group
 
87
    DistributionGroup = 3,        ///< Distribution Group used for fragmentation.
 
88
                                  ///< One fragment per node group
 
89
    DistributionKey = 4,          ///< Distribution Key used for fragmentation.
 
90
                                  ///< One fragment per node group.
 
91
    AllLarge = 5,                 ///< Sixten fragments per node group.
 
92
    DGroupLarge = 6,              ///< Distribution Group used for fragmentation.
 
93
                                  ///< Sixten fragments per node group
 
94
    DKeyLarge = 7                 ///< Distribution Key used for fragmentation.
 
95
                                  ///< Sixten fragments per node group
 
96
  };
 
97
  
 
98
  /**
 
99
   *  Type of table or index.
 
100
   */
 
101
  enum TableType {
 
102
    UndefTableType = 0,
 
103
    SystemTable = 1,              ///< Internal.Table cannot be updated by user
 
104
    UserTable = 2,                  ///< Normal application table
 
105
    UniqueHashIndex = 3,          ///< Unique un-ordered hash index
 
106
    HashIndex = 4,                ///< Non-unique un-ordered hash index
 
107
    UniqueOrderedIndex = 5,       ///< Unique ordered index
 
108
    OrderedIndex = 6              ///< Non-unique ordered index
 
109
  };
 
110
 
 
111
 
 
112
class NdbSchemaCon;
 
113
class Ndb;
 
114
  
 
115
 
 
116
/** 
 
117
 * @class NdbSchemaOp
 
118
 * @brief Represents various operations for use in schema transactions
 
119
 *
 
120
 * This class is used for schema operations, e.g. creating tables and
 
121
 * attributes.
 
122
 *
 
123
 * The NdbSchemaOp object is created using NdbSchemaCon::getNdbSchemaOp.
 
124
 * 
 
125
 * @note  This class is depricated and is now replaced with the class
 
126
 *        NdbDictionary.
 
127
 */
 
128
class NdbSchemaOp 
 
129
{
 
130
  friend class Ndb;
 
131
  friend class NdbSchemaCon;
 
132
 
 
133
public:
 
134
 
 
135
  
 
136
  /**
 
137
   * Create a new table in the database.
 
138
   * 
 
139
   * @note The NdbSchemaCon should be closed with 
 
140
   *       Ndb::closeSchemaTransaction, even if this method fails.
 
141
   *
 
142
   * @param  aTableName   Table name.  Should not be NULL.
 
143
   * @param  aTableSize   (Performance parameter.)
 
144
   *                      Initial size of the data part of the table
 
145
   *                      expressed in kByte. 
 
146
   *                      The database handles
 
147
   *                      bad parameter setting but at a certain 
 
148
   *                      loss in performance.
 
149
   *                      The size given here is
 
150
   *                      the initial size allocated for the table 
 
151
   *                      storage (the data part).
 
152
   *                      When calculating the data storage one should 
 
153
   *                      add the size of all attributes (each attribute
 
154
   *                      consumes at least 4 bytes) and also an overhead
 
155
   *                      of 12 byte. 
 
156
   *                      Variable size attributes (not supported yet)
 
157
   *                      will have a size of 12 bytes plus the actual 
 
158
   *                      data storage parts where there is an 
 
159
   *                      additional overhead based on the size of the
 
160
   *                      variable part.
 
161
   *                      <br>
 
162
   *                      An example table with 5 attributes: 
 
163
   *                      one 64 bit attribute, one 32 bit attribute, 
 
164
   *                      two 16 bit attributes and one array of 64 8 bits. 
 
165
   *                      This table will consume 
 
166
   *                        12 (overhead) + 8 + 4 + 2*4 (4 is minimum) + 64 = 
 
167
   *                        96 bytes per record.
 
168
   *                      Additionally an overhead of about 2 % as page 
 
169
   *                      headers and waste should be allocated. 
 
170
   *                      Thus, 1 million records should consume 96 MBytes
 
171
   *                      plus the overhead 2 MByte and rounded up to 
 
172
   *                      100 000 kBytes.
 
173
   *                      <br><em>
 
174
   *                      This parameter is currently not used.
 
175
   *                      </em>
 
176
   * @param  aTupleKey    Indicates if the table has a primary key or not.
 
177
   *                      <br>
 
178
   *                        <b>TupleKey</b> means that a <em>primary key</em> 
 
179
   *                        consisting of one to four attributes
 
180
   *                        (at most one of variable size) 
 
181
   *                        uniquely identifies each record in the created
 
182
   *                        table.
 
183
   *                        <br>
 
184
   *                        <b>TupleId</b> means that a <em>tuple identity</em>
 
185
   *                        is used.  The tuple identity is 
 
186
   *                        a unique key indentifying each record of the 
 
187
   *                        created table.
 
188
   *                        The tuple identity is a (non-stored)
 
189
   *                        64 bit attribute named <b>NDB$TID</b>.
 
190
   *                        <br>
 
191
   *                        When inserting a record (tuple), the method 
 
192
   *                        NdbOperation::setTupleId 
 
193
   *                        will generate a unique tuple identity
 
194
   *                        and return it to the user. 
 
195
   *                        <br>
 
196
   *                        When reading, updating or deleting a record
 
197
   *                        in a table with <b>TupleId</b>,
 
198
   *                        NdbOperation::equal("NDB$TID", value_Uint64)
 
199
   *                        can be used to identify the record.
 
200
   *                        <br>
 
201
   *                        Legal values: TupleKey or TupleId.
 
202
   * @param aNrOfPages    (Performance parameter.)
 
203
   *                      Specifies the initial size of the index storage. 
 
204
   *                      When calculating the index storage,
 
205
   *                      each key has approximately 14 byte of 
 
206
   *                      overhead plus the size of the key. 
 
207
   *                      Each key attribute takes up at least 4 bytes 
 
208
   *                      of storage. 
 
209
   *                      Thus a mixed key consisting of a 
 
210
   *                      64 bit attribute, a 32 bit attribute
 
211
   *                      and a 16 bit attribute will 
 
212
   *                      consume approx. 30 bytes per key.
 
213
   *                      Thus, the if initial size is to be 1 million rows,
 
214
   *                      then aNrOfPages should be set to 
 
215
   *                      30 M / 8k = 2670 pages.
 
216
   *                      <br><em>
 
217
   *                      This parameter is currently not used.
 
218
   *                       </em>
 
219
   * @param aFragmentType Type of fragmentation.<br>
 
220
   *                      <b>All</b> (default) means that the 
 
221
   *                      table fragments are automatically 
 
222
   *                      distributed on all nodes in the system.<br>
 
223
   *                      <b>DistributionGroup</b> and 
 
224
   *                      <b>DistributionKey</b> are 
 
225
   *                      also supported. For further details about
 
226
   *                      these types see the documentation of 
 
227
   *                      Ndb::startTransaction.
 
228
   * @param aKValue       (Hash parameter.)
 
229
   *                      Only allowed value is 6.
 
230
   *                      Later implementations might add flexibility
 
231
   *                      in this parameter.
 
232
   * @param aMinLoadFactor  (Hash parameter.)
 
233
   *                        This value specifies the load factor when 
 
234
   *                        starting to shrink the hash table. 
 
235
   *                        It must be smaller than aMaxLoadFactor.
 
236
   *                        Both these factors are given in percentage.
 
237
   * @param aMaxLoadFactor  (Hash parameter.)
 
238
   *                        This value specifies the load factor when 
 
239
   *                        starting to split the containers in the local
 
240
   *                        hash tables. 100 is the maximum which will
 
241
   *                        optimize memory usage (this is the figure 
 
242
   *                        used for the above calculations).
 
243
   *                        A lower figure will store less information in 
 
244
   *                        each container and thus
 
245
   *                        find the key faster but consume more memory.
 
246
   * @param aMemoryType     Currently only 1 is allowed which specifies 
 
247
   *                        storage of table in main memory. 
 
248
   *                        Later 2 will be added where the table is stored
 
249
   *                        completely on disk 
 
250
   *                        and 3 where the index is in main memory but
 
251
   *                        data is on disk. 
 
252
   *                        If 1 is chosen an individual attribute can
 
253
   *                        still be specified as a disk attribute.
 
254
   * @param aStoredTable    If set to false it indicates that the table is 
 
255
   *                        a temporary table and should not be logged 
 
256
   *                        to disk.
 
257
   *                        In case of a system restart the table will still
 
258
   *                        be defined and exist but will be empty. 
 
259
   *                        Thus no checkpointing and
 
260
   *                        no logging is performed on the table.
 
261
   *                        The default value is true and indicates a 
 
262
   *                        normal table with full checkpointing and 
 
263
   *                        logging activated.
 
264
   * @return                Returns 0 when successful and returns -1 otherwise.
 
265
   */
 
266
  int           createTable(    const char* aTableName, 
 
267
                                Uint32 aTableSize = 8, 
 
268
                                KeyType aTupleKey = TupleKey,
 
269
                                int aNrOfPages = 2, 
 
270
                                FragmentType aFragmentType = All, 
 
271
                                int aKValue = 6,
 
272
                                int aMinLoadFactor = 78,
 
273
                                int aMaxLoadFactor = 80,
 
274
                                int aMemoryType = 1,
 
275
                                bool aStoredTable = true);
 
276
 
 
277
  /** 
 
278
   * This is the old function declaration, don't use.
 
279
   *
 
280
   * @deprecated do not use!
 
281
   */
 
282
#ifndef NDB_WIN32
 
283
  inline int    createTable(    const char* aTableName, 
 
284
                                Uint32 aTableSize, 
 
285
                                KeyType aTupleKey,
 
286
                                int aNrOfPages, 
 
287
                                FragmentType aFragmentType, 
 
288
                                int aKValue,
 
289
                                int aMinLoadFactor,
 
290
                                int aMaxLoadFactor,
 
291
                                int aMemoryType,
 
292
                                int aStoredTable){
 
293
    return createTable(aTableName, 
 
294
                       aTableSize, 
 
295
                       aTupleKey,                        
 
296
                       aNrOfPages, 
 
297
                       aFragmentType, 
 
298
                       aKValue, 
 
299
                       aMinLoadFactor, 
 
300
                       aMaxLoadFactor, 
 
301
                       aMemoryType, 
 
302
                       (aStoredTable == 1 ? true : false));
 
303
  }
 
304
#endif
 
305
 
 
306
  /**
 
307
   * Add a new attribute to a database table.
 
308
   *
 
309
   * Attributes can only be added to a table in the same transaction
 
310
   * as the transaction creating the table.
 
311
   *
 
312
   * @note The NdbSchemaCon transaction should be closed with 
 
313
   *       Ndb::closeSchemaTransaction, even if this method fails.
 
314
   *
 
315
   * Example creating an unsigned int attribute belonging to the primary key
 
316
   * of the table it is created in:
 
317
   * @code 
 
318
   *   MySchemaOp->createAttribute("Attr1",   // Attribute name
 
319
   *                               TupleKey,  // Belongs to primary key
 
320
   *                               32,        // 32 bits
 
321
   *                               1,         // Not an array attribute
 
322
   *                               UnSigned,  // Unsigned type
 
323
   *                              );
 
324
   * @endcode 
 
325
   * 
 
326
   * Example creating a string attribute belonging to the primary key
 
327
   * of the table it is created in:
 
328
   * @code
 
329
   *   MySchemaOp->createAttribute("Attr1",       // Attribute name
 
330
   *                               TupleKey,      // Belongs to primary key
 
331
   *                               8,             // Each character is 8 bits
 
332
   *                               12,            // Max 12 chars in string
 
333
   *                               String,        // Attribute if of type string
 
334
   *                              );
 
335
   * @endcode
 
336
   *
 
337
   * A <em>distribution key</em> is a set of attributes which are used
 
338
   * to distribute the tuples onto the NDB nodes.
 
339
   * A <em>distribution group</em> is a part (currently 16 bits) 
 
340
   * of an attribute used to distribute the tuples onto the NDB nodes.
 
341
   * The distribution key uses the NDB Cluster hashing function,
 
342
   * while the distribution group uses a simpler function.
 
343
   *
 
344
   * @param  aAttrName   Attribute name.  Should not be NULL.
 
345
   * @param  aTupleKey   This parameter specifies whether the 
 
346
   *                     attribute is part of the primary key or not.
 
347
   *                     Floats are not allowed in the primary key.
 
348
   *                     <br>
 
349
   *                     Legal values: NoKey, TupleKey
 
350
   * @param  aAttrSize   Specifies the size of the elements of the 
 
351
   *                     attribute.  (An attribute can consist
 
352
   *                     of an array of elements.)
 
353
   *                     <br>
 
354
   *                     Legal values: 8, 16, 32, 64 and 128 bits.
 
355
   * @param  aArraySize  Size of array.
 
356
   *                     <br>
 
357
   *                     Legal values:
 
358
   *                     0 = variable-sized array, 
 
359
   *                     1 = no array, and
 
360
   *                     2- = fixed size array.
 
361
   *                     <br>
 
362
   *                     <em>
 
363
   *                     Variable-sized array attributes are 
 
364
   *                     not yet supported.
 
365
   *                     </em>
 
366
   *                     <br>
 
367
   *                     There is no upper limit of the array size
 
368
   *                     for a single attribute. 
 
369
   * @param  aAttrType   The attribute type.
 
370
   *                     This is only of interest if calculations are 
 
371
   *                     made within NDB.
 
372
   *                     <br>
 
373
   *                     Legal values: UnSigned, Signed, Float, String
 
374
   * @param aStorageMode    Main memory based or disk based attribute.<br>
 
375
   *                     Legal values: MMBased, DiskBased
 
376
   *                     <br>
 
377
   *                     <em>
 
378
   *                     Disk-based attributes are not yet supported.
 
379
   *                     </em>
 
380
   * @param nullable     Set to true if NULL is a correct value for
 
381
   *                     the attribute.
 
382
   *                     <br>
 
383
   *                     Legal values: true, false
 
384
   * @param aStType      Obsolete since wl-2066
 
385
   * @param aDistributionKey    Sometimes it is preferable to use a subset
 
386
   *                            of the primary key as the distribution key. 
 
387
   *                            An example is TPC-C where it might be
 
388
   *                            good to use the warehouse id and district id 
 
389
   *                            as the distribution key. 
 
390
   *                            <br>
 
391
   *                            Locally in the fragments the full primary key 
 
392
   *                            will still be used with the hashing algorithm.
 
393
   *                            Set to 1 if this attribute is part of the 
 
394
   *                            distribution key.
 
395
   *                            All distribution key attributes must be 
 
396
   *                            defined before
 
397
   *                            any other attributes are defined.
 
398
   * @param aDistributionGroup    In other applications it is desirable to use 
 
399
   *                              only a part of an attribute to create the 
 
400
   *                              distribution key.
 
401
   *                              This is applicable for some telecom
 
402
   *                              applications.
 
403
   *                              <br>
 
404
   *                              In these situations one must provide how many 
 
405
   *                              bits of the attribute that is to
 
406
   *                              be used as the distribution hash value.
 
407
   *                              <br>
 
408
   *                              This provides some control to the
 
409
   *                              application of the distribution. 
 
410
   *                              It still needs to be part of a primary key
 
411
   *                              the attribute and must be defined as the 
 
412
   *                              first attribute.
 
413
   * @param  aDistributionGroupNoOfBits
 
414
   *                              Number of bits to use of the 
 
415
   *                              distribution group attribute in the
 
416
   *                              distribution hash value.
 
417
   *                              <br>
 
418
   *                              Currently, only 16 bits is supported. It will
 
419
   *                              always be the last 16 bits in the attribute
 
420
   *                              which is used for the distribution group.
 
421
   * @param aAutoIncrement        Set to autoincrement attribute.
 
422
   * @param aDefaultValue         Set a default value of attribute.
 
423
   *
 
424
   * @return Returns 0 when successful and returns -1 otherwise.
 
425
   ****************************************************************************/
 
426
  int createAttribute(const char* aAttrName,
 
427
                      KeyType aTupleKey = NoKey,
 
428
                      int aAttrSize = 32,
 
429
                      int aArraySize = 1,
 
430
                      AttrType aAttrType = UnSigned,
 
431
                      StorageMode aStorageMode = MMBased,
 
432
                      bool nullable = false,
 
433
                      int aStType= 0, // obsolete
 
434
                      int aDistributionKey = 0,
 
435
                      int aDistributionGroup = 0,
 
436
                      int aDistributionGroupNoOfBits = 16,
 
437
                      bool aAutoIncrement = false,
 
438
                      const char* aDefaultValue = 0);
 
439
 
 
440
  /**
 
441
   * @deprecated do not use!
 
442
   */
 
443
  int createAttribute(const char* aAttrName,
 
444
                      KeyType aTupleKey,
 
445
                      int aAttrSize,
 
446
                      int aArraySize,
 
447
                      AttrType aAttrType,
 
448
                      StorageMode aStorageMode,
 
449
                      NullAttributeType aNullAttr,
 
450
                      int aStType, // obsolete
 
451
                      int aDistributionKey = 0,
 
452
                      int aDistributionGroup = 0,
 
453
                      int aDistributionGroupNoOfBits = 16){
 
454
    return createAttribute(aAttrName,
 
455
                           aTupleKey,
 
456
                           aAttrSize,
 
457
                           aArraySize,
 
458
                           aAttrType,
 
459
                           aStorageMode,
 
460
                           aNullAttr == NullAttribute,
 
461
                           aStType,
 
462
                           aDistributionKey,
 
463
                           aDistributionGroup,
 
464
                           aDistributionGroupNoOfBits);
 
465
  }
 
466
 
 
467
  const NdbError & getNdbError() const;
 
468
 
 
469
protected:
 
470
 
 
471
/*****************************************************************************
 
472
 *   These are the methods used to create and delete the NdbOperation objects.
 
473
 ****************************************************************************/
 
474
                        NdbSchemaOp(Ndb* aNdb);
 
475
 
 
476
                        ~NdbSchemaOp();     
 
477
 
 
478
/******************************************************************************
 
479
 *      These methods are service routines used by the other NDBAPI classes.
 
480
 *****************************************************************************/
 
481
 
 
482
  void                  release();      // Release all memory connected
 
483
                                              // to the operations object.          
 
484
 
 
485
/****************************************************************************
 
486
 *      The methods below is the execution part of the NdbSchemaOp class. 
 
487
 *****************************************************************************/
 
488
 
 
489
  int sendRec();        
 
490
  int sendSignals(Uint32 aNodeId, bool HaveMutex);
 
491
 
 
492
  int init(NdbSchemaCon* aSchemaCon);
 
493
 
 
494
  /**************************************************************************
 
495
   * These are the private variables that are defined in the operation 
 
496
   * objects.
 
497
   **************************************************************************/
 
498
  Ndb*                  theNdb;         // Point back to the Ndb object.      
 
499
  NdbSchemaCon*         theSchemaCon;   // Point back to the connection object.
 
500
  
 
501
 
 
502
  class NdbDictionary::Table * m_currentTable;
 
503
};
 
504
 
 
505
 
 
506
/**
 
507
 * Get old attribute type from new type
 
508
 * 
 
509
 * NOTE! attrType is deprecated, use getType instead!
 
510
 *
 
511
 * @return Type of attribute: { Signed, UnSigned, Float,a String }
 
512
 */
 
513
inline 
 
514
AttrType 
 
515
convertColumnTypeToAttrType(NdbDictionary::Column::Type _type)
 
516
{      
 
517
  
 
518
  switch(_type){
 
519
  case NdbDictionary::Column::Bigint:
 
520
  case NdbDictionary::Column::Int:
 
521
    return Signed;
 
522
  case NdbDictionary::Column::Bigunsigned:
 
523
  case NdbDictionary::Column::Unsigned:
 
524
    return UnSigned;
 
525
  case NdbDictionary::Column::Float:
 
526
  case NdbDictionary::Column::Olddecimal:
 
527
  case NdbDictionary::Column::Olddecimalunsigned:
 
528
  case NdbDictionary::Column::Decimal:
 
529
  case NdbDictionary::Column::Decimalunsigned:
 
530
  case NdbDictionary::Column::Double:
 
531
    return Float;
 
532
  case NdbDictionary::Column::Char:
 
533
  case NdbDictionary::Column::Varchar:
 
534
  case NdbDictionary::Column::Binary:
 
535
  case NdbDictionary::Column::Varbinary:
 
536
    return String;
 
537
  default:
 
538
    return NoAttrTypeDef;
 
539
  }
 
540
}
 
541
#endif
 
542
 
 
543
#endif
 
544
 
 
545