[jbosscache-commits] JBoss Cache SVN: r5571 - in core/trunk/src: main/java/org/jboss/cache/commands and 4 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Apr 15 12:02:29 EDT 2008


Author: manik.surtani at jboss.com
Date: 2008-04-15 12:02:29 -0400 (Tue, 15 Apr 2008)
New Revision: 5571

Added:
   core/trunk/src/main/java/org/jboss/cache/commands/cachedata/CreateNodeCommand.java
Modified:
   core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
   core/trunk/src/main/java/org/jboss/cache/commands/CommandsFactory.java
   core/trunk/src/main/java/org/jboss/cache/commands/cachedata/PutKeyValueCommand.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/CacheStoreInterceptor.java
   core/trunk/src/main/java/org/jboss/cache/transaction/TransactionEntry.java
   core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderTestsBase.java
Log:
Fixed more loader issues, added a CreateNodeCommand mainly for rolling back PutKeyValue commands that implicitly create nodes.

Modified: core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java	2008-04-15 15:11:00 UTC (rev 5570)
+++ core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java	2008-04-15 16:02:29 UTC (rev 5571)
@@ -10,6 +10,7 @@
 import org.apache.commons.logging.LogFactory;
 import static org.jboss.cache.AbstractNode.NodeFlags.*;
 import org.jboss.cache.commands.CommandsFactory;
+import org.jboss.cache.commands.cachedata.CreateNodeCommand;
 import org.jboss.cache.factories.annotations.CacheInjectionMethods;
 import org.jboss.cache.factories.annotations.Inject;
 import org.jboss.cache.lock.IdentityLock;
@@ -272,13 +273,11 @@
                child = newChild;
                children.put(child_name, child);
 
-               // why is this needed?
-//               if (gtx != null)
-//               {
-
-//                  RemoveNodeCommand undoOp = commandsFactory.buildRemoveNodeCommand(gtx, child_fqn, false, false, false);
-//                  transactionTable.addUndoOperation(gtx, undoOp);
-//               }
+               if (gtx != null)
+               {
+                  CreateNodeCommand createNodeCommand = commandsFactory.buildCreateNodeCommand(child_fqn);
+                  transactionTable.addModification(gtx, createNodeCommand);
+               }
             }
          }
 

Modified: core/trunk/src/main/java/org/jboss/cache/commands/CommandsFactory.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/commands/CommandsFactory.java	2008-04-15 15:11:00 UTC (rev 5570)
+++ core/trunk/src/main/java/org/jboss/cache/commands/CommandsFactory.java	2008-04-15 16:02:29 UTC (rev 5571)
@@ -266,6 +266,13 @@
       return command;
    }
 
+   public CreateNodeCommand buildCreateNodeCommand(Fqn fqn)
+   {
+      CreateNodeCommand cmd = new CreateNodeCommand(fqn);
+      registry.wireDependencies(cmd);
+      return cmd;
+   }
+
    /**
     * Builds a cache command based on the ID passed in and an object array of parameters
     *

Added: core/trunk/src/main/java/org/jboss/cache/commands/cachedata/CreateNodeCommand.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/commands/cachedata/CreateNodeCommand.java	                        (rev 0)
+++ core/trunk/src/main/java/org/jboss/cache/commands/cachedata/CreateNodeCommand.java	2008-04-15 16:02:29 UTC (rev 5571)
@@ -0,0 +1,79 @@
+package org.jboss.cache.commands.cachedata;
+
+import org.jboss.cache.Fqn;
+import org.jboss.cache.InvocationContext;
+import org.jboss.cache.NodeSPI;
+import org.jboss.cache.commands.CommandsVisitor;
+import org.jboss.cache.commands.functional.TxCacheCommand;
+import org.jboss.cache.commands.state.BaseCacheDataCommand;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Command that creates a node.  Primarily to be used as an undo command for removing nodes.
+ *
+ * @author Manik Surtani (<a href="mailto:manik at jboss.org">manik at jboss.org</a>)
+ * @since 2.2.0
+ */
+public class CreateNodeCommand extends BaseCacheDataCommand implements TxCacheCommand
+{
+   public static final int METHOD_ID = 48;
+   private List<Fqn> newlyCreated = new LinkedList<Fqn>();
+
+   public CreateNodeCommand()
+   {
+   }
+
+   public CreateNodeCommand(Fqn fqn)
+   {
+      this.fqn = fqn;
+      newlyCreated.add(fqn);
+   }
+
+   public int getCommandId()
+   {
+      return METHOD_ID;
+   }
+
+   public Object perform(InvocationContext ctx) throws Throwable
+   {
+      Object[] results = cacheData.createNodes(fqn);
+      List<NodeSPI> created = (List<NodeSPI>) results[0];
+
+      boolean foundFqn = false;
+      if (!created.isEmpty())
+      {
+         for (NodeSPI n : created)
+         {
+            if (fqn.equals(n.getFqn())) foundFqn = true;
+            newlyCreated.add(n.getFqn());
+         }
+      }
+      if (!foundFqn) newlyCreated.remove(fqn);
+
+      return results[1];
+   }
+
+   public Object accept(InvocationContext ctx, CommandsVisitor handler) throws Throwable
+   {
+      throw new RuntimeException("Not designed to be called via any handlers!!");
+   }
+
+   public void rollback()
+   {
+      if (newlyCreated != null)
+      {
+         for (Fqn f : newlyCreated) cacheData.realRemove(f, true);
+      }
+   }
+
+   @Override
+   public String toString()
+   {
+      return "CreateNodeCommand{" +
+            "fqn=" + fqn +
+            ", newlyCreated=" + newlyCreated +
+            '}';
+   }
+}

Modified: core/trunk/src/main/java/org/jboss/cache/commands/cachedata/PutKeyValueCommand.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/commands/cachedata/PutKeyValueCommand.java	2008-04-15 15:11:00 UTC (rev 5570)
+++ core/trunk/src/main/java/org/jboss/cache/commands/cachedata/PutKeyValueCommand.java	2008-04-15 16:02:29 UTC (rev 5571)
@@ -1,5 +1,6 @@
 package org.jboss.cache.commands.cachedata;
 
+import org.jboss.cache.CacheException;
 import org.jboss.cache.Fqn;
 import org.jboss.cache.InvocationContext;
 import org.jboss.cache.NodeSPI;
@@ -85,7 +86,7 @@
       if (this.oldValue == null)
       {
          NodeSPI n = cacheData.findNode(fqn);
-         if (n == null) log.warn("node " + fqn + " not found");
+         if (n == null) throw new CacheException("node " + fqn + " not found for rollback!");
          n.removeDirect(key);
       }
       else

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/CacheStoreInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/CacheStoreInterceptor.java	2008-04-15 15:11:00 UTC (rev 5570)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/CacheStoreInterceptor.java	2008-04-15 16:02:29 UTC (rev 5571)
@@ -392,9 +392,9 @@
          loader.prepare(gtx, modsBuilder.modifications, onePhase);
 
          preparingTxs.put(gtx, modsBuilder.affectedFqns);
-         if (configuration.getExposeManagementStatistics() && getStatisticsEnabled() && modsBuilder.putConunt > 0)
+         if (configuration.getExposeManagementStatistics() && getStatisticsEnabled() && modsBuilder.putCount > 0)
          {
-            txStores.put(gtx, modsBuilder.putConunt);
+            txStores.put(gtx, modsBuilder.putCount);
          }
       }
    }
@@ -402,7 +402,7 @@
    public static class StoreModificationsBuilder extends AbstractCommandsVisitor
    {
       boolean generateStatistics;
-      int putConunt;
+      int putCount;
       Set<Fqn> affectedFqns = new HashSet<Fqn>();
       List<Modification> modifications = new ArrayList<Modification>();
 
@@ -411,9 +411,10 @@
          this.generateStatistics = generateStatistics;
       }
 
+      @Override
       public Object handlePutDataMapCommand(InvocationContext ctx, PutDataMapCommand command) throws Throwable
       {
-         if (generateStatistics) putConunt++;
+         if (generateStatistics) putCount++;
          if (command.isEraseContents())
          {
             modifications.add(new Modification(Modification.ModificationType.PUT_DATA_ERASE, command.getFqn(), command.getData()));
@@ -426,15 +427,17 @@
          return null;
       }
 
+      @Override
       public Object handlePutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable
       {
-         if (generateStatistics) putConunt++;
+         if (generateStatistics) putCount++;
          modifications.add(new Modification(Modification.ModificationType.PUT_KEY_VALUE, command.getFqn(),
                command.getKey(), command.getValue()));
          affectedFqns.add(command.getFqn());
          return null;
       }
 
+      @Override
       public Object handleRemoveKeyCommand(InvocationContext ctx, RemoveKeyCommand command) throws Throwable
       {
          modifications.add(new Modification(Modification.ModificationType.REMOVE_KEY_VALUE, command.getFqn(), command.getKey()));
@@ -442,6 +445,7 @@
          return null;
       }
 
+      @Override
       public Object handleRemoveDataCommand(InvocationContext ctx, RemoveDataCommand command) throws Throwable
       {
          modifications.add(new Modification(Modification.ModificationType.REMOVE_DATA, command.getFqn()));
@@ -449,6 +453,15 @@
          return null;
       }
 
+      @Override
+      public Object handleRemoveNodeCommand(InvocationContext ctx, RemoveNodeCommand command) throws Throwable
+      {
+         modifications.add(new Modification(Modification.ModificationType.REMOVE_NODE, command.getFqn()));
+         affectedFqns.add(command.getFqn());
+         return null;
+      }
+
+      @Override
       public Object handleMoveCommand(InvocationContext ctx, MoveCommand command) throws Throwable
       {
          Fqn moveFrom = command.getFqn();

Modified: core/trunk/src/main/java/org/jboss/cache/transaction/TransactionEntry.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/transaction/TransactionEntry.java	2008-04-15 15:11:00 UTC (rev 5570)
+++ core/trunk/src/main/java/org/jboss/cache/transaction/TransactionEntry.java	2008-04-15 16:02:29 UTC (rev 5571)
@@ -101,15 +101,15 @@
    /**
     * Adds a modification to the modification list.
     */
-   public void addModification(TxCacheCommand TxCacheCommand)
+   public void addModification(TxCacheCommand command)
    {
-      if (TxCacheCommand == null) return;
-      modificationList.add(TxCacheCommand);
+      if (command == null) return;
+      modificationList.add(command);
    }
 
-   public void addCacheLoaderModification(TxCacheCommand TxCacheCommand)
+   public void addCacheLoaderModification(TxCacheCommand command)
    {
-      if (TxCacheCommand != null) classLoadeModList.add(TxCacheCommand);
+      if (command != null) classLoadeModList.add(command);
    }
 
    /**
@@ -326,6 +326,7 @@
       {
          Object undoOp = i.previous();
          TxCacheCommand txCommand = (TxCacheCommand) undoOp;
+         if (log.isDebugEnabled()) log.debug("Calling rollback() on command " + undoOp);
          txCommand.rollback();
       }
    }

Modified: core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderTestsBase.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderTestsBase.java	2008-04-15 15:11:00 UTC (rev 5570)
+++ core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderTestsBase.java	2008-04-15 16:02:29 UTC (rev 5571)
@@ -1097,8 +1097,8 @@
       assertEquals(null, cache.get("/one/two/three", "key1"));
       assertEquals(null, cache.get("/one/two/three/four", "key2"));
       addDelay();
-      assertNull("Loader does not have node /one/two/three", loader.get(Fqn.fromString("/one/two/three")));
-      assertNull("Cache does not have node /one/two/three", cache.getKeys("/one/two/three"));
+      assertNull("Loader should not have node /one/two/three", loader.get(Fqn.fromString("/one/two/three")));
+      assertNull("Cache should not have node /one/two/three", cache.getKeys("/one/two/three"));
       Set<?> children = cache.getChildrenNames("/one");
       assertEquals("Cache has no children under /one", 0, children.size());
       children = loader.getChildrenNames(Fqn.fromString("/one"));




More information about the jbosscache-commits mailing list