~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to storage/ndb/test/ndbapi/initronja.cpp

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2003-2007 MySQL AB
 
3
    All rights reserved. Use is subject to license terms.
 
4
 
 
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.
 
8
 
 
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.
 
13
 
 
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
 
17
*/
 
18
 
 
19
 
 
20
/* ***************************************************
 
21
       INITRONJA
 
22
       Initialise benchmark for Ronja Database
 
23
 * *************************************************** */
 
24
 
 
25
#include "NdbApi.hpp"
 
26
#include "NdbSchemaCon.hpp"
 
27
#include <NdbOut.hpp>
 
28
#include <NdbMain.h>
 
29
#include <NdbTest.hpp>
 
30
#include <string.h>
 
31
 
 
32
#define MAXSTRLEN 16 
 
33
#define MAXATTR 64
 
34
#define MAXTABLES 64
 
35
#define NDB_MAXTHREADS 256
 
36
/*
 
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.
 
41
*/
 
42
#undef MAXTHREADS
 
43
#define MAXATTRSIZE 8000
 
44
 
 
45
static unsigned int tNoOfRecords;
 
46
static unsigned int tNoOfLoops;
 
47
static unsigned int tNoOfTables;
 
48
static int tAttributeSize;
 
49
static int tNodeId;
 
50
static unsigned int tValue;
 
51
static unsigned int tNoOfOperations;
 
52
static char tableName[MAXTABLES][MAXSTRLEN];
 
53
static char attrName[MAXATTR][MAXSTRLEN];
 
54
 
 
55
inline int InsertRecords(Ndb*, int) ;
 
56
 
 
57
NDB_COMMAND(initronja, "initronja", "initronja", "initronja", 65535){
 
58
  ndb_init();
 
59
 
 
60
  Ndb*                   pNdb = NULL ;
 
61
  NdbSchemaCon  *MySchemaTransaction = NULL ;
 
62
  NdbSchemaOp   *MySchemaOp = NULL ;
 
63
  
 
64
  
 
65
  int check, status, i, j, cont ;
 
66
  check = status = i = j = cont = 0 ;
 
67
  tNoOfRecords = 500 ;
 
68
  tNoOfLoops = tNoOfRecords / 10;
 
69
 
 
70
  i = 1;
 
71
  while (argc > 1){
 
72
 
 
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;
 
79
    }else{
 
80
      goto error_input;
 
81
    }
 
82
 
 
83
    argc -= 2;
 
84
    i = i + 2; //
 
85
  }
 
86
 
 
87
  pNdb = new Ndb( "TEST_DB" ) ; 
 
88
  ndbout << "Initialisation started. " << endl;
 
89
  pNdb->init();
 
90
  ndbout << "Initialisation completed. " << endl;
 
91
  
 
92
  tNodeId = pNdb->getNodeId();
 
93
  ndbout << endl << "Initial loading of Ronja Database" << endl;
 
94
  ndbout << "  NdbAPI node with id = " << tNodeId << endl;
 
95
  
 
96
  if (pNdb->waitUntilReady(30) != 0) {
 
97
    ndbout << "Benchmark failed - NDB is not ready" << endl;
 
98
        delete pNdb ;
 
99
    return NDBT_ProgramExit(NDBT_FAILED) ;
 
100
  }//if
 
101
  
 
102
  ndbout << endl << "Creating the table SHORT_REC" << "..." << endl;
 
103
 
 
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"
 
109
                                   ,8           // Table Size
 
110
                                   ,TupleKey    // Key Type
 
111
                                   ,40          // Nr of Pages
 
112
                                   );
 
113
  if (check == -1) goto error_handler;
 
114
 
 
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 ;
 
120
 
 
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 ;
 
126
  
 
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 ;
 
132
  
 
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 ;
 
138
  
 
139
  if (MySchemaTransaction->execute() == -1) {
 
140
          if(721 == MySchemaOp->getNdbError().code){
 
141
                  ndbout << "Table SHORT_REC already exists" << endl ;
 
142
          }else{
 
143
                  ndbout << MySchemaTransaction->getNdbError() << endl;
 
144
          }   
 
145
  }else{
 
146
          ndbout << "SHORT_REC created " << endl;
 
147
  }// if
 
148
 
 
149
  NdbSchemaCon::closeSchemaTrans(MySchemaTransaction);
 
150
  
 
151
  ndbout << endl << "Creating the table LONG_REC..." << endl;
 
152
 
 
153
  MySchemaTransaction = NdbSchemaCon::startSchemaTrans(pNdb);
 
154
  if(!MySchemaTransaction) goto error_handler;
 
155
   
 
156
  MySchemaOp = MySchemaTransaction->getNdbSchemaOp();   
 
157
  if(!MySchemaOp) goto error_handler;
 
158
  check = MySchemaOp->createTable( "LONG_REC"
 
159
                                       ,8               // Table Size
 
160
                                       ,TupleKey        // Key Type
 
161
                                       ,40              // Nr of Pages
 
162
                                       );
 
163
 
 
164
  if (check == -1) goto error_handler;
 
165
 
 
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 ;
 
171
 
 
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 ;
 
177
  
 
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 ;
 
183
  
 
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 ;
 
189
  
 
190
  if (MySchemaTransaction->execute() == -1) {
 
191
          if(721 == MySchemaOp->getNdbError().code){
 
192
                  ndbout << "Table LONG_REC already exists" << endl ;
 
193
          }else{
 
194
                  ndbout << MySchemaTransaction->getNdbError() << endl;
 
195
          }   
 
196
  }else{
 
197
          ndbout << "LONG_REC created" << endl;
 
198
  }// if
 
199
  
 
200
  NdbSchemaCon::closeSchemaTrans(MySchemaTransaction);
 
201
  
 
202
 
 
203
  check = InsertRecords(pNdb, tNoOfRecords);
 
204
  
 
205
  delete pNdb ;
 
206
 
 
207
  if(-1 == check){
 
208
          ndbout << endl << "Initial loading of Ronja Database failed" << endl;
 
209
          return NDBT_ProgramExit(NDBT_FAILED) ; 
 
210
  }else{
 
211
      ndbout << endl << "Initial loading of Ronja Database completed" << endl;
 
212
          return NDBT_ProgramExit(NDBT_OK) ; 
 
213
  }
 
214
 
 
215
  
 
216
  
 
217
 
 
218
 
 
219
error_handler:
 
220
  ndbout << "SchemaTransaction returned error:" ;
 
221
  ndbout << MySchemaTransaction->getNdbError() << endl;
 
222
  NdbSchemaCon::closeSchemaTrans(MySchemaTransaction);
 
223
  delete pNdb ;
 
224
  NDBT_ProgramExit(NDBT_FAILED) ;
 
225
  exit(-1);
 
226
 
 
227
error_input:
 
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) ;
 
233
  exit(1);
 
234
}
 
235
////////////////////////////////////////
 
236
 
 
237
inline int InsertRecords(Ndb* pNdb, int nNoRecords){
 
238
           
 
239
    NdbConnection               *MyTransaction = NULL ;
 
240
    NdbOperation*               MyOperation[10];
 
241
 
 
242
    int                         Tsuccess = 0 ;
 
243
        int                             loop_count_ops = 2 * tNoOfLoops;
 
244
    int                         loop_count_tables = 10;
 
245
    int                         loop_count_attributes = 0 ;
 
246
    int                         check = 0;
 
247
    int                         count = 0 ;
 
248
    int                         count_tables = 0;
 
249
    int                         count_attributes = 0 ;
 
250
    int                         i = 0 ;
 
251
    int                         tType = 0 ;
 
252
    unsigned int        attrValue[1000];
 
253
        unsigned int    setAttrValue = 0;
 
254
        unsigned int    keyValue[3];
 
255
 
 
256
        for (i = 0; i < 1000; i ++) attrValue[i] = 1;
 
257
        
 
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;
 
261
                  }
 
262
                  
 
263
                  MyTransaction = pNdb->startTransaction();
 
264
                  if(!MyTransaction){
 
265
                          ndbout << "startTransaction: " << pNdb->getNdbError();
 
266
                          ndbout << " count = " << count << endl;
 
267
                          return -1 ;
 
268
                  }
 
269
                                                  
 
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") ;
 
274
                          }else{
 
275
                                  keyValue[0] = (count - tNoOfLoops)*10 + count_tables;
 
276
                                  MyOperation[count_tables] = MyTransaction->getNdbOperation("LONG_REC");
 
277
                          }//if
 
278
                                  
 
279
                          if (!MyOperation[count_tables]) goto error_handler1;
 
280
                          
 
281
                          check = MyOperation[count_tables]->insertTuple();
 
282
                          if (check == -1) goto error_handler2;
 
283
          
 
284
                      check = MyOperation[count_tables]->equal("Key",(char*)&keyValue[0]);
 
285
              if (check == -1) goto error_handler4;
 
286
 
 
287
              check = MyOperation[count_tables]->setValue("Flip",(char*)&setAttrValue);
 
288
              if (check == -1) goto error_handler5;
 
289
          
 
290
                      check = MyOperation[count_tables]->setValue("Count",(char*)&setAttrValue);
 
291
              if (check == -1) goto error_handler5;
 
292
          
 
293
                      check = MyOperation[count_tables]->setValue("Placeholder",(char*)&attrValue[0]);
 
294
              if (check == -1) goto error_handler5;
 
295
                  }//for
 
296
     
 
297
                  if (MyTransaction->execute( Commit ) == -1){
 
298
                          ndbout << MyTransaction->getNdbError()<< endl ;
 
299
                          ndbout << "count = " << count << endl;
 
300
                  }//if
 
301
                  
 
302
                  pNdb->closeTransaction(MyTransaction) ;
 
303
          }//for
 
304
          return 0;
 
305
 
 
306
error_handler1:
 
307
   ndbout << "Error occured in getNdbOperation " << endl;
 
308
   ndbout << MyTransaction->getNdbError() << endl;
 
309
   pNdb->closeTransaction(MyTransaction);
 
310
   return -1 ;
 
311
 
 
312
error_handler2:
 
313
   ndbout << "Error occured in defining operation " << endl;
 
314
   ndbout << MyOperation[count_tables]->getNdbError() << endl;
 
315
   pNdb->closeTransaction(MyTransaction);
 
316
   return -1 ;
 
317
 
 
318
error_handler3:
 
319
   pNdb->closeTransaction(MyTransaction);
 
320
   return -1 ;
 
321
  
 
322
error_handler4:
 
323
   ndbout << "Error occured in equal " << endl;
 
324
   ndbout << MyOperation[count_tables]->getNdbError() << endl;
 
325
   pNdb->closeTransaction(MyTransaction);
 
326
   return -1 ;
 
327
  
 
328
error_handler5:
 
329
   ndbout << "Error occured in get/setValue " << endl;
 
330
   ndbout << MyOperation[count_tables]->getNdbError() << endl;
 
331
   pNdb->closeTransaction(MyTransaction);
 
332
   return -1 ;
 
333
 
 
334
}