[jboss-cvs] JBossCache/src/org/jboss/cache/interceptors ...

Manik Surtani msurtani at jboss.com
Wed Sep 20 12:28:57 EDT 2006


  User: msurtani
  Date: 06/09/20 12:28:57

  Modified:    src/org/jboss/cache/interceptors    
                        OptimisticCreateIfNotExistsInterceptor.java
                        OptimisticInterceptor.java
                        OptimisticNodeInterceptor.java
                        OptimisticValidatorInterceptor.java
  Log:
  Fixed moving with optlocking
  
  Revision  Changes    Path
  1.30      +13 -1     JBossCache/src/org/jboss/cache/interceptors/OptimisticCreateIfNotExistsInterceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: OptimisticCreateIfNotExistsInterceptor.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/interceptors/OptimisticCreateIfNotExistsInterceptor.java,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -b -r1.29 -r1.30
  --- OptimisticCreateIfNotExistsInterceptor.java	19 Sep 2006 17:10:50 -0000	1.29
  +++ OptimisticCreateIfNotExistsInterceptor.java	20 Sep 2006 16:28:57 -0000	1.30
  @@ -64,7 +64,11 @@
   
         //  peek into Node and get a hold of all child fqns as these need to be in the workspace.
         Node node = cache.getChild(nodeFqn);
  -      greedyGetFqns(fqns, node);
  +      greedyGetFqns(fqns, node, newParent);
  +
  +
  +      if (log.isTraceEnabled()) log.trace("Adding Fqns to workspace " + fqns);
  +
   
         for (Fqn f : fqns)
         {
  @@ -161,14 +165,18 @@
                  {
                     //add a new one or overwrite an existing one that has been deleted
                     if (log.isDebugEnabled())
  +                  {
                        log.debug("Parent node doesn't exist in workspace or has been deleted.  Adding to workspace in gtx " + gtx);
  +                  }
                     workspace.addNode(workspaceNode);
                  }
                  else
                  {
                     if (log.isDebugEnabled())
  +                  {
                        log.debug(" Parent node " + workspaceNode.getFqn() + " exists in workspace " + gtx);
                  }
  +               }
                  copy = tmpFqn.clone();
                  // this does not add it into the real child nodes - but in its
                  // local child map for the transaction
  @@ -200,14 +208,18 @@
                  if (childWorkspaceNode == null || childWorkspaceNode.isDeleted())
                  {
                     if (log.isDebugEnabled())
  +                  {
                        log.debug("Child node doesn't exist in workspace or has been deleted.  Adding to workspace in gtx " + gtx);
  +                  }
                     childWorkspaceNode = NodeFactory.getInstance().createWorkspaceNode(tempchildNode, workspace);
                     if (!workspace.isVersioningImplicit()) childWorkspaceNode.setVersion(version);
                  }
                  else
                  {
                     if (log.isDebugEnabled())
  +                  {
                        log.debug(" Already found " + tempchildNode.getFqn() + " node in workspace " + gtx);
  +                  }
   
                  }
               }
  
  
  
  1.7       +4 -2      JBossCache/src/org/jboss/cache/interceptors/OptimisticInterceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: OptimisticInterceptor.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/interceptors/OptimisticInterceptor.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -b -r1.6 -r1.7
  --- OptimisticInterceptor.java	19 Sep 2006 17:10:50 -0000	1.6
  +++ OptimisticInterceptor.java	20 Sep 2006 16:28:57 -0000	1.7
  @@ -54,13 +54,15 @@
       * @param list
       * @param n
       */
  -   protected void greedyGetFqns(List<Fqn> list, Node n)
  +   protected void greedyGetFqns(List<Fqn> list, Node n, Fqn newBase)
      {
         list.add(n.getFqn());
  +      Fqn newFqn = new Fqn(newBase, n.getFqn().getLast());
  +      list.add(newFqn);
   
         for (Node child : n.getChildren())
         {
  -         greedyGetFqns(list, child);
  +         greedyGetFqns(list, child, newFqn);
         }
      }
   
  
  
  
  1.29      +82 -2     JBossCache/src/org/jboss/cache/interceptors/OptimisticNodeInterceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: OptimisticNodeInterceptor.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/interceptors/OptimisticNodeInterceptor.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -b -r1.28 -r1.29
  --- OptimisticNodeInterceptor.java	19 Sep 2006 17:10:50 -0000	1.28
  +++ OptimisticNodeInterceptor.java	20 Sep 2006 16:28:57 -0000	1.29
  @@ -47,18 +47,32 @@
         if (MethodDeclarations.moveMethodLocal_id == m.getMethodId())
         {
            if (ctx.getOptionOverrides().getDataVersion() != null)
  +         {
               throw new CacheException("Setting a data version while performing a move() is not supported!!");
  +         }
   
            Fqn parentFqn = (Fqn) args[0], nodeFqn = (Fqn) args[1];
  +
  +         if (log.isTraceEnabled())
  +         {
  +            log.trace("Adding nodes " + parentFqn + " and " + nodeFqn + " to the workspace.");
  +         }
  +
            WorkspaceNode parent = getOrCreateWorkspaceNode(parentFqn, workspace);
            WorkspaceNode node = getOrCreateWorkspaceNode(nodeFqn, workspace);
   
  +
  +         if (log.isTraceEnabled()) log.trace("Parent: " + parent);
  +         if (log.isTraceEnabled()) log.trace("Node: " + node);
  +
  +         if (log.isTraceEnabled()) log.trace("Workspace snapshot: " + workspace);
  +
            // be greedy about it - get children as well.
            greedyGetNodes(node, workspace);
   
            // now that we have all we need in the workspace, perform the move.
   
  -         throw new CacheException("Implement optimistic moving!!");
  +         doMove(parent, node, workspace);
         }
         else if (MethodDeclarations.isCrudMethod(m.getMethodId()))
         {
  @@ -79,9 +93,11 @@
               else
               {
                  if (ctx.getOptionOverrides() == null || !ctx.getOptionOverrides().isFailSilently())
  +               {
                     throw new CacheException("Unable to set node version for " + getFqn(args) + ", node is null.");
               }
            }
  +         }
   
            switch (m.getMethodId())
            {
  @@ -137,7 +153,9 @@
                  break;
               default:
                  if (log.isInfoEnabled())
  +               {
                     log.info("read Method " + m + " called - don't know how to handle, passing on!");
  +               }
                  result = super.invoke(m);
                  break;
            }
  @@ -145,6 +163,49 @@
         return result;
      }
   
  +   public void doMove(WorkspaceNode parent, WorkspaceNode node, TransactionWorkspace ws)
  +   {
  +      Fqn nodeFqn = node.getFqn();
  +      WorkspaceNode oldParent = getOrCreateWorkspaceNode(nodeFqn.getParent(), ws);
  +      Object nodeName = nodeFqn.getLast();
  +
  +      // now that we have the parent and target nodes:
  +      // first correct the pointers at the pruning point
  +      oldParent.removeChild(nodeName);
  +//      parent.addChild(nodeName, node);
  +//      parent.createChild(nodeName, nodeFqn, parent.getNode(), ((TreeCacheProxyImpl)cache).treeCache, null);
  +
  +      // parent pointer is calculated on the fly using Fqns.
  +
  +      // now adjust Fqns of node and all children.
  +      moveFqns(node, parent.getFqn(), ws);
  +
  +      // remove old nodes.
  +      removeNode(ws, node);
  +
  +
  +      System.out.println("WS: " + ws);
  +   }
  +
  +   private void moveFqns(WorkspaceNode node, Fqn newBase, TransactionWorkspace ws)
  +   {
  +      Fqn newFqn = new Fqn(newBase, node.getName());
  +      WorkspaceNode movedNode = getOrCreateWorkspaceNode(newFqn, ws);
  +      movedNode.put(node.getData());
  +
  +      System.out.println("  Moved node: " + movedNode);
  +      System.out.println("  Data: " + movedNode.getData());
  +
  +      // process children
  +      for (Object n : node.getChildrenNames())
  +      {
  +//         movedNode.createChild(n, new Fqn(node.getFqn(), n), movedNode.getNode(), ((TreeCacheProxyImpl)cache).treeCache, null);
  +         WorkspaceNode child = getOrCreateWorkspaceNode(new Fqn(node.getFqn(), n), ws);
  +         moveFqns(child, newFqn, ws);
  +         //movedNode.addChild(n, child);
  +      }
  +   }
  +
      /**
       * performs a getOrCreateNode on all n's children, recursively.
       *
  @@ -157,7 +218,9 @@
         for (Object child : n.getChildrenNames())
         {
            Fqn childFqn = new Fqn(myFqn, child);
  -         greedyGetNodes(getOrCreateWorkspaceNode(childFqn, ws), ws);
  +         WorkspaceNode cn = getOrCreateWorkspaceNode(childFqn, ws);
  +         if (!ws.hasNode(childFqn)) ws.addNode(cn);
  +         greedyGetNodes(cn, ws);
         }
      }
   
  @@ -172,7 +235,9 @@
   
         Map data = (Map) args[2];
         if (workspaceNode == null)
  +      {
            return;
  +      }
         workspaceNode.put(data, eraseExisitng);
         workspace.addNode(workspaceNode);
      }
  @@ -196,18 +261,24 @@
      private void removeNode(TransactionWorkspace workspace, WorkspaceNode workspaceNode) throws CacheException
      {
         if (log.isTraceEnabled())
  +      {
            log.trace("removeNode " + workspace + " node=" + workspaceNode);
  +      }
   
         // it is already removed - we can ignore it
         if (workspaceNode == null)
  +      {
            return;
  +      }
   
         // get the parent
         TreeNode temp = workspaceNode.getParent();
   
         // can parent be null?
         if (temp == null)
  +      {
            return;
  +      }
   
         boolean debug = log.isDebugEnabled();
   
  @@ -216,7 +287,9 @@
         // get a wrapped parent
         WorkspaceNode parentNode = getOrCreateWorkspaceNode(parentFqn, workspace);
         if (parentNode == null)
  +      {
            throw new CacheException("Unable to find parent node with Fqn " + parentFqn);
  +      }
   
         parentNode.removeChild(workspaceNode.getName());
         workspace.addNode(parentNode);
  @@ -245,7 +318,9 @@
      private Object removeKey(Object[] args, TransactionWorkspace workspace, WorkspaceNode workspaceNode)
      {
         if (workspaceNode == null)
  +      {
            return null;
  +      }
         Object removeKey = args[2];
         Object old = workspaceNode.remove(removeKey);
         workspace.addNode(workspaceNode);
  @@ -255,7 +330,9 @@
      private void removeData(TransactionWorkspace workspace, WorkspaceNode workspaceNode)
      {
         if (workspaceNode == null)
  +      {
            return;
  +      }
         workspaceNode.clear();
         workspace.addNode(workspaceNode);
      }
  @@ -344,8 +421,11 @@
         {
            DataNode node = ((TreeCacheProxyImpl) cache).peek(fqn);
            if (node == null)
  +         {
               return null; // seems to happen quite a bit
  +         }
            workspaceNode = NodeFactory.getInstance().createWorkspaceNode(node, workspace);
  +         workspace.addNode(workspaceNode);
         }
         // the node has been deleted dude!
         if (workspaceNode.isDeleted())
  
  
  
  1.35      +11 -1     JBossCache/src/org/jboss/cache/interceptors/OptimisticValidatorInterceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: OptimisticValidatorInterceptor.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/interceptors/OptimisticValidatorInterceptor.java,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -b -r1.34 -r1.35
  --- OptimisticValidatorInterceptor.java	7 Sep 2006 13:52:28 -0000	1.34
  +++ OptimisticValidatorInterceptor.java	20 Sep 2006 16:28:57 -0000	1.35
  @@ -52,7 +52,9 @@
         Object retval = null;
   
         if (tx == null)
  +      {
            throw new CacheException("Not in a transaction");
  +      }
   
         // Methods we are interested in are prepare/commit
         // They do not go further than this interceptor
  @@ -197,7 +199,9 @@
                  boolean needsNotify = !dataEquals(mergedData, current.getData());
   
                  if (needsNotify)
  +               {
                     cache.getNotifier().notifyNodeModified(wrappedNode.getFqn(), true, current.getData() == null ? Collections.emptyMap() : Collections.unmodifiableMap(current.getData()));
  +               }
   
                  current.put(mergedData, true);
                  if (workspace.isVersioningImplicit())
  @@ -210,15 +214,21 @@
                     current.setVersion(wrappedNode.getVersion());
                  }
                  if (trace)
  -                  log.trace("Setting version of node " + current.getName() + " from " + wrappedNode.getVersion() + " to " + current.getVersion());
  +               {
  +                  log.trace("Setting version of node " + current.getFqn() + " from " + wrappedNode.getVersion() + " to " + current.getVersion());
  +               }
   
                  if (needsNotify)
  +               {
                     cache.getNotifier().notifyNodeModified(wrappedNode.getFqn(), false, mergedData == null ? Collections.emptyMap() : Collections.unmodifiableMap(mergedData));
               }
  +            }
               else
               {
                  if (trace)
  +               {
                     log.trace("Merging node " + wrappedNode.getFqn() + " not necessary since the node is not dirty");
  +               }
                  cache.getNotifier().notifyNodeVisited(wrappedNode.getFqn(), true);
                  cache.getNotifier().notifyNodeVisited(wrappedNode.getFqn(), false);
               }
  
  
  



More information about the jboss-cvs-commits mailing list