summaryrefslogtreecommitdiff
path: root/ndb/test/ndbapi/bank/Bank.hpp
blob: d9dd7b2594483da23f6c1cc072dcafb9d5eccc88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* 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 */

#ifndef BANK_HPP
#define BANK_HPP

#include <NdbOut.hpp>
#include <NdbApi.hpp>
#include <NDBT.hpp>
#include <NdbTick.h>
#include <random.h>


class Bank {
public:  

  Bank(Ndb_cluster_connection&);

  int createAndLoadBank(bool overWrite, int num_accounts=10);
  int dropBank();
  
  int performTransactions(int maxSleepBetweenTrans = 20, int yield=0);
  int performMakeGLs(int yield=0);
  int performValidateAllGLs();
  int performSumAccounts(int maxSleepBetweenSums = 2000, int yield=0);
  int performIncreaseTime(int maxSleepBetweenDays = 30, int yield=0);
private:

  int init();

  enum TransactionTypes{
    WithDrawal = 2000,
    Deposit = 3000
  };
  
  static const int NOT_ENOUGH_FUNDS = 1000;
  static const int VERIFICATION_FAILED = 1001;  

  int performTransaction();
  int performTransaction(int fromAccountId,
			 int toAccountId,
			 int amount );
  int performTransactionImpl1(int fromAccountId,
			      int toAccountId,
			      int amount );

  int performValidateGLs(Uint64 age = 20);
  int performValidateGL(Uint64 GLTime);
  int performValidatePurged();

  int performMakeGL(int time);
  int performMakeGLForAccountType(NdbConnection* pTrans, 
				  Uint64 time,
				  Uint32 accountTypeId);
  int sumTransactionsForGL(const Uint64 time, 
			   const Uint32 accountType,
			   Uint32& balance,
			   Uint32& withdrawalCount,
			   Uint32& withdrawalSum,
			   Uint32& depositSum,
			   Uint32& depositCount,
			   Uint32& transactionsCount,
			   NdbConnection* pTrans);
  int getBalanceForAccountType(const Uint32 accountType,
			       Uint32& balance);
  int getBalanceForGL(const Uint64 glTime,
		      const Uint32 accountType,
		      Uint32 &balance);
    
  int checkNoTransactionsOlderThan(const Uint32 accountType,
				   const Uint64 oldest);
  int getOldestPurgedGL(const Uint32 accountType,
			Uint64 &oldest);
  int getOldestNotPurgedGL(Uint64 &oldest,
			   Uint32 &accountTypeId,
			   bool &found);
  int findLastGL(Uint64 &lastTime);
  int purgeOldGLTransactions(Uint64 currTime, Uint32 age);

  int purgeTransactions(const Uint64 glTime, 
			const Uint32 accountTypeId);
  int findTransactionsToPurge(const Uint64 glTime, 
			     const Uint32 accountType,
			     NdbConnection* pTrans);


  int getSumAccounts(Uint32 &sumAccounts, 
		     Uint32 &numAccounts);
  int getNumAccounts();
  int getNumAccountTypes();
  int getMaxAmount();


  enum SystemValueId {
    LastTransactionId = 0,
    CurrentTime = 1
  };


  int readSystemValue(SystemValueId sysValId, Uint64 & value);
  int increaseSystemValue(SystemValueId sysValId, Uint64 &value);
  int increaseSystemValue2(SystemValueId sysValId, Uint64 &value);
  int writeSystemValue(SystemValueId sysValId, Uint64 value);
  int getNextTransactionId(Uint64 &value);
  int incCurrTime(Uint64 &value);
  int getCurrTime(Uint64 &time);

  int prepareReadSystemValueOp(NdbConnection*, SystemValueId sysValId, Uint64 &time);
  int prepareGetCurrTimeOp(NdbConnection*, Uint64 &time);

  int createTables();
  int createTable(const char* tabName);

  int dropTables();
  int dropTable(const char* tabName);

  int clearTables();
  int clearTable(const char* tabName);

  int loadGl();
  int loadAccountType();
  int loadAccount (int numAccounts);
  int loadSystemValues();

private:

  Ndb m_ndb;
  int m_maxAccount;
  bool m_initialized;
};

#endif