42 #include <ndb_global.h>
48 #include <NdbTest.hpp>
49 #include <NDBT_Error.hpp>
52 #define MIN(x,y) (((x)<(y))?(x):(y))
55 #define MAX_NO_PARALLEL_OPERATIONS 100
57 bool testPassed =
true;
60 error_handler(
const char* errorText)
63 ndbout << endl <<
"ErrorMessage: " << errorText << endl;
68 error_handler4(
int line,
int status,
int classif,
int errNo,
const char* errorText)
70 ndbout << endl <<
"Line " << line << endl;
72 ndbout <<
"Status " << status <<
", Classification " << classif<<
", Error code " << errNo <<
"\n" << errorText << endl;
76 static char *longName, *sixtysix, *ninetynine, *hundred;
78 static void createTable(
Ndb &myNdb,
bool storeInACC,
bool twoKey,
bool longKey)
90 table.addColumn(column);
97 table.addColumn(column);
103 ndbout <<
"Created table" << ((longKey)?
" with long key":
"") <<endl;
106 static void createIndex(
Ndb &myNdb,
bool includePrimary,
unsigned int noOfIndexes)
108 Uint64 before, after;
110 char indexName[] =
"INDEX0000";
113 for(
unsigned int indexNum = 0; indexNum < noOfIndexes; indexNum++) {
114 sprintf(indexName,
"INDEX%.4u", indexNum);
116 index.setTable(
"THE_TABLE");
118 if (includePrimary) {
119 const char* attr_arr[] = {
"X",
"Y"};
120 index.addIndexColumns(2, attr_arr);
123 const char* attr_arr[] = {
"Y"};
124 index.addIndexColumns(2, attr_arr);
126 before = NdbTick_CurrentMillisecond();
130 after = NdbTick_CurrentMillisecond();
131 ndbout <<
"Created index " << indexName <<
", " << after - before <<
" msec"<< endl;
135 static void insertTable(
Ndb &myNdb,
unsigned int noOfTuples,
unsigned int noOfOperations,
bool oneTrans,
bool twoKey,
bool longKey)
137 Uint64 tbefore, tafter, before, after;
141 tbefore = NdbTick_CurrentMillisecond();
143 for (
unsigned int i = 0;
i<noOfTuples;
i++) {
145 for(
unsigned int j = 1;
146 ((j<=noOfOperations)&&(
i<noOfTuples));
147 (++j<=noOfOperations)?
i++:
i) {
158 if (myOp->equal(
"X",
i) == -1) {
164 if (myOp->setValue(
"Y",
i+1) == -1) {
171 before = NdbTick_CurrentMillisecond();
172 if (myTrans->
execute( (oneTrans) ? NoCommit : Commit ) == -1)
179 after = NdbTick_CurrentMillisecond();
180 if (noOfOperations == 1)
181 printf(
"Inserted 1 tuple, %u msec\n", (Uint32) after - before);
183 printf(
"Inserted %u tuples, %u msec\n", noOfOperations, (Uint32) after - before);
188 if (myTrans->
execute( Commit ) == -1) {
194 tafter = NdbTick_CurrentMillisecond();
196 ndbout <<
"Inserted "<< noOfTuples <<
" tuples in " << ((oneTrans) ? 1 : noOfTuples) <<
" transaction(s), " << tafter - tbefore <<
" msec" << endl;
199 static void updateTable(
Ndb &myNdb,
unsigned int noOfTuples,
unsigned int noOfOperations,
bool oneTrans,
bool twoKey,
bool longKey)
201 Uint64 tbefore, tafter, before, after;
205 tbefore = NdbTick_CurrentMillisecond();
207 for (
unsigned int i = 0;
i<noOfTuples;
i++) {
209 for(
unsigned int j = 1;
210 ((j<=noOfOperations)&&(
i<noOfTuples));
211 (++j<=noOfOperations)?
i++:
i) {
222 if (myOp->equal(
"X",
i) == -1) {
228 if (myOp->setValue(
"Y",
i+2) == -1) {
235 before = NdbTick_CurrentMillisecond();
236 if (myTrans->
execute( (oneTrans) ? NoCommit : Commit ) == -1)
243 after = NdbTick_CurrentMillisecond();
244 if (noOfOperations == 1)
245 printf(
"Updated 1 tuple, %u msec\n", (Uint32) after - before);
247 printf(
"Update %u tuples, %u msec\n", noOfOperations, (Uint32) after - before);
252 if (myTrans->
execute( Commit ) == -1) {
258 tafter = NdbTick_CurrentMillisecond();
260 ndbout <<
"Updated "<< noOfTuples <<
" tuples in " << ((oneTrans) ? 1 : noOfTuples) <<
" transaction(s), " << tafter - tbefore <<
" msec" << endl;
263 static void deleteTable(
Ndb &myNdb,
unsigned int noOfTuples,
unsigned int noOfOperations,
bool oneTrans,
bool twoKey,
bool longKey)
265 Uint64 tbefore, tafter, before, after;
269 tbefore = NdbTick_CurrentMillisecond();
271 for (
unsigned int i = 0;
i<noOfTuples;
i++) {
273 for(
unsigned int j = 1;
274 ((j<=noOfOperations)&&(
i<noOfTuples));
275 (++j<=noOfOperations)?
i++:
i) {
286 if (myOp->equal(
"X",
i) == -1) {
292 before = NdbTick_CurrentMillisecond();
293 if (myTrans->
execute( (oneTrans) ? NoCommit : Commit ) == -1)
301 after = NdbTick_CurrentMillisecond();
302 if (noOfOperations == 1)
303 printf(
"Deleted 1 tuple, %u msec\n", (Uint32) after - before);
305 printf(
"Deleted %u tuples, %u msec\n", noOfOperations, (Uint32) after - before);
310 if (myTrans->
execute( Commit ) == -1) {
316 tafter = NdbTick_CurrentMillisecond();
318 ndbout <<
"Deleted "<< noOfTuples <<
" tuples in " << ((oneTrans) ? 1 : noOfTuples) <<
" transaction(s), " << tafter - tbefore <<
" msec" << endl;
321 static void readTable(
Ndb &myNdb,
unsigned int noOfTuples,
unsigned int noOfOperations,
bool oneTrans,
bool twoKey,
bool longKey)
323 Uint64 tbefore, tafter, before, after;
326 NdbRecAttr* myRecAttrArr[MAX_NO_PARALLEL_OPERATIONS];
328 tbefore = NdbTick_CurrentMillisecond();
330 for (
unsigned int i = 0;
i<noOfTuples;
i++) {
332 for(
unsigned int j = 1;
333 ((j<=noOfOperations)&&(
i<noOfTuples));
334 (++j<=noOfOperations)?
i++:
i) {
345 if (myOp->equal(
"X",
i) == -1) {
351 myRecAttrArr[j-1] = myOp->getValue(
"Y", NULL);
353 before = NdbTick_CurrentMillisecond();
354 if (myTrans->
execute( (oneTrans) ? NoCommit : Commit ) == -1)
361 after = NdbTick_CurrentMillisecond();
362 if (noOfOperations == 1)
363 printf(
"Read 1 tuple, %u msec\n", (Uint32) after - before);
365 printf(
"Read %u tuples, %u msec\n", noOfOperations, (Uint32) after - before);
366 for(
unsigned int j = 0; j<noOfOperations; j++)
367 printf(
"Y = %u\n", myRecAttrArr[j]->u_32_value());
371 if (myTrans->
execute( Commit ) == -1) {
377 tafter = NdbTick_CurrentMillisecond();
379 ndbout <<
"Read "<< noOfTuples <<
" tuples in " << ((oneTrans) ? 1 : noOfTuples) <<
" transaction(s), " << tafter - tbefore <<
" msec" << endl;
382 static void readIndex(
Ndb &myNdb,
unsigned int noOfTuples,
unsigned int noOfOperations,
bool includePrimary,
bool oneTrans,
bool longKey)
384 Uint64 tbefore, tafter, before, after;
387 char indexName[] =
"INDEX0000";
388 NdbRecAttr* myRecAttrArr[MAX_NO_PARALLEL_OPERATIONS];
390 tbefore = NdbTick_CurrentMillisecond();
392 for (
unsigned int i = 0;
i<noOfTuples;
i++) {
394 for(
unsigned int j = 1;
395 ((j<=noOfOperations)&&(
i<noOfTuples));
396 (++j<=noOfOperations)?
i++:
i) {
407 if (includePrimary) {
408 if (myOp->equal(
"X",
i) == -1) {
415 if (myOp->equal(
"Y",
i+1) == -1) {
421 myRecAttrArr[j-1] = myOp->getValue(
"Y", NULL);
423 before = NdbTick_CurrentMillisecond();
424 if (myTrans->
execute( (oneTrans) ? NoCommit : Commit ) == -1)
431 after = NdbTick_CurrentMillisecond();
432 if (noOfOperations == 1)
433 printf(
"Read 1 tuple, %u msec\n", (Uint32) after - before);
435 printf(
"Read %u tuples, %u msec\n", noOfOperations, (Uint32) after - before);
436 for(
unsigned int j = 0; j<noOfOperations; j++)
437 printf(
"Y = %u\n", myRecAttrArr[j]->u_32_value());
441 if (myTrans->
execute( Commit ) == -1) {
447 tafter = NdbTick_CurrentMillisecond();
449 ndbout <<
"Read "<< noOfTuples <<
" tuples in " << ((oneTrans) ? 1 : noOfTuples) <<
" transaction(s), " << tafter - tbefore <<
" msec" << endl;
452 static void updateIndex(
Ndb &myNdb,
unsigned int noOfTuples,
unsigned int noOfOperations,
bool includePrimary,
bool oneTrans,
bool longKey)
454 Uint64 tbefore, tafter, before, after;
457 char indexName[] =
"INDEX0000";
459 tbefore = NdbTick_CurrentMillisecond();
461 for (
unsigned int i = 0;
i<noOfTuples;
i++) {
463 for(
unsigned int j = 1;
464 ((j<=noOfOperations)&&(
i<noOfTuples));
465 (++j<=noOfOperations)?
i++:
i) {
476 if (includePrimary) {
477 if (myOp->equal(
"X",
i) == -1) {
484 if (myOp->equal(
"Y",
i+1) == -1) {
491 if (myOp->setValue(
"Y",
i+2) == -1) {
498 before = NdbTick_CurrentMillisecond();
499 if (myTrans->
execute( (oneTrans) ? NoCommit : Commit ) == -1)
507 after = NdbTick_CurrentMillisecond();
508 if (noOfOperations == 1)
509 printf(
"Updated 1 tuple, %u msec\n", (Uint32) after - before);
511 printf(
"Updated %u tuples, %u msec\n", noOfOperations, (Uint32) after - before);
515 if (myTrans->
execute( Commit ) == -1) {
521 tafter = NdbTick_CurrentMillisecond();
523 ndbout <<
"Updated "<< noOfTuples <<
" tuples in " << ((oneTrans) ? 1 : noOfTuples) <<
" transaction(s), " << tafter - tbefore <<
" msec" << endl;
526 static void deleteIndex(
Ndb &myNdb,
unsigned int noOfTuples,
unsigned int noOfOperations,
bool includePrimary,
bool oneTrans,
bool longKey)
528 Uint64 tbefore, tafter, before, after;
531 char indexName[] =
"INDEX0000";
533 tbefore = NdbTick_CurrentMillisecond();
535 for (
unsigned int i = 0;
i<noOfTuples;
i++) {
536 for(
unsigned int j = 1;
537 ((j<=noOfOperations)&&(
i<noOfTuples));
538 (++j<=noOfOperations)?
i++:
i) {
550 if (includePrimary) {
551 if (myOp->equal(
"X",
i) == -1) {
558 if (myOp->equal(
"Y",
i+1) == -1) {
565 before = NdbTick_CurrentMillisecond();
566 if (myTrans->
execute( (oneTrans) ? NoCommit : Commit ) == -1)
573 after = NdbTick_CurrentMillisecond();
574 if (noOfOperations == 1)
575 printf(
"Deleted 1 tuple, %u msec\n", (Uint32) after - before);
577 printf(
"Deleted %u tuples, %u msec\n", noOfOperations, (Uint32) after - before);
581 if (myTrans->
execute( Commit ) == -1) {
587 tafter = NdbTick_CurrentMillisecond();
589 ndbout <<
"Deleted "<< noOfTuples <<
" tuples in " << ((oneTrans) ? 1 : noOfTuples) <<
" transaction(s), " << tafter - tbefore <<
" msec" << endl;
592 static void dropIndex(
Ndb &myNdb,
unsigned int noOfIndexes)
594 for(
unsigned int indexNum = 0; indexNum < noOfIndexes; indexNum++) {
596 sprintf(indexName,
"INDEX%.4u", indexNum);
597 const Uint64 before = NdbTick_CurrentMillisecond();
599 const Uint64 after = NdbTick_CurrentMillisecond();
602 ndbout <<
"Dropped index " << indexName <<
", "
603 << after - before <<
" msec" << endl;
605 ndbout <<
"Failed to drop index " << indexName << endl;
611 NDB_COMMAND(indexTest,
"indexTest",
"indexTest",
"indexTest", 65535)
614 bool createTableOp, createIndexOp, dropIndexOp, insertOp, updateOp, deleteOp, readOp, readIndexOp, updateIndexOp, deleteIndexOp, twoKey, longKey;
615 unsigned int noOfTuples = 1;
616 unsigned int noOfOperations = 1;
617 unsigned int noOfIndexes = 1;
619 Ndb myNdb(
"TEST_DB" );
621 bool storeInACC =
false;
622 bool includePrimary =
false;
623 bool oneTransaction =
false;
625 createTableOp = createIndexOp = dropIndexOp = insertOp = updateOp = deleteOp = readOp = readIndexOp = updateIndexOp = deleteIndexOp = twoKey = longKey =
false;
630 if (strcmp(argv[i],
"-T") == 0)
632 createTableOp =
true;
636 else if (strcmp(argv[i],
"-c") == 0)
638 createIndexOp =
true;
642 else if (strcmp(argv[i],
"-X") == 0)
648 else if (strcmp(argv[i],
"-I") == 0)
654 else if (strcmp(argv[i],
"-D") == 0)
660 else if (strcmp(argv[i],
"-U") == 0)
666 else if (strcmp(argv[i],
"-R") == 0)
672 else if (strcmp(argv[i],
"-r") == 0)
678 else if (strcmp(argv[i],
"-u") == 0)
680 updateIndexOp =
true;
684 else if (strcmp(argv[i],
"-d") == 0)
686 deleteIndexOp =
true;
690 else if (strcmp(argv[i],
"-s") == 0)
696 else if (strcmp(argv[i],
"-p") == 0)
698 includePrimary =
true;
702 else if (strcmp(argv[i],
"-L") == 0)
708 else if (strcmp(argv[i],
"-1") == 0)
710 oneTransaction =
true;
714 else if (strcmp(argv[i],
"-2") == 0)
720 else if (strstr(argv[i],
"-n") != 0)
722 noOfTuples = atoi(argv[i]+2);
726 else if (strstr(argv[i],
"-o") != 0)
728 noOfOperations = MIN(MAX_NO_PARALLEL_OPERATIONS, atoi(argv[i]+2));
732 else if (strstr(argv[i],
"-m") != 0)
734 noOfIndexes = atoi(argv[i]+2);
738 else if (strstr(argv[i],
"-h") != 0)
743 -L include a long attribute in key or index\
744 -2 define primary key with two attributes\
746 -p make index unique (include primary key attribute)\
748 -u update using index\
749 -d delete using index\
750 -n<no operations> do n operations (for -I -r -u -d -R -U -D)\
751 -o<no parallel operations> (for -I -r -u -d -R -U -D)\
759 sprintf(errStr,
"Illegal argument: %s", argv[i]);
760 error_handler(errStr);
766 createTableOp = createIndexOp = dropIndexOp = insertOp = updateOp = deleteOp =
true;
769 longName = (
char *) malloc(1024);
770 for (
int i = 0; i < 1023; i++)
772 longName[1023] =
'\0';
774 sixtysix = (
char *) malloc(256);
775 for (
int i = 0; i < 255; i++)
777 sixtysix[255] =
'\0';
778 strncpy(sixtysix,
"sixtysix", strlen(
"sixtysix"));
779 ninetynine = (
char *) malloc(256);
780 for (
int i = 0; i < 255; i++)
782 ninetynine[255] =
'\0';
783 strncpy(ninetynine,
"ninetynine", strlen(
"ninetynine"));
784 hundred = (
char *) malloc(256);
785 for (
int i = 0; i < 255; i++)
788 strncpy(hundred,
"hundred", strlen(
"hundred"));
794 createTable(myNdb, storeInACC, twoKey, longKey);
797 createIndex(myNdb, includePrimary, noOfIndexes);
800 insertTable(myNdb, noOfTuples, noOfOperations, oneTransaction, twoKey, longKey);
803 updateTable(myNdb, noOfTuples, noOfOperations, oneTransaction, twoKey, longKey);
806 deleteTable(myNdb, noOfTuples, noOfOperations, oneTransaction, twoKey, longKey);
809 readTable(myNdb, noOfTuples, noOfOperations, oneTransaction, twoKey, longKey);
812 readIndex(myNdb, noOfTuples, noOfOperations, includePrimary, oneTransaction, longKey);
815 updateIndex(myNdb, noOfTuples, noOfOperations, includePrimary, oneTransaction, longKey);
818 deleteIndex(myNdb, noOfTuples, noOfOperations, includePrimary, oneTransaction, longKey);
821 dropIndex(myNdb, noOfIndexes);
827 ndbout <<
"OK - Test passed" << endl;
832 ndbout <<
"FAIL - Test failed" << endl;