📄 bank.cpp
字号:
g_err << "performValidateGLs verification failed" << endl; return NDBT_FAILED; } g_err << "performValidateGLs failed" << endl; return NDBT_FAILED; } /** * * */ result = performValidatePurged(); if (result != NDBT_OK){ if (result == VERIFICATION_FAILED){ g_err << "performValidatePurged verification failed" << endl; return NDBT_FAILED; } g_err << "performValidatePurged failed" << endl; return NDBT_FAILED; } return NDBT_OK; } return NDBT_FAILED; }int Bank::findLastGL(Uint64 &lastTime){ int check; /** * SELECT MAX(time) FROM GL */ NdbConnection* pScanTrans = m_ndb.startTransaction(); if (pScanTrans == NULL) { ERR(m_ndb.getNdbError()); return NDBT_FAILED; } NdbScanOperation* pOp = pScanTrans->getNdbScanOperation("GL"); if (pOp == NULL) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } if( pOp->readTuples() ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } check = pOp->interpret_exit_ok(); if( check == -1 ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } NdbRecAttr* timeRec = pOp->getValue("TIME"); if( timeRec ==NULL ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } check = pScanTrans->execute(NoCommit); if( check == -1 ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } int eof; int rows = 0; eof = pOp->nextResult(); lastTime = 0; while(eof == 0){ rows++; Uint64 t = timeRec->u_32_value(); if (t > lastTime) lastTime = t; eof = pOp->nextResult(); } if (eof == -1) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } m_ndb.closeTransaction(pScanTrans); return NDBT_OK;}int Bank::performMakeGL(int time){ g_info << "performMakeGL: " << time << endl; /** * Create one GL record for each account type. * All in the same transaction */ // Start transaction NdbConnection* pTrans = m_ndb.startTransaction(); if (pTrans == NULL){ ERR(m_ndb.getNdbError()); return NDBT_FAILED; } for (int i = 0; i < getNumAccountTypes(); i++){ if (performMakeGLForAccountType(pTrans, time, i) != NDBT_OK){ g_err << "performMakeGLForAccountType returned NDBT_FAILED"<<endl; m_ndb.closeTransaction(pTrans); return NDBT_FAILED; } } // Execute transaction if( pTrans->execute(Commit) == -1 ) { ERR(pTrans->getNdbError()); m_ndb.closeTransaction(pTrans); return NDBT_FAILED; } m_ndb.closeTransaction(pTrans); return NDBT_OK;}int Bank::performMakeGLForAccountType(NdbConnection* pTrans, Uint64 glTime, Uint32 accountTypeId){ int check; Uint32 balance = 0; Uint32 withdrawalCount = 0; Uint32 withdrawalSum = 0; Uint32 depositSum = 0; Uint32 depositCount = 0; Uint32 countTransactions = 0; Uint32 purged = 0; // Insert record in GL so that we know // that no one else is performing the same task // Set purged = 0 to indicate that TRANSACTION // records still exist NdbOperation* pOp = pTrans->getNdbOperation("GL"); if (pOp == NULL) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp->insertTuple(); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp->equal("TIME", glTime); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp->equal("ACCOUNT_TYPE", accountTypeId); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp->setValue("BALANCE", balance); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp->setValue("DEPOSIT_COUNT", depositCount); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp->setValue("DEPOSIT_SUM", depositSum); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp->setValue("WITHDRAWAL_COUNT", withdrawalCount); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp->setValue("WITHDRAWAL_SUM", withdrawalSum); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp->setValue("PURGED", purged); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pTrans->execute(NoCommit); if( check == -1 ) { ERR(pOp->getNdbError()); return NDBT_FAILED; } // Read previous GL record to get old balance NdbOperation* pOp2 = pTrans->getNdbOperation("GL"); if (pOp2 == NULL) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp2->readTuple(); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp2->equal("TIME", glTime-1); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp2->equal("ACCOUNT_TYPE", accountTypeId); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } NdbRecAttr* oldBalanceRec = pOp2->getValue("BALANCE"); if( oldBalanceRec == NULL ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pTrans->execute(NoCommit); if( check == -1 ) { ERR(pOp2->getNdbError()); return NDBT_FAILED; } Uint32 oldBalance = oldBalanceRec->u_32_value(); // ndbout << "oldBalance = "<<oldBalance<<endl; balance = oldBalance; // Start a scan transaction to search // for TRANSACTION records with TIME = time // and ACCOUNT_TYPE = accountTypeId // Build sum of all found transactions if (sumTransactionsForGL(glTime, accountTypeId, balance, withdrawalCount, withdrawalSum, depositSum, depositCount, countTransactions, pTrans) != NDBT_OK){ return NDBT_FAILED; } // ndbout << "sumTransactionsForGL completed" << endl; // ndbout << "balance="<<balance<<endl // << "withdrawalCount="<<withdrawalCount<<endl // << "withdrawalSum="<<withdrawalSum<<endl // << "depositCount="<<depositCount<<endl // << "depositSum="<<depositSum<<endl; NdbOperation* pOp3 = pTrans->getNdbOperation("GL"); if (pOp3 == NULL) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp3->updateTuple(); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp3->equal("TIME", glTime); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp3->equal("ACCOUNT_TYPE", accountTypeId); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp3->setValue("BALANCE", balance); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp3->setValue("DEPOSIT_COUNT", depositCount); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp3->setValue("DEPOSIT_SUM", depositSum); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp3->setValue("WITHDRAWAL_COUNT", withdrawalCount); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp3->setValue("WITHDRAWAL_SUM", withdrawalSum); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } check = pOp3->setValue("PURGED", purged); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } // Execute transaction check = pTrans->execute(NoCommit); if( check == -1 ) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } return NDBT_OK;}int Bank::sumTransactionsForGL(const Uint64 glTime, const Uint32 accountType, Uint32& balance, Uint32& withdrawalCount, Uint32& withdrawalSum, Uint32& depositSum, Uint32& depositCount, Uint32& transactionsCount, NdbConnection* pTrans){ int check; // g_info << "sumTransactionsForGL: " << glTime << ", " << accountType << endl; NdbConnection* pScanTrans = m_ndb.startTransaction(); if (pScanTrans == NULL) { ERR(m_ndb.getNdbError()); return NDBT_FAILED; } NdbScanOperation* pOp = pScanTrans->getNdbScanOperation("TRANSACTION"); if (pOp == NULL) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } if( pOp->readTuplesExclusive()) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } check = pOp->interpret_exit_ok(); if( check == -1 ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } NdbRecAttr* accountTypeRec = pOp->getValue("ACCOUNT_TYPE"); if( accountTypeRec ==NULL ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } NdbRecAttr* timeRec = pOp->getValue("TIME"); if( timeRec ==NULL ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } NdbRecAttr* transTypeRec = pOp->getValue("TRANSACTION_TYPE"); if( transTypeRec ==NULL ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } NdbRecAttr* amountRec = pOp->getValue("AMOUNT"); if( amountRec ==NULL ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } check = pScanTrans->execute(NoCommit); if( check == -1 ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } int eof; int rows = 0; int rowsFound = 0; eof = pOp->nextResult(); while(eof == 0){ rows++; Uint32 a = accountTypeRec->u_32_value(); Uint64 t = timeRec->u_64_value(); if (a == accountType && t == glTime){ rowsFound++; // One record found int transType = transTypeRec->u_32_value(); int amount = amountRec->u_32_value(); if (transType == WithDrawal){ withdrawalCount++; withdrawalSum += amount; balance -= amount; } else { assert(transType == Deposit); depositCount++; depositSum += amount; balance += amount; } } eof = pOp->nextResult(); if ((rows % 100) == 0){ // "refresh" ownner transaction every 100th row if (pTrans->refresh() == -1) { ERR(pTrans->getNdbError()); return NDBT_FAILED; } } } if (eof == -1) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } m_ndb.closeTransaction(pScanTrans); // ndbout << rows << " TRANSACTIONS have been read" << endl; transactionsCount = rowsFound; return NDBT_OK;} int Bank::performValidateGLs(Uint64 age){ Uint64 currTime; if (getCurrTime(currTime) != NDBT_OK){ return NDBT_FAILED; } Uint64 glTime = currTime - 1; while((glTime > 0) && ((glTime + age) >= currTime)){ int result = performValidateGL(glTime); if (result != NDBT_OK){ g_err << "performValidateGL failed" << endl; return result; } glTime--; } return NDBT_OK; }int Bank::performValidateGL(Uint64 glTime){ ndbout << "performValidateGL: " << glTime << endl; /** * Rules: * - There should be zero or NoAccountTypes GL records for each glTime * - If purged == 0, then the TRANSACTION table should be checked * to see that there are: * + DEPOSIT_COUNT deposit transactions with account_type == ACCOUNT_TYPE * and TIME == glTime. The sum of these transactions should be * DEPOSIT_SUM * + WITHDRAWAL_COUNT withdrawal transactions with account_type == * ACCOUNT_TYPE and TIME == glTime. The sum of these transactions * should be WITHDRAWAL_SUM * + BALANCE should be equal to the sum of all transactions plus * the balance of the previous GL record * - If purged == 1 then there should be NO transactions with TIME == glTime * and ACCOUNT_TYPE == account_type * */ int check; /** * SELECT * FROM GL WHERE account_type = @accountType and time = @time */ NdbConnection* pScanTrans = m_ndb.startTransaction(); if (pScanTrans == NULL) { ERR(m_ndb.getNdbError()); return NDBT_FAILED; } NdbScanOperation* pOp = pScanTrans->getNdbScanOperation("GL"); if (pOp == NULL) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } if( pOp->readTuples() ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } check = pOp->interpret_exit_ok(); if( check == -1 ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } NdbRecAttr* accountTypeRec = pOp->getValue("ACCOUNT_TYPE"); if( accountTypeRec ==NULL ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } NdbRecAttr* timeRec = pOp->getValue("TIME"); if( timeRec ==NULL ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } NdbRecAttr* purgedRec = pOp->getValue("PURGED"); if( purgedRec ==NULL ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } NdbRecAttr* balanceRec = pOp->getValue("BALANCE"); if( balanceRec ==NULL ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } NdbRecAttr* depositSumRec = pOp->getValue("DEPOSIT_SUM"); if( depositSumRec ==NULL ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } NdbRecAttr* depositCountRec = pOp->getValue("DEPOSIT_COUNT"); if( depositCountRec ==NULL ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } NdbRecAttr* withdrawalSumRec = pOp->getValue("WITHDRAWAL_SUM"); if( withdrawalSumRec ==NULL ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } NdbRecAttr* withdrawalCountRec = pOp->getValue("WITHDRAWAL_COUNT"); if( withdrawalCountRec ==NULL ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } check = pScanTrans->execute(NoCommit); if( check == -1 ) { ERR(pScanTrans->getNdbError()); m_ndb.closeTransaction(pScanTrans); return NDBT_FAILED; } int eof; int rows = 0; int countGlRecords = 0; int result = NDBT_OK; eof = pOp->nextResult();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -