package org.jboss.cache;
import org.jboss.cache.lock.IdentityLock;
import org.jboss.logging.Logger;
import org.jgroups.blocks.MethodCall;
import javax.transaction.Transaction;
import java.util.*;
import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
public class TransactionTable {
protected ConcurrentReaderHashMap tx_map=new ConcurrentReaderHashMap();
protected HashMap txs=new HashMap();
private static final Logger log=Logger.getLogger(TransactionTable.class);
public TransactionTable() {
;
}
public int getNumLocalTransactions() {
return tx_map.size();
}
public int getNumGlobalTransactions() {
return txs.size();
}
public GlobalTransaction get(Transaction tx) {
return tx != null ? (GlobalTransaction)tx_map.get(tx) : null;
}
public Transaction getLocalTransaction(GlobalTransaction gtx) {
Map.Entry entry;
Transaction local_tx=null;
GlobalTransaction global_tx;
if(gtx == null)
return null;
synchronized(tx_map) {
for(Iterator it=tx_map.entrySet().iterator(); it.hasNext();) {
entry=(Map.Entry)it.next();
local_tx=(Transaction)entry.getKey();
global_tx=(GlobalTransaction)entry.getValue();
if(gtx.equals(global_tx)) {
return local_tx;
}
}
}
return null;
}
public void put(Transaction tx, GlobalTransaction gtx) {
if(tx == null) {
log.error("key (Transaction) is null");
return;
}
tx_map.put(tx, gtx);
}
public TransactionEntry get(GlobalTransaction gtx) {
return gtx != null ? (TransactionEntry)txs.get(gtx) : null;
}
public void put(GlobalTransaction tx, TransactionEntry entry) {
if(tx == null) {
log.error("key (GlobalTransaction) is null");
return;
}
txs.put(tx, entry);
}
public TransactionEntry remove(GlobalTransaction tx) {
return (TransactionEntry)txs.remove(tx);
}
public GlobalTransaction remove(Transaction tx) {
if(tx == null)
return null;
return (GlobalTransaction)tx_map.remove(tx);
}
public void addModification(GlobalTransaction gtx, MethodCall m) {
TransactionEntry entry=get(gtx);
if(entry == null) {
log.error("transaction not found (gtx=" + gtx + ")");
return;
}
entry.addModification(m);
}
public void addUndoOperation(GlobalTransaction gtx, MethodCall m) {
TransactionEntry entry=get(gtx);
if(entry == null) {
log.error("transaction not found (gtx=" + gtx + ")");
return;
}
entry.addUndoOperation(m);
}
public void addNode(GlobalTransaction gtx, Fqn node) {
TransactionEntry entry=get(gtx);
if(entry == null) {
log.error("transaction not found (gtx=" + gtx + ")");
return;
}
entry.addNode(node);
}
public void addLock(GlobalTransaction gtx, IdentityLock l) {
TransactionEntry entry=get(gtx);
if(entry == null) {
log.error("transaction entry not found for (gtx=" + gtx + ")");
return;
}
entry.addLock(l);
}
public void addLocks(GlobalTransaction gtx, Collection locks) {
TransactionEntry entry=get(gtx);
if(entry == null) {
log.error("transaction entry not found for (gtx=" + gtx + ")");
return;
}
entry.addLocks(locks);
}
public String toString() {
StringBuffer sb=new StringBuffer();
sb.append(tx_map.size()).append(" mappings, ");
sb.append(txs.size()).append(" transactions");
return sb.toString();
}
public String toString(boolean print_details) {
if(!print_details)
return toString();
StringBuffer sb=new StringBuffer();
Map.Entry entry;
sb.append("LocalTransactions: ").append(tx_map.size()).append("\n");
sb.append("GlobalTransactions: ").append(txs.size()).append("\n");
sb.append("tx_map:\n");
for(Iterator it=tx_map.entrySet().iterator(); it.hasNext();) {
entry=(Map.Entry)it.next();
sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
}
sb.append("txs:\n");
for(Iterator it=txs.entrySet().iterator(); it.hasNext();) {
entry=(Map.Entry)it.next();
sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
}
return sb.toString();
}
}