jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1133912 - in /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem: Constants.java JsopBuilder.java MemoryKernelImpl.java NodeImpl.java Val.java
Date Thu, 09 Jun 2011 15:08:19 GMT
Author: thomasm
Date: Thu Jun  9 15:08:19 2011
New Revision: 1133912

URL: http://svn.apache.org/viewvc?rev=1133912&view=rev
Log:
Improve compatibility with persistent implementation.

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Constants.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/JsopBuilder.java
    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
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Val.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Constants.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Constants.java?rev=1133912&r1=1133911&r2=1133912&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Constants.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Constants.java
Thu Jun  9 15:08:19 2011
@@ -20,6 +20,11 @@ package org.apache.jackrabbit.mk.mem;
  * Constants used in this project.
  */
 public class Constants {
+    public static final boolean TYPE_AS_COMMENT = false;
+    public static final boolean NODE_NAME_AS_PROPERTY = true;
+    public static final boolean CHILD_NODE_COUNT_0 = true;
+    public static final boolean JSON_NEWLINES = false;
+
     public static final int VERSION_MAJOR = 3;
     public static final int VERSION_MINOR = 0;
     public static final int BUILD = 1000;

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/JsopBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/JsopBuilder.java?rev=1133912&r1=1133911&r2=1133912&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/JsopBuilder.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/JsopBuilder.java
Thu Jun  9 15:08:19 2011
@@ -30,7 +30,11 @@ public class JsopBuilder {
     }
 
     public void endObject() {
-        buff.append("\n}");
+        if (Constants.JSON_NEWLINES) {
+            buff.append("\n}");
+        } else {
+            buff.append("}");
+        }
         commas.clear(level--);
     }
 
@@ -59,7 +63,9 @@ public class JsopBuilder {
         } else {
             commas.set(level);
         }
-        buff.append('\n');
+        if (Constants.JSON_NEWLINES) {
+            buff.append('\n');
+        }
         buff.append(JsopTokenizer.encode(propertyName)).append(':').append(encodedValue);
     }
 

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=1133912&r1=1133911&r2=1133912&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  9 15:08:19 2011
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.mk.mem;
 
 import java.io.InputStream;
+import java.util.HashMap;
 import java.util.TreeMap;
 import org.apache.jackrabbit.mk.api.MicroKernel;
 
@@ -26,12 +27,30 @@ import org.apache.jackrabbit.mk.api.Micr
  */
 public class MemoryKernelImpl implements MicroKernel {
 
-    private final MemoryDataStore ds = new MemoryDataStore();
+    private static final HashMap<String, MemoryKernelImpl> INSTANCES = new HashMap<String,
MemoryKernelImpl>();
+
+    private MemoryDataStore ds;
     private long headRevId;
-    private TreeMap<Long, NodeImpl> roots = new TreeMap<Long, NodeImpl>();
+    private TreeMap<Long, NodeImpl> roots;
     private NodeImpl headRoot;
 
-    public MemoryKernelImpl() {
+    public synchronized static MemoryKernelImpl get(String name) {
+        MemoryKernelImpl instance = INSTANCES.get(name);
+        if (instance == null) {
+            instance = new MemoryKernelImpl();
+            INSTANCES.put(name, instance);
+        }
+        return instance;
+    }
+
+    private MemoryKernelImpl() {
+        clear();
+    }
+
+    public void clear() {
+        headRevId = 0;
+        ds = new MemoryDataStore();
+        roots = new TreeMap<Long, NodeImpl>();
         headRoot = new NodeImpl(headRevId);
         commit();
     }
@@ -46,14 +65,22 @@ 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
         headRevId++;
-        apply(jsonDiff);
+        apply(path, jsonDiff);
         commit();
         return getHeadRevision();
     }
 
-    private void apply(String jsonDiff) {
+    private void apply(String rootPath, String jsonDiff) {
         JsopTokenizer t = new JsopTokenizer(jsonDiff);
+        if (rootPath.startsWith("/")) {
+            rootPath = rootPath.substring(1);
+        }
+        if (rootPath.length() > 0 && !rootPath.endsWith("/")) {
+            rootPath += "/";
+        }
         while (true) {
             int r = t.read();
             if (r == JsopTokenizer.END) {
@@ -66,11 +93,22 @@ public class MemoryKernelImpl implements
                 t.read(':');
                 t.read('{');
                 NodeImpl n = NodeImpl.parse(t, headRevId);
-                headRoot = headRoot.cloneAndAddChildNode(path, n, headRevId);
+                headRoot = headRoot.cloneAndAddChildNode(rootPath + path, n, headRevId);
                 break;
             case '-':
                 path = t.readString();
-                headRoot = headRoot.cloneAndRemoveChildNode(path, headRevId);
+                headRoot = headRoot.cloneAndRemoveChildNode(rootPath + path, headRevId);
+                break;
+            case '^':
+                path = t.readString();
+                t.read(':');
+                Val val;
+                if (t.matches(JsopTokenizer.NULL)) {
+                    val = null;
+                } else {
+                    val = NodeImpl.parseValue(t);
+                }
+                headRoot = headRoot.cloneAndSetProperty(rootPath + path, val, headRevId);
                 break;
             default:
                 throw new AssertionError("token type: " + t.getTokenType());

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=1133912&r1=1133911&r2=1133912&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  9 15:08:19 2011
@@ -120,6 +120,24 @@ public class NodeImpl {
         return c;
     }
 
+    public NodeImpl cloneAndSetProperty(String path, Val value, long revId) {
+        int index = path.indexOf('/');
+        if (index < 0) {
+            NodeImpl clone = clone(revId);
+            clone.setProperty(path, value);
+            return clone;
+        }
+        String child = path.substring(0, index);
+        NodeImpl n = childNodes.get(child);
+        if (n == null) {
+            throw new RuntimeException("Node not found: " + path);
+        }
+        NodeImpl n2 = n.cloneAndSetProperty(path.substring(index + 1), value, revId);
+        NodeImpl c = clone(revId);
+        c.childNodes.put(child, n2);
+        return c;
+    }
+
     public String toString() {
         return toString(true, 1, -1, -1);
     }
@@ -135,12 +153,16 @@ public class NodeImpl {
             }
         }
         if (childNodes != null) {
+            json.addProperty(":childNodeCount", "" + childNodes.size());
             if (depth > 0) {
                 for (Entry<String, NodeImpl> e : childNodes.entrySet()) {
                     json.addProperty(e.getKey(), e.getValue().toString(true, depth - 1, -1,
-1));
                 }
             }
-            json.addProperty(":childNodeCount", "" + childNodes.size());
+        } else {
+            if (Constants.CHILD_NODE_COUNT_0) {
+                json.addProperty(":childNodeCount", "0");
+            }
         }
         if (includeProperties) {
             json.endObject();
@@ -154,6 +176,9 @@ public class NodeImpl {
         } else if (childNodes.containsKey(name)) {
             throw new RuntimeException("Node already exiss: " + name);
         }
+        if (Constants.NODE_NAME_AS_PROPERTY) {
+            node.setProperty(":name", Val.get(name));
+        }
         childNodes.put(name, node);
     }
 
@@ -175,6 +200,9 @@ public class NodeImpl {
         }
         if (value == null) {
             properties.remove(name);
+            if (properties.size() == 0) {
+                properties = null;
+            }
         } else {
             properties.put(name, value);
         }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Val.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Val.java?rev=1133912&r1=1133911&r2=1133912&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Val.java (original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Val.java Thu
Jun  9 15:08:19 2011
@@ -286,6 +286,43 @@ public class Val implements Comparable<V
     }
 
     public String toString() {
+        if (Constants.TYPE_AS_COMMENT) {
+            return toStringTypeAsComment();
+        }
+        switch (type) {
+        case TYPE_MULTI_VALUE: {
+            StringBuilder buff = new StringBuilder("[ ");
+            int i = 0;
+            for (Val v : (Val[]) value) {
+                if (i++ > 0) {
+                    buff.append(", ");
+                }
+                // should in theory never be null (unless there is a bug)
+                if (v != null) {
+                    buff.append(v.toString());
+                }
+            }
+            return buff.append(" ]").toString();
+        }
+        case TYPE_BINARY_REFERENCE: {
+            return "\"" + value + "\"";
+        }
+        case PropertyType.BOOLEAN:
+        case PropertyType.LONG:
+        case PropertyType.DECIMAL:
+            return value.toString();
+        case PropertyType.DOUBLE:
+            String s = value.toString();
+            if (s.equals("NaN") || s.endsWith("Infinity")) {
+                s = "\"" + s + "\"";
+            }
+            return s;
+        default:
+            return JsopTokenizer.encode(value.toString());
+        }
+    }
+
+    private String toStringTypeAsComment() {
         switch (type) {
         case TYPE_MULTI_VALUE: {
             StringBuilder buff = new StringBuilder("[ ");



Mime
View raw message