1 4 package com.tc.object.tx; 5 6 import com.tc.object.lockmanager.api.LockID; 7 8 import java.util.Collection ; 9 import java.util.Collections ; 10 import java.util.HashMap ; 11 import java.util.HashSet ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 import java.util.Set ; 15 16 public class LockAccounting { 17 18 private final Map tx2Locks = new HashMap (); 19 private final Map lock2Txs = new HashMap (); 20 21 public synchronized Object dump() { 22 return "LockAccounting:\ntx2Locks=" + tx2Locks + "\nlock2Txs=" + lock2Txs + "/LockAccounting"; 23 } 24 25 public String toString() { 26 return "LockAccounting[tx2Locks=" + tx2Locks + ", lock2Txs=" + lock2Txs + "]"; 27 } 28 29 public synchronized void add(TransactionID txID, Collection lockIDs) { 30 getOrCreateSetFor(txID, tx2Locks).addAll(lockIDs); 31 for (Iterator i = lockIDs.iterator(); i.hasNext();) { 32 LockID lid = (LockID) i.next(); 33 getOrCreateSetFor(lid, lock2Txs).add(txID); 34 } 35 } 36 37 public synchronized Collection getTransactionsFor(LockID lockID) { 38 Collection rv = new HashSet (); 39 Collection toAdd = (Collection ) lock2Txs.get(lockID); 40 if (toAdd != null) { 41 rv.addAll(toAdd); 42 } 43 return rv; 44 } 45 46 public synchronized Set acknowledge(TransactionID txID) { 48 Set completedLockIDs = null; 49 Set lockIDs = getSetFor(txID, tx2Locks); 50 if (lockIDs != null) { 51 for (Iterator i = lockIDs.iterator(); i.hasNext();) { 53 LockID lid = (LockID) i.next(); 54 Set txIDs = getOrCreateSetFor(lid, lock2Txs); 55 if (!txIDs.remove(txID)) throw new AssertionError ("No lock=>transaction found for " + lid + ", " + txID); 56 if (txIDs.isEmpty()) { 57 lock2Txs.remove(lid); 58 if (completedLockIDs == null) { 59 completedLockIDs = new HashSet (); 60 } 61 completedLockIDs.add(lid); 62 } 63 } 64 } 65 tx2Locks.remove(txID); 66 return (completedLockIDs == null ? Collections.EMPTY_SET : completedLockIDs); 67 } 68 69 public synchronized boolean isEmpty() { 70 return tx2Locks.isEmpty() && lock2Txs.isEmpty(); 71 } 72 73 private static Set getSetFor(Object key, Map m) { 74 return (Set ) m.get(key); 75 } 76 77 private static Set getOrCreateSetFor(Object key, Map m) { 78 Set rv = getSetFor(key, m); 79 if (rv == null) { 80 rv = new HashSet (); 81 m.put(key, rv); 82 } 83 return rv; 84 } 85 86 } 87 | Popular Tags |