jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1099831 - in /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk: MicroKernelImpl.java Repository.java json/DiffParser.java store/CommitBuilder.java store/RevisionDiffBuilder.java util/ util/PathUtil.java
Date Thu, 05 May 2011 14:55:59 GMT
Author: stefan
Date: Thu May  5 14:55:59 2011
New Revision: 1099831

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

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtil.java
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/Repository.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/DiffParser.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/CommitBuilder.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/RevisionDiffBuilder.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java?rev=1099831&r1=1099830&r2=1099831&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
Thu May  5 14:55:59 2011
@@ -23,6 +23,8 @@ import org.apache.jackrabbit.mk.json.Dif
 import org.apache.jackrabbit.mk.store.Commit;
 import org.apache.jackrabbit.mk.store.CommitBuilder;
 import org.apache.jackrabbit.mk.store.Node;
+import org.apache.jackrabbit.mk.store.RevisionDiffBuilder;
+import org.apache.jackrabbit.mk.util.PathUtil;
 import org.json.simple.JSONObject;
 import org.json.simple.parser.ContainerFactory;
 import org.json.simple.parser.JSONParser;
@@ -34,6 +36,7 @@ import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.ListIterator;
 import java.util.Map;
 
 /**
@@ -104,7 +107,8 @@ public class MicroKernelImpl implements 
 
         StringBuilder buf = new StringBuilder();
         buf.append("[");
-        for (Commit commit : history) {
+        for (int i = history.size() - 1; i >= 0; i--) {
+            Commit commit = history.get(i);
             buf.append("{");
             buf.append("\"id\":\"");
             buf.append(commit.getId());
@@ -125,9 +129,95 @@ public class MicroKernelImpl implements 
             throw new IllegalStateException("this instance has already been disposed");
         }
 
-        // todo implement
+        List<Commit> commits = new ArrayList<Commit>();
+        try {
+            Commit commit = rep.getCommit(toRevisionId);
+            while (commit != null) {
+                commits.add(commit);
+                if (commit.getId().equals(fromRevisionId)) {
+                    break;
+                }
+                String commitId = commit.getParentId();
+                if (commitId == null) {
+                    break;
+                }
+                commit = rep.getCommit(commitId);
+            }
+        } catch (Exception e) {
+            throw new MicroKernelException(e);
+        }
 
-        return null;
+        final StringBuilder buf = new StringBuilder();
+        buf.append("[");
+        for (int i = commits.size() - 1; i >= 0; i--) {
+            Commit commit = commits.get(i);
+            if (commit.getParentId() == null) {
+                continue;
+            }
+            buf.append("{");
+            buf.append("\"id\":\"");
+            buf.append(commit.getId());
+            buf.append("\",\"ts\":\"");
+            buf.append(commit.getCommitTS());
+            buf.append("\",\"changes\":\"");
+
+            try {
+                new RevisionDiffBuilder(commit.getParentId(), commit.getId(), rep, new RevisionDiffBuilder.RevisionDiffHandler()
{
+                    public void propAdded(String nodePath, String propName, String value)
{
+                        buf.append("+\"");
+                        buf.append(PathUtil.concat(nodePath, propName));
+                        buf.append("\":\"");
+                        buf.append(value);
+                        buf.append("\"\n");
+                    }
+
+                    public void propChanged(String nodePath, String propName, String oldValue,
String newValue) {
+                        buf.append("^\"");
+                        buf.append(PathUtil.concat(nodePath, propName));
+                        buf.append("\":\"");
+                        buf.append(newValue);
+                        buf.append("\"\n");
+                    }
+
+                    public void propDeleted(String nodePath, String propName) {
+                        buf.append("-\"");
+                        buf.append(PathUtil.concat(nodePath, propName));
+                        buf.append("\"\n");
+                    }
+
+                    public void childNodeAdded(String nodePath, String childName, String
id) {
+                        buf.append("+\"");
+                        buf.append(PathUtil.concat(nodePath, childName));
+                        buf.append("\":");
+                        try {
+                            toJson(rep.getStore().getNode(id), childName, Integer.MAX_VALUE,
buf);
+                        } catch (Exception e) {
+                            buf.append("\"ERROR: failed to retrieve node ");
+                            buf.append(id);
+                            buf.append("\"");
+                        }
+                        buf.append("\n");
+                    }
+
+                    public void childNodeDeleted(String nodePath, String childName, String
id) {
+                        buf.append("-\"");
+                        buf.append(PathUtil.concat(nodePath, childName));
+                        buf.append("\"\n");
+                    }
+                }).build();
+            } catch (Exception e) {
+                throw new MicroKernelException(e);
+            }
+
+            buf.append("\"},");
+        }
+        // trim redundant trailing comma
+        if (buf.charAt(buf.length() - 1) == ',') {
+            buf.deleteCharAt(buf.length() - 1);
+        }
+        buf.append("]");
+
+        return buf.toString();
     }
 
     public boolean nodeExists(String path, String revisionId) throws MicroKernelException
{
@@ -149,9 +239,8 @@ public class MicroKernelImpl implements 
         }
 
         try {
-            StringBuffer buf = new StringBuffer();
-            String name =  path.substring(path.lastIndexOf('/') + 1);
-            toJson(rep.getNode(revisionId, path), name, depth, buf);
+            StringBuilder buf = new StringBuilder();
+            toJson(rep.getNode(revisionId, path), PathUtil.getName(path), depth, buf);
             return buf.toString();
         } catch (Exception e) {
             throw new MicroKernelException(e);
@@ -165,7 +254,7 @@ public class MicroKernelImpl implements 
 
         try {
             Node node = rep.getNode(revisionId, path);
-            StringBuffer buf = new StringBuffer();
+            StringBuilder buf = new StringBuilder();
             buf.append('[');
             long pos = 0;
             for (Map.Entry<String, String> entry : node.getChildNodeEntries().entrySet())
{
@@ -251,7 +340,7 @@ public class MicroKernelImpl implements 
                 }
 
                 public void move(String nodeName, String targetPath) throws Exception {
-                    cb.moveNode((path.equals("/") ? path : path + "/") + nodeName, targetPath);
+                    cb.moveNode(PathUtil.concat(path, nodeName), targetPath);
                 }
             };
             DiffParser parser = new DiffParser(handler);
@@ -297,7 +386,7 @@ public class MicroKernelImpl implements 
 
     //-------------------------------------------------------< implementation >
 
-    void toJson(Node node, String name, int depth, StringBuffer buf) throws Exception {
+    void toJson(Node node, String name, int depth, StringBuilder buf) throws Exception {
         buf.append("{");
         buf.append("\":name\":\"" + name + "\",");
         for (Map.Entry prop : node.getProperties().entrySet()) {

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/Repository.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/Repository.java?rev=1099831&r1=1099830&r2=1099831&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/Repository.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/Repository.java
Thu May  5 14:55:59 2011
@@ -22,6 +22,8 @@ import org.apache.jackrabbit.mk.store.Mu
 import org.apache.jackrabbit.mk.store.MutableNode;
 import org.apache.jackrabbit.mk.store.Node;
 import org.apache.jackrabbit.mk.store.ObjectStore;
+import org.apache.jackrabbit.mk.util.PathUtil;
+
 import java.io.File;
 
 /**
@@ -104,13 +106,13 @@ public class Repository {
     }
 
     String[] /* array of node id's */ resolvePath(String revId, String path) throws Exception
{
-        if (path == null || path.length() == 0 || path.charAt(0) != '/') {
+        if (!PathUtil.isAbsolute(path)) {
             throw new IllegalArgumentException("illegal path");
         }
 
         Commit commit = store.getCommit(revId);
 
-        if (path.equals("/")) {
+        if (PathUtil.denotesRoot(path)) {
             return new String[] { commit.getRootNodeId() };
         }
         String[] names = path.substring(1).split("/");

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/DiffParser.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/DiffParser.java?rev=1099831&r1=1099830&r2=1099831&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/DiffParser.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/DiffParser.java
Thu May  5 14:55:59 2011
@@ -90,8 +90,8 @@ public class DiffParser {
         int action = -1;
         String path = null;
 
-        StringBuffer lineSeparator = null;
-        StringBuffer bf = null;
+        StringBuilder lineSeparator = null;
+        StringBuilder bf = null;
 
         int state = STATE_START_LINE;
         int next = reader.read();
@@ -133,7 +133,7 @@ public class DiffParser {
                     if (Character.isWhitespace((char) next) || next == ':') {
                         throw new Exception("Invalid start of target path '" + next + "'");
                     }
-                    bf = new StringBuffer();
+                    bf = new StringBuilder();
                     bf.append((char) next);
                     state = STATE_TARGET;
                     break;
@@ -154,12 +154,12 @@ public class DiffParser {
 
                 case STATE_START_VALUE:
                     if (isLineSeparator(next)) {
-                        lineSeparator = new StringBuffer();
+                        lineSeparator = new StringBuilder();
                         lineSeparator.append((char) next);
-                        bf = new StringBuffer();
+                        bf = new StringBuilder();
                         state = STATE_START_LINE;
                     } else {
-                        bf = new StringBuffer();
+                        bf = new StringBuilder();
                         bf.append((char) next);
                         state = STATE_VALUE;
                     }
@@ -167,7 +167,7 @@ public class DiffParser {
 
                 case STATE_VALUE:
                     if (isLineSeparator(next)) {
-                        lineSeparator = new StringBuffer();
+                        lineSeparator = new StringBuilder();
                         lineSeparator.append((char) next);
                         state = STATE_START_LINE;
                     } else {
@@ -200,7 +200,7 @@ public class DiffParser {
         informAction(action, path, bf);
     }
 
-    private void informAction(int action, String path, StringBuffer diffVal) throws Exception
{
+    private void informAction(int action, String path, StringBuilder diffVal) throws Exception
{
         if (path == null) {
             throw new Exception("Missing path for action " + action + "(diffValue = '"+ diffVal
+"')");
         }
@@ -231,7 +231,7 @@ public class DiffParser {
         return c == '\n' || c == '\r';
 
     }
-    private static boolean endsWithDelim(StringBuffer bf) {
+    private static boolean endsWithDelim(StringBuilder bf) {
         if (bf.length() < 2) {
             return false;
         } else {

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=1099831&r1=1099830&r2=1099831&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 May  5 14:55:59 2011
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.mk.store;
 
 import org.apache.jackrabbit.mk.Repository;
+import org.apache.jackrabbit.mk.util.PathUtil;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -60,15 +61,15 @@ public class CommitBuilder {
         newChild.getProperties().putAll(properties);
 
         modParent.getChildNodeEntries().put(nodeName, ""); // value will be computed on commit
-        String newPath = "/".equals(parentNodePath) ? "/" + nodeName : parentNodePath + "/"
+ nodeName;
+        String newPath = PathUtil.concat(parentNodePath, nodeName);
         staged.put(newPath, newChild);
         // update change log
         changeLog.add(new AddNode(parentNodePath, nodeName, properties));
     }
 
     public void removeNode(String nodePath) throws Exception {
-        String parentPath = getParentPath(nodePath);
-        String nodeName = getName(nodePath);
+        String parentPath = PathUtil.getParentPath(nodePath);
+        String nodeName = PathUtil.getName(nodePath);
 
         MutableNode parent = getOrCreateStagedNode(parentPath);
         if (!parent.getChildNodeEntries().containsKey(nodeName)) {
@@ -85,11 +86,11 @@ public class CommitBuilder {
     }
 
     public void moveNode(String srcPath, String destPath) throws Exception {
-        String srcParentPath = getParentPath(srcPath);
-        String srcNodeName = getName(srcPath);
+        String srcParentPath = PathUtil.getParentPath(srcPath);
+        String srcNodeName = PathUtil.getName(srcPath);
 
-        String destParentPath = getParentPath(destPath);
-        String destNodeName = getName(destPath);
+        String destParentPath = PathUtil.getParentPath(destPath);
+        String destNodeName = PathUtil.getName(destPath);
 
         MutableNode srcParent = getOrCreateStagedNode(srcParentPath);
         String targetId = srcParent.getChildNodeEntries().get(srcNodeName);
@@ -109,11 +110,11 @@ public class CommitBuilder {
     }
 
     public void copyNode(String srcPath, String destPath) throws Exception {
-        String srcParentPath = getParentPath(srcPath);
-        String srcNodeName = getName(srcPath);
+        String srcParentPath = PathUtil.getParentPath(srcPath);
+        String srcNodeName = PathUtil.getName(srcPath);
 
-        String destParentPath = getParentPath(destPath);
-        String destNodeName = getName(destPath);
+        String destParentPath = PathUtil.getParentPath(destPath);
+        String destNodeName = PathUtil.getName(destPath);
 
         MutableNode srcParent = getOrCreateStagedNode(srcParentPath);
         String targetId = srcParent.getChildNodeEntries().get(srcNodeName);
@@ -176,16 +177,7 @@ public class CommitBuilder {
             }
 
             int getDepth(String path) {
-                if ("/".equals(path)) {
-                    return 0;
-                }
-                int count = 0;
-                for (char c : path.toCharArray()) {
-                    if (c == '/') {
-                        count++;
-                    }
-                }
-                return count;
+                return PathUtil.getDepth(path);
             }
         });
         // iterate over staged entries in depth-descending order
@@ -193,10 +185,10 @@ public class CommitBuilder {
         for (String path : orderedPaths) {
             // persist node
             String id = store.putNode(staged.get(path));
-            if (path.equals("/")) {
+            if (PathUtil.denotesRoot(path)) {
                 rootNodeId = id;
             } else {
-                staged.get(getParentPath(path)).getChildNodeEntries().put(getName(path),
id);
+                staged.get(PathUtil.getParentPath(path)).getChildNodeEntries().put(PathUtil.getName(path),
id);
             }
         }
         if (rootNodeId == null) {
@@ -225,8 +217,8 @@ public class CommitBuilder {
             staged.put(nodePath, node);
             // make sure all direct ancestors are staged as well
             String p1 = nodePath;
-            while (!"/".equals(p1)) {
-                String p0 = getParentPath(p1);
+            while (!PathUtil.denotesRoot(p1)) {
+                String p0 = PathUtil.getParentPath(p1);
                 MutableNode n0 = staged.get(p0);
                 if (n0 != null) {
                     break;
@@ -245,7 +237,7 @@ public class CommitBuilder {
             staged.remove(srcPath);
             staged.put(destPath, node);
             for (String childName : node.getChildNodeEntries().keySet()) {
-                moveStagedNodes(srcPath + "/" + childName, destPath + "/" + childName);
+                moveStagedNodes(PathUtil.concat(srcPath, childName), PathUtil.concat(destPath,
childName));
             }
         }
     }
@@ -255,27 +247,11 @@ public class CommitBuilder {
         if (node != null) {
             staged.remove(nodePath);
             for (String childName : node.getChildNodeEntries().keySet()) {
-                removeStagedNodes(nodePath + "/" + childName);
+                removeStagedNodes(PathUtil.concat(nodePath, childName));
             }
         }
     }
 
-    static String getParentPath(String path) {
-        int pos = path.lastIndexOf('/');
-        if (pos > 0) {
-            return path.substring(0, pos);
-        }
-        return "/";
-    }
-
-    static String getName(String path) {
-        int pos = path.lastIndexOf('/');
-        if (pos != -1) {
-            return path.substring(pos + 1);
-        }
-        return path;
-    }
-
     //--------------------------------------------------------< inner classes >
     abstract class Change {
         abstract void apply() throws Exception;

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/RevisionDiffBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/RevisionDiffBuilder.java?rev=1099831&r1=1099830&r2=1099831&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/RevisionDiffBuilder.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/RevisionDiffBuilder.java
Thu May  5 14:55:59 2011
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.mk.store;
 
 import org.apache.jackrabbit.mk.Repository;
+import org.apache.jackrabbit.mk.util.PathUtil;
 
 import java.util.HashSet;
 import java.util.Map;
@@ -32,7 +33,8 @@ public class RevisionDiffBuilder {
     final Repository rep;
     final RevisionDiffHandler handler;
 
-    public RevisionDiffBuilder(String revisionId1, String revisionId2, Repository rep, RevisionDiffHandler
handler) {
+    public RevisionDiffBuilder(String revisionId1, String revisionId2,
+                               Repository rep, RevisionDiffHandler handler) {
         this.revisionId1 = revisionId1;
         this.revisionId2 = revisionId2;
         this.rep = rep;
@@ -97,12 +99,8 @@ public class RevisionDiffBuilder {
                 if (id != null && !entry.getValue().equals(id)) {
                     node1 = rep.getStore().getNode(entry.getValue());
                     node2 = rep.getStore().getNode(id);
-                    if (!path.equals("/")) {
-                        path += "/";
-                    }
-                    path += entry.getKey();
                     // do recurse
-                    diff(path, node1, node2);
+                    diff(PathUtil.concat(path, entry.getKey()), node1, node2);
                 }
             }
         }

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtil.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtil.java?rev=1099831&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtil.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtil.java
Thu May  5 14:55:59 2011
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.mk.util;
+
+/**
+ *
+ */
+public class PathUtil {
+
+    private PathUtil() {
+    }
+
+    public static boolean denotesRoot(String path) {
+        return "/".equals(path);
+    }
+
+    public static boolean isAbsolute(String path) {
+        return path.startsWith("/");
+    }
+
+    public static String getParentPath(String path) {
+        int pos = path.lastIndexOf('/');
+        if (pos > 0) {
+            return path.substring(0, pos);
+        }
+        return "/";
+    }
+
+    public static String getName(String path) {
+        int pos = path.lastIndexOf('/');
+        if (pos != -1) {
+            return path.substring(pos + 1);
+        }
+        return path;
+    }
+
+    public static int getDepth(String path) {
+        if (PathUtil.denotesRoot(path)) {
+            return 0;
+        }
+        int count = 0;
+        for (char c : path.toCharArray()) {
+            if (c == '/') {
+                count++;
+            }
+        }
+        return count;
+    }
+
+    public static String concat(String parentPath, String nameOrRelativePath) {
+        if (nameOrRelativePath.startsWith("/")) {
+            throw new IllegalArgumentException("can't append absolute path");
+        }
+        StringBuilder buf = new StringBuilder(parentPath);
+        if (buf.charAt(buf.length() - 1) != '/') {
+            buf.append('/');
+        }
+        buf.append(nameOrRelativePath);
+        return buf.toString();
+    }
+}



Mime
View raw message