1 4 package com.tc.object.tx; 5 6 import gnu.trove.TLinkable; 7 import gnu.trove.TLinkedList; 8 9 import java.util.Collection ; 10 import java.util.HashMap ; 11 import java.util.HashSet ; 12 import java.util.Iterator ; 13 import java.util.LinkedList ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.Set ; 17 18 public class TransactionBatchAccounting { 19 20 private final Map batchesByTransaction = new HashMap (); 21 private final TLinkedList batches = new TLinkedList(); 22 private final Set completedTransactionIDs = new HashSet (); 23 private boolean stopped = false; 24 25 public Object dump() { 26 return this.toString(); 27 } 28 29 public String toString() { 30 return "TransactionBatchAccounting[batchesByTransaction=" + batchesByTransaction 31 + ", completedGlobalTransactionIDs=" + completedTransactionIDs + "]"; 32 } 33 34 public synchronized void addBatch(TxnBatchID batchID, Collection transactionIDs) { 35 if (stopped || transactionIDs.size() == 0) return; 36 BatchDescriptor desc = new BatchDescriptor(batchID, transactionIDs); 37 batches.add(desc); 38 for (Iterator i = transactionIDs.iterator(); i.hasNext();) { 39 TransactionID txID = (TransactionID) i.next(); 40 Object removed = batchesByTransaction.put(txID, desc); 41 if (removed != null) { throw new AssertionError ("TransactionID is already accounted for: " + txID + "=>" 42 + removed); } 43 } 44 } 45 46 public synchronized TxnBatchID getBatchByTransactionID(TransactionID txID) { 47 BatchDescriptor desc = (BatchDescriptor) batchesByTransaction.get(txID); 48 return desc == null ? TxnBatchID.NULL_BATCH_ID : desc.batchID; 49 } 50 51 58 public synchronized List addIncompleteBatchIDsTo(List c) { 59 for (Iterator i = batches.iterator(); i.hasNext();) { 60 BatchDescriptor desc = (BatchDescriptor) i.next(); 61 c.add(desc.batchID); 62 } 63 return c; 64 } 65 66 public synchronized TxnBatchID getMinIncompleteBatchID() { 67 return batches.isEmpty() ? TxnBatchID.NULL_BATCH_ID : ((BatchDescriptor) batches.getFirst()).batchID; 68 } 69 70 public synchronized TxnBatchID acknowledge(TransactionID txID) { 71 if (stopped) return TxnBatchID.NULL_BATCH_ID; 72 final TxnBatchID completed; 73 final BatchDescriptor desc = (BatchDescriptor) batchesByTransaction.remove(txID); 74 if (desc == null) throw new AssertionError ("Batch not found for " + txID); 75 completedTransactionIDs.add(txID); 76 if (desc.acknowledge(txID) == 0) { 77 batches.remove(desc); 79 completed = desc.batchID; 80 } else { 81 completed = TxnBatchID.NULL_BATCH_ID; 82 } 83 if (batches.size() == 0 && batchesByTransaction.size() > 0) { throw new AssertionError ( 84 "Batches list and batchesByTransaction map aren't zero at the same time"); } 85 return completed; 86 } 87 88 public synchronized Collection addCompletedTransactionIDsTo(Collection c) { 89 c.addAll(completedTransactionIDs); 90 return c; 91 } 92 93 public synchronized void clear() { 94 batches.clear(); 95 batchesByTransaction.clear(); 96 clearCompletedTransactionIds(); 97 } 98 99 public synchronized void clearCompletedTransactionIds() { 100 completedTransactionIDs.clear(); 101 } 102 103 private static final class BatchDescriptor implements TLinkable { 104 private final TxnBatchID batchID; 105 private final Set transactionIDs = new HashSet (); 106 107 private TLinkable next; 108 private TLinkable previous; 109 110 public BatchDescriptor(TxnBatchID batchID, Collection txIDs) { 111 this.batchID = batchID; 112 transactionIDs.addAll(txIDs); 113 } 114 115 public String toString() { 116 return "BatchDescriptor[" + batchID + ", transactionIDs=" + transactionIDs + "]"; 117 } 118 119 public int acknowledge(TransactionID txID) { 120 transactionIDs.remove(txID); 121 return transactionIDs.size(); 122 } 123 124 public TLinkable getNext() { 125 return next; 126 } 127 128 public TLinkable getPrevious() { 129 return previous; 130 } 131 132 public void setNext(TLinkable linkable) { 133 next = linkable; 134 } 135 136 public void setPrevious(TLinkable linkable) { 137 previous = linkable; 138 } 139 } 140 141 144 public synchronized List addIncompleteTransactionIDsTo(LinkedList list) { 145 list.addAll(batchesByTransaction.keySet()); 146 return list; 147 } 148 149 152 public synchronized void stop() { 153 this.stopped = true; 154 } 155 156 } 157 | Popular Tags |