📄 testindex.cpp
字号:
/* 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 */#include <NDBT.hpp>#include <NDBT_Test.hpp>#include <HugoTransactions.hpp>#include <UtilTransactions.hpp>#include <NdbRestarter.hpp>#include <NdbRestarts.hpp>#include <Vector.hpp>#include <signaldata/DumpStateOrd.hpp>#define CHECK(b) if (!(b)) { \ g_err << "ERR: "<< step->getName() \ << " failed on line " << __LINE__ << endl; \ result = NDBT_FAILED; break;\} struct Attrib { bool indexCreated; int numAttribs; int attribs[1024]; Attrib(){ numAttribs = 0; indexCreated = false; }};class AttribList {public: AttribList(){}; ~AttribList(){ for(size_t i = 0; i < attriblist.size(); i++){ delete attriblist[i]; } }; void buildAttribList(const NdbDictionary::Table* pTab); Vector<Attrib*> attriblist;};void AttribList::buildAttribList(const NdbDictionary::Table* pTab){ attriblist.clear(); Attrib* attr; // Build attrib definitions that describes which attributes to build index // Try to build strange combinations, not just "all" or all PK's int i; for(i = 1; i <= pTab->getNoOfColumns(); i++){ attr = new Attrib; attr->numAttribs = i; for(int a = 0; a<i; a++) attr->attribs[a] = a; attriblist.push_back(attr); } int b = 0; for(i = pTab->getNoOfColumns()-1; i > 0; i--){ attr = new Attrib; attr->numAttribs = i; b++; for(int a = 0; a<i; a++) attr->attribs[a] = a+b; attriblist.push_back(attr); } for(i = pTab->getNoOfColumns(); i > 0; i--){ attr = new Attrib; attr->numAttribs = pTab->getNoOfColumns() - i; for(int a = 0; a<pTab->getNoOfColumns() - i; a++) attr->attribs[a] = pTab->getNoOfColumns()-a-1; attriblist.push_back(attr); } for(i = 1; i < pTab->getNoOfColumns(); i++){ attr = new Attrib; attr->numAttribs = pTab->getNoOfColumns() - i; for(int a = 0; a<pTab->getNoOfColumns() - i; a++) attr->attribs[a] = pTab->getNoOfColumns()-a-1; attriblist.push_back(attr); } for(i = 1; i < pTab->getNoOfColumns(); i++){ attr = new Attrib; attr->numAttribs = 2; for(int a = 0; a<2; a++){ attr->attribs[a] = i%pTab->getNoOfColumns(); } attriblist.push_back(attr); } // Last attr = new Attrib; attr->numAttribs = 1; attr->attribs[0] = pTab->getNoOfColumns()-1; attriblist.push_back(attr); // Last and first attr = new Attrib; attr->numAttribs = 2; attr->attribs[0] = pTab->getNoOfColumns()-1; attr->attribs[1] = 0; attriblist.push_back(attr); // First and last attr = new Attrib; attr->numAttribs = 2; attr->attribs[0] = 0; attr->attribs[1] = pTab->getNoOfColumns()-1; attriblist.push_back(attr); #if 0 for(size_t i = 0; i < attriblist.size(); i++){ ndbout << attriblist[i]->numAttribs << ": " ; for(int a = 0; a < attriblist[i]->numAttribs; a++) ndbout << attriblist[i]->attribs[a] << ", "; ndbout << endl; }#endif}char idxName[255];char pkIdxName[255];static const int SKIP_INDEX = 99;int create_index(NDBT_Context* ctx, int indxNum, const NdbDictionary::Table* pTab, Ndb* pNdb, Attrib* attr, bool logged){ bool orderedIndex = ctx->getProperty("OrderedIndex", (unsigned)0); int result = NDBT_OK; HugoCalculator calc(*pTab); if (attr->numAttribs == 1 && calc.isUpdateCol(attr->attribs[0]) == true){ // Don't create index for the Hugo update column // since it's not unique return SKIP_INDEX; } // Create index BaseString::snprintf(idxName, 255, "IDC%d", indxNum); if (orderedIndex) ndbout << "Creating " << ((logged)?"logged ": "temporary ") << "ordered index "<<idxName << " ("; else ndbout << "Creating " << ((logged)?"logged ": "temporary ") << "unique index "<<idxName << " ("; ndbout << flush; NdbDictionary::Index pIdx(idxName); pIdx.setTable(pTab->getName()); if (orderedIndex) pIdx.setType(NdbDictionary::Index::OrderedIndex); else pIdx.setType(NdbDictionary::Index::UniqueHashIndex); for (int c = 0; c< attr->numAttribs; c++){ int attrNo = attr->attribs[c]; pIdx.addIndexColumn(pTab->getColumn(attrNo)->getName()); ndbout << pTab->getColumn(attrNo)->getName()<<" "; } pIdx.setStoredIndex(logged); ndbout << ") "; if (pNdb->getDictionary()->createIndex(pIdx) != 0){ attr->indexCreated = false; ndbout << "FAILED!" << endl; const NdbError err = pNdb->getDictionary()->getNdbError(); ERR(err); if(err.classification == NdbError::ApplicationError) return SKIP_INDEX; return NDBT_FAILED; } else { ndbout << "OK!" << endl; attr->indexCreated = true; } return result;}int drop_index(int indxNum, Ndb* pNdb, const NdbDictionary::Table* pTab, Attrib* attr){ int result = NDBT_OK; if (attr->indexCreated == false) return NDBT_OK; BaseString::snprintf(idxName, 255, "IDC%d", indxNum); // Drop index ndbout << "Dropping index "<<idxName<<"(" << pTab->getName() << ") "; if (pNdb->getDictionary()->dropIndex(idxName, pTab->getName()) != 0){ ndbout << "FAILED!" << endl; ERR(pNdb->getDictionary()->getNdbError()); result = NDBT_FAILED; } else { ndbout << "OK!" << endl; } return result;}int runCreateIndexes(NDBT_Context* ctx, NDBT_Step* step){ int loops = ctx->getNumLoops(); int l = 0; const NdbDictionary::Table* pTab = ctx->getTab(); Ndb* pNdb = GETNDB(step); int result = NDBT_OK; // NOTE If we need to test creating both logged and non logged indexes // this should be divided into two testcases // The paramater logged should then be specified // as a TC_PROPERTY. ex TC_PROPERTY("LoggedIndexes", 1); // and read into the test step like bool logged = ctx->getProperty("LoggedIndexes", 1); AttribList attrList; attrList.buildAttribList(pTab); while (l < loops && result == NDBT_OK){ unsigned int i; for (i = 0; i < attrList.attriblist.size(); i++){ // Try to create index if (create_index(ctx, i, pTab, pNdb, attrList.attriblist[i], logged) == NDBT_FAILED) result = NDBT_FAILED; } // Now drop all indexes that where created for (i = 0; i < attrList.attriblist.size(); i++){ // Try to drop index if (drop_index(i, pNdb, pTab, attrList.attriblist[i]) != NDBT_OK) result = NDBT_FAILED; } l++; } return result;}int createRandomIndex(NDBT_Context* ctx, NDBT_Step* step){ const NdbDictionary::Table* pTab = ctx->getTab(); Ndb* pNdb = GETNDB(step); bool logged = ctx->getProperty("LoggedIndexes", 1); AttribList attrList; attrList.buildAttribList(pTab); int retries = 100; while(retries > 0){ const Uint32 i = rand() % attrList.attriblist.size(); int res = create_index(ctx, i, pTab, pNdb, attrList.attriblist[i], logged); if (res == SKIP_INDEX){ retries--; continue; } if (res == NDBT_FAILED){ return NDBT_FAILED; } ctx->setProperty("createRandomIndex", i); // Now drop all indexes that where created return NDBT_OK; } return NDBT_FAILED;}int createRandomIndex_Drop(NDBT_Context* ctx, NDBT_Step* step){ Ndb* pNdb = GETNDB(step); Uint32 i = ctx->getProperty("createRandomIndex"); BaseString::snprintf(idxName, 255, "IDC%d", i); // Drop index ndbout << "Dropping index " << idxName << " "; if (pNdb->getDictionary()->dropIndex(idxName, ctx->getTab()->getName()) != 0){ ndbout << "FAILED!" << endl; ERR(pNdb->getDictionary()->getNdbError()); return NDBT_FAILED; } else { ndbout << "OK!" << endl; } return NDBT_OK;}int createPkIndex(NDBT_Context* ctx, NDBT_Step* step){ bool orderedIndex = ctx->getProperty("OrderedIndex", (unsigned)0); const NdbDictionary::Table* pTab = ctx->getTab(); Ndb* pNdb = GETNDB(step); bool logged = ctx->getProperty("LoggedIndexes", 1); // Create index BaseString::snprintf(pkIdxName, 255, "IDC_PK_%s", pTab->getName()); if (orderedIndex) ndbout << "Creating " << ((logged)?"logged ": "temporary ") << "ordered index " << pkIdxName << " ("; else ndbout << "Creating " << ((logged)?"logged ": "temporary ") << "unique index " << pkIdxName << " ("; NdbDictionary::Index pIdx(pkIdxName); pIdx.setTable(pTab->getName()); if (orderedIndex) pIdx.setType(NdbDictionary::Index::OrderedIndex); else pIdx.setType(NdbDictionary::Index::UniqueHashIndex); for (int c = 0; c< pTab->getNoOfColumns(); c++){ const NdbDictionary::Column * col = pTab->getColumn(c); if(col->getPrimaryKey()){ pIdx.addIndexColumn(col->getName()); ndbout << col->getName() <<" "; } } pIdx.setStoredIndex(logged); ndbout << ") "; if (pNdb->getDictionary()->createIndex(pIdx) != 0){ ndbout << "FAILED!" << endl; const NdbError err = pNdb->getDictionary()->getNdbError(); ERR(err); return NDBT_FAILED; } ndbout << "OK!" << endl; return NDBT_OK;}int createPkIndex_Drop(NDBT_Context* ctx, NDBT_Step* step){ const NdbDictionary::Table* pTab = ctx->getTab(); Ndb* pNdb = GETNDB(step); // Drop index ndbout << "Dropping index " << pkIdxName << " "; if (pNdb->getDictionary()->dropIndex(pkIdxName, pTab->getName()) != 0){ ndbout << "FAILED!" << endl; ERR(pNdb->getDictionary()->getNdbError()); return NDBT_FAILED; } else { ndbout << "OK!" << endl; } return NDBT_OK;}intrunVerifyIndex(NDBT_Context* ctx, NDBT_Step* step){ // Verify that data in index match // table data Ndb* pNdb = GETNDB(step); UtilTransactions utilTrans(*ctx->getTab()); const int batchSize = ctx->getProperty("BatchSize", 16); const int parallelism = batchSize > 240 ? 240 : batchSize; do { if (utilTrans.verifyIndex(pNdb, idxName, parallelism, true) != 0){ g_err << "Inconsistent index" << endl; return NDBT_FAILED; } } while(ctx->isTestStopped() == false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -