index2.cpp

来自「MySQL数据库开发源码 值得一看哦」· C++ 代码 · 共 837 行 · 第 1/2 页

CPP
837
字号
/* 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 *//* ***************************************************       INDEX TEST 1       Test index functionality of NDB       Arguments:        -T create table        -L include a long attribute in key or index        -2 define primary key with two attributes        -c create index	-p make index unique (include primary key attribute)        -r read using index	-u update using index	-d delete using index	-n<no operations> do n operations (for -I -r -u -d -R -U -D)	-o<no parallel operations> (for -I -r -u -d -R -U -D)	-m<no indexes>       Returns:        0 - Test passed       -1 - Test failed        1 - Invalid arguments * *************************************************** */#include <ndb_global.h>#include <NdbApi.hpp>#include <NdbOut.hpp>#include <NdbTick.h>#include <NdbMain.h>#include <NdbTest.hpp>#include <NDBT_Error.hpp>#ifndef MIN#define MIN(x,y) (((x)<(y))?(x):(y))#endif#define MAX_NO_PARALLEL_OPERATIONS 100bool testPassed = true;static voiderror_handler(const char* errorText){  // Test failed   ndbout << endl << "ErrorMessage: " << errorText << endl;  testPassed = false;}static voiderror_handler4(int line, int status, int classif, int errNo, const char* errorText){  ndbout << endl << "Line " << line << endl;  // Test failed   ndbout << "Status " << status << ", Classification " << classif<< ", Error code " << errNo << "\n" << errorText << endl;  testPassed = false;}static char *longName, *sixtysix, *ninetynine, *hundred;static void createTable(Ndb &myNdb, bool storeInACC, bool twoKey, bool longKey){  NdbDictionary::Dictionary* dict = myNdb.getDictionary();  NdbDictionary::Table table("THE_TABLE");  NdbDictionary::Column column;  int res;  column.setName("X");  column.setType(NdbDictionary::Column::Unsigned);  column.setLength(1);  column.setPrimaryKey(true);  column.setNullable(false);  table.addColumn(column);  column.setName("Y");  column.setType(NdbDictionary::Column::Unsigned);  column.setLength(1);  column.setPrimaryKey(false);  column.setNullable(false);  table.addColumn(column);  if ((res = dict->createTable(table)) == -1) {    error_handler(dict->getNdbError().message);  }  else      ndbout << "Created table" << ((longKey)?" with long key":"") <<endl;}static void createIndex(Ndb &myNdb, bool includePrimary, unsigned int noOfIndexes){  Uint64 before, after;  NdbDictionary::Dictionary* dict = myNdb.getDictionary();  char indexName[] = "INDEX0000";  int res;  for(unsigned int indexNum = 0; indexNum < noOfIndexes; indexNum++) {    sprintf(indexName, "INDEX%.4u", indexNum);    NdbDictionary::Index index(indexName);    index.setTable("THE_TABLE");    index.setType(NdbDictionary::Index::UniqueHashIndex);    if (includePrimary) {      const char* attr_arr[] = {"X", "Y"};      index.addIndexColumns(2, attr_arr);    }    else {      const char* attr_arr[] = {"Y"};      index.addIndexColumns(2, attr_arr);    }    before = NdbTick_CurrentMillisecond();    if ((res = dict->createIndex(index)) == -1) {      error_handler(dict->getNdbError().message);    }        after = NdbTick_CurrentMillisecond();    ndbout << "Created index " << indexName << ", " << after - before << " msec"<< endl;  }}  static void insertTable(Ndb &myNdb, unsigned int noOfTuples, unsigned int noOfOperations, bool oneTrans, bool twoKey, bool longKey){  Uint64 tbefore, tafter, before, after;  NdbConnection  *myTrans;    NdbOperation   *myOp;       tbefore = NdbTick_CurrentMillisecond();  if (oneTrans) myTrans = myNdb.startTransaction();  for (unsigned int i = 0; i<noOfTuples; i++) {    if (!oneTrans) myTrans = myNdb.startTransaction();    for(unsigned int j = 1; 	((j<=noOfOperations)&&(i<noOfTuples)); 	(++j<=noOfOperations)?i++:i) {       if (myTrans == NULL) 	error_handler4(__LINE__, myNdb.getNdbError().status, myNdb.getNdbError().classification,		       myNdb.getNdbError().code, myNdb.getNdbError().message);            myOp = myTrans->getNdbOperation("THE_TABLE");       if (myOp == NULL) 	error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification, 		       myTrans->getNdbError().code, myTrans->getNdbError().message);            myOp->insertTuple();      if (myOp->equal("X", i) == -1) {	error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification, 		       myTrans->getNdbError().code, myTrans->getNdbError().message);	myNdb.closeTransaction(myTrans);	break;      }            if (myOp->setValue("Y", i+1) == -1) {	error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                        myTrans->getNdbError().code, myTrans->getNdbError().message);	myNdb.closeTransaction(myTrans);	break;      }          }    before = NdbTick_CurrentMillisecond();    if (myTrans->execute( (oneTrans) ? NoCommit : Commit ) == -1)      {        error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                        myTrans->getNdbError().code, myTrans->getNdbError().message);        myNdb.closeTransaction(myTrans);        break;      }    after = NdbTick_CurrentMillisecond();    if (noOfOperations == 1)      printf("Inserted 1 tuple, %u msec\n", (Uint32) after - before);    else      printf("Inserted %u tuples, %u msec\n", noOfOperations, (Uint32) after - before);    if (!oneTrans) myNdb.closeTransaction(myTrans);  }  if (oneTrans) {    if (myTrans->execute( Commit ) == -1) {      error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                      myTrans->getNdbError().code, myTrans->getNdbError().message);    }    myNdb.closeTransaction(myTrans);  }  tafter = NdbTick_CurrentMillisecond();  ndbout << "Inserted "<< noOfTuples << " tuples in " << ((oneTrans) ? 1 : noOfTuples) << " transaction(s), " << tafter - tbefore << " msec" << endl;        } static void updateTable(Ndb &myNdb, unsigned int noOfTuples, unsigned int noOfOperations, bool oneTrans, bool twoKey, bool longKey){  Uint64 tbefore, tafter, before, after;  NdbConnection  *myTrans;    NdbOperation   *myOp;       tbefore = NdbTick_CurrentMillisecond();  if (oneTrans) myTrans = myNdb.startTransaction();  for (unsigned int i = 0; i<noOfTuples; i++) {    if (!oneTrans) myTrans = myNdb.startTransaction();    for(unsigned int j = 1; 	((j<=noOfOperations)&&(i<noOfTuples)); 	(++j<=noOfOperations)?i++:i) {       if (myTrans == NULL) 	error_handler4(__LINE__, myNdb.getNdbError().status, myNdb.getNdbError().classification,		       myNdb.getNdbError().code, myNdb.getNdbError().message);            myOp = myTrans->getNdbOperation("THE_TABLE");       if (myOp == NULL) 	error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification, 		       myTrans->getNdbError().code, myTrans->getNdbError().message);            myOp->updateTuple();      if (myOp->equal("X", i) == -1) {	error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification, 		       myTrans->getNdbError().code, myTrans->getNdbError().message);	myNdb.closeTransaction(myTrans);	break;      }            if (myOp->setValue("Y", i+2) == -1) {	error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                        myTrans->getNdbError().code, myTrans->getNdbError().message);	myNdb.closeTransaction(myTrans);	break;      }          }    before = NdbTick_CurrentMillisecond();    if (myTrans->execute( (oneTrans) ? NoCommit : Commit ) == -1)      {	error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification, 		       myTrans->getNdbError().code, myTrans->getNdbError().message);	myNdb.closeTransaction(myTrans);	break;      }      after = NdbTick_CurrentMillisecond();      if (noOfOperations == 1)	printf("Updated 1 tuple, %u msec\n", (Uint32) after - before);      else	printf("Update %u tuples, %u msec\n", noOfOperations, (Uint32) after - before);      if (!oneTrans) myNdb.closeTransaction(myTrans);  }      if (oneTrans) {    if (myTrans->execute( Commit ) == -1) {      error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification, 		     myTrans->getNdbError().code, myTrans->getNdbError().message);    }    myNdb.closeTransaction(myTrans);  }  tafter = NdbTick_CurrentMillisecond();    ndbout << "Updated "<< noOfTuples << " tuples in " << ((oneTrans) ? 1 : noOfTuples) << " transaction(s), " << tafter - tbefore << " msec" << endl;        }static void deleteTable(Ndb &myNdb, unsigned int noOfTuples, unsigned int noOfOperations, bool oneTrans, bool twoKey, bool longKey){  Uint64 tbefore, tafter, before, after;  NdbConnection  *myTrans;    NdbOperation   *myOp;       tbefore = NdbTick_CurrentMillisecond();  if (oneTrans) myTrans = myNdb.startTransaction();  for (unsigned int i = 0; i<noOfTuples; i++) {    if (!oneTrans) myTrans = myNdb.startTransaction();    for(unsigned int j = 1; 	((j<=noOfOperations)&&(i<noOfTuples)); 	(++j<=noOfOperations)?i++:i) {       if (myTrans == NULL)         error_handler4(__LINE__, myNdb.getNdbError().status, myNdb.getNdbError().classification,                       myNdb.getNdbError().code, myNdb.getNdbError().message);            myOp = myTrans->getNdbOperation("THE_TABLE");       if (myOp == NULL)         error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                        myTrans->getNdbError().code, myTrans->getNdbError().message);            myOp->deleteTuple();      if (myOp->equal("X", i) == -1) {        error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                        myTrans->getNdbError().code, myTrans->getNdbError().message);        myNdb.closeTransaction(myTrans);        break;      }          before = NdbTick_CurrentMillisecond();    if (myTrans->execute( (oneTrans) ? NoCommit : Commit ) == -1)      {        error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                        myTrans->getNdbError().code, myTrans->getNdbError().message);        myNdb.closeTransaction(myTrans);        break;      }    }    after = NdbTick_CurrentMillisecond();    if (noOfOperations == 1)      printf("Deleted 1 tuple, %u msec\n", (Uint32) after - before);    else      printf("Deleted %u tuples, %u msec\n", noOfOperations, (Uint32) after - before);        if (!oneTrans) myNdb.closeTransaction(myTrans);  }  if (oneTrans) {    if (myTrans->execute( Commit ) == -1) {      error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                      myTrans->getNdbError().code, myTrans->getNdbError().message);    }    myNdb.closeTransaction(myTrans);  }  tafter = NdbTick_CurrentMillisecond();  ndbout << "Deleted "<< noOfTuples << " tuples in " << ((oneTrans) ? 1 : noOfTuples) << " transaction(s), " << tafter - tbefore << " msec" << endl;        }static void readTable(Ndb &myNdb, unsigned int noOfTuples, unsigned int noOfOperations, bool oneTrans, bool twoKey, bool longKey){  Uint64 tbefore, tafter, before, after;  NdbConnection  *myTrans;    NdbOperation   *myOp;     NdbRecAttr* myRecAttrArr[MAX_NO_PARALLEL_OPERATIONS];    tbefore = NdbTick_CurrentMillisecond();  if (oneTrans) myTrans = myNdb.startTransaction();  for (unsigned int i = 0; i<noOfTuples; i++) {    if (!oneTrans) myTrans = myNdb.startTransaction();    for(unsigned int j = 1; 	((j<=noOfOperations)&&(i<noOfTuples)); 	(++j<=noOfOperations)?i++:i) {       if (myTrans == NULL)         error_handler4(__LINE__, myNdb.getNdbError().status, myNdb.getNdbError().classification,                       myNdb.getNdbError().code, myNdb.getNdbError().message);            myOp = myTrans->getNdbOperation("THE_TABLE");       if (myOp == NULL)         error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                        myTrans->getNdbError().code, myTrans->getNdbError().message);            myOp->readTuple();      if (myOp->equal("X", i) == -1) {        error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                        myTrans->getNdbError().code, myTrans->getNdbError().message);        myNdb.closeTransaction(myTrans);        break;      }               myRecAttrArr[j-1] = myOp->getValue("Y", NULL);    }    before = NdbTick_CurrentMillisecond();    if (myTrans->execute( (oneTrans) ? NoCommit : Commit ) == -1)      {        error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                        myTrans->getNdbError().code, myTrans->getNdbError().message);        myNdb.closeTransaction(myTrans);        break;      }    after = NdbTick_CurrentMillisecond();    if (noOfOperations == 1)      printf("Read 1 tuple, %u msec\n", (Uint32) after - before);    else      printf("Read %u tuples, %u msec\n", noOfOperations, (Uint32) after - before);    for(unsigned int j = 0; j<noOfOperations; j++)      printf("Y = %u\n", myRecAttrArr[j]->u_32_value());    if (!oneTrans) myNdb.closeTransaction(myTrans);  }  if (oneTrans) {    if (myTrans->execute( Commit ) == -1) {      error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                      myTrans->getNdbError().code, myTrans->getNdbError().message);    }    myNdb.closeTransaction(myTrans);  }  tafter = NdbTick_CurrentMillisecond();  ndbout << "Read "<< noOfTuples << " tuples in " << ((oneTrans) ? 1 : noOfTuples) << " transaction(s), " << tafter - tbefore << " msec" << endl;        }static void readIndex(Ndb &myNdb, unsigned int noOfTuples, unsigned int noOfOperations, bool includePrimary, bool oneTrans, bool longKey){  Uint64 tbefore, tafter, before, after;  NdbConnection  *myTrans;    NdbIndexOperation   *myOp;   char indexName[] = "INDEX0000";  NdbRecAttr* myRecAttrArr[MAX_NO_PARALLEL_OPERATIONS];    tbefore = NdbTick_CurrentMillisecond();  if (oneTrans) myTrans = myNdb.startTransaction();  for (unsigned int i = 0; i<noOfTuples; i++) {    if (!oneTrans) myTrans = myNdb.startTransaction();    for(unsigned int j = 1; 	((j<=noOfOperations)&&(i<noOfTuples)); 	(++j<=noOfOperations)?i++:i) {       if (myTrans == NULL)         error_handler4(__LINE__, myNdb.getNdbError().status, myNdb.getNdbError().classification,                       myNdb.getNdbError().code, myNdb.getNdbError().message);            myOp = myTrans->getNdbIndexOperation(indexName, "THE_TABLE");       if (myOp == NULL)         error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                        myTrans->getNdbError().code, myTrans->getNdbError().message);            myOp->readTuple();      if (includePrimary) {        if (myOp->equal("X", i) == -1) {          error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                          myTrans->getNdbError().code, myTrans->getNdbError().message);          myNdb.closeTransaction(myTrans);          break;        }               }       if (myOp->equal("Y", i+1) == -1) {        error_handler4(__LINE__, myTrans->getNdbError().status, myTrans->getNdbError().classification,                        myTrans->getNdbError().code, myTrans->getNdbError().message);        myNdb.closeTransaction(myTrans);        break;      }                myRecAttrArr[j-1] = myOp->getValue("Y", NULL);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?