Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 266   Methods: 17
NCLOC: 160   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TransactionTable.java 60.7% 72.9% 94.1% 73%
coverage coverage
 1    /*
 2    * JBoss, the OpenSource J2EE webOS
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache.transaction;
 8   
 9    import org.apache.commons.logging.Log;
 10    import org.apache.commons.logging.LogFactory;
 11    import org.jboss.cache.lock.NodeLock;
 12    import org.jboss.cache.marshall.MethodCall;
 13   
 14    import javax.transaction.Transaction;
 15    import java.util.Collection;
 16    import java.util.Map;
 17    import java.util.concurrent.ConcurrentHashMap;
 18   
 19    /**
 20    * Maintains the mapping between a local {@link Transaction} and a {@link GlobalTransaction}.
 21    * Also stores {@link TransactionEntry} instances under a given transaction.
 22    *
 23    * @author <a href="mailto:bela@jboss.org">Bela Ban</a> Apr 14, 2003
 24    * @version $Revision: 1.4 $
 25    */
 26    public class TransactionTable
 27    {
 28   
 29    /**
 30    * Mapping between local (javax.transaction.Transaction)
 31    * and GlobalTransactions.
 32    * New: a local TX can have a number of GTXs
 33    */
 34    protected Map<Transaction, GlobalTransaction> tx_map = new ConcurrentHashMap<Transaction, GlobalTransaction>();
 35   
 36    /**
 37    * Mappings between GlobalTransactions and modifications.
 38    */
 39    protected Map<GlobalTransaction, TransactionEntry> txs = new ConcurrentHashMap<GlobalTransaction, TransactionEntry>();
 40   
 41    /**
 42    * our logger
 43    */
 44    private static final Log log = LogFactory.getLog(TransactionTable.class);
 45   
 46    /**
 47    * Constructs a new table.
 48    */
 49  2871 public TransactionTable()
 50    {
 51    }
 52   
 53    /**
 54    * Returns the number of local transactions.
 55    */
 56  101 public int getNumLocalTransactions()
 57    {
 58  101 return tx_map.size();
 59    }
 60   
 61    /**
 62    * Returns the number of global transactions.
 63    */
 64  102 public int getNumGlobalTransactions()
 65    {
 66  102 return txs.size();
 67    }
 68   
 69    /**
 70    * Returns the global transaction associated with the local transaction.
 71    * Returns null if tx is null or it was not found.
 72    */
 73  4069537 public GlobalTransaction get(Transaction tx)
 74    {
 75  4069524 if (tx == null)
 76  1083161 return null;
 77  2986376 return tx_map.get(tx);
 78    }
 79   
 80    /**
 81    * Returns the local transaction associated with a GlobalTransaction. Not
 82    * very efficient as the values have to be iterated over, don't use
 83    * frequently
 84    *
 85    * @param gtx The GlobalTransaction
 86    * @return Transaction. The local transaction associated with a given
 87    * GlobalTransaction). This will be null if no local transaction is
 88    * associated with a given GTX
 89    */
 90  1488 public Transaction getLocalTransaction(GlobalTransaction gtx)
 91    {
 92  1488 Transaction local_tx;
 93  1488 GlobalTransaction global_tx;
 94   
 95  1488 if (gtx == null)
 96  0 return null;
 97  1488 for (Map.Entry<Transaction, GlobalTransaction> entry : tx_map.entrySet())
 98    {
 99  811 local_tx = entry.getKey();
 100  811 global_tx = entry.getValue();
 101  811 if (gtx.equals(global_tx))
 102    {
 103  730 return local_tx;
 104    }
 105    }
 106  758 return null;
 107    }
 108   
 109    /**
 110    * Associates the global transaction with the local transaction.
 111    */
 112  1121973 public void put(Transaction tx, GlobalTransaction gtx)
 113    {
 114  1121973 if (tx == null)
 115    {
 116  0 log.error("key (Transaction) is null");
 117  0 return;
 118    }
 119  1121973 tx_map.put(tx, gtx);
 120    }
 121   
 122    /**
 123    * Returns the local transaction entry for the global transaction.
 124    * Returns null if tx is null or it was not found.
 125    */
 126  9674373 public TransactionEntry get(GlobalTransaction gtx)
 127    {
 128  9674373 return gtx != null ? txs.get(gtx) : null;
 129    }
 130   
 131    /**
 132    * Associates the global transaction with a transaction entry.
 133    */
 134  1121973 public void put(GlobalTransaction tx, TransactionEntry entry)
 135    {
 136  1121973 if (tx == null)
 137    {
 138  0 log.error("key (GlobalTransaction) is null");
 139  0 return;
 140    }
 141  1121973 txs.put(tx, entry);
 142    }
 143   
 144    /**
 145    * Removes a global transation, returns the old transaction entry.
 146    */
 147  1122490 public TransactionEntry remove(GlobalTransaction tx)
 148    {
 149  1122490 return txs.remove(tx);
 150    }
 151   
 152    /**
 153    * Removes a local transation, returns the global transaction entry.
 154    */
 155  1122490 public GlobalTransaction remove(Transaction tx)
 156    {
 157  1122490 if (tx == null)
 158  0 return null;
 159  1122490 return tx_map.remove(tx);
 160    }
 161   
 162    /**
 163    * Adds a motification to the global transaction.
 164    */
 165  168016 public void addModification(GlobalTransaction gtx, MethodCall m)
 166    {
 167  168016 TransactionEntry entry = get(gtx);
 168  168016 if (entry == null)
 169    {
 170  0 log.error("transaction not found (gtx=" + gtx + ")");
 171  0 return;
 172    }
 173  168016 entry.addModification(m);
 174    }
 175   
 176  41080 public void addCacheLoaderModification(GlobalTransaction gtx, MethodCall m)
 177    {
 178  41080 if (m != null)
 179    {
 180  41080 TransactionEntry entry = get(gtx);
 181  41080 if (entry == null)
 182    {
 183  0 log.error("transaction not found (gtx=" + gtx + ")");
 184  0 return;
 185    }
 186  41080 entry.addCacheLoaderModification(m);
 187    }
 188    }
 189   
 190    /**
 191    * Adds an undo operation to the global transaction.
 192    */
 193  183192 public void addUndoOperation(GlobalTransaction gtx, MethodCall m)
 194    {
 195  183192 TransactionEntry entry = get(gtx);
 196  183192 if (entry == null)
 197    {
 198  2 log.error("transaction not found (gtx=" + gtx + ")");
 199  2 return;
 200    }
 201  183190 entry.addUndoOperation(m);
 202    }
 203   
 204    /**
 205    * Adds a lock to the global transaction.
 206    */
 207  294779 public void addLock(GlobalTransaction gtx, NodeLock l)
 208    {
 209  294779 TransactionEntry entry = get(gtx);
 210  294779 if (entry == null)
 211    {
 212  0 log.error("transaction entry not found for (gtx=" + gtx + ")");
 213  0 return;
 214    }
 215  294779 entry.addLock(l);
 216    }
 217   
 218    /**
 219    * Adds a collection of locks to the global transaction.
 220    */
 221  51 public void addLocks(GlobalTransaction gtx, Collection locks)
 222    {
 223  51 TransactionEntry entry = get(gtx);
 224  51 if (entry == null)
 225    {
 226  0 log.error("transaction entry not found for (gtx=" + gtx + ")");
 227  0 return;
 228    }
 229  51 entry.addLocks(locks);
 230    }
 231   
 232    /**
 233    * Returns summary debug information.
 234    */
 235  0 public String toString()
 236    {
 237  0 StringBuffer sb = new StringBuffer();
 238  0 sb.append(tx_map.size()).append(" mappings, ");
 239  0 sb.append(txs.size()).append(" transactions");
 240  0 return sb.toString();
 241    }
 242   
 243    /**
 244    * Returns detailed debug information.
 245    */
 246  16 public String toString(boolean print_details)
 247    {
 248  16 if (!print_details)
 249  0 return toString();
 250  16 StringBuffer sb = new StringBuffer();
 251  16 sb.append("LocalTransactions: ").append(tx_map.size()).append("\n");
 252  16 sb.append("GlobalTransactions: ").append(txs.size()).append("\n");
 253  16 sb.append("tx_map:\n");
 254  16 for (Map.Entry<Transaction, GlobalTransaction> entry : tx_map.entrySet())
 255    {
 256  12 sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
 257    }
 258  16 sb.append("txs:\n");
 259  16 for (Map.Entry<GlobalTransaction, TransactionEntry> entry : txs.entrySet())
 260    {
 261  12 sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
 262    }
 263  16 return sb.toString();
 264    }
 265   
 266    }