2
Copyright (C) 2003-2007 MySQL AB
3
All rights reserved. Use is subject to license terms.
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
/* ***************************************************
22
Initialise benchmark for Ronja Database
23
* *************************************************** */
26
#include "NdbSchemaCon.hpp"
29
#include <NdbTest.hpp>
35
#define NDB_MAXTHREADS 256
37
NDB_MAXTHREADS used to be just MAXTHREADS, which collides with a
38
#define from <sys/thread.h> on AIX (IBM compiler). We explicitly
39
#undef it here lest someone use it by habit and get really funny
40
results. K&R says we may #undef non-existent symbols, so let's go.
43
#define MAXATTRSIZE 8000
45
static unsigned int tNoOfRecords;
46
static unsigned int tNoOfLoops;
47
static unsigned int tNoOfTables;
48
static int tAttributeSize;
50
static unsigned int tValue;
51
static unsigned int tNoOfOperations;
52
static char tableName[MAXTABLES][MAXSTRLEN];
53
static char attrName[MAXATTR][MAXSTRLEN];
55
inline int InsertRecords(Ndb*, int) ;
57
NDB_COMMAND(initronja, "initronja", "initronja", "initronja", 65535){
61
NdbSchemaCon *MySchemaTransaction = NULL ;
62
NdbSchemaOp *MySchemaOp = NULL ;
65
int check, status, i, j, cont ;
66
check = status = i = j = cont = 0 ;
68
tNoOfLoops = tNoOfRecords / 10;
73
if (strcmp(argv[i], "-r") == 0){
74
if( NULL == argv[i+1] ) goto error_input ;
75
tNoOfRecords = atoi(argv[i+1]);
76
tNoOfRecords = tNoOfRecords - (tNoOfRecords % 10);
77
tNoOfLoops = tNoOfRecords / 10;
78
if ((tNoOfRecords < 1) || (tNoOfRecords > 1000000000)) goto error_input;
87
pNdb = new Ndb( "TEST_DB" ) ;
88
ndbout << "Initialisation started. " << endl;
90
ndbout << "Initialisation completed. " << endl;
92
tNodeId = pNdb->getNodeId();
93
ndbout << endl << "Initial loading of Ronja Database" << endl;
94
ndbout << " NdbAPI node with id = " << tNodeId << endl;
96
if (pNdb->waitUntilReady(30) != 0) {
97
ndbout << "Benchmark failed - NDB is not ready" << endl;
99
return NDBT_ProgramExit(NDBT_FAILED) ;
102
ndbout << endl << "Creating the table SHORT_REC" << "..." << endl;
104
MySchemaTransaction = NdbSchemaCon::startSchemaTrans(pNdb);
105
if(!MySchemaTransaction) goto error_handler;
106
MySchemaOp = MySchemaTransaction->getNdbSchemaOp();
107
if(!MySchemaOp) goto error_handler;
108
check = MySchemaOp->createTable( "SHORT_REC"
110
,TupleKey // Key Type
113
if (check == -1) goto error_handler;
115
ndbout << "Key attribute..." ;
116
check = MySchemaOp->createAttribute( (char*)"Key", TupleKey, 32, 1,
117
UnSigned, MMBased, NotNullAttribute );
118
if (check == -1) goto error_handler;
119
ndbout << "\t\tOK" << endl ;
121
ndbout << "Flip attribute..." ;
122
check = MySchemaOp->createAttribute("Flip", NoKey, 32, 1,
123
UnSigned, MMBased, NotNullAttribute );
124
if (check == -1) goto error_handler;
125
ndbout << "\t\tOK" << endl ;
127
ndbout << "Count attribute..." ;
128
check = MySchemaOp->createAttribute("Count", NoKey, 32, 1,
129
UnSigned, MMBased, NotNullAttribute );
130
if (check == -1) goto error_handler;
131
ndbout << "\t\tOK" << endl ;
133
ndbout << "Placeholder attribute..." ;
134
check = MySchemaOp->createAttribute("Placeholder", NoKey, 8, 90,
135
UnSigned, MMBased, NotNullAttribute );
136
if (check == -1) goto error_handler;
137
ndbout << "\tOK" << endl ;
139
if (MySchemaTransaction->execute() == -1) {
140
if(721 == MySchemaOp->getNdbError().code){
141
ndbout << "Table SHORT_REC already exists" << endl ;
143
ndbout << MySchemaTransaction->getNdbError() << endl;
146
ndbout << "SHORT_REC created " << endl;
149
NdbSchemaCon::closeSchemaTrans(MySchemaTransaction);
151
ndbout << endl << "Creating the table LONG_REC..." << endl;
153
MySchemaTransaction = NdbSchemaCon::startSchemaTrans(pNdb);
154
if(!MySchemaTransaction) goto error_handler;
156
MySchemaOp = MySchemaTransaction->getNdbSchemaOp();
157
if(!MySchemaOp) goto error_handler;
158
check = MySchemaOp->createTable( "LONG_REC"
160
,TupleKey // Key Type
164
if (check == -1) goto error_handler;
166
ndbout << "Key attribute..." ;
167
check = MySchemaOp->createAttribute( (char*)"Key", TupleKey, 32, 1,
168
UnSigned, MMBased, NotNullAttribute );
169
if (check == -1) goto error_handler;
170
ndbout << "\t\tOK" << endl ;
172
ndbout << "Flip attribute..." ;
173
check = MySchemaOp->createAttribute("Flip", NoKey, 32, 1,
174
UnSigned, MMBased, NotNullAttribute );
175
if (check == -1) goto error_handler;
176
ndbout << "\t\tOK" << endl ;
178
ndbout << "Count attribute..." ;
179
check = MySchemaOp->createAttribute("Count", NoKey, 32, 1,
180
UnSigned, MMBased, NotNullAttribute );
181
if (check == -1) goto error_handler;
182
ndbout << "\t\tOK" << endl ;
184
ndbout << "Placeholder attribute..." ;
185
check = MySchemaOp->createAttribute("Placeholder", NoKey, 8, 1014,
186
UnSigned, MMBased, NotNullAttribute );
187
if (check == -1) goto error_handler;
188
ndbout << "\tOK" << endl ;
190
if (MySchemaTransaction->execute() == -1) {
191
if(721 == MySchemaOp->getNdbError().code){
192
ndbout << "Table LONG_REC already exists" << endl ;
194
ndbout << MySchemaTransaction->getNdbError() << endl;
197
ndbout << "LONG_REC created" << endl;
200
NdbSchemaCon::closeSchemaTrans(MySchemaTransaction);
203
check = InsertRecords(pNdb, tNoOfRecords);
208
ndbout << endl << "Initial loading of Ronja Database failed" << endl;
209
return NDBT_ProgramExit(NDBT_FAILED) ;
211
ndbout << endl << "Initial loading of Ronja Database completed" << endl;
212
return NDBT_ProgramExit(NDBT_OK) ;
220
ndbout << "SchemaTransaction returned error:" ;
221
ndbout << MySchemaTransaction->getNdbError() << endl;
222
NdbSchemaCon::closeSchemaTrans(MySchemaTransaction);
224
NDBT_ProgramExit(NDBT_FAILED) ;
228
ndbout << endl << " Ivalid parameter(s)" << endl;
229
ndbout << " Usage: initronja [-r n] , where 'n' is the number of records to be inserted" << endl;
230
ndbout << " If omitted, 500 records will be created by default" << endl;
231
ndbout << " Note: use this number in combination with '-r' argument when running 'benchronja'" << endl << endl;
232
NDBT_ProgramExit(NDBT_WRONGARGS) ;
235
////////////////////////////////////////
237
inline int InsertRecords(Ndb* pNdb, int nNoRecords){
239
NdbConnection *MyTransaction = NULL ;
240
NdbOperation* MyOperation[10];
243
int loop_count_ops = 2 * tNoOfLoops;
244
int loop_count_tables = 10;
245
int loop_count_attributes = 0 ;
248
int count_tables = 0;
249
int count_attributes = 0 ;
252
unsigned int attrValue[1000];
253
unsigned int setAttrValue = 0;
254
unsigned int keyValue[3];
256
for (i = 0; i < 1000; i ++) attrValue[i] = 1;
258
for (count=0 ; count < loop_count_ops ; count++){
259
if ((((count / 100)* 100) == count) && (count != 0)){
260
ndbout << "1000 records inserted again, " << (count/100) << "000 records now inserted" << endl;
263
MyTransaction = pNdb->startTransaction();
265
ndbout << "startTransaction: " << pNdb->getNdbError();
266
ndbout << " count = " << count << endl;
270
for (count_tables = 0; count_tables < loop_count_tables; count_tables++) {
271
if (count < tNoOfLoops) {
272
keyValue[0] = count*10 + count_tables ;
273
MyOperation[count_tables] = MyTransaction->getNdbOperation("SHORT_REC") ;
275
keyValue[0] = (count - tNoOfLoops)*10 + count_tables;
276
MyOperation[count_tables] = MyTransaction->getNdbOperation("LONG_REC");
279
if (!MyOperation[count_tables]) goto error_handler1;
281
check = MyOperation[count_tables]->insertTuple();
282
if (check == -1) goto error_handler2;
284
check = MyOperation[count_tables]->equal("Key",(char*)&keyValue[0]);
285
if (check == -1) goto error_handler4;
287
check = MyOperation[count_tables]->setValue("Flip",(char*)&setAttrValue);
288
if (check == -1) goto error_handler5;
290
check = MyOperation[count_tables]->setValue("Count",(char*)&setAttrValue);
291
if (check == -1) goto error_handler5;
293
check = MyOperation[count_tables]->setValue("Placeholder",(char*)&attrValue[0]);
294
if (check == -1) goto error_handler5;
297
if (MyTransaction->execute( Commit ) == -1){
298
ndbout << MyTransaction->getNdbError()<< endl ;
299
ndbout << "count = " << count << endl;
302
pNdb->closeTransaction(MyTransaction) ;
307
ndbout << "Error occured in getNdbOperation " << endl;
308
ndbout << MyTransaction->getNdbError() << endl;
309
pNdb->closeTransaction(MyTransaction);
313
ndbout << "Error occured in defining operation " << endl;
314
ndbout << MyOperation[count_tables]->getNdbError() << endl;
315
pNdb->closeTransaction(MyTransaction);
319
pNdb->closeTransaction(MyTransaction);
323
ndbout << "Error occured in equal " << endl;
324
ndbout << MyOperation[count_tables]->getNdbError() << endl;
325
pNdb->closeTransaction(MyTransaction);
329
ndbout << "Error occured in get/setValue " << endl;
330
ndbout << MyOperation[count_tables]->getNdbError() << endl;
331
pNdb->closeTransaction(MyTransaction);