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/27 18:47:21 UTC

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

Author: stefan
Date: Wed Apr 27 16:47:21 2011
New Revision: 1097173

URL: http://svn.apache.org/viewvc?rev=1097173&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=1097173&r1=1097172&r2=1097173&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 Wed Apr 27 16:47:21 2011
@@ -22,6 +22,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -29,18 +30,21 @@ import java.util.Map;
  */
 public class CommitBuilder {
 
-    final String baseRevId;
+    String baseRevId;
 
     final Repository rep;
     final ObjectStore store;
 
     final Map<String, MutableNode> staged;
+    // change log
+    final List<Change> changeLog;
 
     public CommitBuilder(String baseRevId, Repository rep) throws Exception {
         this.baseRevId = baseRevId;
         this.rep = rep;
         store = rep.getStore();
         staged = new HashMap<String, MutableNode>();
+        changeLog = new ArrayList<Change>();
     }
 
     public void addNode(String parentNodePath, String nodeName) throws Exception {
@@ -58,6 +62,8 @@ public class CommitBuilder {
         modParent.getChildNodeEntries().put(nodeName, ""); // value will be computed on commit
         String newPath = "/".equals(parentNodePath) ? "/" + nodeName : parentNodePath + "/" + nodeName;
         staged.put(newPath, newChild);
+        // update change log
+        changeLog.add(new AddNode(parentNodePath, nodeName, properties));
     }
 
     public void removeNode(String nodePath) throws Exception {
@@ -73,6 +79,9 @@ public class CommitBuilder {
 
         // update staging area
         removeStagedNodes(nodePath);
+
+        // update change log
+        changeLog.add(new RemoveNode(nodePath));
     }
 
     public void moveNode(String srcPath, String destPath) throws Exception {
@@ -94,6 +103,9 @@ public class CommitBuilder {
 
         // update staging area
         moveStagedNodes(srcPath, destPath);
+
+        // update change log
+        changeLog.add(new MoveNode(srcPath, destPath));
     }
 
     public void copyNode(String srcPath, String destPath) throws Exception {
@@ -111,12 +123,18 @@ public class CommitBuilder {
 
         MutableNode destParent = getOrCreateStagedNode(destParentPath);
         destParent.getChildNodeEntries().put(destNodeName, targetId);
+
+        // update change log
+        changeLog.add(new CopyNode(srcPath, destPath));
     }
 
     public void setProperty(String nodePath, String propName, String propValue) throws Exception {
         MutableNode node = getOrCreateStagedNode(nodePath);
 
         node.getProperties().put(propName, propValue);
+
+        // update change log
+        changeLog.add(new SetProperty(nodePath, propName, propValue));
     }
 
     public void setProperties(String nodePath, Map<String, String> properties) throws Exception {
@@ -124,11 +142,24 @@ public class CommitBuilder {
 
         node.getProperties().clear();
         node.getProperties().putAll(properties);
+
+        // update change log
+        changeLog.add(new SetProperties(nodePath, properties));
     }
 
     public String /* new revId */ doCommit() throws Exception {
+        // todo acquire revisions lock
         if (rep.getHeadRevision() != baseRevId) {
-            // todo lock head revision and re-apply changes
+            // update base revision to new head
+            this.baseRevId = rep.getHeadRevision();
+            // clear staging area
+            staged.clear();
+            // replay change log on new base revision
+            // copy log in order to avoid concurent modifications
+            List<Change> log = new ArrayList<Change>(changeLog);
+            for (Change change : log) {
+                change.apply();
+            }
         }
 
         ArrayList<String> orderedPaths = new ArrayList<String>(staged.keySet());
@@ -173,8 +204,11 @@ public class CommitBuilder {
         String newRevId = store.putCommit(newCommit);
         store.setHeadCommit(newRevId);
 
+        // todo release revisions lock
+
         // reset instance in order to be reusable
         staged.clear();
+        changeLog.clear();
 
         return newRevId;
     }
@@ -236,4 +270,95 @@ public class CommitBuilder {
         }
         return path;
     }
+
+    //--------------------------------------------------------< inner classes >
+    abstract class Change {
+        abstract void apply() throws Exception;
+    }
+
+    class AddNode extends Change {
+        String parentNodePath;
+        String nodeName;
+        Map<String, String> properties;
+
+        AddNode(String parentNodePath, String nodeName, Map<String, String> properties) {
+            this.parentNodePath = parentNodePath;
+            this.nodeName = nodeName;
+            this.properties = properties;
+        }
+
+        void apply() throws Exception {
+            addNode(parentNodePath, nodeName, properties);
+        }
+    }
+
+    class RemoveNode extends Change {
+        String nodePath;
+
+        RemoveNode(String nodePath) {
+            this.nodePath = nodePath;
+        }
+
+        void apply() throws Exception {
+            removeNode(nodePath);
+        }
+    }
+
+    class MoveNode extends Change {
+        String srcPath;
+        String destPath;
+
+        MoveNode(String srcPath, String destPath) {
+            this.srcPath = srcPath;
+            this.destPath = destPath;
+        }
+
+        void apply() throws Exception {
+            moveNode(srcPath, destPath);
+        }
+    }
+
+    class CopyNode extends Change {
+        String srcPath;
+        String destPath;
+
+        CopyNode(String srcPath, String destPath) {
+            this.srcPath = srcPath;
+            this.destPath = destPath;
+        }
+
+        void apply() throws Exception {
+            copyNode(srcPath, destPath);
+        }
+    }
+
+    class SetProperty extends Change {
+        String nodePath;
+        String propName;
+        String propValue;
+
+        SetProperty(String nodePath, String propName, String propValue) {
+            this.nodePath = nodePath;
+            this.propName = propName;
+            this.propValue = propValue;
+        }
+
+        void apply() throws Exception {
+            setProperty(nodePath, propName, propValue);
+        }
+    }
+
+    class SetProperties extends Change {
+        String nodePath;
+        Map<String, String> properties;
+
+        SetProperties(String nodePath, Map<String, String> properties) {
+            this.nodePath = nodePath;
+            this.properties = properties;
+        }
+
+        void apply() throws Exception {
+            setProperties(nodePath, properties);
+        }
+    }
 }