jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1095722 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/CommitBuilder.java
Date Thu, 21 Apr 2011 14:05:39 GMT
Author: stefan
Date: Thu Apr 21 14:05:39 2011
New Revision: 1095722

URL: http://svn.apache.org/viewvc?rev=1095722&view=rev
Log:
MicroKernel prototype (WIP)

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/CommitBuilder.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/CommitBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/CommitBuilder.java?rev=1095722&r1=1095721&r2=1095722&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/CommitBuilder.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/CommitBuilder.java
Thu Apr 21 14:05:39 2011
@@ -18,7 +18,9 @@ package org.apache.jackrabbit.mk.store;
 
 import org.apache.jackrabbit.mk.Repository;
 
+import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -46,52 +48,41 @@ public class CommitBuilder {
     }
 
     public void addNode(String parentNodePath, String nodeName, Map<String, String>
properties) throws Exception {
-        MutableNode modParent = getStagedNode(parentNodePath);
+        MutableNode modParent = getOrCreateStagedNode(parentNodePath);
         if (modParent.getChildNodeEntries().containsKey(nodeName)) {
             throw new Exception("there's already a child node with name '" + nodeName + "'");
         }
         MutableNode newChild = new MutableNode(baseRevId);
         newChild.getProperties().putAll(properties);
 
-        String newId = store.computeId(newChild.toBytes());
+        modParent.getChildNodeEntries().put(nodeName, ""); // value will be computed on commit
         String newPath = "/".equals(parentNodePath) ? "/" + nodeName : parentNodePath + "/"
+ nodeName;
         staged.put(newPath, newChild);
-
-        updateParents(parentNodePath, nodeName, newId);
     }
 
     public void removeNode(String nodePath) throws Exception {
         String parentPath = getParentPath(nodePath);
         String nodeName = getName(nodePath);
 
-        MutableNode parent = getStagedNode(parentPath);
+        MutableNode parent = getOrCreateStagedNode(parentPath);
         if (!parent.getChildNodeEntries().containsKey(nodeName)) {
             throw new Exception("node not found");
         }
 
         parent.getChildNodeEntries().remove(nodeName);
-        if (!"/".equals(parentPath)) {
-            updateParents(getParentPath(parentPath), getName(parentPath), store.computeId(parent.toBytes()));
-        }
     }
 
     public void setProperty(String nodePath, String propName, String propValue) throws Exception
{
-        MutableNode node = getStagedNode(nodePath);
+        MutableNode node = getOrCreateStagedNode(nodePath);
 
         node.getProperties().put(propName, propValue);
-        if (!"/".equals(nodePath)) {
-            updateParents(getParentPath(nodePath), getName(nodePath), store.computeId(node.toBytes()));
-        }
     }
 
     public void setProperties(String nodePath, Map<String, String> properties) throws
Exception {
-        MutableNode node = getStagedNode(nodePath);
+        MutableNode node = getOrCreateStagedNode(nodePath);
 
         node.getProperties().clear();
         node.getProperties().putAll(properties);
-        if (!"/".equals(nodePath)) {
-            updateParents(getParentPath(nodePath), getName(nodePath), store.computeId(node.toBytes()));
-        }
     }
 
     public String /* new revId */ doCommit() throws Exception {
@@ -99,11 +90,35 @@ public class CommitBuilder {
             // todo implement merge
         }
 
+        ArrayList<String> orderedPaths = new ArrayList<String>(staged.keySet());
+        Collections.sort(orderedPaths, new Comparator<String>() {
+            public int compare(String path1, String path2) {
+                // paths should be ordered by depth, descending
+                int result = getDepth(path2) - getDepth(path1);
+                return (result != 0) ? result : 1;
+            }
+
+            int getDepth(String path) {
+                if ("/".equals(path)) {
+                    return 0;
+                }
+                int count = 0;
+                for (char c : path.toCharArray()) {
+                    if (c == '/') {
+                        count++;
+                    }
+                }
+                return count;
+            }
+        });
+        // iterate over staged entries in depth-descending order
         String rootNodeId = null;
-        for (Map.Entry<String, MutableNode> entry : staged.entrySet()) {
-            String id = store.putNode(entry.getValue());
-            if (entry.getKey().equals("/")) {
+        for (String path : orderedPaths) {
+            String id = store.putNode(staged.get(path));
+            if (path.equals("/")) {
                 rootNodeId = id;
+            } else {
+                staged.get(getParentPath(path)).getChildNodeEntries().put(getName(path),
id);
             }
         }
         if (rootNodeId == null) {
@@ -116,10 +131,14 @@ public class CommitBuilder {
         newCommit.setRootNodeId(rootNodeId);
         String newRevId = store.putCommit(newCommit);
         store.setHeadCommit(newRevId);
+
+        // reset instance in order to be reusable
+        staged.clear();
+
         return newRevId;
     }
 
-    MutableNode getStagedNode(String nodePath) throws Exception {
+    MutableNode getOrCreateStagedNode(String nodePath) throws Exception {
         MutableNode node = staged.get(nodePath);
         if (node == null) {
             node = new MutableNode(baseRevId, rep.getNode(baseRevId, nodePath));
@@ -140,14 +159,6 @@ public class CommitBuilder {
         return node;
     }
 
-    void updateParents(String path, String childName, String childId) throws Exception {
-        MutableNode node = staged.get(path);
-        node.getChildNodeEntries().put(childName, childId);
-        if (!"/".equals(path)) {
-            updateParents(getParentPath(path), getName(path), store.computeId(node.toBytes()));
-        }
-    }
-
     static String getParentPath(String path) {
         int pos = path.lastIndexOf('/');
         if (pos > 0) {



Mime
View raw message