You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by st...@apache.org on 2011/04/18 23:24:35 UTC

svn commit: r1094758 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/CommitBuilder.java

Author: stefan
Date: Mon Apr 18 21:24:35 2011
New Revision: 1094758

URL: http://svn.apache.org/viewvc?rev=1094758&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=1094758&r1=1094757&r2=1094758&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 Mon Apr 18 21:24:35 2011
@@ -28,7 +28,6 @@ import java.util.Map;
 public class CommitBuilder {
 
     final String baseRevId;
-    final String rootNodeId;
 
     final Repository rep;
     final ObjectStore store;
@@ -39,7 +38,6 @@ public class CommitBuilder {
         this.baseRevId = baseRevId;
         this.rep = rep;
         store = rep.getStore();
-        rootNodeId = store.getCommit(baseRevId).getRootNodeId();
         staged = new HashMap<String, MutableNode>();
     }
 
@@ -48,65 +46,121 @@ public class CommitBuilder {
     }
 
     public void addNode(String parentNodePath, String nodeName, Map<String, String> properties) throws Exception {
-        String parentIds[] = resolvePath(baseRevId, parentNodePath);
-
-        Node parent = rep.getNode(baseRevId, parentNodePath);
-        MutableNode modParent;
-        if (!staged.containsKey(parent.getId())) {
-            modParent = new MutableNode(baseRevId, parent);
-            staged.put(parent.getId(), modParent);
-        } else {
-            modParent = staged.get(parent.getId());
-        }
+        MutableNode modParent = getStagedNode(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 = rep.getStore().computeId(newChild.toBytes());
-        staged.put(newId, newChild);
+        String newId = store.computeId(newChild.toBytes());
+        String newPath = "/".equals(parentNodePath) ? "/" + nodeName : parentNodePath + "/" + nodeName;
+        staged.put(newPath, newChild);
 
-        modParent.getChildNodeEntries().put(nodeName, newId);
+        updateParents(parentNodePath, nodeName, newId);
     }
 
     public void removeNode(String nodePath) throws Exception {
+        String parentPath = getParentPath(nodePath);
+        String nodeName = getName(nodePath);
 
+        MutableNode parent = getStagedNode(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);
 
+        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);
 
+        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 {
-        // todo implement
-        return null;
-    }
-
+        if (rep.getHeadRevision() != baseRevId) {
+            // todo implement merge
+        }
 
-    String[] /* array of node id's */ resolvePath(String revId, String path) throws Exception {
-        if (path == null || path.length() == 0 || path.charAt(0) != '/') {
-            throw new IllegalArgumentException("illegal path");
+        String rootNodeId = null;
+        for (Map.Entry<String, MutableNode> entry : staged.entrySet()) {
+            String id = store.putNode(entry.getValue());
+            if (entry.getKey().equals("/")) {
+                rootNodeId = id;
+            }
+        }
+        if (rootNodeId == null) {
+            throw new Exception("internal error: inconsistent staging area content");
         }
 
-        Commit commit = store.getCommit(revId);
+        MutableCommit newCommit = new MutableCommit();
+        newCommit.getParentIds().add(baseRevId);
+        newCommit.setCommitTS(System.currentTimeMillis());
+        newCommit.setRootNodeId(rootNodeId);
+        String newRevId = store.putCommit(newCommit);
+        store.setHeadCommit(newRevId);
+        return newRevId;
+    }
+
+    MutableNode getStagedNode(String nodePath) throws Exception {
+        MutableNode node = staged.get(nodePath);
+        if (node == null) {
+            node = new MutableNode(baseRevId, rep.getNode(baseRevId, nodePath));
+            staged.put(nodePath, node);
+            // make sure all direct ancestors are staged as well
+            String p1 = nodePath;
+            while (!"/".equals(p1)) {
+                String p0 = getParentPath(p1);
+                MutableNode n0 = staged.get(p0);
+                if (n0 != null) {
+                    break;
+                }
+                n0 = new MutableNode(baseRevId, rep.getNode(baseRevId, p0));
+                staged.put(p0, n0);
+                p1 = p0;
+            }
+        }
+        return node;
+    }
 
-        String[] names = path.split("/");
-        String[] ids = new String[names.length + 1];
+    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()));
+        }
+    }
 
-        // get root node
-        ids[0] = commit.getRootNodeId();
-        Node parent = store.getNode(ids[0]);
-        // traverse path and store id of each element
-        for (int i = 1; i <= names.length; i++) {
-            ids[i] = parent.getChildNodeEntries().get(names[i]);
-            parent = store.getNode(ids[i]);
+    static String getParentPath(String path) {
+        int pos = path.lastIndexOf('/');
+        if (pos > 0) {
+            return path.substring(0, pos);
         }
-        return ids;
+        return "/";
     }
 
+    static String getName(String path) {
+        int pos = path.lastIndexOf('/');
+        if (pos != -1) {
+            return path.substring(pos + 1);
+        }
+        return path;
+    }
 }