KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > tx > LockAccounting


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.tx;
5
6 import com.tc.object.lockmanager.api.LockID;
7
8 import java.util.Collection JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Set JavaDoc;
15
16 public class LockAccounting {
17
18   private final Map JavaDoc tx2Locks = new HashMap JavaDoc();
19   private final Map JavaDoc lock2Txs = new HashMap JavaDoc();
20
21   public synchronized Object JavaDoc dump() {
22     return "LockAccounting:\ntx2Locks=" + tx2Locks + "\nlock2Txs=" + lock2Txs + "/LockAccounting";
23   }
24
25   public String JavaDoc toString() {
26     return "LockAccounting[tx2Locks=" + tx2Locks + ", lock2Txs=" + lock2Txs + "]";
27   }
28
29   public synchronized void add(TransactionID txID, Collection JavaDoc lockIDs) {
30     getOrCreateSetFor(txID, tx2Locks).addAll(lockIDs);
31     for (Iterator JavaDoc i = lockIDs.iterator(); i.hasNext();) {
32       LockID lid = (LockID) i.next();
33       getOrCreateSetFor(lid, lock2Txs).add(txID);
34     }
35   }
36
37   public synchronized Collection JavaDoc getTransactionsFor(LockID lockID) {
38     Collection JavaDoc rv = new HashSet JavaDoc();
39     Collection JavaDoc toAdd = (Collection JavaDoc) lock2Txs.get(lockID);
40     if (toAdd != null) {
41       rv.addAll(toAdd);
42     }
43     return rv;
44   }
45
46   // This method returns a set of lockIds that has no more transactions to wait for
47
public synchronized Set JavaDoc acknowledge(TransactionID txID) {
48     Set JavaDoc completedLockIDs = null;
49     Set JavaDoc lockIDs = getSetFor(txID, tx2Locks);
50     if (lockIDs != null) {
51       // this may be null if there are phantom acknowledgements caused by server restart.
52
for (Iterator JavaDoc i = lockIDs.iterator(); i.hasNext();) {
53         LockID lid = (LockID) i.next();
54         Set JavaDoc txIDs = getOrCreateSetFor(lid, lock2Txs);
55         if (!txIDs.remove(txID)) throw new AssertionError JavaDoc("No lock=>transaction found for " + lid + ", " + txID);
56         if (txIDs.isEmpty()) {
57           lock2Txs.remove(lid);
58           if (completedLockIDs == null) {
59             completedLockIDs = new HashSet JavaDoc();
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 JavaDoc getSetFor(Object JavaDoc key, Map JavaDoc m) {
74     return (Set JavaDoc) m.get(key);
75   }
76
77   private static Set JavaDoc getOrCreateSetFor(Object JavaDoc key, Map JavaDoc m) {
78     Set JavaDoc rv = getSetFor(key, m);
79     if (rv == null) {
80       rv = new HashSet JavaDoc();
81       m.put(key, rv);
82     }
83     return rv;
84   }
85
86 }
87
Popular Tags