jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1136582 - in /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem: MemoryKernelImpl.java NodeImpl.java Revision.java
Date Thu, 16 Jun 2011 18:19:41 GMT
Author: thomasm
Date: Thu Jun 16 18:19:41 2011
New Revision: 1136582

URL: http://svn.apache.org/viewvc?rev=1136582&view=rev
Log:
Support revisions and support JCR paths.

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Revision.java
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java?rev=1136582&r1=1136581&r2=1136582&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
Thu Jun 16 18:19:41 2011
@@ -17,11 +17,16 @@
 package org.apache.jackrabbit.mk.mem;
 
 import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
+import java.util.SortedMap;
 import java.util.TreeMap;
 import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.json.JsopTokenizer;
+import org.apache.jackrabbit.mk.util.NonDescendingClock;
 
 /**
  * An in-memory implementation.
@@ -33,8 +38,10 @@ public class MemoryKernelImpl implements
 
     private MemoryDataStore ds;
     private long headRevId;
-    private TreeMap<Long, NodeImpl> roots;
+    private TreeMap<Long, Revision> revisions;
+    private ArrayList<Revision> revisionList;
     private NodeImpl headRoot;
+    private NonDescendingClock clock = new NonDescendingClock(System.currentTimeMillis());
 
     public synchronized static MemoryKernelImpl get(String name) {
         MemoryKernelImpl instance = INSTANCES.get(name);
@@ -52,13 +59,16 @@ public class MemoryKernelImpl implements
     public void clear() {
         headRevId = 0;
         ds = new MemoryDataStore();
-        roots = new TreeMap<Long, NodeImpl>();
+        revisions = new TreeMap<Long, Revision>();
+        revisionList = new ArrayList<Revision>();
         headRoot = new NodeImpl(headRevId);
-        commit();
+        commit("+\"/\" {}");
     }
 
-    private void commit() {
-        roots.put(headRevId, headRoot);
+    private void commit(String jsonDiff) {
+        Revision r = new Revision(headRevId, clock.time(), headRoot, jsonDiff);
+        revisions.put(headRevId, r);
+        revisionList.add(r);
     }
 
     public synchronized String commit(String path, String jsonDiff, String revisionId) {
@@ -67,18 +77,13 @@ public class MemoryKernelImpl implements
         // using an old revision, if yes when is it allowed, or how is it different from
using head?
         // TODO increment headRevId less often? commit in the background?
         // would be possible if we don't (always) return the head revision
-        // TODO the hidden property ":name" prevents shareable nodes
         // TODO property type as comment versus as special property
         // TODO metadata in storage (version)
         // TODO optional read / write version in json 'api' (as comments?)
-        // TODO data type encoding mechanism:
-        // http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx
-        // http://stackoverflow.com/questions/206384/how-to-format-a-json-date
-        // String as Json: "/Date(1)/"; date as Json: "\/Date(1)\/"
 
         headRevId++;
         apply(path, jsonDiff);
-        commit();
+        commit(jsonDiff);
         return getHeadRevision();
     }
 
@@ -129,10 +134,41 @@ public class MemoryKernelImpl implements
         return getRevisionId(headRevId);
     }
 
+    public String getRevisions(long since, int maxEntries) {
+        Revision find = new Revision(0, since, null, null);
+        int index = Collections.binarySearch(revisionList, find, new Comparator<Revision>()
{
+            public int compare(Revision o1, Revision o2) {
+                long t1 = o1.getTime(), t2 = o2.getTime();
+                return t1 < t2 ? -1 : t2 > t1 ? 1 : 0;
+            }
+        });
+        if (index < 0) {
+            index = -index + 1;
+        }
+        JsopBuilder buff = new JsopBuilder().array();
+        for (int i= index; i < revisionList.size(); i++) {
+            Revision rev = revisionList.get(i);
+            buff.object().
+                key("id").value(Long.toString(rev.getId())).
+                key("ts").value(rev.getTime()).
+            endObject();
+        }
+        return buff.endArray().toString();
+    }
+
     public String getJournal(String fromRevisionId, String toRevisionId) {
         long fromRevId = parseRevisionId(fromRevisionId);
         long toRevId = parseRevisionId(fromRevisionId);
-        return "";
+        JsopBuilder buff = new JsopBuilder().array();
+        SortedMap<Long, Revision> map = revisions.subMap(fromRevId, true, toRevId,
true);
+        for (Revision rev : map.values()) {
+            buff.object().
+                key("id").value(Long.toString(rev.getId())).
+                key("ts").value(rev.getTime()).
+                key("changes").value(rev.getDiff()).
+            endObject();
+        }
+        return buff.endArray().toString();
     }
 
     public String getNodes(String path, String revisionId) {
@@ -145,7 +181,7 @@ public class MemoryKernelImpl implements
         }
         path = path.substring(1);
         long revId = parseRevisionId(revisionId);
-        NodeImpl root = roots.get(revId);
+        NodeImpl root = revisions.get(revId).getRootNode();
         NodeImpl n = root.getNode(path);
         if (n == null) {
             throw new RuntimeException("path not found: " + path);
@@ -155,11 +191,6 @@ public class MemoryKernelImpl implements
         return json.toString();
     }
 
-    public String getRevisions(long since, int maxEntries) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
     public boolean nodeExists(String path, String revisionId) {
         // TODO use a bloom filter
         if (!path.startsWith("/")) {
@@ -167,7 +198,7 @@ public class MemoryKernelImpl implements
         }
         path = path.substring(1);
         long revId = parseRevisionId(revisionId);
-        NodeImpl n = roots.get(revId);
+        NodeImpl n = revisions.get(revId).getRootNode();
         if (n == null) {
             return false;
         }
@@ -178,7 +209,6 @@ public class MemoryKernelImpl implements
         return Long.toHexString(revId);
     }
 
-    // TODO revisionId could be a long
     private static long parseRevisionId(String revisionId) {
         return Long.parseLong(revisionId, 16);
     }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java?rev=1136582&r1=1136581&r2=1136582&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
Thu Jun 16 18:19:41 2011
@@ -21,6 +21,7 @@ import java.util.LinkedHashMap;
 import java.util.Map.Entry;
 import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.json.JsopTokenizer;
+import org.apache.jackrabbit.mk.util.PathUtils;
 
 public class NodeImpl {
 
@@ -54,7 +55,7 @@ public class NodeImpl {
         if (childNodes == null) {
             return false;
         }
-        int index = path.indexOf('/');
+        int index = PathUtils.getNextSlash(path, 0);
         if (index < 0) {
             return childNodes.containsKey(path);
         }
@@ -73,7 +74,7 @@ public class NodeImpl {
         if (childNodes == null) {
             return null;
         }
-        int index = path.indexOf('/');
+        int index = PathUtils.getNextSlash(path, 0);
         if (index < 0) {
             return childNodes.get(path);
         }
@@ -86,7 +87,7 @@ public class NodeImpl {
     }
 
     public NodeImpl cloneAndAddChildNode(String path, NodeImpl newNode, long revId) {
-        int index = path.indexOf('/');
+        int index = PathUtils.getNextSlash(path, 0);
         if (index < 0) {
             NodeImpl clone = clone(revId);
             clone.addChildNode(path, newNode);
@@ -104,7 +105,7 @@ public class NodeImpl {
     }
 
     public NodeImpl cloneAndRemoveChildNode(String path, long revId) {
-        int index = path.indexOf('/');
+        int index = PathUtils.getNextSlash(path, 0);
         if (index < 0) {
             NodeImpl clone = clone(revId);
             clone.removeChildNode(path);
@@ -122,7 +123,7 @@ public class NodeImpl {
     }
 
     public NodeImpl cloneAndSetProperty(String path, String value, long revId) {
-        int index = path.indexOf('/');
+        int index = PathUtils.getNextSlash(path, 0);
         if (index < 0) {
             NodeImpl clone = clone(revId);
             clone.setProperty(path, value);

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Revision.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Revision.java?rev=1136582&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Revision.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Revision.java
Thu Jun 16 18:19:41 2011
@@ -0,0 +1,49 @@
+/*
+ * 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.mem;
+
+public class Revision {
+
+    private final long id;
+    private final long time;
+    private final NodeImpl root;
+    private final String diff;
+
+    public Revision(long id, long time, NodeImpl root, String diff) {
+        this.id = id;
+        this.time = time;
+        this.root = root;
+        this.diff = diff;
+    }
+
+    public NodeImpl getRootNode() {
+        return root;
+    }
+
+    public long getId() {
+        return id;
+    }
+
+    public long getTime() {
+        return time;
+    }
+
+    public String getDiff() {
+        return diff;
+    }
+
+}



Mime
View raw message