1 /* Copyright (c) 2003-2007 MySQL AB
2    Use is subject to license terms
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
16 
17 #ifndef BANK_HPP
18 #define BANK_HPP
19 
20 #include <NdbOut.hpp>
21 #include <NdbApi.hpp>
22 #include <NDBT.hpp>
23 #include <NdbTick.h>
24 #include <random.h>
25 
26 
27 class Bank {
28 public:
29 
30   Bank(Ndb_cluster_connection&, bool init = true, const char *dbase="BANK");
31 
setSkipCreate(bool skip)32   void setSkipCreate(bool skip) { m_skip_create = skip; }
33   int createAndLoadBank(bool overWrite, bool disk= false, int num_accounts=10);
34   int dropBank();
35 
36   int performTransactions(int maxSleepBetweenTrans = 20, int yield=0);
37   int performMakeGLs(int yield=0);
38   int performValidateAllGLs();
39   int performSumAccounts(int maxSleepBetweenSums = 2000, int yield=0);
40   int performIncreaseTime(int maxSleepBetweenDays = 30, int yield=0);
41 private:
42 
43   int init();
44 
45   enum TransactionTypes{
46     WithDrawal = 2000,
47     Deposit = 3000
48   };
49 
50   static const int NOT_ENOUGH_FUNDS = 1000;
51   static const int VERIFICATION_FAILED = 1001;
52 
53   int performTransaction();
54   int performTransaction(int fromAccountId,
55 			 int toAccountId,
56 			 int amount );
57   int performTransactionImpl1(int fromAccountId,
58 			      int toAccountId,
59 			      int amount );
60 
61   int performValidateGLs(Uint64 age = 20);
62   int performValidateGL(Uint64 GLTime);
63   int performValidatePurged();
64 
65   int performMakeGL(int time);
66   int performMakeGLForAccountType(NdbConnection* pTrans,
67 				  Uint64 time,
68 				  Uint32 accountTypeId);
69   int sumTransactionsForGL(const Uint64 time,
70 			   const Uint32 accountType,
71 			   Uint32& balance,
72 			   Uint32& withdrawalCount,
73 			   Uint32& withdrawalSum,
74 			   Uint32& depositSum,
75 			   Uint32& depositCount,
76 			   Uint32& transactionsCount,
77 			   NdbConnection* pTrans);
78   int getBalanceForAccountType(const Uint32 accountType,
79 			       Uint32& balance);
80   int getBalanceForGL(const Uint64 glTime,
81 		      const Uint32 accountType,
82 		      Uint32 &balance);
83 
84   int checkNoTransactionsOlderThan(const Uint32 accountType,
85 				   const Uint64 oldest);
86   int getOldestPurgedGL(const Uint32 accountType,
87 			Uint64 &oldest);
88   int getOldestNotPurgedGL(Uint64 &oldest,
89 			   Uint32 &accountTypeId,
90 			   bool &found);
91   int findLastGL(Uint64 &lastTime);
92   int purgeOldGLTransactions(Uint64 currTime, Uint32 age);
93 
94   int purgeTransactions(const Uint64 glTime,
95 			const Uint32 accountTypeId);
96   int findTransactionsToPurge(const Uint64 glTime,
97 			     const Uint32 accountType,
98 			     NdbConnection* pTrans);
99 
100 
101   int getSumAccounts(Uint32 &sumAccounts,
102 		     Uint32 &numAccounts);
103   int getNumAccounts();
104   int getNumAccountTypes();
105   int getMaxAmount();
106 
107 
108   enum SystemValueId {
109     LastTransactionId = 0,
110     CurrentTime = 1
111   };
112 
113 
114   int readSystemValue(SystemValueId sysValId, Uint64 & value);
115   int increaseSystemValue(SystemValueId sysValId, Uint64 &value);
116   int increaseSystemValue2(SystemValueId sysValId, Uint64 &value);
117   int writeSystemValue(SystemValueId sysValId, Uint64 value);
118   int getNextTransactionId(Uint64 &value);
119   int incCurrTime(Uint64 &value);
120   int getCurrTime(Uint64 &time);
121 
122   int prepareReadSystemValueOp(NdbConnection*, SystemValueId sysValId, Uint64 &time);
123   int prepareGetCurrTimeOp(NdbConnection*, Uint64 &time);
124 
125   int createTables(bool disk);
126   int createTable(const char* tabName, bool disk);
127 
128   int dropTables();
129   int dropTable(const char* tabName);
130 
131   int clearTables();
132   int clearTable(const char* tabName);
133 
134   int loadGl();
135   int loadAccountType();
136   int loadAccount (int numAccounts);
137   int loadSystemValues();
138 
139 private:
140 
141   Ndb m_ndb;
142   int m_maxAccount;
143   bool m_initialized;
144   bool m_skip_create;
145 };
146 
147 #endif
148