ozeigermann 2004/06/03 07:15:31 Modified: transaction/src/java/org/apache/commons/transaction/memory OptimisticMapWrapper.java TransactionalMapWrapper.java PessimisticMapWrapper.java transaction/src/test/org/apache/commons/transaction/memory OptimisticMapWrapperTest.java Log: Unified format to 120 characters/line and no tabs Revision Changes Path 1.3 +96 -111 jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/OptimisticMapWrapper.java Index: OptimisticMapWrapper.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/OptimisticMapWrapper.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- OptimisticMapWrapper.java 3 Jun 2004 14:09:40 -0000 1.2 +++ OptimisticMapWrapper.java 3 Jun 2004 14:15:31 -0000 1.3 @@ -71,10 +71,7 @@ * @param mapFactory factory for temporary maps * @param setFactory factory for temporary sets */ - public OptimisticMapWrapper( - Map wrapped, - MapFactory mapFactory, - SetFactory setFactory) { + public OptimisticMapWrapper(Map wrapped, MapFactory mapFactory, SetFactory setFactory) { super(wrapped, mapFactory, setFactory); activeTransactions = new HashSet(); } @@ -82,9 +79,7 @@ public synchronized void startTransaction() { if (getActiveTx() != null) { throw new IllegalStateException( - "Active thread " - + Thread.currentThread() - + " already associated with a transaction!"); + "Active thread " + Thread.currentThread() + " already associated with a transaction!"); } CopyingTxContext context = new CopyingTxContext(); activeTransactions.add(context); @@ -96,16 +91,11 @@ if (txContext == null) { throw new IllegalStateException( - "Active thread " - + Thread.currentThread() - + " not associated with a transaction!"); + "Active thread " + Thread.currentThread() + " not associated with a transaction!"); } if (txContext.rollbackOnly) { - throw new IllegalStateException( - "Active thread " - + Thread.currentThread() - + " is marked for rollback!"); + throw new IllegalStateException("Active thread " + Thread.currentThread() + " is marked for rollback!"); } checkForConflicts(); @@ -118,7 +108,7 @@ } protected void checkForConflicts() { - // TODO + // TODO } protected void copyChangesToConcurrentTransactions() { @@ -127,35 +117,30 @@ for (Iterator it = activeTransactions.iterator(); it.hasNext();) { CopyingTxContext otherTxContext = (CopyingTxContext) it.next(); - // no need to copy data if the other transaction does not access global map anyway - if (otherTxContext.cleared) continue; - + // no need to copy data if the other transaction does not access global map anyway + if (otherTxContext.cleared) + continue; + if (thisTxContext.cleared) { // we will clear everything, so we have to copy everything before otherTxContext.externalChanges.putAll(wrapped); } else // no need to check if we have already copied everthing { - - for (Iterator it2 = thisTxContext.changes.entrySet().iterator(); - it2.hasNext(); - ) { + + for (Iterator it2 = thisTxContext.changes.entrySet().iterator(); it2.hasNext();) { Map.Entry entry = (Map.Entry) it2.next(); Object value = wrapped.get(entry.getKey()); if (value != null) { - // undo change - otherTxContext.externalChanges.put( - entry.getKey(), - value); + // undo change + otherTxContext.externalChanges.put(entry.getKey(), value); } else { // undo add otherTxContext.externalDeletes.add(entry.getKey()); } } - for (Iterator it2 = thisTxContext.deletes.iterator(); - it2.hasNext(); - ) { - // undo delete + for (Iterator it2 = thisTxContext.deletes.iterator(); it2.hasNext();) { + // undo delete Object key = it2.next(); Object value = wrapped.get(key); otherTxContext.externalChanges.put(key, value); @@ -167,88 +152,88 @@ public class CopyingTxContext extends TxContext { protected Map externalChanges; - protected Map externalAdds; - protected Set externalDeletes; + protected Map externalAdds; + protected Set externalDeletes; protected CopyingTxContext() { super(); externalChanges = mapFactory.createMap(); - externalDeletes = setFactory.createSet(); - externalAdds = mapFactory.createMap(); + externalDeletes = setFactory.createSet(); + externalAdds = mapFactory.createMap(); + } + + protected Set keys() { + Set keySet = super.keys(); + keySet.removeAll(externalDeletes); + keySet.addAll(externalAdds.keySet()); + return keySet; + } + + protected Object get(Object key) { + + if (deletes.contains(key)) { + // reflects that entry has been deleted in this tx + return null; + } + + Object changed = changes.get(key); + if (changed != null) { + return changed; + } + + Object added = adds.get(key); + if (added != null) { + return added; + } + + if (cleared) { + return null; + } else { + if (externalDeletes.contains(key)) { + // reflects that entry has been deleted in this tx + return null; + } + + changed = externalChanges.get(key); + if (changed != null) { + return changed; + } + + added = externalAdds.get(key); + if (added != null) { + return added; + } + + // not modified in this tx + return wrapped.get(key); + } + } + + protected int size() { + int size = super.size(); + + size -= externalDeletes.size(); + size += externalAdds.size(); + + return size; + } + + protected void clear() { + super.clear(); + externalDeletes.clear(); + externalChanges.clear(); + externalAdds.clear(); + } + + protected void dispose() { + super.dispose(); + setFactory.disposeSet(externalDeletes); + externalDeletes = null; + mapFactory.disposeMap(externalChanges); + externalChanges = null; + mapFactory.disposeMap(externalAdds); + externalAdds = null; } - - protected Set keys() { - Set keySet = super.keys(); - keySet.removeAll(externalDeletes); - keySet.addAll(externalAdds.keySet()); - return keySet; - } - - protected Object get(Object key) { - - if (deletes.contains(key)) { - // reflects that entry has been deleted in this tx - return null; - } - - Object changed = changes.get(key); - if (changed != null) { - return changed; - } - - Object added = adds.get(key); - if (added != null) { - return added; - } - - if (cleared) { - return null; - } else { - if (externalDeletes.contains(key)) { - // reflects that entry has been deleted in this tx - return null; - } - - changed = externalChanges.get(key); - if (changed != null) { - return changed; - } - - added = externalAdds.get(key); - if (added != null) { - return added; - } - - // not modified in this tx - return wrapped.get(key); - } - } - - protected int size() { - int size = super.size(); - - size -= externalDeletes.size(); - size += externalAdds.size(); - - return size; - } - - protected void clear() { - super.clear(); - externalDeletes.clear(); - externalChanges.clear(); - externalAdds.clear(); - } - - protected void dispose() { - super.dispose(); - setFactory.disposeSet(externalDeletes); - externalDeletes = null; - mapFactory.disposeMap(externalChanges); - externalChanges = null; - mapFactory.disposeMap(externalAdds); - externalAdds = null; - } } } 1.12 +21 -58 jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/TransactionalMapWrapper.java Index: TransactionalMapWrapper.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/TransactionalMapWrapper.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- TransactionalMapWrapper.java 3 Jun 2004 13:34:35 -0000 1.11 +++ TransactionalMapWrapper.java 3 Jun 2004 14:15:31 -0000 1.12 @@ -79,10 +79,7 @@ * @param mapFactory factory for temporary maps * @param setFactory factory for temporary sets */ - public TransactionalMapWrapper( - Map wrapped, - MapFactory mapFactory, - SetFactory setFactory) { + public TransactionalMapWrapper(Map wrapped, MapFactory mapFactory, SetFactory setFactory) { this.wrapped = wrapped; this.mapFactory = mapFactory; this.setFactory = setFactory; @@ -93,9 +90,7 @@ if (txContext == null) { throw new IllegalStateException( - "Active thread " - + Thread.currentThread() - + " not associated with a transaction!"); + "Active thread " + Thread.currentThread() + " not associated with a transaction!"); } return txContext.readOnly; @@ -106,9 +101,7 @@ if (txContext == null) { throw new IllegalStateException( - "Active thread " - + Thread.currentThread() - + " not associated with a transaction!"); + "Active thread " + Thread.currentThread() + " not associated with a transaction!"); } return txContext.rollbackOnly; @@ -119,9 +112,7 @@ if (txContext == null) { throw new IllegalStateException( - "Active thread " - + Thread.currentThread() - + " not associated with a transaction!"); + "Active thread " + Thread.currentThread() + " not associated with a transaction!"); } txContext.rollbackOnly = true; @@ -133,9 +124,7 @@ if (txContext == null) { throw new IllegalStateException( - "Active thread " - + Thread.currentThread() - + " not associated with a transaction!"); + "Active thread " + Thread.currentThread() + " not associated with a transaction!"); } setActiveTx(null); @@ -145,9 +134,7 @@ public synchronized void resumeTransaction(TxContext suspendedTx) { if (getActiveTx() != null) { throw new IllegalStateException( - "Active thread " - + Thread.currentThread() - + " already associated with a transaction!"); + "Active thread " + Thread.currentThread() + " already associated with a transaction!"); } if (suspendedTx == null) { @@ -169,9 +156,7 @@ public synchronized void startTransaction() { if (getActiveTx() != null) { throw new IllegalStateException( - "Active thread " - + Thread.currentThread() - + " already associated with a transaction!"); + "Active thread " + Thread.currentThread() + " already associated with a transaction!"); } setActiveTx(new TxContext()); } @@ -181,9 +166,7 @@ if (txContext == null) { throw new IllegalStateException( - "Active thread " - + Thread.currentThread() - + " not associated with a transaction!"); + "Active thread " + Thread.currentThread() + " not associated with a transaction!"); } // simply forget about tx @@ -196,16 +179,11 @@ if (txContext == null) { throw new IllegalStateException( - "Active thread " - + Thread.currentThread() - + " not associated with a transaction!"); + "Active thread " + Thread.currentThread() + " not associated with a transaction!"); } if (txContext.rollbackOnly) { - throw new IllegalStateException( - "Active thread " - + Thread.currentThread() - + " is marked for rollback!"); + throw new IllegalStateException("Active thread " + Thread.currentThread() + " is marked for rollback!"); } txContext.merge(); @@ -284,10 +262,7 @@ } else { for (Iterator it = map.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); - txContext.put( - entry.getKey(), - entry.getValue(), - wrapped.get(entry.getKey())); + txContext.put(entry.getKey(), entry.getValue(), wrapped.get(entry.getKey())); } } } @@ -314,7 +289,7 @@ if (txContext == null) { return wrapped.keySet(); } else { - return txContext.keys(); + return txContext.keys(); } } @@ -395,26 +370,16 @@ return false; } Map.Entry other = (Map.Entry) obj; - return ( - getKey() == null - ? other.getKey() == null - : getKey().equals(other.getKey())) - && (getValue() == null - ? other.getValue() == null - : getValue().equals(other.getValue())); + return (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) + && (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue())); } public int hashCode() { - return (getKey() == null ? 0 : getKey().hashCode()) - ^ (getValue() == null ? 0 : getValue().hashCode()); + return (getKey() == null ? 0 : getKey().hashCode()) ^ (getValue() == null ? 0 : getValue().hashCode()); } public String toString() { - return new StringBuffer() - .append(getKey()) - .append('=') - .append(getValue()) - .toString(); + return new StringBuffer().append(getKey()).append('=').append(getValue()).toString(); } } @@ -528,9 +493,7 @@ wrapped.clear(); } - for (Iterator it = changes.entrySet().iterator(); - it.hasNext(); - ) { + for (Iterator it = changes.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); wrapped.put(entry.getKey(), entry.getValue()); } 1.2 +24 -24 jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/PessimisticMapWrapper.java Index: PessimisticMapWrapper.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/PessimisticMapWrapper.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- PessimisticMapWrapper.java 2 Jun 2004 21:54:04 -0000 1.1 +++ PessimisticMapWrapper.java 3 Jun 2004 14:15:31 -0000 1.2 @@ -46,25 +46,25 @@ */ public class PessimisticMapWrapper extends TransactionalMapWrapper { - /** - * Creates a new pessimistic transactional map wrapper. Temporary maps and sets to store transactional - * data will be instances of [EMAIL PROTECTED] HashMap} and [EMAIL PROTECTED] HashSet}. - * - * @param wrapped map to be wrapped - */ - public PessimisticMapWrapper(Map wrapped) { - super(wrapped, new HashMapFactory(), new HashSetFactory()); - } + /** + * Creates a new pessimistic transactional map wrapper. Temporary maps and sets to store transactional + * data will be instances of [EMAIL PROTECTED] HashMap} and [EMAIL PROTECTED] HashSet}. + * + * @param wrapped map to be wrapped + */ + public PessimisticMapWrapper(Map wrapped) { + super(wrapped, new HashMapFactory(), new HashSetFactory()); + } - /** - * Creates a new pessimistic transactional map wrapper. Temporary maps and sets to store transactional - * data will be created and disposed using [EMAIL PROTECTED] MapFactory} and [EMAIL PROTECTED] SetFactory}. - * - * @param wrapped map to be wrapped - * @param mapFactory factory for temporary maps - * @param setFactory factory for temporary sets - */ - public PessimisticMapWrapper(Map wrapped, MapFactory mapFactory, SetFactory setFactory) { - super(wrapped, mapFactory, setFactory); - } + /** + * Creates a new pessimistic transactional map wrapper. Temporary maps and sets to store transactional + * data will be created and disposed using [EMAIL PROTECTED] MapFactory} and [EMAIL PROTECTED] SetFactory}. + * + * @param wrapped map to be wrapped + * @param mapFactory factory for temporary maps + * @param setFactory factory for temporary sets + */ + public PessimisticMapWrapper(Map wrapped, MapFactory mapFactory, SetFactory setFactory) { + super(wrapped, mapFactory, setFactory); + } } 1.2 +45 -55 jakarta-commons-sandbox/transaction/src/test/org/apache/commons/transaction/memory/OptimisticMapWrapperTest.java Index: OptimisticMapWrapperTest.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/transaction/src/test/org/apache/commons/transaction/memory/OptimisticMapWrapperTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- OptimisticMapWrapperTest.java 3 Jun 2004 14:09:40 -0000 1.1 +++ OptimisticMapWrapperTest.java 3 Jun 2004 14:15:31 -0000 1.2 @@ -40,8 +40,7 @@ */ public class OptimisticMapWrapperTest extends TestCase { - private static final Logger logger = - Logger.getLogger(OptimisticMapWrapperTest.class.getName()); + private static final Logger logger = Logger.getLogger(OptimisticMapWrapperTest.class.getName()); private static final LoggerFacade sLogger = new Jdk14Logger(logger); private static final long BARRIER_TIMEOUT = 2000; @@ -49,12 +48,7 @@ // XXX need this, as JUnit seems to print only part of these strings private static void report(String should, String is) { if (!should.equals(is)) { - fail( - "\nWrong output:\n'" - + is - + "'\nShould be:\n'" - + should - + "'\n"); + fail("\nWrong output:\n'" + is + "'\nShould be:\n'" + should + "'\n"); } } @@ -77,8 +71,7 @@ final Map map1 = new HashMap(); - final OptimisticMapWrapper txMap1 = - new OptimisticMapWrapper(map1); + final OptimisticMapWrapper txMap1 = new OptimisticMapWrapper(map1); // make sure changes are propagated to wrapped map outside tx txMap1.put("key1", "value1"); @@ -106,55 +99,52 @@ final Map map1 = new HashMap(); - final OptimisticMapWrapper txMap1 = - new OptimisticMapWrapper(map1); + final OptimisticMapWrapper txMap1 = new OptimisticMapWrapper(map1); - final RendezvousBarrier beforeCommitBarrier = - new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger); + final RendezvousBarrier beforeCommitBarrier = + new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger); - final RendezvousBarrier afterCommitBarrier = - new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger); + final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger); - Thread thread1 = new Thread(new Runnable() { - public void run() { - txMap1.startTransaction(); - try { - beforeCommitBarrier.meet(); - txMap1.put("key1", "value2"); - txMap1.commitTransaction(); - afterCommitBarrier.call(); - } catch (InterruptedException e) { - logger.log(Level.WARNING, "Thread interrupted", e); - afterCommitBarrier.reset(); - beforeCommitBarrier.reset(); - } - } - }, "Thread1"); - - txMap1.put("key1", "value1"); - - - txMap1.startTransaction(); - thread1.start(); - - report("value1", (String) txMap1.get("key1")); - beforeCommitBarrier.call(); - afterCommitBarrier.meet(); - // we have serializable as isolation level, that's why I will still see the old value - report("value1", (String) txMap1.get("key1")); - - // now when I override it it should of course be my value - txMap1.put("key1", "value3"); - report("value3", (String) txMap1.get("key1")); - - // after rollback it must be the value written by the other thread - txMap1.rollbackTransaction(); - report("value2", (String) txMap1.get("key1")); + Thread thread1 = new Thread(new Runnable() { + public void run() { + txMap1.startTransaction(); + try { + beforeCommitBarrier.meet(); + txMap1.put("key1", "value2"); + txMap1.commitTransaction(); + afterCommitBarrier.call(); + } catch (InterruptedException e) { + logger.log(Level.WARNING, "Thread interrupted", e); + afterCommitBarrier.reset(); + beforeCommitBarrier.reset(); + } + } + }, "Thread1"); + + txMap1.put("key1", "value1"); + + txMap1.startTransaction(); + thread1.start(); + + report("value1", (String) txMap1.get("key1")); + beforeCommitBarrier.call(); + afterCommitBarrier.meet(); + // we have serializable as isolation level, that's why I will still see the old value + report("value1", (String) txMap1.get("key1")); + + // now when I override it it should of course be my value + txMap1.put("key1", "value3"); + report("value3", (String) txMap1.get("key1")); + + // after rollback it must be the value written by the other thread + txMap1.rollbackTransaction(); + report("value2", (String) txMap1.get("key1")); } public void testTxControl() throws Throwable { logger.info("Checking advanced transaction control (heavily used in JCA implementation)"); - + } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]