1 24 25 package com.mckoi.database; 26 27 import java.util.ArrayList ; 28 import com.mckoi.debug.Lvl; 29 30 43 44 final class OpenTransactionList { 45 46 49 private static final boolean TRACKING = false; 50 51 54 private TransactionSystem system; 55 56 60 private ArrayList open_transactions; 61 62 66 private ArrayList open_transaction_stacks; 67 68 71 private long minimum_commit_id; 72 73 76 private long maximum_commit_id; 77 78 81 OpenTransactionList(TransactionSystem system) { 82 this.system = system; 83 open_transactions = new ArrayList (); 84 if (TRACKING) { 85 open_transaction_stacks = new ArrayList (); 86 } 87 minimum_commit_id = Long.MAX_VALUE; 88 maximum_commit_id = 0; 89 } 90 91 95 synchronized void addTransaction(Transaction transaction) { 96 long current_commit_id = transaction.getCommitID(); 97 if (current_commit_id >= maximum_commit_id) { 98 open_transactions.add(transaction); 99 if (TRACKING) { 100 open_transaction_stacks.add(new Error ()); 101 } 102 system.stats().increment("OpenTransactionList.count"); 103 maximum_commit_id = current_commit_id; 104 } 105 else { 106 throw new Error ( 107 "Added a transaction with a lower than maximum commit_id"); 108 } 109 } 110 111 114 synchronized void removeTransaction(Transaction transaction) { 115 116 int size = open_transactions.size(); 117 int i = open_transactions.indexOf(transaction); 118 if (i == 0) { 119 if (i == size - 1) { 121 minimum_commit_id = Integer.MAX_VALUE; 123 maximum_commit_id = 0; 124 } 125 else { 126 minimum_commit_id = 127 ((Transaction) open_transactions.get(i + 1)).getCommitID(); 128 } 129 } 130 else if (i == open_transactions.size() - 1) { 131 maximum_commit_id = 133 ((Transaction) open_transactions.get(i - 1)).getCommitID(); 134 } 135 else if (i == -1) { 136 throw new Error ("Unable to find transaction in the list."); 137 } 138 open_transactions.remove(i); 139 if (TRACKING) { 140 open_transaction_stacks.remove(i); 141 } 142 system.stats().decrement("OpenTransactionList.count"); 143 144 if (TRACKING) { 145 system.Debug().write(Lvl.MESSAGE, this, "Stacks:"); 146 for (int n = 0; n < open_transaction_stacks.size(); ++n) { 147 system.Debug().writeException(Lvl.MESSAGE, 148 (Error ) open_transaction_stacks.get(n)); 149 } 150 } 151 152 } 153 154 157 synchronized int count() { 158 return open_transactions.size(); 159 } 160 161 166 synchronized long minimumCommitID(Transaction transaction) { 167 168 long minimum_commit_id = Long.MAX_VALUE; 169 if (open_transactions.size() > 0) { 170 Transaction test_transaction = (Transaction)open_transactions.get(0); 174 if (test_transaction != transaction) { 175 minimum_commit_id = test_transaction.getCommitID(); 176 } 177 else if (open_transactions.size() > 1) { 178 minimum_commit_id = 179 ((Transaction) open_transactions.get(1)).getCommitID(); 180 } 181 } 182 183 return minimum_commit_id; 184 185 } 186 187 public synchronized String toString() { 188 StringBuffer buf = new StringBuffer (); 189 buf.append("[ OpenTransactionList: "); 190 for (int i = 0; i < open_transactions.size(); ++i) { 191 Transaction t = (Transaction) open_transactions.get(i); 192 buf.append(t.getCommitID()); 193 buf.append(", "); 194 } 195 buf.append(" ]"); 196 return new String (buf); 197 } 198 199 } 200 | Popular Tags |