1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
/* Copyright (C) 2003 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#ifndef NdbSchemaOp_H
#define NdbSchemaOp_H
#ifndef DOXYGEN_SHOULD_SKIP_DEPRECATED
#include "NdbDictionary.hpp"
#include "AttrType.hpp"
#include "NdbSchemaCon.hpp"
class NdbApiSignal;
class Ndb;
/**
* @class NdbSchemaOp
* @brief Represents various operations for use in schema transactions
*
* This class is used for schema operations, e.g. creating tables and
* attributes.
*
* The NdbSchemaOp object is created using NdbSchemaCon::getNdbSchemaOp.
*
* @note This class is depricated and is now replaced with the class
* NdbDictionary.
*/
class NdbSchemaOp
{
friend class Ndb;
friend class NdbSchemaCon;
public:
/**
* Create a new table in the database.
*
* @note The NdbSchemaCon should be closed with
* Ndb::closeSchemaTransaction, even if this method fails.
*
* @param aTableName Table name. Should not be NULL.
* @param aTableSize (Performance parameter.)
* Initial size of the data part of the table
* expressed in kByte.
* The database handles
* bad parameter setting but at a certain
* loss in performance.
* The size given here is
* the initial size allocated for the table
* storage (the data part).
* When calculating the data storage one should
* add the size of all attributes (each attribute
* consumes at least 4 bytes) and also an overhead
* of 12 byte.
* Variable size attributes (not supported yet)
* will have a size of 12 bytes plus the actual
* data storage parts where there is an
* additional overhead based on the size of the
* variable part.
* <br>
* An example table with 5 attributes:
* one 64 bit attribute, one 32 bit attribute,
* two 16 bit attributes and one array of 64 8 bits.
* This table will consume
* 12 (overhead) + 8 + 4 + 2*4 (4 is minimum) + 64 =
* 96 bytes per record.
* Additionally an overhead of about 2 % as page
* headers and waste should be allocated.
* Thus, 1 million records should consume 96 MBytes
* plus the overhead 2 MByte and rounded up to
* 100 000 kBytes.
* <br><em>
* This parameter is currently not used.
* </em>
* @param aTupleKey Indicates if the table has a primary key or not.
* <br>
* <b>TupleKey</b> means that a <em>primary key</em>
* consisting of one to four attributes
* (at most one of variable size)
* uniquely identifies each record in the created
* table.
* <br>
* <b>TupleId</b> means that a <em>tuple identity</em>
* is used. The tuple identity is
* a unique key indentifying each record of the
* created table.
* The tuple identity is a (non-stored)
* 64 bit attribute named <b>NDB$TID</b>.
* <br>
* When inserting a record (tuple), the method
* NdbOperation::setTupleId
* will generate a unique tuple identity
* and return it to the user.
* <br>
* When reading, updating or deleting a record
* in a table with <b>TupleId</b>,
* NdbOperation::equal("NDB$TID", value_Uint64)
* can be used to identify the record.
* <br>
* Legal values: TupleKey or TupleId.
* @param aNrOfPages (Performance parameter.)
* Specifies the initial size of the index storage.
* When calculating the index storage,
* each key has approximately 14 byte of
* overhead plus the size of the key.
* Each key attribute takes up at least 4 bytes
* of storage.
* Thus a mixed key consisting of a
* 64 bit attribute, a 32 bit attribute
* and a 16 bit attribute will
* consume approx. 30 bytes per key.
* Thus, the if initial size is to be 1 million rows,
* then aNrOfPages should be set to
* 30 M / 8k = 2670 pages.
* <br><em>
* This parameter is currently not used.
* </em>
* @param aFragmentType Type of fragmentation.<br>
* <b>All</b> (default) means that the
* table fragments are automatically
* distributed on all nodes in the system.<br>
* <b>DistributionGroup</b> and
* <b>DistributionKey</b> are
* also supported. For further details about
* these types see the documentation of
* Ndb::startTransaction.
* @param aKValue (Hash parameter.)
* Only allowed value is 6.
* Later implementations might add flexibility
* in this parameter.
* @param aMinLoadFactor (Hash parameter.)
* This value specifies the load factor when
* starting to shrink the hash table.
* It must be smaller than aMaxLoadFactor.
* Both these factors are given in percentage.
* @param aMaxLoadFactor (Hash parameter.)
* This value specifies the load factor when
* starting to split the containers in the local
* hash tables. 100 is the maximum which will
* optimize memory usage (this is the figure
* used for the above calculations).
* A lower figure will store less information in
* each container and thus
* find the key faster but consume more memory.
* @param aMemoryType Currently only 1 is allowed which specifies
* storage of table in main memory.
* Later 2 will be added where the table is stored
* completely on disk
* and 3 where the index is in main memory but
* data is on disk.
* If 1 is chosen an individual attribute can
* still be specified as a disk attribute.
* @param aStoredTable If set to false it indicates that the table is
* a temporary table and should not be logged
* to disk.
* In case of a system restart the table will still
* be defined and exist but will be empty.
* Thus no checkpointing and
* no logging is performed on the table.
* The default value is true and indicates a
* normal table with full checkpointing and
* logging activated.
* @return Returns 0 when successful and returns -1 otherwise.
*/
int createTable( const char* aTableName,
Uint32 aTableSize = 8,
KeyType aTupleKey = TupleKey,
int aNrOfPages = 2,
FragmentType aFragmentType = All,
int aKValue = 6,
int aMinLoadFactor = 78,
int aMaxLoadFactor = 80,
int aMemoryType = 1,
bool aStoredTable = true);
#ifndef DOXYGEN_SHOULD_SKIP_DEPRECATED
/**
* This is the old function declaration, don't use.
*
* @deprecated do not use!
*/
inline int createTable( const char* aTableName,
Uint32 aTableSize,
KeyType aTupleKey,
int aNrOfPages,
FragmentType aFragmentType,
int aKValue,
int aMinLoadFactor,
int aMaxLoadFactor,
int aMemoryType,
int aStoredTable){
return createTable(aTableName,
aTableSize,
aTupleKey,
aNrOfPages,
aFragmentType,
aKValue,
aMinLoadFactor,
aMaxLoadFactor,
aMemoryType,
(aStoredTable == 1 ? true : false));
}
#endif
/**
* Add a new attribute to a database table.
*
* Attributes can only be added to a table in the same transaction
* as the transaction creating the table.
*
* @note The NdbSchemaCon transaction should be closed with
* Ndb::closeSchemaTransaction, even if this method fails.
*
* Example creating an unsigned int attribute belonging to the primary key
* of the table it is created in:
* @code
* MySchemaOp->createAttribute("Attr1", // Attribute name
* TupleKey, // Belongs to primary key
* 32, // 32 bits
* 1, // Not an array attribute
* UnSigned, // Unsigned type
* );
* @endcode
*
* Example creating a string attribute belonging to the primary key
* of the table it is created in:
* @code
* MySchemaOp->createAttribute("Attr1", // Attribute name
* TupleKey, // Belongs to primary key
* 8, // Each character is 8 bits
* 12, // Max 12 chars in string
* String, // Attribute if of type string
* );
* @endcode
*
* A <em>distribution key</em> is a set of attributes which are used
* to distribute the tuples onto the NDB nodes.
* A <em>distribution group</em> is a part (currently 16 bits)
* of an attribute used to distribute the tuples onto the NDB nodes.
* The distribution key uses the NDB Cluster hashing function,
* while the distribution group uses a simpler function.
*
* @param aAttrName Attribute name. Should not be NULL.
* @param aTupleKey This parameter specifies whether the
* attribute is part of the primary key or not.
* Floats are not allowed in the primary key.
* <br>
* Legal values: NoKey, TupleKey
* @param aAttrSize Specifies the size of the elements of the
* attribute. (An attribute can consist
* of an array of elements.)
* <br>
* Legal values: 8, 16, 32, 64 and 128 bits.
* @param aArraySize Size of array.
* <br>
* Legal values:
* 0 = variable-sized array,
* 1 = no array, and
* 2- = fixed size array.
* <br>
* <em>
* Variable-sized array attributes are
* not yet supported.
* </em>
* <br>
* There is no upper limit of the array size
* for a single attribute.
* @param aAttrType The attribute type.
* This is only of interest if calculations are
* made within NDB.
* <br>
* Legal values: UnSigned, Signed, Float, String
* @param aStorageMode Main memory based or disk based attribute.<br>
* Legal values: MMBased, DiskBased
* <br>
* <em>
* Disk-based attributes are not yet supported.
* </em>
* @param nullable Set to true if NULL is a correct value for
* the attribute.
* <br>
* Legal values: true, false
* @param aStType Stored in both index and data storage or
* only store in index data storage.
* <br>
* This parameter is only of interest for tuple
* key attributes.
* All tuple key attributes values are always stored
* in the index storage part.
* If this parameter is set to
* IndexStorageAttribute, then the attribute values
* will <em>only</em> be stored in the index
* storage part and <em>not</em> in the data
* storage part.
* <br>
* If there will be no scans using the primary
* key attribute and if the size of the attribute
* is large, then this might be of interest.
* A typical example is a table where
* http-addresses are used as primary key.
* <br>
* Legal values: NormalStorageAttribute,
* IndexStorageAttribute
* @param aDistributionKey Sometimes it is preferable to use a subset
* of the primary key as the distribution key.
* An example is TPC-C where it might be
* good to use the warehouse id and district id
* as the distribution key.
* <br>
* Locally in the fragments the full primary key
* will still be used with the hashing algorithm.
* Set to 1 if this attribute is part of the
* distribution key.
* All distribution key attributes must be
* defined before
* any other attributes are defined.
* @param aDistributionGroup In other applications it is desirable to use
* only a part of an attribute to create the
* distribution key.
* This is applicable for some telecom
* applications.
* <br>
* In these situations one must provide how many
* bits of the attribute that is to
* be used as the distribution hash value.
* <br>
* This provides some control to the
* application of the distribution.
* It still needs to be part of a primary key
* the attribute and must be defined as the
* first attribute.
* @param aDistributionGroupNoOfBits
* Number of bits to use of the
* distribution group attribute in the
* distribution hash value.
* <br>
* Currently, only 16 bits is supported. It will
* always be the last 16 bits in the attribute
* which is used for the distribution group.
* @param aAutoIncrement Set to autoincrement attribute.
* @param aDefaultValue Set a default value of attribute.
*
* @return Returns 0 when successful and returns -1 otherwise.
****************************************************************************/
int createAttribute(const char* aAttrName,
KeyType aTupleKey = NoKey,
int aAttrSize = 32,
int aArraySize = 1,
AttrType aAttrType = UnSigned,
StorageMode aStorageMode = MMBased,
bool nullable = false,
StorageAttributeType aStType= NormalStorageAttribute,
int aDistributionKey = 0,
int aDistributionGroup = 0,
int aDistributionGroupNoOfBits = 16,
bool aAutoIncrement = false,
const char* aDefaultValue = 0);
#ifndef DOXYGEN_SHOULD_SKIP_DEPRECATED
/**
* @deprecated do not use!
*/
int createAttribute(const char* aAttrName,
KeyType aTupleKey,
int aAttrSize,
int aArraySize,
AttrType aAttrType,
StorageMode aStorageMode,
NullAttributeType aNullAttr,
StorageAttributeType aStType = NormalStorageAttribute,
int aDistributionKey = 0,
int aDistributionGroup = 0,
int aDistributionGroupNoOfBits = 16){
return createAttribute(aAttrName,
aTupleKey,
aAttrSize,
aArraySize,
aAttrType,
aStorageMode,
aNullAttr == NullAttribute,
aStType,
aDistributionKey,
aDistributionGroup,
aDistributionGroupNoOfBits);
}
#endif
/**
* Get the last error which occurred during the transaction.
*
* If an error occured (NdbSchemaCon::execute returned -1 or
* NdbSchemaCon::getNdbSchemaOp returned NULL), then this method
* retrieves the error object containing information about
* the error.
*
* @return Error object containing information about last error.
*/
const NdbError & getNdbError() const;
protected:
/*****************************************************************************
* These are the methods used to create and delete the NdbOperation objects.
****************************************************************************/
NdbSchemaOp(Ndb* aNdb);
~NdbSchemaOp();
/******************************************************************************
* These methods are service routines used by the other NDBAPI classes.
*****************************************************************************/
void release(); // Release all memory connected
// to the operations object.
/****************************************************************************
* The methods below is the execution part of the NdbSchemaOp class.
*****************************************************************************/
int sendRec();
int sendSignals(Uint32 aNodeId, bool HaveMutex);
int init(NdbSchemaCon* aSchemaCon);
/**************************************************************************
* These are the private variables that are defined in the operation
* objects.
**************************************************************************/
Ndb* theNdb; // Point back to the Ndb object.
NdbSchemaCon* theSchemaCon; // Point back to the connection object.
class NdbDictionary::Table * m_currentTable;
};
inline
const NdbError &
NdbSchemaOp::getNdbError() const
{
return theSchemaCon->getNdbError();
}
#endif
#endif
|