jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From meteata...@apache.org
Subject svn commit: r1431954 - in /jackrabbit/oak/trunk/oak-mongomk/src: main/java/org/apache/jackrabbit/mongomk/impl/ main/java/org/apache/jackrabbit/mongomk/impl/command/ main/java/org/apache/jackrabbit/mongomk/impl/instruction/ main/java/org/apache/jackrabb...
Date Fri, 11 Jan 2013 09:44:56 GMT
Author: meteatamel
Date: Fri Jan 11 09:44:56 2013
New Revision: 1431954

URL: http://svn.apache.org/viewvc?rev=1431954&view=rev
Log:
OAK-551 - MongoMK deleted nodes optimization

Added a new "deleted" flag to nodes and changed code to add/check for this deleted flag.

Modified:
    jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoNodeStore.java
    jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/command/NodeExistsCommand.java
    jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/instruction/CommitCommandInstructionVisitor.java
    jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/model/MongoNode.java
    jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/impl/MongoMKCommitRemoveTest.java

Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoNodeStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoNodeStore.java?rev=1431954&r1=1431953&r2=1431954&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoNodeStore.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoNodeStore.java
Fri Jan 11 09:44:56 2013
@@ -255,7 +255,7 @@ public class MongoNodeStore implements N
             return null;
         }
         LOG.debug("Returning node from cache: {}", key);
-        return node;
+        return node.copy();
     }
 
     private void init() {

Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/command/NodeExistsCommand.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/command/NodeExistsCommand.java?rev=1431954&r1=1431953&r2=1431954&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/command/NodeExistsCommand.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/command/NodeExistsCommand.java
Fri Jan 11 09:44:56 2013
@@ -16,14 +16,11 @@
  */
 package org.apache.jackrabbit.mongomk.impl.command;
 
-import java.util.HashSet;
 import java.util.Map;
-import java.util.Set;
 
 import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
 import org.apache.jackrabbit.mongomk.impl.action.FetchNodesActionNew;
 import org.apache.jackrabbit.mongomk.impl.model.MongoNode;
-import org.apache.jackrabbit.oak.commons.PathUtils;
 
 /**
  * {@code Command} for {@code MongoMicroKernel#nodeExists(String, String)}
@@ -59,42 +56,16 @@ public class NodeExistsCommand extends B
 
     @Override
     public Boolean execute() throws Exception {
-        // To check a path really exists, all the paths from root need to be checked.
-        Set<String> paths = new HashSet<String>();
-        char[] path = this.path.toCharArray();
-        StringBuilder current = new StringBuilder();
-        for (int i = 0; i < path.length; i++) {
-            if (i == 0) {
-                paths.add("/");
-            } else if (path[i] == '/') {
-                paths.add(current.toString());
-            }
-            current.append(path[i]);
-        }
-        paths.add(current.toString());
-
         if (revisionId == null) {
             revisionId = new GetHeadRevisionCommand(nodeStore).execute();
         }
 
-        FetchNodesActionNew action = new FetchNodesActionNew(nodeStore, paths, revisionId);
+        FetchNodesActionNew action = new FetchNodesActionNew(nodeStore, path, 0, revisionId);
         action.setBranchId(branchId);
-        //action.setValidCommits(validCommits);
 
         Map<String, MongoNode> pathAndNodeMap = action.execute();
-        String currentPath = this.path;
-        while (!PathUtils.denotesRoot(currentPath)) {
-            String childName = PathUtils.getName(currentPath);
-            String parentPath = PathUtils.getParentPath(currentPath);
-            MongoNode parentNode = pathAndNodeMap.get(parentPath);
-            if (parentNode == null || !parentNode.childExists(childName)) {
-                node = null;
-                return false;
-            }
-            currentPath = PathUtils.getParentPath(currentPath);
-        }
         node = pathAndNodeMap.get(this.path);
-        return true;
+        return node != null && !node.isDeleted();
     }
 
     /**

Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/instruction/CommitCommandInstructionVisitor.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/instruction/CommitCommandInstructionVisitor.java?rev=1431954&r1=1431953&r2=1431954&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/instruction/CommitCommandInstructionVisitor.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/instruction/CommitCommandInstructionVisitor.java
Fri Jan 11 09:44:56 2013
@@ -128,6 +128,7 @@ public class CommitCommandInstructionVis
                     + " does not exists at parent path: " + parentPath);
         }
         parent.removeChild(nodeName);
+        markAsDeleted(nodePath);
     }
 
     @Override
@@ -156,25 +157,29 @@ public class CommitCommandInstructionVis
             throw new RuntimeException("Node already exists at copy destination path: " +
destPath);
         }
 
-        // First, copy the existing nodes.
-        FetchNodesActionNew action = new FetchNodesActionNew(nodeStore,
-                srcPath, FetchNodesActionNew.LIMITLESS_DEPTH, baseRevisionId);
-        action.setBranchId(branchId);
-        Map<String, MongoNode> nodesToCopy = action.execute();
-        for (MongoNode nodeMongo : nodesToCopy.values()) {
-            String oldPath = nodeMongo.getPath();
-            String oldPathRel = PathUtils.relativize(srcPath, oldPath);
-            String newPath = PathUtils.concat(destPath, oldPathRel);
-
-            nodeMongo.setPath(newPath);
-            nodeMongo.removeField("_id");
-            pathNodeMap.put(newPath, nodeMongo);
+        // First, copy existing nodes.
+        Map<String, MongoNode> nodesToCopy = fetchNodes(srcPath);
+        for (MongoNode srcNode : nodesToCopy.values()) {
+            String srcNodePath = srcNode.getPath();
+            MongoNode stagedSrcNode = pathNodeMap.get(srcNodePath);
+            if (stagedSrcNode != null && stagedSrcNode.isDeleted()) {
+                // Skip nodes that are staged to be deleted.
+                continue;
+            }
+            String destNodePath = PathUtils.concat(destPath, PathUtils.relativize(srcPath,
srcNodePath));
+            MongoNode destNode = srcNode.copy();
+            destNode.setPath(destNodePath);
+            destNode.removeField("_id");
+            if (stagedSrcNode != null) {
+                copyStagedChanges(stagedSrcNode, destNode, false);
+            }
+            pathNodeMap.put(destNodePath, destNode);
         }
 
         // Then, copy any staged changes.
-        MongoNode srcNode = getStoredNode(srcPath, false);
+        MongoNode srcNode = getStagedNode(srcPath);
         MongoNode destNode = getStagedNode(destPath);
-        copyStagedChanges(srcNode, destNode);
+        copyStagedChanges(srcNode, destNode, false);
 
         // Finally, add to destParent.
         destParent.addChild(destNodeName);
@@ -205,29 +210,40 @@ public class CommitCommandInstructionVis
             throw new RuntimeException("Node already exists at move destination path: " +
destPath);
         }
 
-        // First, copy the existing nodes.
-        FetchNodesActionNew action = new FetchNodesActionNew(nodeStore,
-                srcPath, FetchNodesActionNew.LIMITLESS_DEPTH, baseRevisionId);
-        action.setBranchId(branchId);
-        Map<String, MongoNode> nodesToCopy = action.execute();
-        for (MongoNode nodeMongo : nodesToCopy.values()) {
-            String oldPath = nodeMongo.getPath();
-            String oldPathRel = PathUtils.relativize(srcPath, oldPath);
-            String newPath = PathUtils.concat(destPath, oldPathRel);
-
-            nodeMongo.setPath(newPath);
-            nodeMongo.removeField("_id");
-            pathNodeMap.put(newPath, nodeMongo);
+        // First, copy existing nodes.
+        Map<String, MongoNode> nodesToCopy = fetchNodes(srcPath);
+        for (MongoNode srcNode : nodesToCopy.values()) {
+            String srcNodePath = srcNode.getPath();
+            MongoNode stagedSrcNode = pathNodeMap.get(srcNodePath);
+            if (stagedSrcNode != null && stagedSrcNode.isDeleted()) {
+                // Skip nodes that are staged to be deleted.
+                continue;
+            }
+            String destNodePath = PathUtils.concat(destPath, PathUtils.relativize(srcPath,
srcNodePath));
+            MongoNode destNode = srcNode.copy();
+            destNode.setPath(destNodePath);
+            destNode.removeField("_id");
+            if (stagedSrcNode != null) {
+                copyStagedChanges(stagedSrcNode, destNode, true);
+            }
+            pathNodeMap.put(destNodePath, destNode);
         }
 
         // Then, copy any staged changes.
-        MongoNode srcNode = getStoredNode(srcPath, false);
+        MongoNode srcNode = getStagedNode(srcPath);
         MongoNode destNode = getStagedNode(destPath);
-        copyStagedChanges(srcNode, destNode);
+        copyStagedChanges(srcNode, destNode, true);
 
         // Finally, add to destParent and remove from srcParent.
         destParent.addChild(destNodeName);
         srcParent.removeChild(srcNodeName);
+
+        if (!srcParent.hasPendingChanges()) {
+            pathNodeMap.remove(srcPath);
+            pathNodeMap.remove(srcParentPath);
+        } else {
+            markAsDeleted(srcPath);
+        }
     }
 
     private void checkAbsolutePath(String srcPath) {
@@ -276,42 +292,14 @@ public class CommitCommandInstructionVis
         return node;
     }
 
-    private void copyStagedChanges(MongoNode srcNode, MongoNode destNode) {
-
-        // Copy staged changes at the top level.
-        copyAddedNodes(srcNode, destNode);
+    private void copyStagedChanges(MongoNode srcNode, MongoNode destNode, boolean move) {
+        copyAddedNodes(srcNode, destNode, move);
         copyRemovedNodes(srcNode, destNode);
         copyAddedProperties(srcNode, destNode);
         copyRemovedProperties(srcNode, destNode);
-
-        // Recursively add staged changes of the descendants.
-        List<String> srcChildren = srcNode.getChildren();
-        if (srcChildren == null || srcChildren.isEmpty()) {
-            return;
-        }
-
-        for (String childName : srcChildren) {
-            String oldChildPath = PathUtils.concat(srcNode.getPath(), childName);
-            MongoNode oldChild = getStoredNode(oldChildPath);
-
-            String newChildPath = PathUtils.concat(destNode.getPath(), childName);
-            MongoNode newChild = getStagedNode(newChildPath);
-            copyStagedChanges(oldChild, newChild);
-        }
     }
 
-    private void copyRemovedProperties(MongoNode srcNode, MongoNode destNode) {
-        Map<String, Object> removedProps = srcNode.getRemovedProps();
-        if (removedProps == null || removedProps.isEmpty()) {
-            return;
-        }
-
-        for (String key : removedProps.keySet()) {
-            destNode.removeProp(key);
-        }
-    }
-
-    private void copyAddedNodes(MongoNode srcNode, MongoNode destNode) {
+    private void copyAddedNodes(MongoNode srcNode, MongoNode destNode, boolean move) {
         List<String> addedChildren = srcNode.getAddedChildren();
         if (addedChildren == null || addedChildren.isEmpty()) {
             return;
@@ -320,6 +308,9 @@ public class CommitCommandInstructionVis
         for (String childName : addedChildren) {
             getStagedNode(PathUtils.concat(destNode.getPath(), childName));
             destNode.addChild(childName);
+            if (move) {
+                pathNodeMap.remove(PathUtils.concat(srcNode.getPath(), childName));
+            }
         }
     }
 
@@ -344,4 +335,32 @@ public class CommitCommandInstructionVis
             destNode.addProperty(entry.getKey(), entry.getValue());
         }
     }
+
+    private void copyRemovedProperties(MongoNode srcNode, MongoNode destNode) {
+        Map<String, Object> removedProps = srcNode.getRemovedProps();
+        if (removedProps == null || removedProps.isEmpty()) {
+            return;
+        }
+
+        for (String key : removedProps.keySet()) {
+            destNode.removeProp(key);
+        }
+    }
+
+    private void markAsDeleted(String path) {
+        // Mark the path and all the children path with deleted flag.
+        Map<String, MongoNode> nodes = fetchNodes(path);
+        for (MongoNode nodeMongo : nodes.values()) {
+            nodeMongo.setDeleted();
+            nodeMongo.removeField("_id");
+            pathNodeMap.put(nodeMongo.getPath(), nodeMongo);
+        }
+    }
+
+    private Map<String, MongoNode> fetchNodes(String path) {
+        FetchNodesActionNew action = new FetchNodesActionNew(nodeStore, path,
+                FetchNodesActionNew.LIMITLESS_DEPTH, baseRevisionId);
+        action.setBranchId(branchId);
+        return action.execute();
+    }
 }
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/model/MongoNode.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/model/MongoNode.java?rev=1431954&r1=1431953&r2=1431954&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/model/MongoNode.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/model/MongoNode.java
Fri Jan 11 09:44:56 2013
@@ -32,6 +32,7 @@ import com.mongodb.BasicDBObject;
 public class MongoNode extends BasicDBObject {
 
     public static final String KEY_CHILDREN = "children";
+    public static final String KEY_DELETED = "deleted";
     public static final String KEY_PATH = "path";
     public static final String KEY_PROPERTIES = "props";
     public static final String KEY_REVISION_ID = "revId";
@@ -101,6 +102,14 @@ public class MongoNode extends BasicDBOb
         }
     }
 
+    public boolean isDeleted() {
+        return getBoolean(KEY_DELETED);
+    }
+
+    public void setDeleted() {
+        put(KEY_DELETED, Boolean.TRUE);
+    }
+
     public String getPath() {
         return getString(KEY_PATH);
     }
@@ -145,10 +154,18 @@ public class MongoNode extends BasicDBOb
     //--------------------------------------------------------------------------
 
     public void addChild(String childName) {
+        if (removedChildren != null && removedChildren.contains(childName)) {
+            removedChildren.remove(childName);
+            return;
+        }
+
         if (addedChildren == null) {
             addedChildren = new LinkedList<String>();
         }
-        addedChildren.add(childName);
+
+        if (!addedChildren.contains(childName)) {
+            addedChildren.add(childName);
+        }
     }
 
     public List<String> getAddedChildren() {
@@ -156,10 +173,18 @@ public class MongoNode extends BasicDBOb
     }
 
     public void removeChild(String childName) {
+        if (addedChildren != null && addedChildren.contains(childName)) {
+            addedChildren.remove(childName);
+            return;
+        }
+
         if (removedChildren == null) {
             removedChildren = new LinkedList<String>();
         }
-        removedChildren.add(childName);
+
+        if (!removedChildren.contains(childName)) {
+            removedChildren.add(childName);
+        }
     }
 
     public List<String> getRemovedChildren() {
@@ -238,4 +263,20 @@ public class MongoNode extends BasicDBOb
         sb.append(" }");
         return sb.toString();
     }
+
+    public boolean hasPendingChanges() {
+        if (addedChildren != null && !addedChildren.isEmpty()) {
+            return true;
+        }
+        if (removedChildren != null && !removedChildren.isEmpty()) {
+            return true;
+        }
+        if (addedProps != null && !addedProps.isEmpty()) {
+            return true;
+        }
+        if (removedProps != null && !removedProps.isEmpty()) {
+            return true;
+        }
+        return false;
+    }
 }

Modified: jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/impl/MongoMKCommitRemoveTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/impl/MongoMKCommitRemoveTest.java?rev=1431954&r1=1431953&r2=1431954&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/impl/MongoMKCommitRemoveTest.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/impl/MongoMKCommitRemoveTest.java
Fri Jan 11 09:44:56 2013
@@ -6,7 +6,6 @@ import static org.junit.Assert.assertTru
 import static org.junit.Assert.fail;
 
 import org.apache.jackrabbit.mongomk.BaseMongoMicroKernelTest;
-import org.json.simple.JSONObject;
 import org.junit.Test;
 
 /**



Mime
View raw message