60 #include <NdbThread.h>
63 #include <NdbSchemaCon.hpp>
69 #define NUMBEROFRECORDS 1000
77 inline void setAttrNames() ;
78 inline void setTableNames() ;
79 void error_handler(
const NdbError & err, TTYPE);
81 void write_rows(
Ndb*);
82 void update_rows(
Ndb*,
int,
int);
83 void delete_rows(
Ndb*,
int,
int);
84 void verify_deleted(
Ndb*);
85 void read_and_verify_rows(
Ndb*,
bool pre);
86 void scan_rows(
Ndb*,
int,
int,
int);
99 char tableName[MAXSTRLEN+1];
100 char attrName[MAXATTR][MAXSTRLEN+1];
101 int attrValue[NUMBEROFRECORDS] = {0};
102 int pkValue[NUMBEROFRECORDS] = {0};
103 const int tAttributeSize = 1 ;
104 const int nRecords = 20 ;
109 int main(
int argc,
const char** argv) {
112 int operationType = 0;
116 Ndb* pNdb =
new Ndb(
"TEST_DB");
119 if (argc != 4 || sscanf(argv[1],
"%d", &operationType) != 1) {
122 if (argc != 4 || sscanf(argv[2],
"%d", &tupTest) != 1) {
125 if (argc != 4 || sscanf(argv[3],
"%d", &scanTest) != 1) {
130 <<
"Test the interpreter in TUP using SimpleTable with\n"
131 << nRecords <<
" records" << endl << endl ;
134 ndbout <<
"NDB is not ready" << endl;
139 for (
int i = 0;
i < NUMBEROFRECORDS;
i ++)
140 pkValue[
i] = attrValue[
i] =
i ;
145 const void * p = NDBT_Table::discoverTableFromDb(pNdb, tableName);
152 ndbout << endl <<
"Starting interpreter in TUP test." << endl <<
"Operation type: " ;
154 switch(operationType) {
156 ndbout <<
"openScanRead" << endl;
157 scan_rows(pNdb, operationType, tupTest, scanTest);
160 ndbout <<
"openScanExclusive" << endl;
161 scan_rows(pNdb, operationType, tupTest, scanTest);
164 ndbout <<
"interpretedUpdateTuple" << endl;
165 update_rows(pNdb, tupTest, operationType);
168 ndbout <<
"interpretedDirtyUpdate" << endl;
169 update_rows(pNdb, tupTest, operationType);
172 ndbout <<
"interpretedDeleteTuple" << endl;
173 delete_rows(pNdb, tupTest, operationType);
176 ndbout <<
"deleteTuple" << endl;
179 ndbout <<
"insertTuple" << endl;
182 ndbout <<
"updateTuple" << endl;
185 ndbout <<
"writeTuple" << endl;
188 ndbout <<
"readTuple" << endl;
191 ndbout <<
"readTupleExclusive" << endl;
194 ndbout <<
"simpleRead" << endl;
197 ndbout <<
"dirtyRead" << endl;
200 ndbout <<
"dirtyUpdate" << endl;
203 ndbout <<
"dirtyWrite" << endl;
215 if (bTestPassed == 0) {
216 ndbout <<
"OK: test passed" << endl;
219 ndbout <<
"FAIL: test failed" << endl;
224 void error_handler(
const NdbError & err, TTYPE type_expected) {
226 ndbout << err << endl ;
228 switch (type_expected){
233 ndbout <<
"OK: error is expected" << endl;
236 ndbout <<
"assumed OK: expected result undefined" << endl ;
259 ndbout << endl <<
"Creating " << tableName <<
" ... " << endl;
261 NdbSchemaCon* MySchemaTransaction = NdbSchemaCon::startSchemaTrans(pMyNdb);
262 if(!MySchemaTransaction) error_handler(MySchemaTransaction->
getNdbError(), NO_FAIL);
265 if( !MySchemaOp ) error_handler(MySchemaTransaction->
getNdbError(), NO_FAIL);
267 check = MySchemaOp->createTable( tableName,
273 if( check == -1 ) error_handler(MySchemaTransaction->
getNdbError(), NO_FAIL);
275 ndbout <<
"Creating attributes ... " << flush;
278 check = MySchemaOp->createAttribute( attrName[0],
286 if( check == -1 ) error_handler(MySchemaTransaction->
getNdbError(), NO_FAIL);
289 for (
int i = 1;
i < MAXATTR;
i++ ){
290 check = MySchemaOp->createAttribute( attrName[
i],
298 if( check == -1 ) error_handler(MySchemaTransaction->
getNdbError(), NO_FAIL);
301 ndbout <<
"OK" << endl;
303 if( MySchemaTransaction->
execute() == -1 ) {
304 ndbout << MySchemaTransaction->
getNdbError() << endl;
306 ndbout << tableName[0] <<
" created" << endl;
310 NdbSchemaCon::closeSchemaTrans(MySchemaTransaction);
318 void write_rows (
Ndb* pMyNdb) {
326 int loop_count_ops = nRecords ;
330 ndbout << endl <<
"Writing records ..." << flush;
332 for (
int count=0 ; count < loop_count_ops ; count++){
334 if (!MyTransaction) {
344 if( check == -1 ) error_handler(MyTransaction->
getNdbError(), NO_FAIL);
346 check = MyOperation->
equal( attrName[0],(
char*)&pkValue[count] );
347 if( check == -1 ) error_handler(MyTransaction->
getNdbError(), NO_FAIL);
350 for (
int i = 1 ; i < MAXATTR; i++){
351 if ((i == 2) && (count > 4)){
352 check = MyOperation->
setValue(attrName[i], (
char*)&attrValue[count + 1]);
354 check = MyOperation->
setValue(attrName[i], (
char*)&attrValue[count]);
356 if( check == -1 ) error_handler(MyTransaction->
getNdbError(), NO_FAIL);
358 check = MyTransaction->
execute( Commit );
359 if(check == -1 ) error_handler(MyTransaction->
getNdbError(), NO_FAIL);
363 ndbout <<
" \tOK" << endl;
367 void verify_deleted(
Ndb* pMyNdb) {
370 int loop_count_ops = nRecords;
373 ndbout <<
"Verifying deleted records..."<< flush;
375 for (
int count=0 ; count < loop_count_ops ; count++){
378 if (!pMyTransaction) error_handler(pMyNdb->
getNdbError(), NO_FAIL);
381 if (!pMyOperation) error_handler(pMyNdb->
getNdbError(), NO_FAIL);
383 check = pMyOperation->readTuple();
384 if( check == -1 ) error_handler( pMyTransaction->
getNdbError(), NO_FAIL);
386 check = pMyOperation->equal( attrName[0],(
char*)&pkValue[count] );
387 if( check == -1 ) error_handler( pMyTransaction->
getNdbError(), NO_FAIL);
390 if(pMyTransaction->
execute(Commit) != -1)
393 ndbout <<
"OK" << endl ;
395 error_handler(pMyTransaction->
getNdbError(), NO_FAIL) ;
401 ndbout <<
"OK" << endl;
405 void read_and_verify_rows(
Ndb* pMyNdb,
bool pre) {
408 int loop_count_ops = nRecords;
409 char expectedCOL1[NUMBEROFRECORDS] = {0} ;
410 char expectedCOL2[NUMBEROFRECORDS] = {0} ;
414 int readValue[MAXATTR] = {0} ;
416 ndbout <<
"Verifying records...\n"<< endl;
418 for (
int count=0 ; count < loop_count_ops ; count++){
421 if (!pMyTransaction) error_handler(pMyNdb->
getNdbError(), NO_FAIL);
424 if (!MyOp) error_handler( pMyTransaction->
getNdbError(), NO_FAIL);
427 check = MyOp->readTuple();
428 if( check == -1 ) error_handler( MyOp->getNdbError(), NO_FAIL);
430 check = MyOp->equal( attrName[0],(
char*)&pkValue[count] );
431 if( check == -1 ) error_handler( MyOp->getNdbError(), NO_FAIL);
433 for (
int count_attributes = 1; count_attributes < MAXATTR; count_attributes++){
435 tTmp = MyOp->getValue( (
char*)attrName[count_attributes], (
char*)&readValue[count_attributes] );
436 if(!tTmp) error_handler( MyOp->getNdbError(), NO_FAIL);
439 if( pMyTransaction->
execute( Commit ) == -1 ) {
440 error_handler(pMyTransaction->
getNdbError(), NO_FAIL);
443 expectedCOL1[count] = readValue[1];
444 expectedCOL2[count] = readValue[2];
447 ndbout << attrName[1] <<
"\t " << readValue[1] <<
"\t "
448 << attrName[2] <<
"\t " << readValue[2] << endl;
455 ndbout <<
"\nOK\n" << endl;
461 TTYPE t_exitMethods(
int nCalls,
NdbOperation * pOp,
int opType) {
463 ndbout <<
"Defining exitMethods test " << nCalls <<
": " << endl ;;
464 TTYPE ret_val = NO_FAIL ;
470 pOp->branch_eq(1, 2, 0);
478 pOp->branch_eq(1, 2, 0);
491 pOp->branch_eq(1, 2, 0);
500 pOp->branch_eq(1, 2, 0);
509 pOp->branch_eq(1, 2, 0);
517 pOp->branch_eq(1, 2, 0);
530 pOp->branch_eq(1, 2, 0);
538 pOp->branch_ne(1, 2, 0);
551 pOp->branch_eq(1, 2, 0);
559 pOp->branch_eq(1, 2, 0);
572 pOp->branch_eq(1, 2, 0);
580 pOp->branch_eq(1, 2, 0);
593 pOp->branch_eq(1, 2, 0);
601 pOp->branch_eq(1, 2, 0);
609 pOp->branch_eq(1, 2, 0);
637 ndbout <<
"Defining incValue test " << nCalls <<
": ";
638 TTYPE ret_val = NO_FAIL;
699 ndbout <<
"Defining subValue test " << nCalls <<
": ";
707 TTYPE ret_val = NO_FAIL;
735 ndbout <<
"Missing implementation" << endl;
740 ndbout <<
"Missing implementation" << endl;
745 ndbout <<
"Missing implementation" << endl;
750 ndbout <<
"Missing implementation" << endl;
755 ndbout <<
"Missing implementation" << endl;
760 ndbout <<
"Missing implementation" << endl;
770 ndbout <<
"Defining readAttr test " << nCalls <<
": ";
775 TTYPE ret_val = NO_FAIL;
817 ndbout <<
"Defining writeAttr test " << nCalls <<
": ";
824 TTYPE ret_val = NO_FAIL ;
871 TTYPE t_loadConst(
int nCalls,
NdbOperation* pOp,
int opType) {
873 ndbout <<
"Defining loadConst test " << nCalls <<
" : ";
874 TTYPE ret_val = NO_FAIL ;
901 pOp->load_const_u64(1, 65600);
905 pOp->load_const_u64(8, 2);
910 pOp->load_const_u64(1, -1);
918 if (opType == 3 && FAIL != ret_val)
925 ndbout <<
"Defining branch test " << nCalls <<
": " ;
927 TTYPE ret_val = NO_FAIL ;
935 pOp->branch_eq(1, 2, 0);
941 pOp->branch_eq(2, 1, 0);
947 pOp->branch_eq(1, 1, 0);
962 TTYPE ret_val = NO_FAIL;
963 ndbout <<
"Defining branchIfNull test " << nCalls <<
": " << endl ;
1035 TTYPE ret_val = NO_FAIL ;
1037 ndbout <<
"Defining addReg test " << nCalls <<
": ";
1041 pOp->load_const_u64(3, 65600);
1042 pOp->load_const_u64(4, 95600);
1079 pOp->branch_eq(5, 7, 0);
1089 TTYPE ret_val = NO_FAIL ;
1090 ndbout <<
"Defining subReg test: " << nCalls << endl;
1094 pOp->load_const_u64(3, 65600);
1095 pOp->load_const_u64(4, 95600);
1104 pOp->load_const_u64(7, (Uint64)-100);
1108 pOp->load_const_u64(7, (Uint64)100);
1112 pOp->load_const_u64(7, (Uint64)-30000);
1116 pOp->load_const_u64(7, 0);
1121 pOp->load_const_u64(7, 0);
1126 pOp->load_const_u64(7, 0);
1131 pOp->load_const_u64(7, 0);
1144 pOp->branch_eq(5, 7, 0);
1152 TTYPE t_subroutineWithBranchLabel(
int nCalls,
NdbOperation* pOp) {
1154 TTYPE ret_val = NO_FAIL ;
1155 ndbout <<
"Defining subroutineWithBranchLabel test:" << nCalls << endl;
1159 pOp->load_const_u64(3, 65600);
1160 pOp->load_const_u64(4, 95600);
1205 for(
int sub = 0; sub < 25 ; ++sub){
1226 void scan_rows(
Ndb* pMyNdb,
int opType,
int tupleType,
int scanType) {
1229 int loop_count_ops = nRecords ;
1232 int readValue2 = 0 ;
1234 TTYPE fail = NO_FAIL ;
1236 for (
int count=0 ; count < loop_count_ops ; count++) {
1238 if (!MyTransaction) error_handler(pMyNdb->
getNdbError(), NO_FAIL);
1241 if (!MyOperation) error_handler(pMyNdb->
getNdbError(), NO_FAIL);
1246 check = MyOperation->openScanRead(1);
1247 else if (opType == 2)
1249 check = MyOperation->openScanExclusive(1);
1254 scanCount = count+1 ;
1258 fail = t_exitMethods(scanCount, MyOperation, opType);
1261 fail = t_incValue(scanCount, MyOperation);
1264 fail = t_subValue(scanCount, MyOperation);
1267 fail = t_readAttr(scanCount, MyOperation);
1270 fail = t_writeAttr(scanCount, MyOperation);
1273 fail = t_loadConst(scanCount, MyOperation, opType);
1276 fail = t_branch(scanCount, MyOperation);
1279 fail = t_branchIfNull(scanCount, MyOperation);
1282 fail = t_addReg(scanCount, MyOperation);
1285 fail = t_subReg(scanCount, MyOperation);
1288 fail = t_subroutineWithBranchLabel(scanCount, MyOperation);
1294 if(11 != tupleType) MyOperation->getValue(attrName[1], (
char*)&readValue);
1299 check = MyTransaction->executeScan();
1302 error_handler(MyTransaction->
getNdbError(), fail) ;
1307 while ((eOf = MyTransaction->nextScanResult()) == 0) {
1308 ndbout << readValue <<
"; ";
1315 ndbout << endl <<
"nextScanResult returned: "<< MyTransaction->
getNdbError() << endl;
1317 ndbout <<
"OK" << endl;
1326 void update_rows(
Ndb* pMyNdb,
int tupleType,
int opType) {
1333 int loop_count_ops = nRecords ;
1334 int readValue[MAXATTR] = {0} ;
1335 TTYPE ret_val = NO_FAIL ;
1339 ndbout <<
"Updating records ..." << endl << endl;
1341 for (
int count=0 ; count < loop_count_ops ; count++){
1344 if (!MyTransaction) {
1350 if (MyOperation == NULL) {
1360 error_handler(MyTransaction->
getNdbError(), NO_FAIL);
1362 check = MyOperation->
equal( attrName[0], (
char*)&pkValue[count] );
1364 error_handler(MyTransaction->
getNdbError(), NO_FAIL);
1368 ret_val = t_exitMethods(count+1, MyOperation, opType);
1371 ret_val = t_incValue(count+1, MyOperation);
1374 ret_val = t_subValue(count+1, MyOperation);
1377 ret_val = t_readAttr(count+1, MyOperation);
1380 ret_val = t_writeAttr(count+1, MyOperation);
1383 ret_val = t_loadConst(count+1, MyOperation, opType);
1386 ret_val = t_branch(count+1, MyOperation);
1389 ret_val = t_branchIfNull(count+1, MyOperation);
1392 ret_val = t_addReg(count+1, MyOperation);
1395 ret_val = t_subReg(count+1, MyOperation);
1398 ret_val = t_subroutineWithBranchLabel(count+1, MyOperation);
1404 MyOperation->
getValue(
"COL1", (
char*)&readValue);
1406 if (MyTransaction->
execute( Commit ) == -1 ) {
1407 error_handler(MyTransaction->
getNdbError(), ret_val);
1408 }
else if (NO_FAIL == ret_val ) {
1409 ndbout <<
"OK" << endl;
1412 ndbout <<
"Test passed when expected to fail" << endl;
1418 ndbout <<
"Finished updating records" << endl;
1422 void delete_rows(
Ndb* pMyNdb,
int tupleTest,
int opType) {
1430 int loop_count_ops = nRecords ;
1431 int readValue[MAXATTR] = {0};
1434 TTYPE ret_val = NO_FAIL ;
1436 ndbout <<
"Deleting records ..."<< endl << endl;
1438 for (
int count=0 ; count < loop_count_ops ; count++) {
1441 if (!MyTransaction) error_handler(pMyNdb->
getNdbError(), NO_FAIL) ;
1444 if (!MyOperation) error_handler(pMyNdb->
getNdbError(), NO_FAIL) ;
1446 check = MyOperation->interpretedDeleteTuple();
1447 if( check == -1 ) error_handler(MyTransaction->
getNdbError(), NO_FAIL) ;
1449 check = MyOperation->equal( attrName[0], (
char*)&pkValue[count] );
1450 if( check == -1 ) error_handler(MyTransaction->
getNdbError(), NO_FAIL) ;
1454 ret_val = t_exitMethods(count+1, MyOperation, opType);
1457 ret_val = t_incValue(count+1, MyOperation);
1460 ret_val = t_subValue(count+1, MyOperation);
1463 ret_val = t_readAttr(count+1, MyOperation);
1466 ret_val = t_writeAttr(count+1, MyOperation);
1469 ret_val = t_loadConst(count+1, MyOperation, opType);
1472 ret_val = t_branch(count+1, MyOperation);
1475 ret_val = t_branchIfNull(count+1, MyOperation);
1478 ret_val = t_addReg(count+1, MyOperation);
1481 ret_val = t_subReg(count+1, MyOperation);
1484 ret_val = t_subroutineWithBranchLabel(count+1, MyOperation);
1490 if(11 != tupleTest)MyOperation->getValue(attrName[1], (
char*)&readValue) ;
1492 if (MyTransaction->
execute( Commit ) == -1 ) {
1493 error_handler(MyTransaction->
getNdbError(), ret_val);
1494 }
else if (NO_FAIL == ret_val ) {
1495 ndbout <<
"OK" << endl;
1498 ndbout <<
"Test passed when expected to fail" << endl;
1504 ndbout <<
"Finished deleting records" << endl;
1510 inline void setAttrNames(){
1511 for (
int i = 0; i < MAXATTR; i++){
1517 inline void setTableNames(){