jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1213220 - in /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem: MemoryKernelImpl.java NodeId.java NodeIdInline.java NodeImpl.java NodeList.java NodeListLarge.java NodeListSmall.java NodeMap.java NodeMapInDb.java
Date Mon, 12 Dec 2011 12:57:10 GMT
Author: thomasm
Date: Mon Dec 12 12:57:09 2011
New Revision: 1213220

URL: http://svn.apache.org/viewvc?rev=1213220&view=rev
Log:
Support 'inline' node ids (where the node itself is the 'node id')

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeId.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeIdInline.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
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeList.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListLarge.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListSmall.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMap.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMapInDb.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=1213220&r1=1213219&r2=1213220&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
Mon Dec 12 12:57:09 2011
@@ -21,16 +21,15 @@ import org.apache.jackrabbit.mk.blobs.Ab
 import org.apache.jackrabbit.mk.blobs.FileBlobStore;
 import org.apache.jackrabbit.mk.blobs.MemoryBlobStore;
 import org.apache.jackrabbit.mk.fs.FileUtils;
-import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.json.JsopReader;
 import org.apache.jackrabbit.mk.json.JsopStream;
 import org.apache.jackrabbit.mk.json.JsopTokenizer;
 import org.apache.jackrabbit.mk.json.JsopWriter;
 import org.apache.jackrabbit.mk.server.Server;
-import org.apache.jackrabbit.mk.util.AscendingClock;
 import org.apache.jackrabbit.mk.util.Cache;
 import org.apache.jackrabbit.mk.util.CommitGate;
 import org.apache.jackrabbit.mk.util.ExceptionFactory;
+import org.apache.jackrabbit.mk.util.NonDescendingClock;
 import org.apache.jackrabbit.mk.util.PathUtils;
 import org.apache.jackrabbit.mk.wrapper.WrapperBase;
 
@@ -65,15 +64,16 @@ Node structure:
  */
 public class MemoryKernelImpl extends WrapperBase implements MicroKernel {
 
-    private static final int MAX_REVISIONS_PER_NODE = 100;
+    private static final int MAX_REVISIONS_PER_NODE = 5;
     private static final HashMap<String, MemoryKernelImpl> INSTANCES = new HashMap<String,
MemoryKernelImpl>();
 
     private final String name;
     private final AbstractBlobStore ds;
-    private final AscendingClock clock = new AscendingClock(System.currentTimeMillis());
+    private final NonDescendingClock clock = new NonDescendingClock(System.currentTimeMillis());
     private final CommitGate gate = new CommitGate();
     private final Cache<Long, Revision> revisionCache = Cache.newInstance(null, 1024
* 1024);
     private volatile long headRevId;
+    private volatile String headRevision;
     private NodeMap nodeMap;
     private Server server;
     private boolean disposed;
@@ -97,7 +97,7 @@ public class MemoryKernelImpl extends Wr
         } else {
             ds = new MemoryBlobStore();
         }
-        if (nodeMap.getRootId() == 0) {
+        if (nodeMap.getRootId() == null) {
             NodeImpl head = new NodeImpl(nodeMap, 0);
             Revision revNode = new Revision(0, 0, "", "");
             head = revNode.store(head, new NodeImpl(nodeMap, 0));
@@ -113,6 +113,7 @@ public class MemoryKernelImpl extends Wr
             headRevId = Revision.parseId(JsopTokenizer.decodeQuoted(rev));
             applyConfig(head);
         }
+        headRevision = Revision.formatId(headRevId);
         if (startServer) {
             server = new Server(this);
             try {
@@ -186,10 +187,10 @@ public class MemoryKernelImpl extends Wr
 
     private String doCommit(String rootPath, JsopReader t, String revisionId, String message)
{
         long oldRevision = headRevId, rev = headRevId + 1;
-        NodeImpl root = nodeMap.getNode(nodeMap.getRootId());
+        NodeImpl root = nodeMap.getRootId().getNode(nodeMap);
         NodeImpl head = root.getNode("head"), oldHead = head;
         NodeImpl data = head.getNode("data");
-        JsopWriter diff = new JsopBuilder();
+        JsopWriter diff = new JsopStream();
         while (true) {
             int r = t.read();
             if (r == JsopTokenizer.END) {
@@ -332,26 +333,26 @@ public class MemoryKernelImpl extends Wr
         root = root.setChild("old", old, rev);
         nodeMap.commit(root);
         headRevId = rev;
-        String headRev = getHeadRevision();
-        gate.commit(headRev);
-        return headRev;
+        headRevision = Revision.formatId(rev);
+        gate.commit(headRevision);
+        return headRevision;
     }
 
     private NodeImpl getRoot() {
-        return nodeMap.getNode(nodeMap.getRootId());
+        return nodeMap.getRootId().getNode(nodeMap);
     }
 
     public String getHeadRevision() {
-        return Revision.formatId(headRevId);
+        return headRevision;
     }
 
     public JsopReader getRevisionsStream(long since, int maxEntries) {
         NodeImpl node = getRoot();
         ArrayList<Revision> revisions = new ArrayList<Revision>();
         Revision r = Revision.get(node.getNode("head"));
-        if (since < r.getTime()) {
+        if (since < r.getTime() && maxEntries > 0) {
             revisions.add(r);
-            while (node.exists("old")) {
+            while (node.exists("old") && revisions.size() < maxEntries) {
                 node = node.getNode("old");
                 for (Iterator<String> it = node.getChildNodeNames(Integer.MAX_VALUE);
it.hasNext();) {
                     r = Revision.get(node.getNode(it.next()));

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeId.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeId.java?rev=1213220&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeId.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeId.java
Mon Dec 12 12:57:09 2011
@@ -0,0 +1,52 @@
+/*
+ * 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;
+
+/**
+ * A node id.
+ */
+public class NodeId {
+
+    public static final NodeId[] EMPTY_ARRAY = new NodeId[0];
+
+    private final long x;
+
+    protected NodeId(long x) {
+        this.x = x;
+    }
+
+    public static NodeId get(long x) {
+        return new NodeId(x);
+    }
+
+    public NodeImpl getNode(NodeMap map) {
+        return map.getNode(x);
+    }
+
+    public String toString() {
+        return Long.toString(x);
+    }
+
+    public long getLong() {
+        return x;
+    }
+
+    public boolean isInline() {
+        return false;
+    }
+
+}

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeIdInline.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeIdInline.java?rev=1213220&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeIdInline.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeIdInline.java
Mon Dec 12 12:57:09 2011
@@ -0,0 +1,47 @@
+/*
+ * 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;
+
+/**
+ * An inline node.
+ */
+public class NodeIdInline extends NodeId {
+
+    private final NodeImpl node;
+
+    protected NodeIdInline(NodeImpl node) {
+        super(0);
+        this.node = node;
+    }
+
+    public static NodeIdInline get(NodeImpl node) {
+        return new NodeIdInline(node);
+    }
+
+    public NodeImpl getNode(NodeMap map) {
+        return node;
+    }
+
+    public String toString() {
+        return node.toString();
+    }
+
+    public boolean isInline() {
+        return true;
+    }
+
+}

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=1213220&r1=1213219&r2=1213220&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
Mon Dec 12 12:57:09 2011
@@ -45,6 +45,11 @@ public class NodeImpl implements Cache.V
     public static final String DESCENDANT_COUNT = ":descendantCount";
 
     /**
+     * The total number of child nodes that are stored inline.
+     */
+    public static final String DESCENDANT_INLINE_COUNT = ":descendantInlineCount";
+
+    /**
      * Used by NodeListLarge when there are many child nodes.
      * The id of an internal node.
      */
@@ -72,20 +77,21 @@ public class NodeImpl implements Cache.V
     private String[] propertyValuePairs;
     private NodeList childNodes;
     private String path;
-    private long id;
+    private NodeId id;
     private int memory;
     private long descendantCount;
+    private int descendantInlineCount;
 
     public NodeImpl(NodeMap map, long revId) {
         this.map = map;
         this.revId = revId;
     }
 
-    long getId() {
+    NodeId getId() {
         return id;
     }
 
-    public void setId(long id) {
+    public void setId(NodeId id) {
         this.id = id;
     }
 
@@ -102,6 +108,7 @@ public class NodeImpl implements Cache.V
         if (childNodes != null) {
             clone.childNodes = childNodes.createClone(map, revId);
             clone.descendantCount = descendantCount;
+            clone.descendantInlineCount = descendantInlineCount;
         }
         return clone;
     }
@@ -114,6 +121,10 @@ public class NodeImpl implements Cache.V
         return descendantCount;
     }
 
+    public long getDescendantInlineCount() {
+        return descendantInlineCount;
+    }
+
     public boolean exists(String path) {
         if (childNodes == null) {
             return false;
@@ -153,7 +164,7 @@ public class NodeImpl implements Cache.V
     }
 
     private NodeImpl getChildNode(String name) {
-        return map.getNode(childNodes.get(name));
+        return childNodes.get(name).getNode(map);
     }
 
     private void addNode(String name, NodeImpl node) {
@@ -175,14 +186,17 @@ public class NodeImpl implements Cache.V
         if (n == null) {
             throw ExceptionFactory.get("Node not found: " + path);
         }
-        long diff = -n.descendantCount;
+        long diffDescendant = -n.descendantCount;
+        long diffInline = -n.descendantInlineCount;
         NodeImpl n2 = n.cloneAndAddChildNode(path.substring(index + 1), before, position,
newNode, revId);
-        diff += n2.descendantCount;
-        NodeImpl c = createClone(revId);
-        c.descendantCount += diff;
-        c.childNodes.remove(child);
-        c.addNode(child, n2);
-        return c;
+        NodeImpl clone = createClone(revId);
+        clone.childNodes.remove(child);
+        clone.addNode(child, n2);
+        diffDescendant += n2.descendantCount;
+        diffInline += n2.descendantInlineCount;
+        clone.descendantCount += diffDescendant;
+        clone.descendantInlineCount += diffDescendant;
+        return clone;
     }
 
     public NodeImpl cloneAndRemoveChildNode(String path, long revId) {
@@ -197,14 +211,17 @@ public class NodeImpl implements Cache.V
         if (n == null) {
             throw ExceptionFactory.get("Node not found: " + path);
         }
-        long diff = -n.descendantCount;
+        long diffDescendant = -n.descendantCount;
+        long diffInline = -n.descendantInlineCount;
         NodeImpl n2 = n.cloneAndRemoveChildNode(path.substring(index + 1), revId);
-        diff += n2.descendantCount;
-        NodeImpl c = createClone(revId);
-        c.descendantCount += diff;
-        c.childNodes.remove(child);
-        c.addNode(child, n2);
-        return c;
+        NodeImpl clone = createClone(revId);
+        clone.childNodes.remove(child);
+        clone.addNode(child, n2);
+        diffDescendant += n2.descendantCount;
+        diffInline += n2.descendantInlineCount;
+        clone.descendantCount += diffDescendant;
+        clone.descendantInlineCount += diffInline;
+        return clone;
     }
 
     public NodeImpl cloneAndSetProperty(String path, String value, long revId) {
@@ -288,6 +305,7 @@ public class NodeImpl implements Cache.V
             if (descendantCount > childNodes.size()) {
                 if (map.getDescendantCount()) {
                     json.key(DESCENDANT_COUNT).value(descendantCount);
+                    json.key(DESCENDANT_INLINE_COUNT).value(descendantInlineCount);
                 }
             }
             if (count != 0) {
@@ -320,6 +338,9 @@ public class NodeImpl implements Cache.V
         }
         addNode(name, node);
         descendantCount += node.descendantCount + 1;
+        if (node.getId().isInline()) {
+            descendantInlineCount += node.descendantInlineCount + 1;
+        }
         if (before || position != null) {
             boolean moveNext = false;
             ArrayList<String> move = new ArrayList<String>();
@@ -351,9 +372,13 @@ public class NodeImpl implements Cache.V
         }
         if (childNodes.size() == 1 || descendantCount <= 1) {
             descendantCount = 0;
+            descendantInlineCount = 0;
         } else {
-            NodeImpl n = map.getNode(childNodes.get(name));
+            NodeImpl n = childNodes.get(name).getNode(map);
             descendantCount -= n.descendantCount + 1;
+            if (n.getId().isInline()) {
+                descendantInlineCount -= n.descendantInlineCount + 1;
+            }
         }
         childNodes.remove(name);
         if (childNodes.size() == 0) {
@@ -488,7 +513,12 @@ public class NodeImpl implements Cache.V
     public String asString() {
         JsopWriter json = new JsopBuilder();
         json.setLineLength(120);
-        json.encodedValue(map.formatId(id)).tag('=');
+        if (id != null && !id.isInline()) {
+            String nodeId = map.formatId(id);
+            if (nodeId != null) {
+                json.encodedValue(nodeId).tag('=');
+            }
+        }
         json.object();
         String[] pv = propertyValuePairs;
         if (pv != null) {
@@ -508,9 +538,12 @@ public class NodeImpl implements Cache.V
     public static NodeImpl fromString(NodeMap map, String s) {
         JsopTokenizer t = new JsopTokenizer(s);
         NodeImpl node = new NodeImpl(map, 0);
-        node.id = map.parseId(t.readRawValue());
-        t.read('=');
-        t.read('{');
+        if (!t.matches('{')) {
+            node.id = map.parseId(t.readRawValue());
+            t.read('=');
+            t.read('{');
+        }
+        boolean descendantCountSet = false;
         if (!t.matches('}')) {
             do {
                 String key = t.readString();
@@ -521,6 +554,7 @@ public class NodeImpl implements Cache.V
                         node.childNodes = NodeListLarge.read(t, map, value);
                     } else if (key.equals(DESCENDANT_COUNT)) {
                         node.descendantCount = Long.parseLong(value);
+                        descendantCountSet = true;
                     } else {
                         node.setProperty(key, value);
                     }
@@ -528,7 +562,17 @@ public class NodeImpl implements Cache.V
                     if (node.childNodes == null) {
                         node.childNodes = new NodeListSmall();
                     }
-                    node.childNodes.add(key, map.parseId(value));
+                    NodeId id = map.parseId(value);
+                    node.childNodes.add(key, id);
+                    if (!descendantCountSet) {
+                        node.descendantCount++;
+                    }
+                    if (id.isInline()) {
+                        if (!descendantCountSet) {
+                            node.descendantCount += id.getNode(map).descendantCount;
+                        }
+                        node.descendantInlineCount += 1 + id.getNode(map).descendantInlineCount;
+                    }
                 } else {
                     node.setProperty(key, value);
                 }
@@ -568,7 +612,7 @@ public class NodeImpl implements Cache.V
     }
 
     interface ChildVisitor {
-        void accept(long childId);
+        void accept(NodeId childId);
     }
 
     public int getMemory() {

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeList.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeList.java?rev=1213220&r1=1213219&r2=1213220&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeList.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeList.java
Mon Dec 12 12:57:09 2011
@@ -29,15 +29,15 @@ interface NodeList {
 
     boolean containsKey(String name);
 
-    long get(String name);
+    NodeId get(String name);
 
-    void add(String name, long x);
+    void add(String name, NodeId x);
 
     String getName(long pos);
 
     Iterator<String> getNames(long offset, int maxCount);
 
-    long remove(String name);
+    NodeId remove(String name);
 
     NodeList createClone(NodeMap map, long revId);
 

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListLarge.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListLarge.java?rev=1213220&r1=1213219&r2=1213220&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListLarge.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListLarge.java
Mon Dec 12 12:57:09 2011
@@ -69,7 +69,7 @@ public class NodeListLarge implements No
     }
 
     NodeList getList(Child c, boolean modify) {
-        NodeImpl n = map.getNode(c.id);
+        NodeImpl n = c.id.getNode(map);
         if (modify) {
             n = n.createClone(revId);
             c.id = map.addNode(n);
@@ -77,7 +77,7 @@ public class NodeListLarge implements No
         return n.getNodeList();
     }
 
-    public long get(String name) {
+    public NodeId get(String name) {
         for (Child c : children) {
             if (c.possiblyContains(name)) {
                 NodeList child = getList(c, false);
@@ -150,7 +150,7 @@ public class NodeListLarge implements No
         return it;
     }
 
-    public void add(String name, long x) {
+    public void add(String name, NodeId x) {
         Child c = children.get(children.size() - 1);
         NodeList child = getList(c, false);
         if (child.size() >= map.getMaxMemoryChildren()) {
@@ -164,12 +164,12 @@ public class NodeListLarge implements No
         size++;
     }
 
-    public long remove(String name) {
+    public NodeId remove(String name) {
         for (Child c : children) {
             if (c.possiblyContains(name)) {
                 NodeList child = getList(c, true);
                 if (child.containsKey(name)) {
-                    Long x = child.remove(name);
+                    NodeId x = child.remove(name);
                     if (x == null) {
                         throw ExceptionFactory.get("Could not remove " + name);
                     }
@@ -187,7 +187,7 @@ public class NodeListLarge implements No
 
     static class Child {
 
-        long id;
+        NodeId id;
         byte[] nameFilter;
 
         boolean possiblyContains(String name) {
@@ -213,7 +213,7 @@ public class NodeListLarge implements No
         ArrayList<Child> newChildren = new ArrayList<Child>();
         for (Child c : children) {
             Child c2 = new Child();
-            c2.id = map.addNode(map.getNode(c.id));
+            c2.id = map.addNode(c.id.getNode(map));
             c2.nameFilter = c.nameFilter;
             newChildren.add(c2);
         }
@@ -233,8 +233,8 @@ public class NodeListLarge implements No
     public void append(JsopWriter json, NodeMap map) {
         for (Child c : children) {
             json.key(NodeImpl.CHILDREN);
-            long x = c.id;
-            long y = map.getId(x);
+            NodeId x = c.id;
+            NodeId y = map.getId(x);
             if (x != y) {
                 c.id = y;
             }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListSmall.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListSmall.java?rev=1213220&r1=1213219&r2=1213220&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListSmall.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListSmall.java
Mon Dec 12 12:57:09 2011
@@ -21,28 +21,24 @@ import java.util.Iterator;
 import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.json.JsopWriter;
 import org.apache.jackrabbit.mk.mem.NodeImpl.ChildVisitor;
+import org.apache.jackrabbit.mk.util.ArrayUtils;
 import org.apache.jackrabbit.mk.util.ExceptionFactory;
 import org.apache.jackrabbit.mk.util.IOUtils;
 import org.apache.jackrabbit.mk.util.StringCache;
-import org.apache.jackrabbit.mk.util.StringUtils;
 
 public class NodeListSmall implements NodeList {
 
-    private static final String[] EMPTY_STRING_ARRAY = new String[0];
-    private static final long[] EMPTY_LONG_ARRAY = new long[0];
-    private static final int[] EMPTY_INTEGER_ARRAY = new int[0];
-
     int size;
     String[] names;
-    long[] children;
+    NodeId[] children;
     private int[] sort;
     private int lastNameIndexCache;
 
     NodeListSmall() {
-        this(EMPTY_STRING_ARRAY, EMPTY_LONG_ARRAY, EMPTY_INTEGER_ARRAY, 0);
+        this(ArrayUtils.EMPTY_STRING_ARRAY, NodeId.EMPTY_ARRAY, ArrayUtils.EMPTY_INTEGER_ARRAY,
0);
     }
 
-    private NodeListSmall(String[] names, long[] children, int[] sort, int size) {
+    private NodeListSmall(String[] names, NodeId[] children, int[] sort, int size) {
         this.names = names;
         this.children = children;
         this.sort = sort;
@@ -81,7 +77,7 @@ public class NodeListSmall implements No
         return -(min + 1);
     }
 
-    public long get(String name) {
+    public NodeId get(String name) {
         int index = find(name);
         if (index < 0) {
             throw ExceptionFactory.get("Node not found: " + name);
@@ -89,16 +85,16 @@ public class NodeListSmall implements No
         return children[sort[index]];
     }
 
-    public void add(String name, long x) {
+    public void add(String name, NodeId x) {
         int index = find(name);
         if (index >= 0) {
             throw ExceptionFactory.get("Node already exists: " + name);
         }
         index = -index - 1;
         name = StringCache.cache(name);
-        names = StringUtils.arrayInsert(names, size, name);
-        children = StringUtils.arrayInsert(children, size, x);
-        sort = StringUtils.arrayInsert(sort, index, size);
+        names = ArrayUtils.arrayInsert(names, size, name);
+        children = ArrayUtils.arrayInsert(children, size, x);
+        sort = ArrayUtils.arrayInsert(sort, index, size);
         size++;
     }
 
@@ -123,16 +119,16 @@ public class NodeListSmall implements No
         };
     }
 
-    public long remove(String name) {
+    public NodeId remove(String name) {
         int index = find(name);
         if (index < 0) {
             throw ExceptionFactory.get("Node not found: " + name);
         }
         int s = sort[index];
-        long result = children[s];
-        names = StringUtils.arrayRemove(names, s);
-        children = StringUtils.arrayRemove(children, s);
-        sort = StringUtils.arrayRemove(sort, index);
+        NodeId result = children[s];
+        names = ArrayUtils.arrayRemove(names, s);
+        children = ArrayUtils.arrayRemove(children, s);
+        sort = ArrayUtils.arrayRemove(sort, index);
         if (s != size - 1) {
             for (int i = 0; i < sort.length; i++) {
                 if (sort[i] >= s) {
@@ -148,7 +144,7 @@ public class NodeListSmall implements No
         JsopWriter json = new JsopBuilder();
         json.object();
         for (int i = 0; i < size; i++) {
-            json.key(names[i]).value(children[i]);
+            json.key(names[i]).value(children[i].toString());
         }
         json.endObject();
         return json.toString();
@@ -163,7 +159,7 @@ public class NodeListSmall implements No
     }
 
     public void visit(ChildVisitor v) {
-        for (long c : children) {
+        for (NodeId c : children) {
             v.accept(c);
         }
     }
@@ -171,8 +167,8 @@ public class NodeListSmall implements No
     public void append(JsopWriter json, NodeMap map) {
         for (int i = 0; i < size; i++) {
             json.key(names[i]);
-            long x = children[i];
-            long y = map.getId(x);
+            NodeId x = children[i];
+            NodeId y = map.getId(x);
             if (x != y) {
                 children[i] = y;
             }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMap.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMap.java?rev=1213220&r1=1213219&r2=1213220&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMap.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMap.java
Mon Dec 12 12:57:09 2011
@@ -24,22 +24,35 @@ import org.apache.jackrabbit.mk.util.Exc
 
 public class NodeMap {
 
-    private static final String MAX_MEMORY_CHILDREN = "maxMemoryChildren";
-    private static final String DESCENDANT_COUNT = "descendantCount";
+    public static final String MAX_MEMORY_CHILDREN = "maxMemoryChildren";
+    public static final String DESCENDANT_COUNT = "descendantCount";
+    public static final String DESCENDANT_INLINE_COUNT = "descendantInlineCount";
+
     private static final int DEFAULT_MAX_MEMORY_CHILDREN = Integer.MAX_VALUE;
 
+    protected boolean descendantCount;
+
+    protected int descendantInlineCount = -1;
+
     private Map<Long, NodeImpl> nodes = Collections.synchronizedMap(new HashMap<Long,
NodeImpl>());
     private AtomicLong nextId = new AtomicLong();
-    private AtomicLong rootId = new AtomicLong();
+    private NodeId rootId;
     private int maxMemoryChildren = DEFAULT_MAX_MEMORY_CHILDREN;
-    private boolean descendantCount;
 
-    public long addNode(NodeImpl node) {
-        long x = node.getId();
-        if (x == 0) {
-            x = nextId.incrementAndGet();
+    public NodeId addNode(NodeImpl node) {
+        return addNode(node, true);
+    }
+
+    private NodeId addNode(NodeImpl node, boolean allowInline) {
+        NodeId x = node.getId();
+        if (x == null) {
+            if (allowInline && node.getDescendantInlineCount() < descendantInlineCount)
{
+                x = new NodeIdInline(node);
+            } else {
+                x = NodeId.get(nextId.incrementAndGet());
+                nodes.put(x.getLong(), node);
+            }
             node.setId(x);
-            nodes.put(x, node);
         }
         return x;
     }
@@ -53,6 +66,8 @@ public class NodeMap {
             maxMemoryChildren = Integer.parseInt(value);
         } else if (key.equals(DESCENDANT_COUNT)) {
             descendantCount = Boolean.parseBoolean(value);
+        } else if (key.equals(DESCENDANT_INLINE_COUNT)) {
+            descendantInlineCount = Integer.parseInt(value);
         } else {
             throw ExceptionFactory.get("Unknown setting: " + key);
         }
@@ -70,30 +85,41 @@ public class NodeMap {
         // ignore
     }
 
-    public long getId(long id) {
+    public NodeId getId(NodeId id) {
         return id;
     }
 
-    public long commit(NodeImpl root) {
-        long x = addNode(root);
-        rootId.set(x);
-        return x;
+    public NodeId commit(NodeImpl root) {
+        return rootId = addNode(root, false);
+    }
+
+    public NodeId getRootId() {
+        return rootId;
     }
 
-    public long getRootId() {
-        return rootId.get();
+    public String formatId(NodeId id) {
+        if (id.isInline()) {
+            return id.getNode(this).asString();
+        }
+        return "n" + Long.toHexString(id.getLong());
     }
 
-    public String formatId(long id) {
-        return "n" + Long.toHexString(id);
+    public NodeId parseId(String id) {
+        if (id.startsWith("{")) {
+            return parseInline(id);
+        }
+        return NodeId.get(Long.parseLong(id.substring(1), 16));
     }
 
-    public long parseId(String id) {
-        return Long.parseLong(id.substring(1), 16);
+    protected NodeId parseInline(String id) {
+        NodeImpl inline = NodeImpl.fromString(this, id);
+        NodeId inlineId = new NodeIdInline(inline);
+        inline.setId(inlineId);
+        return inlineId;
     }
 
     public boolean isId(String value) {
-        return value.startsWith("n");
+        return value.startsWith("n") || value.startsWith("{");
     }
 
     public NodeImpl getInfo(String path) {
@@ -107,4 +133,16 @@ public class NodeMap {
         return descendantCount;
     }
 
+    public void setDescendantCount(boolean descendantCount) {
+        this.descendantCount = descendantCount;
+    }
+
+    public int getDescendantInlineCount() {
+        return descendantInlineCount;
+    }
+
+    public void setDescendantInlineCount(int descendantInlineCount) {
+        this.descendantInlineCount = descendantInlineCount;
+    }
+
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMapInDb.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMapInDb.java?rev=1213220&r1=1213219&r2=1213220&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMapInDb.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMapInDb.java
Mon Dec 12 12:57:09 2011
@@ -24,7 +24,6 @@ import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Map;
 import java.util.TreeMap;
 import java.util.Map.Entry;
 import org.apache.jackrabbit.mk.fs.FilePath;
@@ -32,7 +31,6 @@ import org.apache.jackrabbit.mk.json.Jso
 import org.apache.jackrabbit.mk.mem.NodeImpl.ChildVisitor;
 import org.apache.jackrabbit.mk.util.Cache;
 import org.apache.jackrabbit.mk.util.ExceptionFactory;
-import org.apache.jackrabbit.mk.util.SimpleLRUCache;
 
 /**
  * A node map that stores data in a database.
@@ -44,7 +42,6 @@ public class NodeMapInDb extends NodeMap
     private final String url;
     private final TreeMap<String, String> properties = new TreeMap<String, String>();
     private final Cache<Long, NodeImpl> cache = Cache.newInstance(this, 10 * 1024 *
1024);
-    private final Map<NodeImpl, NodeImpl> reuse = SimpleLRUCache.newInstance(10000);
     private final HashMap<Long, Long> pos = new HashMap<Long, Long>();
     private final PreparedStatement select, insert, merge;
     private long nextId;
@@ -63,7 +60,10 @@ public class NodeMapInDb extends NodeMap
             nextId = rs.getLong(1) + 1;
             rs = stat.executeQuery("select max(value) from roots");
             rs.next();
-            root.setId(rs.getLong(1));
+            long x = rs.getLong(1);
+            if (x != 0) {
+                root.setId(NodeId.get(x));
+            }
             select = conn.prepareStatement("select * from nodes where id = ?");
             insert = conn.prepareStatement("insert into nodes sorted select ?, ?");
             merge = conn.prepareStatement("update roots set value = ?");
@@ -72,12 +72,21 @@ public class NodeMapInDb extends NodeMap
         }
     }
 
-    public synchronized long addNode(NodeImpl node) {
-        long x = node.getId();
-        if (x == 0) {
-            x = -temp.size() - 1;
+    public synchronized NodeId addNode(NodeImpl node) {
+        return addNode(node, true);
+    }
+
+    private NodeId addNode(NodeImpl node, boolean allowInline) {
+        NodeId x = node.getId();
+        if (x == null) {
+            if (allowInline && node.getDescendantInlineCount() < descendantInlineCount)
{
+                x = new NodeIdInline(node);
+            } else {
+                long t = -temp.size() - 1;
+                temp.put(t, node);
+                x = NodeId.get(t);
+            }
             node.setId(x);
-            temp.put(x, node);
         }
         return x;
     }
@@ -99,38 +108,34 @@ public class NodeMapInDb extends NodeMap
         }
     }
 
-    public synchronized long commit(NodeImpl newRoot) {
-        addNode(newRoot);
+    public synchronized NodeId commit(NodeImpl newRoot) {
+        addNode(newRoot, false);
         try {
+            final NodeMap map = this;
             final ArrayList<Long> list = new ArrayList<Long>();
             newRoot.visit(new ChildVisitor() {
-                public void accept(long childId) {
-                    if (childId < 0) {
-                        temp.get(childId).visit(this);
-                        list.add(childId);
+                public void accept(NodeId childId) {
+                    if (childId.getLong() < 0) {
+                        temp.get(childId.getLong()).visit(this);
+                        list.add(childId.getLong());
+                    } else if (childId.isInline()) {
+                        childId.getNode(map).visit(this);
                     }
                 }
             });
-            list.add(newRoot.getId());
+            list.add(newRoot.getId().getLong());
             for (Long id : list) {
                 NodeImpl n = temp.get(id);
-                NodeImpl r = reuse.get(n);
                 long newId;
-                if (r != null) {
-                    newId = r.getId();
-                    pos.put(id, newId);
-                } else {
-                    newId = nextId++;
-                    n.setId(newId);
-                    pos.put(id, newId);
-                    insert.setLong(1, newId);
-                    insert.setString(2, n.asString());
-                    insert.execute();
-                    cache.put(n.getId(), n);
-                    reuse.put(n, n);
-                }
+                newId = nextId++;
+                n.setId(NodeId.get(newId));
+                pos.put(id, newId);
+                insert.setLong(1, newId);
+                insert.setString(2, n.asString());
+                insert.execute();
+                cache.put(n.getId().getLong(), n);
             }
-            merge.setLong(1, newRoot.getId());
+            merge.setLong(1, newRoot.getId().getLong());
             merge.execute();
             temp.clear();
             pos.clear();
@@ -141,11 +146,12 @@ public class NodeMapInDb extends NodeMap
         return root.getId();
     }
 
-    public long getId(long id) {
-        return (id > 0 || !pos.containsKey(id)) ? id : pos.get(id);
+    public NodeId getId(NodeId id) {
+        long x = id.getLong();
+        return (x > 0 || !pos.containsKey(x)) ? id : NodeId.get(pos.get(x));
     }
 
-    public long getRootId() {
+    public NodeId getRootId() {
         return root.getId();
     }
 



Mime
View raw message