acid2.cpp

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

CPP
694
字号
/* 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 <NdbApi.hpp>#include <NdbOut.hpp>#include <NdbThread.h>#include <NdbSleep.h>#include <NdbMutex.h>#include "TraceNdbApi.hpp"#include "VerifyNdbApi.hpp"#define Ndb CTraceNdb#define NdbSchemaCon CTraceNdbSchemaCon#define NdbSchemaOp CTraceNdbSchemaOp#define NdbConnection CTraceNdbConnection#define NdbOperation CTraceNdbOperation#define NdbIndexOperation CTraceNdbIndexOperation#define NdbRecAttr CTraceNdbRecAttr#define Table CTraceTable#define Index CTraceIndex#define Column CTraceColumn#define NdbDictionary CTraceNdbDictionary/*#define Ndb CVerifyNdb#define NdbSchemaCon CVerifyNdbSchemaCon#define NdbSchemaOp CVerifyNdbSchemaOp#define NdbConnection CVerifyNdbConnection#define NdbOperation CVerifyNdbOperation#define NdbIndexOperation CVerifyNdbIndexOperation#define NdbRecAttr CVerifyNdbRecAttr#define Table CVerifyTable#define Index CVerifyIndex#define Column CVerifyColumn#define NdbDictionary CVerifyNdbDictionary*/NdbMutex* g_pNdbMutexStop = 0;Uint32 g_nPart = 1;Uint32 g_nTable = 1;Uint32 g_nTuple = 1;Uint32 g_nAttribute = 1;char* g_szTable = 0;char* g_szIndex = 0;char* g_szAttribute = 0;bool g_bVerify = false;bool g_bUseIndex = false;#define N 624#define M 397#define MATRIX_A 0x9908b0df#define UPPER_MASK 0x80000000#define LOWER_MASK 0x7fffffff#define TEMPERING_MASK_B 0x9d2c5680#define TEMPERING_MASK_C 0xefc60000#define TEMPERING_SHIFT_U(y)  (y >> 11)#define TEMPERING_SHIFT_S(y)  (y << 7)#define TEMPERING_SHIFT_T(y)  (y << 15)#define TEMPERING_SHIFT_L(y)  (y >> 18)class MT19937{public:    MT19937(void);    void sgenrand(unsigned long seed);    unsigned long genrand(void);private:    unsigned long mt[N];    int mti;    unsigned long mag01[2];};MT19937::MT19937(void){    mti = N+1;    mag01[0] = 0x0;    mag01[1] = MATRIX_A;    sgenrand(4357);}void MT19937::sgenrand(unsigned long seed){    mt[0]= seed & 0xffffffff;    for (mti=1; mti<N; mti++)        mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;}unsigned long MT19937::genrand(void){    unsigned long y;    if (mti >= N) {        int kk;        if (mti == N+1)        {            sgenrand(4357);        }        for (kk=0;kk<N-M;kk++) {            y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);            mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];        }        for (;kk<N-1;kk++) {            y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);            mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];        }        y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);        mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];        mti = 0;    }    y = mt[mti++];    y ^= TEMPERING_SHIFT_U(y);    y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;    y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;    y ^= TEMPERING_SHIFT_L(y);    return y; }void CreateTables(Ndb* pNdb){    for(Uint32 iTable=0; iTable<g_nTable; ++iTable)    {        NdbDictionary::Dictionary* pDictionary = pNdb->getDictionary();        NdbDictionary::Table table;        table.setName(g_szTable+iTable*4);        NdbDictionary::Index index;        index.setName(g_szIndex+iTable*4);        index.setTable(table.getName());        index.setType(NdbDictionary::Index::UniqueHashIndex);        NdbDictionary::Column columnPK;        columnPK.setName("PK");        columnPK.setTupleKey(true);        table.addColumn(columnPK);        index.addIndexColumn(columnPK.getName());        for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)        {            NdbDictionary::Column columnAttr;            columnAttr.setName(g_szAttribute+iAttr*4);            columnAttr.setTupleKey(false);            table.addColumn(columnAttr);        }        pDictionary->createTable(table);        pDictionary->createIndex(index);        /*        NdbSchemaCon* pNdbSchemaCon = pNdb->startSchemaTransaction();        NdbSchemaOp* pNdbSchemaOp = pNdbSchemaCon->getNdbSchemaOp();        pNdbSchemaOp->createTable(g_szTable+iTable*4);        pNdbSchemaOp->createAttribute("PK", TupleKey);        for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)        {        pNdbSchemaOp->createAttribute(g_szAttribute+iAttr*4, NoKey);        }        pNdbSchemaCon->execute();        pNdb->closeSchemaTransaction(pNdbSchemaCon);        */    }}int InsertTransaction(Ndb* pNdb, const Uint32 iPart, const bool bIndex){    int iExec = -1;    int iCode = -1;    NdbConnection* pNdbConnection = pNdb->startTransaction();    if(pNdbConnection)    {        for(Uint32 iTable=0; iTable<g_nTable; ++iTable)        {            for(Uint32 iTuple=0; iTuple<g_nTuple; ++iTuple)            {                if(bIndex)                {                    NdbIndexOperation* pNdbIndexOperation = pNdbConnection->getNdbIndexOperation(g_szIndex+iTable*4, g_szTable+iTable*4);                    pNdbIndexOperation->insertTuple();                    Uint32 nPK = iPart*g_nTuple + iTuple;                    pNdbIndexOperation->equal("PK", nPK);                    for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)                    {                        Uint32 nValue = ((iPart*g_nTable+iTable)*g_nTuple+iTuple)*g_nAttribute+iAttr;                        pNdbIndexOperation->setValue(g_szAttribute+iAttr*4, nValue);                    }                }                else                {                    NdbOperation* pNdbOperation = pNdbConnection->getNdbOperation(g_szTable+iTable*4);                    pNdbOperation->insertTuple();                    Uint32 nPK = iPart*g_nTuple + iTuple;                    pNdbOperation->equal("PK", nPK);                    for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)                    {                        Uint32 nValue = ((iPart*g_nTable+iTable)*g_nTuple+iTuple)*g_nAttribute+iAttr;                        pNdbOperation->setValue(g_szAttribute+iAttr*4, nValue);                    }                }            }        }        iExec = pNdbConnection->execute_ok(Commit);        if (iExec == -1)         {            ndbout << pNdbConnection->getNdbError() << endl;        }        pNdb->closeTransaction(pNdbConnection);    }    return 0;}int UpdateGetAndSetTransaction(Ndb* pNdb, const Uint32 iPart, const bool bIndex){    int iExec = -1;    int iCode = -1;    NdbRecAttr** ppNdbRecAttr = new NdbRecAttr*[g_nTable*g_nTuple*g_nAttribute];    NdbConnection* pNdbConnection = pNdb->startTransaction();    if(pNdbConnection)    {        for(Uint32 iTable=0; iTable<g_nTable; ++iTable)        {            for(Uint32 iTuple=0; iTuple<g_nTuple; ++iTuple)            {                if(bIndex)                {                    NdbIndexOperation* pNdbIndexOperation = pNdbConnection->getNdbIndexOperation(g_szIndex+iTable*4, g_szTable+iTable*4);                    pNdbIndexOperation->readTupleExclusive();                    Uint32 nPK = iPart*g_nTuple + iTuple;                    pNdbIndexOperation->equal("PK", nPK);                    for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)                    {                        ppNdbRecAttr[(iTable*g_nTuple+iTuple)*g_nAttribute+iAttr]                         = pNdbIndexOperation->getValue(g_szAttribute+iAttr*4);                    }                }                else                {                    NdbOperation* pNdbOperation = pNdbConnection->getNdbOperation(g_szTable+iTable*4);                    pNdbOperation->readTupleExclusive();                    Uint32 nPK = iPart*g_nTuple + iTuple;                    pNdbOperation->equal("PK", nPK);                    for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)                    {                        ppNdbRecAttr[(iTable*g_nTuple+iTuple)*g_nAttribute+iAttr]                         = pNdbOperation->getValue(g_szAttribute+iAttr*4);                    }                }            }        }        iExec = pNdbConnection->execute_ok(NoCommit);        if( iExec == -1)        {            ndbout << pNdbConnection->getNdbError() << endl;        }    }    iCode = pNdbConnection->getNdbError().code;    if(iExec==0)    {        for(Uint32 iTable=0; iTable<g_nTable; ++iTable)        {            for(Uint32 iTuple=0; iTuple<g_nTuple; ++iTuple)            {                if(bIndex)                {                    NdbIndexOperation* pNdbIndexOperation = pNdbConnection->getNdbIndexOperation(g_szIndex+iTable*4, g_szTable+iTable*4);                    pNdbIndexOperation->updateTuple();                    Uint32 nPK = iPart*g_nTuple + iTuple;                    pNdbIndexOperation->equal("PK", nPK);                    for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)                    {                        NdbRecAttr* pNdbRecAttr                            = ppNdbRecAttr[(iTable*g_nTuple+iTuple)*g_nAttribute+iAttr];                        Uint32 nValue = pNdbRecAttr->u_32_value() + 1;                        pNdbIndexOperation->setValue(g_szAttribute+iAttr*4, nValue);                    }                }                else                {                    NdbOperation* pNdbOperation = pNdbConnection->getNdbOperation(g_szTable+iTable*4);                    pNdbOperation->updateTuple();                    Uint32 nPK = iPart*g_nTuple + iTuple;                    pNdbOperation->equal("PK", nPK);                    for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)                    {                        NdbRecAttr* pNdbRecAttr                            = ppNdbRecAttr[(iTable*g_nTuple+iTuple)*g_nAttribute+iAttr];                        Uint32 nValue = pNdbRecAttr->u_32_value() + 1;                        pNdbOperation->setValue(g_szAttribute+iAttr*4, nValue);                    }                }            }        }        iExec = pNdbConnection->execute(Commit);        if (iExec == -1)         {            ndbout << pNdbConnection->getNdbError() << endl;        }        pNdb->closeTransaction(pNdbConnection);    }    delete[] ppNdbRecAttr;    return 0;}int UpdateInterpretedTransaction(Ndb* pNdb, const Uint32 iPart, const bool bIndex){    int iExec = -1;    int iCode = -1;    NdbConnection* pNdbConnection = pNdb->startTransaction();    if(pNdbConnection)    {        for(Uint32 iTable=0; iTable<g_nTable; ++iTable)        {            for(Uint32 iTuple=0; iTuple<g_nTuple; ++iTuple)            {                if(bIndex)                {                    NdbIndexOperation* pNdbIndexOperation = pNdbConnection->getNdbIndexOperation(g_szIndex+iTable*4, g_szTable+iTable*4);

⌨️ 快捷键说明

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