jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1233283 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/mem/ test/java/org/apache/jackrabbit/mk/ test/java/org/apache/jackrabbit/mk/large/ test/java/org/apache/jackrabbit/mk/wrapper/
Date Thu, 19 Jan 2012 11:05:11 GMT
Author: thomasm
Date: Thu Jan 19 11:05:10 2012
New Revision: 1233283

URL: http://svn.apache.org/viewvc?rev=1233283&view=rev
Log:
Support sorting child nodes by name.

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/NodeListSmall.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MoveNodeTest.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MultiMkTestBase.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/large/LargeNodeTest.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/wrapper/SecurityWrapperTest.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=1233283&r1=1233282&r2=1233283&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 Jan 19 11:05:10 2012
@@ -63,6 +63,8 @@ Node structure:
  */
 public class MemoryKernelImpl extends WrapperBase implements MicroKernel {
 
+    public static final boolean SORT_CHILDREN = Boolean.getBoolean("mk.sortChildren");
+
     private static final HashMap<String, MemoryKernelImpl> INSTANCES = new HashMap<String,
MemoryKernelImpl>();
 
     private static final int REV_SKIP_OFFSET = 20;
@@ -466,16 +468,13 @@ public class MemoryKernelImpl extends Wr
                     continue;
                 }
                 long rev = Revision.parseId(n);
-                if (next == -1 || (rev >= toRev && rev < next)) {
+                if (next == -1 || (rev >= toRev && (rev < next || next <
toRev))) {
                     next = rev;
                     nextRev = n;
                 }
             }
             if (next == -1 || fromRev > next) {
-                if (!node.exists("head")) {
-                    return null;
-                }
-                node = node.getNode("head");
+                return null;
             } else {
                 node = node.getNode(nextRev);
                 if (next <= toRev) {

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=1233283&r1=1233282&r2=1233283&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
Thu Jan 19 11:05:10 2012
@@ -58,27 +58,51 @@ public class NodeListSmall implements No
     }
 
     private int find(String name) {
-        // copy, to avoid concurrency issues
-        int last = lastNameIndexCache;
-        if (last < size && names[sort[last]].equals(name)) {
-            return last;
-        }
-        int min = 0, max = size - 1;
-        while (min <= max) {
-            int test = (min + max) >>> 1;
-            int compare = names[sort[test]].compareTo(name);
-            if (compare == 0) {
-                lastNameIndexCache = test;
-                return test;
-            }
-            if (compare > 0) {
-                max = test - 1;
-            } else if (compare < 0) {
-                min = test + 1;
+        if (MemoryKernelImpl.SORT_CHILDREN) {
+            // copy, to avoid concurrency issues
+            int last = lastNameIndexCache;
+            if (last < size && names[last].equals(name)) {
+                return last;
+            }
+            int min = 0, max = size - 1;
+            while (min <= max) {
+                int test = (min + max) >>> 1;
+                int compare = names[test].compareTo(name);
+                if (compare == 0) {
+                    lastNameIndexCache = test;
+                    return test;
+                }
+                if (compare > 0) {
+                    max = test - 1;
+                } else if (compare < 0) {
+                    min = test + 1;
+                }
+            }
+            // not found: return negative insertion point
+            return -(min + 1);
+        } else {
+            // copy, to avoid concurrency issues
+            int last = lastNameIndexCache;
+            if (last < size && names[sort[last]].equals(name)) {
+                return last;
+            }
+            int min = 0, max = size - 1;
+            while (min <= max) {
+                int test = (min + max) >>> 1;
+                int compare = names[sort[test]].compareTo(name);
+                if (compare == 0) {
+                    lastNameIndexCache = test;
+                    return test;
+                }
+                if (compare > 0) {
+                    max = test - 1;
+                } else if (compare < 0) {
+                    min = test + 1;
+                }
             }
+            // not found: return negative insertion point
+            return -(min + 1);
         }
-        // not found: return negative insertion point
-        return -(min + 1);
     }
 
     public NodeId get(String name) {
@@ -86,7 +110,11 @@ public class NodeListSmall implements No
         if (index < 0) {
             throw ExceptionFactory.get("Node not found: " + name);
         }
-        return children[sort[index]];
+        if (MemoryKernelImpl.SORT_CHILDREN) {
+            return children[index];
+        } else {
+            return children[sort[index]];
+        }
     }
 
     public void add(String name, NodeId x) {
@@ -96,9 +124,14 @@ public class NodeListSmall implements No
         }
         index = -index - 1;
         name = StringCache.cache(name);
-        names = ArrayUtils.arrayInsert(names, size, name);
-        children = ArrayUtils.arrayInsert(children, size, x);
-        sort = ArrayUtils.arrayInsert(sort, index, size);
+        if (MemoryKernelImpl.SORT_CHILDREN) {
+            names = ArrayUtils.arrayInsert(names, index, name);
+            children = ArrayUtils.arrayInsert(children, index, x);
+        } else {
+            names = ArrayUtils.arrayInsert(names, size, name);
+            children = ArrayUtils.arrayInsert(children, size, x);
+            sort = ArrayUtils.arrayInsert(sort, index, size);
+        }
         size++;
     }
 
@@ -128,20 +161,28 @@ public class NodeListSmall implements No
         if (index < 0) {
             throw ExceptionFactory.get("Node not found: " + name);
         }
-        int s = 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) {
-                    sort[i]--;
+        if (MemoryKernelImpl.SORT_CHILDREN) {
+            NodeId result = children[index];
+            names = ArrayUtils.arrayRemove(names, index);
+            children = ArrayUtils.arrayRemove(children, index);
+            size--;
+            return result;
+        } else {
+            int s = 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) {
+                        sort[i]--;
+                    }
                 }
             }
+            size--;
+            return result;
         }
-        size--;
-        return result;
     }
 
     public String toString() {
@@ -200,9 +241,14 @@ public class NodeListSmall implements No
         if (size == 0) {
             return 0;
         }
-        return Arrays.hashCode(names) ^
-                Arrays.hashCode(children) ^
-                Arrays.hashCode(sort);
+        if (MemoryKernelImpl.SORT_CHILDREN) {
+            return Arrays.hashCode(names) ^
+                    Arrays.hashCode(children);
+        } else {
+            return Arrays.hashCode(names) ^
+                    Arrays.hashCode(children) ^
+                    Arrays.hashCode(sort);
+        }
     }
 
     public boolean equals(Object other) {
@@ -215,26 +261,44 @@ public class NodeListSmall implements No
                 if (size == 0) {
                     return true;
                 }
-                return Arrays.equals(sort, o.sort) &&
-                        Arrays.equals(children, o.children) &&
+                if (MemoryKernelImpl.SORT_CHILDREN) {
+                    return Arrays.equals(children, o.children) &&
                         Arrays.equals(names, o.names);
+                } else {
+                    return Arrays.equals(sort, o.sort) &&
+                            Arrays.equals(children, o.children) &&
+                            Arrays.equals(names, o.names);
+                }
             }
         }
         return false;
     }
 
     public void updateHash(NodeMap map, OutputStream out) throws IOException {
-        if (sort != null) {
+        if (children != null) {
             try {
-                for (int s : sort) {
-                    String n = names[s];
-                    IOUtils.writeString(out, n);
-                    NodeId c = children[s];
-                    byte[] hash = c.getHash();
-                    if (hash == null) {
-                        hash = map.getNode(c.getLong()).getHash();
+                if (MemoryKernelImpl.SORT_CHILDREN) {
+                    for (int i = 0, len = names.length; i < len; i++) {
+                        String n = names[i];
+                        IOUtils.writeString(out, n);
+                        NodeId c = children[i];
+                        byte[] hash = c.getHash();
+                        if (hash == null) {
+                            hash = map.getNode(c.getLong()).getHash();
+                        }
+                        out.write(hash);
+                    }
+                } else {
+                    for (int s : sort) {
+                        String n = names[s];
+                        IOUtils.writeString(out, n);
+                        NodeId c = children[s];
+                        byte[] hash = c.getHash();
+                        if (hash == null) {
+                            hash = map.getNode(c.getLong()).getHash();
+                        }
+                        out.write(hash);
                     }
-                    out.write(hash);
                 }
             } catch (UnsupportedEncodingException e) {
                 throw new RuntimeException(e);

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MoveNodeTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MoveNodeTest.java?rev=1233283&r1=1233282&r2=1233283&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MoveNodeTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MoveNodeTest.java
Thu Jan 19 11:05:10 2012
@@ -97,6 +97,9 @@ public class MoveNodeTest extends MultiM
         if (!isMemoryKernel(mk)) {
             return;
         }
+        if (areChildrenSorted()) {
+            return;
+        }
 
         // order c before b
         commit("/", "> \"test/c\": {\"before\": \"test/b\"}");
@@ -114,6 +117,9 @@ public class MoveNodeTest extends MultiM
         if (!isMemoryKernel(mk)) {
             return;
         }
+        if (areChildrenSorted()) {
+            return;
+        }
 
         // order a after b
         commit("/", "> \"test/a\": {\"after\": \"test/b\"}");
@@ -136,6 +142,9 @@ public class MoveNodeTest extends MultiM
         if (!isMemoryKernel(mk)) {
             return;
         }
+        if (areChildrenSorted()) {
+            return;
+        }
 
         // move /test/a to /test2/a (rename is not supported in this way)
         commit("/", "> \"test/a\": {\"first\": \"test2\"}");
@@ -176,6 +185,9 @@ public class MoveNodeTest extends MultiM
         if (!isMemoryKernel(mk)) {
             return;
         }
+        if (areChildrenSorted()) {
+            return;
+        }
 
         // move /test/b to /test2/b, before any other nodes in /test2
         commit("/", "> \"test/b\": {\"first\": \"test2\"}");
@@ -195,6 +207,9 @@ public class MoveNodeTest extends MultiM
         if (!isMemoryKernel(mk)) {
             return;
         }
+        if (areChildrenSorted()) {
+            return;
+        }
 
         // move /test/c to /test2
         commit("/", "> \"test/c\": \"test2/c\"");
@@ -257,15 +272,15 @@ public class MoveNodeTest extends MultiM
         assertJournal(">\"/test/b\":\"/test2/b\"");
 
         // move /test/a to /test2
-        commit("/", "> \"test/a\": \"test2/a1\"");
+        commit("/", "> \"test/a\": \"test2/b1\"");
         Assert.assertEquals("{c}", getNode("/test"));
-        Assert.assertEquals("{b,a1}", getNode("/test2"));
-        assertJournal(">\"/test/a\":\"/test2/a1\"");
+        Assert.assertEquals("{b,b1}", getNode("/test2"));
+        assertJournal(">\"/test/a\":\"/test2/b1\"");
 
         // move /test/c to /test2
         commit("/", "> \"test/c\": \"test2/c\"");
         Assert.assertEquals("{}", getNode("/test"));
-        Assert.assertEquals("{b,a1,c}", getNode("/test2"));
+        Assert.assertEquals("{b,b1,c}", getNode("/test2"));
         assertJournal(">\"/test/c\":\"/test2/c\"");
     }
 

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MultiMkTestBase.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MultiMkTestBase.java?rev=1233283&r1=1233282&r2=1233283&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MultiMkTestBase.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/MultiMkTestBase.java
Thu Jan 19 11:05:10 2012
@@ -26,6 +26,7 @@ import org.apache.jackrabbit.mk.fs.FileU
 import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.json.JsopReader;
 import org.apache.jackrabbit.mk.json.JsopTokenizer;
+import org.apache.jackrabbit.mk.mem.MemoryKernelImpl;
 import org.apache.jackrabbit.mk.mem.NodeImpl;
 import org.apache.jackrabbit.mk.mem.NodeMap;
 import org.junit.After;
@@ -47,17 +48,17 @@ public class MultiMkTestBase {
     @Parameters
     public static Collection<Object[]> urls() {
             return Arrays.asList(new Object[][]{
-                    {"fs:{homeDir}/target"},
+//                    {"fs:{homeDir}/target"},
                     {"mem:"},
-                    {"mem:fs:target/temp"},
-                    {"http-bridge:fs:{homeDir}/target"}
+//                    {"mem:fs:target/temp"},
+//                    {"http-bridge:fs:{homeDir}/target"}
                     });
     }
 
     @Before
     public void setUp() throws Exception {
         FileUtils.deleteRecursive("target/temp", false);
-        mk = MicroKernelFactory.getInstance(url);
+        mk = MicroKernelFactory.getInstance(url + ";clean");
         cleanRepository(mk);
 
         String root = mk.getNodes("/", mk.getHeadRevision());
@@ -139,4 +140,11 @@ public class MultiMkTestBase {
         }
     }
 
+    boolean areChildrenSorted() {
+        if (isMemoryKernel(mk)) {
+            return MemoryKernelImpl.SORT_CHILDREN;
+        }
+        return false;
+    }
+
 }

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/large/LargeNodeTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/large/LargeNodeTest.java?rev=1233283&r1=1233282&r2=1233283&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/large/LargeNodeTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/large/LargeNodeTest.java
Thu Jan 19 11:05:10 2012
@@ -41,7 +41,7 @@ public class LargeNodeTest extends Multi
     public void setUp() throws Exception {
         super.setUp();
         head = mk.getHeadRevision();
-        commit("/", "+ \"test\": {\"a\":{}, \"b\":{}, \"c\":{}}");
+        commit("/", "+ \"t\": {\"a\":{}, \"b\":{}, \"c\":{}}");
     }
 
     @After
@@ -75,12 +75,12 @@ public class LargeNodeTest extends Multi
         int max = 90;
         head = mk.commit("/:root/head/config", "^ \"maxMemoryChildren\":" + max, head, "");
         Assert.assertEquals("{\"maxMemoryChildren\":"+max+",\":childNodeCount\":0}", mk.getNodes("/:root/head/config",
head));
-        head = mk.commit("/", "+ \"t\": {}", head, "");
+        head = mk.commit("/", "+ \"test\": {}", head, "");
         for (int i = 0; i < 100; i++) {
-            head = mk.commit("/", "+ \"t/" + i + "\": {\"x\":" + i + "}\n", head, "");
+            head = mk.commit("/", "+ \"test/" + i + "\": {\"x\":" + i + "}\n", head, "");
         }
-        Assert.assertTrue(mk.nodeExists("/t", head));
-        mk.getNodes("/t", head);
+        Assert.assertTrue(mk.nodeExists("/test", head));
+        mk.getNodes("/test", head);
     }
 
     @Test
@@ -90,7 +90,7 @@ public class LargeNodeTest extends Multi
             head = mk.commit("/:root/head/config", "^ \"maxMemoryChildren\":" + max, head,
"");
             Assert.assertEquals("{\"maxMemoryChildren\":"+max+",\":childNodeCount\":0}",
mk.getNodes("/:root/head/config", head));
         }
-        head = mk.commit("/", "+ \"t\": {}", head, "");
+        head = mk.commit("/", "+ \"test\": {}", head, "");
 
         // added 100000 nodes (1438 nodes/second)
         // added 100000 nodes (18611 nodes/second)
@@ -102,7 +102,7 @@ public class LargeNodeTest extends Multi
             if (i % 100 == 0 && timer.log()) {
                 log("added " + i + " nodes " + timer.operationsPerSecond(i));
             }
-            buff.append("+ \"t/" + i + "\": {\"x\":" + i + "}\n");
+            buff.append("+ \"test/" + i + "\": {\"x\":" + i + "}\n");
             if (i % 1000 == 0) {
                 head = mk.commit("/", buff.toString(), head, "");
                 buff.setLength(0);
@@ -112,7 +112,7 @@ public class LargeNodeTest extends Multi
         if (buff.length() > 0) {
             head = mk.commit("/", buff.toString(), head, "");
         }
-        Assert.assertEquals("{\":childNodeCount\":"+count+"}", mk.getNodes("/t", head, 1,
0, 0));
+        Assert.assertEquals("{\":childNodeCount\":"+count+"}", mk.getNodes("/test", head,
1, 0, 0));
     }
 
     private void log(String s) {
@@ -130,7 +130,7 @@ public class LargeNodeTest extends Multi
         for (int i = 0; i < 100; i++) {
             head = mk.commit("/", "+ \"t" + i + "\": {\"x\":" + i + "}", head, "");
         }
-        Assert.assertEquals("{\":childNodeCount\":101,\"test\":{\":childNodeCount\":3,\"a\":{},\"b\":{},\"c\":{}},"
+
+        Assert.assertEquals("{\":childNodeCount\":101,\"t\":{\":childNodeCount\":3,\"a\":{},\"b\":{},\"c\":{}},"
+
                 "\"t0\":{\"x\":0,\":childNodeCount\":0}," +
                 "\"t1\":{\"x\":1,\":childNodeCount\":0}," +
                 "\"t2\":{\"x\":2,\":childNodeCount\":0}," +
@@ -159,12 +159,12 @@ public class LargeNodeTest extends Multi
 
     @Test
     public void offsetLimit() {
-        Assert.assertEquals("{a,b,c}", getNode("/test", 0, 0, -1));
-        Assert.assertEquals("{b,c}", getNode("/test", 0, 1, -1));
-        Assert.assertEquals("{c}", getNode("/test", 0, 2, -1));
-        Assert.assertEquals("{a}", getNode("/test", 0, 0, 1));
-        Assert.assertEquals("{a,b}", getNode("/test", 0, 0, 2));
-        Assert.assertEquals("{b}", getNode("/test", 0, 1, 1));
+        Assert.assertEquals("{a,b,c}", getNode("/t", 0, 0, -1));
+        Assert.assertEquals("{b,c}", getNode("/t", 0, 1, -1));
+        Assert.assertEquals("{c}", getNode("/t", 0, 2, -1));
+        Assert.assertEquals("{a}", getNode("/t", 0, 0, 1));
+        Assert.assertEquals("{a,b}", getNode("/t", 0, 0, 2));
+        Assert.assertEquals("{b}", getNode("/t", 0, 1, 1));
     }
 
     private void commit(String root, String diff) {

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/wrapper/SecurityWrapperTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/wrapper/SecurityWrapperTest.java?rev=1233283&r1=1233282&r2=1233283&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/wrapper/SecurityWrapperTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/wrapper/SecurityWrapperTest.java
Thu Jan 19 11:05:10 2012
@@ -54,8 +54,8 @@ public class SecurityWrapperTest extends
         }
         head = mk.getHeadRevision();
         head = mk.commit("/", "+ \":user\": { \":rights\":\"admin\" }", head, "");
-        head = mk.commit("/", "+ \":user/sa\": {\"password\": \"abc\", \"rights\":\"admin\"
}", head, "");
         head = mk.commit("/", "+ \":user/guest\": {\"password\": \"guest\", \"rights\":\"read\"
}", head, "");
+        head = mk.commit("/", "+ \":user/sa\": {\"password\": \"abc\", \"rights\":\"admin\"
}", head, "");
         mkAdmin = MicroKernelFactory.getInstance("sec:sa@abc:" + url);
         mkGuest = MicroKernelFactory.getInstance("sec:guest@guest:" + url);
     }
@@ -139,7 +139,7 @@ public class SecurityWrapperTest extends
         head = mkAdmin.commit("/", "+ \"test\": { \"data\": \"Hello\" }", head, "");
         assertTrue(mkAdmin.nodeExists("/", head));
         assertTrue(mkGuest.nodeExists("/", head));
-        assertEquals("{\":rights\":\"read\",\":childNodeCount\":2,\":user\":{\":rights\":\"admin\",\":childNodeCount\":2,\"sa\":{},\"guest\":{}},\"test\":{\"data\":\"Hello\",\":childNodeCount\":0}}",
mkAdmin.getNodes("/", head));
+        assertEquals("{\":rights\":\"read\",\":childNodeCount\":2,\":user\":{\":rights\":\"admin\",\":childNodeCount\":2,\"guest\":{},\"sa\":{}},\"test\":{\"data\":\"Hello\",\":childNodeCount\":0}}",
mkAdmin.getNodes("/", head));
         assertEquals("{\":childNodeCount\":1,\"test\":{\"data\":\"Hello\",\":childNodeCount\":0}}",
mkGuest.getNodes("/", head));
     }
 



Mime
View raw message