interpreterintup.cpp
来自「MySQL数据库开发源码 值得一看哦」· C++ 代码 · 共 1,519 行 · 第 1/3 页
CPP
1,519 行
/* 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 *//* *************************************************** TEST OF INTERPRETER IN TUP Verify that the interpreter in TUP is able to handle and execute all the commands that the NdbApi can isssue Arguments: operationType 1 openScanRead, 2 openScanExclusive, 3 interpretedUpdateTuple, 4 interpretedDirtyUpdate, 5 interpretedDeleteTuple 6 deleteTuple 7 insertTuple 8 updateTuple 9 writeTuple 10 readTuple 11 readTupleExclusive 12 simpleRead 13 dirtyRead 14 dirtyUpdate 15 dirtyWrite tupTest 1 exitMethods 2 incValue 3 subValue 4 readAttr 5 writeAttr 6 loadConst 7 branch 8 branchIfNull 9 addReg 10 subReg 11 subroutineWithBranchLabel scanTest Number of the test within each tupTest* *************************************************** */#include <NdbOut.hpp>#include <NdbThread.h>#include <NdbMutex.h>#include <NdbApi.hpp>#include <NdbSchemaCon.hpp>#include <NDBT.hpp>#define MAXATTR 3#define MAXTABLES 12#define MAXSTRLEN 8#define NUMBEROFRECORDS 1000typedef enum { FAIL = 0, NO_FAIL, UNDEF} TTYPE;inline void setAttrNames() ;inline void setTableNames() ;void error_handler(const NdbError & err, TTYPE);void create_table(Ndb*);void write_rows(Ndb*);void update_rows(Ndb*, int, int);void delete_rows(Ndb*, int, int);void verify_deleted(Ndb*);void read_and_verify_rows(Ndb*, bool pre);void scan_rows(Ndb*, int, int, int);TTYPE t_exitMethods(int, NdbOperation*, int);TTYPE t_incValue(int, NdbOperation*);TTYPE t_subValue(int, NdbOperation*);TTYPE t_readAttr(int, NdbOperation*);TTYPE t_writeAttr(int, NdbOperation*);TTYPE t_loadConst(int, NdbOperation*, int);TTYPE t_branch(int, NdbOperation*);TTYPE t_branchIfNull(int, NdbOperation*);TTYPE t_addReg(int, NdbOperation*);TTYPE t_subReg(int, NdbOperation*);TTYPE t_subroutineWithBranchLabel(int, NdbOperation*);char tableName[MAXSTRLEN+1];char attrName[MAXATTR][MAXSTRLEN+1];int attrValue[NUMBEROFRECORDS] = {0};int pkValue[NUMBEROFRECORDS] = {0};const int tAttributeSize = 1 ;const int nRecords = 20 ;int bTestPassed = 0; int main(int argc, const char** argv) { ndb_init(); int operationType = 0; int tupTest = 0; int scanTest = 0; Ndb* pNdb = new Ndb("TEST_DB"); pNdb->init(); if (argc != 4 || sscanf(argv[1],"%d", &operationType) != 1) { operationType = 1 ; } if (argc != 4 || sscanf(argv[2],"%d", &tupTest) != 1) { tupTest = 1 ; } if (argc != 4 || sscanf(argv[3],"%d", &scanTest) != 1) { scanTest = 1 ; } ndbout << endl << "Test the interpreter in TUP using SimpleTable with\n" << nRecords << " records" << endl << endl ; if (pNdb->waitUntilReady(30) != 0) { ndbout << "NDB is not ready" << endl; return -1; } // Init the pk and attr values. for (int i = 0; i < NUMBEROFRECORDS; i ++) pkValue[i] = attrValue[i] = i ; setAttrNames() ; setTableNames() ; const void * p = NDBT_Table::discoverTableFromDb(pNdb, tableName); if (p != 0){ create_table(pNdb); } write_rows(pNdb); ndbout << endl << "Starting interpreter in TUP test." << endl << "Operation type: " ; switch(operationType) { case 1: ndbout << "openScanRead" << endl; scan_rows(pNdb, operationType, tupTest, scanTest); break; case 2: ndbout << "openScanExclusive" << endl; scan_rows(pNdb, operationType, tupTest, scanTest); break; case 3: ndbout << "interpretedUpdateTuple" << endl; update_rows(pNdb, tupTest, operationType); break; case 4: ndbout << "interpretedDirtyUpdate" << endl; update_rows(pNdb, tupTest, operationType); break; case 5: ndbout << "interpretedDeleteTuple" << endl; delete_rows(pNdb, tupTest, operationType); break; case 6: ndbout << "deleteTuple" << endl; break; case 7: ndbout << "insertTuple" << endl; break; case 8: ndbout << "updateTuple" << endl; break; case 9: ndbout << "writeTuple" << endl; break; case 10: ndbout << "readTuple" << endl; break; case 11: ndbout << "readTupleExclusive" << endl; break; case 12: ndbout << "simpleRead" << endl; break; case 13: ndbout << "dirtyRead" << endl; break; case 14: ndbout << "dirtyUpdate" << endl; break; case 15: ndbout << "dirtyWrite" << endl; break; default: break ; }// read_and_verify_rows(pNdb, false); // delete_rows(pNdb, 0, 0) ; delete pNdb ; if (bTestPassed == 0) { ndbout << "OK: test passed" << endl; exit(0); }else{ ndbout << "FAIL: test failed" << endl; exit(-1); }}void error_handler(const NdbError & err, TTYPE type_expected) { ndbout << err << endl ; switch (type_expected){ case NO_FAIL: bTestPassed = -1 ; break ; case FAIL: ndbout << "OK: error is expected" << endl; break ; case UNDEF: ndbout << "assumed OK: expected result undefined" << endl ; break ; default: break ; }}void create_table(Ndb* pMyNdb) { /**************************************************************** * Create SimpleTable and Attributes. * * create table SimpleTable1( * col0 int, * col1 int not null, * col2 int not null, * col3 int not null ... 129) * ***************************************************************/ int check = -1 ; NdbSchemaOp *MySchemaOp = NULL ; ndbout << endl << "Creating " << tableName << " ... " << endl; NdbSchemaCon* MySchemaTransaction = NdbSchemaCon::startSchemaTrans(pMyNdb); if(!MySchemaTransaction) error_handler(MySchemaTransaction->getNdbError(), NO_FAIL); MySchemaOp = MySchemaTransaction->getNdbSchemaOp(); if( !MySchemaOp ) error_handler(MySchemaTransaction->getNdbError(), NO_FAIL); // Create table check = MySchemaOp->createTable( tableName, 8, // Table size TupleKey, // Key Type 40 // Nr of Pages ); if( check == -1 ) error_handler(MySchemaTransaction->getNdbError(), NO_FAIL); ndbout << "Creating attributes ... " << flush; // Create first column, primary key check = MySchemaOp->createAttribute( attrName[0], TupleKey, 32, 1/*3, tAttributeSize */, UnSigned, MMBased, NotNullAttribute ); if( check == -1 ) error_handler(MySchemaTransaction->getNdbError(), NO_FAIL); // create the 2 .. n columns. for ( int i = 1; i < MAXATTR; i++ ){ check = MySchemaOp->createAttribute( attrName[i], NoKey, 32, tAttributeSize, UnSigned, MMBased, NotNullAttribute ); if( check == -1 ) error_handler(MySchemaTransaction->getNdbError(), NO_FAIL); } ndbout << "OK" << endl; if( MySchemaTransaction->execute() == -1 ) { ndbout << MySchemaTransaction->getNdbError() << endl; }else{ ndbout << tableName[0] << " created" << endl; } NdbSchemaCon::closeSchemaTrans(MySchemaTransaction); return;}void write_rows (Ndb* pMyNdb) { /**************************************************************** * Insert rows into SimpleTable * ***************************************************************/ int check = -1 ; int loop_count_ops = nRecords ; NdbOperation *MyOperation = NULL ; NdbConnection *MyTransaction = NULL ; ndbout << endl << "Writing records ..." << flush; for (int count=0 ; count < loop_count_ops ; count++){ MyTransaction = pMyNdb->startTransaction(); if (!MyTransaction) { error_handler(pMyNdb->getNdbError(), NO_FAIL); }//if MyOperation = MyTransaction->getNdbOperation(tableName); if (!MyOperation) { error_handler(pMyNdb->getNdbError(), NO_FAIL); }//if check = MyOperation->writeTuple(); if( check == -1 ) error_handler(MyTransaction->getNdbError(), NO_FAIL); check = MyOperation->equal( attrName[0],(char*)&pkValue[count] ); if( check == -1 ) error_handler(MyTransaction->getNdbError(), NO_FAIL); // Update the columns, index column already ok. for (int i = 1 ; i < MAXATTR; i++){ if ((i == 2) && (count > 4)){ check = MyOperation->setValue(attrName[i], (char*)&attrValue[count + 1]); }else{ check = MyOperation->setValue(attrName[i], (char*)&attrValue[count]); } if( check == -1 ) error_handler(MyTransaction->getNdbError(), NO_FAIL); } check = MyTransaction->execute( Commit ); if(check == -1 ) error_handler(MyTransaction->getNdbError(), NO_FAIL); pMyNdb->closeTransaction(MyTransaction); } ndbout <<" \tOK" << endl; return;}void verify_deleted(Ndb* pMyNdb) { int check = -1 ; int loop_count_ops = nRecords; NdbOperation* pMyOperation = NULL ; ndbout << "Verifying deleted records..."<< flush; for (int count=0 ; count < loop_count_ops ; count++){ NdbConnection* pMyTransaction = pMyNdb->startTransaction(); if (!pMyTransaction) error_handler(pMyNdb->getNdbError(), NO_FAIL); pMyOperation = pMyTransaction->getNdbOperation(tableName); if (!pMyOperation) error_handler(pMyNdb->getNdbError(), NO_FAIL); check = pMyOperation->readTuple(); if( check == -1 ) error_handler( pMyTransaction->getNdbError(), NO_FAIL); check = pMyOperation->equal( attrName[0],(char*)&pkValue[count] ); if( check == -1 ) error_handler( pMyTransaction->getNdbError(), NO_FAIL); // Exepect to receive an error if(pMyTransaction->execute(Commit) != -1) if( 626 == pMyTransaction->getNdbError().code){ ndbout << pMyTransaction->getNdbError() << endl ; ndbout << "OK" << endl ; }else{ error_handler(pMyTransaction->getNdbError(), NO_FAIL) ; } pMyNdb->closeTransaction(pMyTransaction); } ndbout << "OK" << endl; return;};void read_and_verify_rows(Ndb* pMyNdb, bool pre) { int check = -1 ; int loop_count_ops = nRecords; char expectedCOL1[NUMBEROFRECORDS] = {0} ; char expectedCOL2[NUMBEROFRECORDS] = {0} ; NdbConnection *pMyTransaction = NULL ; NdbOperation *MyOp = NULL ; NdbRecAttr* tTmp = NULL ; int readValue[MAXATTR] = {0} ; ndbout << "Verifying records...\n"<< endl; for (int count=0 ; count < loop_count_ops ; count++){ pMyTransaction = pMyNdb->startTransaction(); if (!pMyTransaction) error_handler(pMyNdb->getNdbError(), NO_FAIL); MyOp = pMyTransaction->getNdbOperation(tableName); if (!MyOp) error_handler( pMyTransaction->getNdbError(), NO_FAIL); check = MyOp->readTuple(); if( check == -1 ) error_handler( MyOp->getNdbError(), NO_FAIL); check = MyOp->equal( attrName[0],(char*)&pkValue[count] ); if( check == -1 ) error_handler( MyOp->getNdbError(), NO_FAIL); for (int count_attributes = 1; count_attributes < MAXATTR; count_attributes++){ tTmp = MyOp->getValue( (char*)attrName[count_attributes], (char*)&readValue[count_attributes] ); if(!tTmp) error_handler( MyOp->getNdbError(), NO_FAIL); } if( pMyTransaction->execute( Commit ) == -1 ) { error_handler(pMyTransaction->getNdbError(), NO_FAIL); } else { if (pre) { expectedCOL1[count] = readValue[1]; expectedCOL2[count] = readValue[2]; } ndbout << attrName[1] << "\t " << readValue[1] << "\t " << attrName[2] << "\t " << readValue[2] << endl; } pMyNdb->closeTransaction(pMyTransaction); } ndbout << "\nOK\n" << endl; return;};TTYPE t_exitMethods(int nCalls, NdbOperation * pOp, int opType) { ndbout << "Defining exitMethods test " << nCalls << ": " << endl ;; TTYPE ret_val = NO_FAIL ; switch(nCalls){ case 1: // exit_nok if attr value matches pOp->read_attr("COL1", 1); pOp->load_const_u32(2, 14); pOp->branch_eq(1, 2, 0); pOp->interpret_exit_ok() ; pOp->def_label(0); pOp->interpret_exit_nok(); break; case 2: // exit_ok if attr value doesn't match pOp->read_attr("COL1", 1); pOp->load_const_u32(2, 14); pOp->branch_eq(1, 2, 0); pOp->interpret_exit_nok() ; pOp->def_label(0); if (opType == 3) { // For update transactions use incValue to update the tuple Uint32 val32 = 5; pOp->incValue("COL2", val32); } pOp->interpret_exit_ok(); break ; case 3: // Non-existent value (128): exit_ok if if the value matches pOp->read_attr("COL1", 1); pOp->load_const_u32(2, 128); pOp->branch_eq(1, 2, 0); pOp->interpret_exit_nok(); pOp->def_label(0); pOp->interpret_exit_ok(); ret_val = FAIL ; break; case 4: // Non-existent value (128): exit_nok if the value matches pOp->read_attr("COL1", 1); pOp->load_const_u32(2, 128); pOp->branch_eq(1, 2, 0); pOp->interpret_exit_ok(); pOp->def_label(0); pOp->interpret_exit_nok(); ret_val = FAIL ; break; case 5: // exit_nok of the value matches pOp->read_attr("COL1", 1); pOp->load_const_u32(2, 2); pOp->branch_eq(1, 2, 0);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?