[infinispan-commits] Infinispan SVN: r2510 - in branches/4.2.x/core/src: main/java/org/infinispan/commands/read and 2 other directories.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Thu Oct 14 09:43:54 EDT 2010


Author: mircea.markus
Date: 2010-10-14 09:43:54 -0400 (Thu, 14 Oct 2010)
New Revision: 2510

Modified:
   branches/4.2.x/core/src/main/java/org/infinispan/CacheDelegate.java
   branches/4.2.x/core/src/main/java/org/infinispan/commands/read/AbstractLocalCommand.java
   branches/4.2.x/core/src/main/java/org/infinispan/commands/read/KeySetCommand.java
   branches/4.2.x/core/src/main/java/org/infinispan/commands/read/ValuesCommand.java
   branches/4.2.x/core/src/main/java/org/infinispan/context/impl/LocalTxInvocationContext.java
   branches/4.2.x/core/src/test/java/org/infinispan/tx/LocalModeTxTest.java
Log:
[ISPN-679] - Cache.values does not work correctly

Modified: branches/4.2.x/core/src/main/java/org/infinispan/CacheDelegate.java
===================================================================
--- branches/4.2.x/core/src/main/java/org/infinispan/CacheDelegate.java	2010-10-14 13:25:27 UTC (rev 2509)
+++ branches/4.2.x/core/src/main/java/org/infinispan/CacheDelegate.java	2010-10-14 13:43:54 UTC (rev 2510)
@@ -217,13 +217,13 @@
    @SuppressWarnings("unchecked")
    public Set<K> keySet() {
       KeySetCommand command = commandsFactory.buildKeySetCommand();
-      return (Set<K>) invoker.invoke(icc.createNonTxInvocationContext(), command);
+      return (Set<K>) invoker.invoke(getInvocationContext(false), command);
    }
 
    @SuppressWarnings("unchecked")
    public Collection<V> values() {
       ValuesCommand command = commandsFactory.buildValuesCommand();
-      return (Collection<V>) invoker.invoke(icc.createNonTxInvocationContext(), command);
+      return (Collection<V>) invoker.invoke(getInvocationContext(false), command);
    }
 
    @SuppressWarnings("unchecked")

Modified: branches/4.2.x/core/src/main/java/org/infinispan/commands/read/AbstractLocalCommand.java
===================================================================
--- branches/4.2.x/core/src/main/java/org/infinispan/commands/read/AbstractLocalCommand.java	2010-10-14 13:25:27 UTC (rev 2509)
+++ branches/4.2.x/core/src/main/java/org/infinispan/commands/read/AbstractLocalCommand.java	2010-10-14 13:43:54 UTC (rev 2510)
@@ -2,6 +2,7 @@
 
 import org.infinispan.commands.LocalCommand;
 import org.infinispan.context.InvocationContext;
+import org.infinispan.context.impl.TxInvocationContext;
 
 /**
  * Abstract class
@@ -27,4 +28,8 @@
    public boolean shouldInvoke(InvocationContext ctx) {
       return false;
    }
+
+   protected boolean noTxModifications(InvocationContext ctx) {
+      return !ctx.isInTxScope() || !((TxInvocationContext)ctx).hasModifications();
+   }   
 }

Modified: branches/4.2.x/core/src/main/java/org/infinispan/commands/read/KeySetCommand.java
===================================================================
--- branches/4.2.x/core/src/main/java/org/infinispan/commands/read/KeySetCommand.java	2010-10-14 13:25:27 UTC (rev 2509)
+++ branches/4.2.x/core/src/main/java/org/infinispan/commands/read/KeySetCommand.java	2010-10-14 13:43:54 UTC (rev 2510)
@@ -24,15 +24,19 @@
 import org.infinispan.commands.VisitableCommand;
 import org.infinispan.commands.Visitor;
 import org.infinispan.container.DataContainer;
+import org.infinispan.container.entries.CacheEntry;
 import org.infinispan.context.InvocationContext;
+import org.infinispan.context.impl.TxInvocationContext;
 import org.infinispan.util.Immutables;
 
+import java.util.HashSet;
 import java.util.Set;
 
 /**
  * KeySetCommand.
  *
  * @author Galder Zamarreño
+ * @author Mircea.Markus at jboss.com
  * @since 4.0
  */
 public class KeySetCommand extends AbstractLocalCommand implements VisitableCommand {
@@ -47,7 +51,22 @@
    }
 
    public Set perform(InvocationContext ctx) throws Throwable {
-      return Immutables.immutableSetWrap(container.keySet());
+      Set<Object> objects = container.keySet();
+      if (noTxModifications(ctx)) {
+         return Immutables.immutableSetWrap(objects);
+      }
+      Set<Object> result = new HashSet<Object>();
+      result.addAll(objects);
+      for (CacheEntry ce : ctx.getLookedUpEntries().values()) {
+         if (ce.isRemoved()) {
+            result.remove(ce.getKey());
+         } else {
+            if (ce.isCreated()) {
+               result.add(ce.getKey());
+            }
+         }
+      }
+      return Immutables.immutableSetWrap(result);
    }
 
    @Override

Modified: branches/4.2.x/core/src/main/java/org/infinispan/commands/read/ValuesCommand.java
===================================================================
--- branches/4.2.x/core/src/main/java/org/infinispan/commands/read/ValuesCommand.java	2010-10-14 13:25:27 UTC (rev 2509)
+++ branches/4.2.x/core/src/main/java/org/infinispan/commands/read/ValuesCommand.java	2010-10-14 13:43:54 UTC (rev 2510)
@@ -24,15 +24,20 @@
 import org.infinispan.commands.VisitableCommand;
 import org.infinispan.commands.Visitor;
 import org.infinispan.container.DataContainer;
+import org.infinispan.container.entries.CacheEntry;
+import org.infinispan.container.entries.InternalCacheEntry;
 import org.infinispan.context.InvocationContext;
 import org.infinispan.util.Immutables;
 
 import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * ValuesCommand.
  *
  * @author Galder Zamarreño
+ * @author Mircea.Markus at jboss.com
  * @since 4.0
  */
 public class ValuesCommand extends AbstractLocalCommand implements VisitableCommand {
@@ -47,7 +52,24 @@
    }
 
    public Collection perform(InvocationContext ctx) throws Throwable {
-      return Immutables.immutableCollectionWrap(container.values());
+      if (noTxModifications(ctx)) {
+         return Immutables.immutableCollectionWrap(container.values());
+      }
+      Map result = new HashMap();
+      for (InternalCacheEntry ice : container.entrySet()) {
+         result.put(ice.getKey(), ice.getValue());
+      }      
+      for (CacheEntry ce : ctx.getLookedUpEntries().values()) {
+         if (ce.isRemoved()) {
+            result.remove(ce.getKey());
+         } else {
+            if (ce.isCreated() || ce.isChanged()) {
+               result.put(ce.getKey(), ce.getValue());
+            }
+         }
+      }
+      return Immutables.immutableCollectionWrap(result.values());
+
    }
 
    @Override

Modified: branches/4.2.x/core/src/main/java/org/infinispan/context/impl/LocalTxInvocationContext.java
===================================================================
--- branches/4.2.x/core/src/main/java/org/infinispan/context/impl/LocalTxInvocationContext.java	2010-10-14 13:25:27 UTC (rev 2509)
+++ branches/4.2.x/core/src/main/java/org/infinispan/context/impl/LocalTxInvocationContext.java	2010-10-14 13:43:54 UTC (rev 2510)
@@ -43,7 +43,7 @@
    }
 
    public List<WriteCommand> getModifications() {
-      return xaAdapter.getModifications();
+      return xaAdapter == null ? null : xaAdapter.getModifications();
    }
 
    public void setXaCache(TransactionXaAdapter xaAdapter) {

Modified: branches/4.2.x/core/src/test/java/org/infinispan/tx/LocalModeTxTest.java
===================================================================
--- branches/4.2.x/core/src/test/java/org/infinispan/tx/LocalModeTxTest.java	2010-10-14 13:25:27 UTC (rev 2509)
+++ branches/4.2.x/core/src/test/java/org/infinispan/tx/LocalModeTxTest.java	2010-10-14 13:43:54 UTC (rev 2510)
@@ -77,4 +77,48 @@
       assert cache.get("key").equals("value");
       assert !cache.isEmpty();
    }
+
+   public void testKeySet() throws Exception {
+      tm().begin();
+      cache.put("k1", "v1");
+      cache.put("k2", "v2");
+      assert cache.keySet().size() == 2;
+      assert cache.values().size() == 2;
+      tm().commit();
+      assert cache.keySet().size() == 2;
+      assert cache.values().size() == 2;
+   }
+
+   public void testKeySet2() throws Exception {
+      cache.put("k1", "v1");
+      cache.put("k2", "v2");
+      assert cache.keySet().size() == 2;
+      assert cache.values().size() == 2;
+      tm().begin();
+      assert cache.keySet().size() == 2;
+      assert cache.values().size() == 2;
+      cache.remove("k1");
+      assert cache.keySet().size() == 1;
+      assert cache.values().size() == 1;
+      tm().rollback();
+      assert cache.keySet().size() == 2;
+      assert cache.values().size() == 2;
+   }
+
+   public void testKeySetAlterValue() throws Exception {
+      cache.put("k1", "v1");
+      cache.put("k2", "v2");
+      assert cache.keySet().size() == 2;
+      assert cache.values().size() == 2;
+      tm().begin();
+      assert cache.keySet().size() == 2;
+      assert cache.values().size() == 2;
+      cache.put("k1", "v3");
+      assert cache.keySet().size() == 2;
+      assert cache.values().size() == 2;
+      assert cache.values().contains("v3");
+      tm().rollback();
+      assert cache.keySet().size() == 2;
+      assert cache.values().size() == 2;
+   }
 }



More information about the infinispan-commits mailing list