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