jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1163219 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/index/ test/java/org/apache/jackrabbit/mk/index/ test/java/org/apache/jackrabbit/mk/large/
Date Tue, 30 Aug 2011 14:03:24 GMT
Author: thomasm
Date: Tue Aug 30 14:03:23 2011
New Revision: 1163219

URL: http://svn.apache.org/viewvc?rev=1163219&view=rev
Log:
Index mechanism (WIP)

Added:
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/large/
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/large/LargeNodeTest.java
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Indexer.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/PrefixIndex.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/PropertyIndex.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/IndexTest.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/PrefixIndexTest.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/PropertyIndexTest.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Indexer.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Indexer.java?rev=1163219&r1=1163218&r2=1163219&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Indexer.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Indexer.java
Tue Aug 30 14:03:23 2011
@@ -20,10 +20,12 @@ import org.apache.jackrabbit.mk.api.Micr
 import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.json.JsopTokenizer;
 import org.apache.jackrabbit.mk.mem.NodeImpl;
+import org.apache.jackrabbit.mk.mem.NodeMap;
 import org.apache.jackrabbit.mk.util.PathUtils;
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
 
 /**
  * A index mechanism. An index is bound to a certain repository, and supports
@@ -91,7 +93,8 @@ public class Indexer {
         } else {
             JsopTokenizer t = new JsopTokenizer(json);
             t.read('{');
-            NodeImpl n = NodeImpl.parse(t, 0);
+            NodeMap map = new NodeMap();
+            NodeImpl n = NodeImpl.parse(map, t, 0);
             String keys = n.getProperty("keys");
             String values = n.getProperty("values");
             String children = n.getProperty("children");
@@ -173,31 +176,34 @@ public class Indexer {
             }
             String path;
             switch (r) {
-            case '+':
+            case '+': {
                 path = t.readString();
                 t.read(':');
+                NodeMap map = new NodeMap();
                 if (t.matches('{')) {
-                    NodeImpl n = NodeImpl.parse(t, 0, path);
+                    NodeImpl n = NodeImpl.parse(map, t, 0, path);
                     addNodeRecursive(n);
                 } else {
                     String value = t.readRawValue().trim();
                     String nodePath = PathUtils.getParentPath(path);
-                    NodeImpl node = new NodeImpl(0);
+                    NodeImpl node = new NodeImpl(map, 0);
                     node.setPath(nodePath);
                     String propertyName = PathUtils.getName(path);
                     node.cloneAndSetProperty(propertyName, value, 0);
                     addNodeRecursive(node);
                 }
                 break;
+            }
             case '-':
                 path = t.readString();
                 removeNode(path);
                 break;
-            case '^':
+            case '^': {
                 path = t.readString();
                 t.read(':');
                 String value;
-                NodeImpl node = new NodeImpl(0);
+                NodeMap map = new NodeMap();
+                NodeImpl node = new NodeImpl(map, 0);
                 if (t.matches(JsopTokenizer.NULL)) {
                     value = null;
                 } else {
@@ -209,6 +215,7 @@ public class Indexer {
                 node.cloneAndSetProperty(propertyName, value, 0);
                 addNodeRecursive(node);
                 break;
+            }
             case '>':
                 int todoNotImplementedYet;
                 path = t.readString();
@@ -285,8 +292,8 @@ public class Indexer {
             index.remove(n);
             index.add(n);
         }
-        for (String c : n.getChildNodeNames()) {
-            addNodeRecursive(n.getNode(c));
+        for (Iterator<String> it = n.getChildNodeNames(); it.hasNext();) {
+            addNodeRecursive(n.getNode(it.next()));
         }
     }
 

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/PrefixIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/PrefixIndex.java?rev=1163219&r1=1163218&r2=1163219&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/PrefixIndex.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/PrefixIndex.java
Tue Aug 30 14:03:23 2011
@@ -18,6 +18,7 @@ package org.apache.jackrabbit.mk.index;
 
 import java.util.Iterator;
 import java.util.Map.Entry;
+import org.apache.jackrabbit.mk.json.JsopTokenizer;
 import org.apache.jackrabbit.mk.mem.NodeImpl;
 
 /**
@@ -30,8 +31,6 @@ public class PrefixIndex implements Inde
     private final String prefix;
 
     public PrefixIndex(Indexer indexer, String prefix) {
-        int supportMultiValueProperties;
-
         this.indexer = indexer;
         this.prefix = prefix;
         this.tree = new BTree(indexer, "prefix:" + prefix, false);
@@ -39,33 +38,53 @@ public class PrefixIndex implements Inde
     }
 
     public void add(NodeImpl node) {
-        String path = null;
-        for (Entry<String, String> e : node.getProperties()) {
-            String value = e.getValue();
-            if (value.startsWith(prefix)) {
-                String v = value.substring(prefix.length());
-                if (path == null) {
-                    path = node.getPath();
-                }
-                tree.add(v, path);
-            }
-        }
+        addOrRemove(node, true);
     }
 
     public void remove(NodeImpl node) {
+        addOrRemove(node, false);
+    }
+
+    private void addOrRemove(NodeImpl node, boolean add) {
         String path = null;
         for (Entry<String, String> e : node.getProperties()) {
+            String propertyName = e.getKey();
             String value = e.getValue();
-            if (value.startsWith(prefix)) {
-                String v = value.substring(prefix.length());
+            JsopTokenizer t = new JsopTokenizer(value);
+            if (t.matches(JsopTokenizer.STRING)) {
+                String v = t.getToken();
+                if (v.startsWith(prefix)) {
+                    if (path == null) {
+                        path = node.getPath();
+                    }
+                    addOrRemove(path, propertyName, v, add);
+                }
+            } else if (t.matches('[')) {
                 if (path == null) {
                     path = node.getPath();
                 }
-                tree.remove(v, path);
+                if (!t.matches(']')) {
+                    do {
+                        String v = t.readString();
+                        if (v.startsWith(prefix)) {
+                            addOrRemove(path, propertyName, v, add);
+                        }
+                    } while (t.matches(','));
+                    t.read(']');
+                }
             }
         }
     }
 
+    private void addOrRemove(String path, String propertyName, String value, boolean add)
{
+        String v = value.substring(prefix.length());
+        if (add) {
+            tree.add(v, path + "/" + propertyName);
+        } else {
+            tree.remove(v, path + "/" + propertyName);
+        }
+    }
+
     /**
      * Get an iterator over the paths for the given property value.
      *

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/PropertyIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/PropertyIndex.java?rev=1163219&r1=1163218&r2=1163219&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/PropertyIndex.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/PropertyIndex.java
Tue Aug 30 14:03:23 2011
@@ -17,11 +17,13 @@
 package org.apache.jackrabbit.mk.index;
 
 import java.util.Iterator;
+import org.apache.jackrabbit.mk.json.JsopTokenizer;
 import org.apache.jackrabbit.mk.mem.NodeImpl;
 
 /**
  * A node handler that maps the property value to the key, and the path of the
- * node to the value.
+ * node to the value. Only string and numbers are indexes (arrays, true, false,
+ * and null are not indexes).
  */
 public class PropertyIndex implements Index {
 
@@ -39,14 +41,30 @@ public class PropertyIndex implements In
     public void add(NodeImpl node) {
         String value = node.getProperty(propertyName);
         if (value != null) {
-            tree.add(value, node.getPath());
+            addOrRemoveRaw(value, node.getPath(), true);
         }
     }
 
     public void remove(NodeImpl node) {
         String value = node.getProperty(propertyName);
         if (value != null) {
-            tree.remove(value, node.getPath());
+            addOrRemoveRaw(value, node.getPath(), false);
+        }
+    }
+
+    private void addOrRemoveRaw(String value, String path, boolean add) {
+        JsopTokenizer t = new JsopTokenizer(value);
+        if (t.matches(JsopTokenizer.STRING) || t.matches(JsopTokenizer.NUMBER)) {
+            String v = t.getToken();
+            addOrRemove(v, path, add);
+        }
+    }
+
+    private void addOrRemove(String value, String path, boolean add) {
+        if (add) {
+            tree.add(value, path);
+        } else {
+            tree.remove(value, path);
         }
     }
 

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/IndexTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/IndexTest.java?rev=1163219&r1=1163218&r2=1163219&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/IndexTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/IndexTest.java
Tue Aug 30 14:03:23 2011
@@ -20,7 +20,6 @@ import junit.framework.TestCase;
 import org.apache.jackrabbit.mk.MicroKernelFactory;
 import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.mk.json.JsopTest;
-import org.apache.jackrabbit.mk.mem.NodeImpl;
 
 import java.util.Iterator;
 import java.util.Random;
@@ -77,10 +76,6 @@ public class IndexTest extends TestCase 
         print(mk, tree);
         int len = 30;
         for (int i = 0; i < len; i++) {
-            NodeImpl n;
-            n = new NodeImpl(0);
-            n = n.cloneAndSetProperty("id", "" + i, 0);
-            n.setPath("i" + i);
             log("#insert " + i);
             tree.add("" + i, "p" + i);
             // print(mk, tree);

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/PrefixIndexTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/PrefixIndexTest.java?rev=1163219&r1=1163218&r2=1163219&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/PrefixIndexTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/PrefixIndexTest.java
Tue Aug 30 14:03:23 2011
@@ -37,23 +37,26 @@ public class PrefixIndexTest extends Tes
     private void test(String url) {
         MicroKernel mk = MicroKernelFactory.getInstance(url + ";clean");
         Indexer indexer = new Indexer(mk);
-        PrefixIndex index = indexer.createPrefixIndex("\"d:");
+        PrefixIndex index = indexer.createPrefixIndex("d:");
 
         String head = mk.getHeadRevision();
 
-        assertEquals("", getPathList(index, "\"d:1", head));
+        assertEquals("", getPathList(index, "d:1", head));
 
         head = mk.commit("/", "+\"test\" : {\"blob\":\"d:1\"}", head, null);
-        head = mk.commit("/", "+\"test2\" : {\"blob\":\"d:2\"}", head, null);
+        head = mk.commit("/", "+\"test2\" : {\"blob2\":\"d:2\"}", head, null);
 
-        assertEquals("/test", getPathList(index, "\"d:1\"", head));
-        assertEquals("/test2", getPathList(index, "\"d:2\"", head));
+        assertEquals("/test/blob", getPathList(index, "d:1", head));
+        assertEquals("/test2/blob2", getPathList(index, "d:2", head));
 
-        head = mk.commit("/", "+\"test3\" : {\"blob\":\"d:1\"}", head, null);
-        head = mk.commit("/", "+\"test4\" : {\"blob\":\"d:2\"}", head, null);
+        head = mk.commit("/", "+\"test3\" : {\"blob3\":\"d:1\"}", head, null);
+        head = mk.commit("/", "+\"test4\" : {\"blob4\":\"d:2\"}", head, null);
 
-        assertEquals("/test, /test3", getPathList(index, "\"d:1\"", head));
-        assertEquals("/test2, /test4", getPathList(index, "\"d:2\"", head));
+        assertEquals("/test/blob, /test3/blob3", getPathList(index, "d:1", head));
+        assertEquals("/test2/blob2, /test4/blob4", getPathList(index, "d:2", head));
+
+        head = mk.commit("/", "+\"test5\" : {\"blobs\":[\"a:0\",\"d:2\"]}", head, null);
+        assertEquals("/test2/blob2, /test4/blob4, /test5/blobs", getPathList(index, "d:2",
head));
 
         mk.dispose();
     }

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/PropertyIndexTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/PropertyIndexTest.java?rev=1163219&r1=1163218&r2=1163219&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/PropertyIndexTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/PropertyIndexTest.java
Tue Aug 30 14:03:23 2011
@@ -45,13 +45,13 @@ public class PropertyIndexTest extends T
         head = mk.commit("/", "+\"test\" : {\"id\":\"1\"}", head, null);
         head = mk.commit("/", "+\"test2\" : {\"id\":\"2\"}", head, null);
 
-        assertEquals("/test", index.getPath("\"1\"", head));
-        assertEquals("/test2", index.getPath("\"2\"", head));
+        assertEquals("/test", index.getPath("1", head));
+        assertEquals("/test2", index.getPath("2", head));
 
         head = mk.commit("/", "-\"test2\"", head, null);
         head = mk.commit("/test", "+\"test\" : {\"id\":\"3\"}", head, null);
 
-        assertEquals("/test/test", index.getPath("\"3\"", head));
+        assertEquals("/test/test", index.getPath("3", head));
 
         mk.dispose();
 
@@ -59,7 +59,7 @@ public class PropertyIndexTest extends T
         indexer = new Indexer(mk);
         index = indexer.createPropertyIndex("id", true);
         head = mk.getHeadRevision();
-        assertEquals("/test/test", index.getPath("\"3\"", head));
+        assertEquals("/test/test", index.getPath("3", head));
         mk.dispose();
     }
 

Added: 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=1163219&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/large/LargeNodeTest.java
(added)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/large/LargeNodeTest.java
Tue Aug 30 14:03:23 2011
@@ -0,0 +1,66 @@
+/*
+ * 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.large;
+
+import junit.framework.TestCase;
+import org.apache.jackrabbit.mk.MicroKernelFactory;
+import org.apache.jackrabbit.mk.api.MicroKernel;
+
+/**
+ * Test moving nodes.
+ */
+public class LargeNodeTest extends TestCase {
+
+    // private static final String URL = "fs:{homeDir};clean";
+    private static final String URL = "mem:;clean";
+
+    private MicroKernel mk;
+
+    private String head;
+
+    public void setUp() throws Exception {
+        mk = MicroKernelFactory.getInstance(URL);
+        head = mk.getHeadRevision();
+        commit("/", "+ \"test\": {\"a\":{}, \"b\":{}, \"c\":{}}");
+    }
+
+    public void tearDown() throws InterruptedException {
+        mk.dispose();
+    }
+
+    public void testOffsetLimit() {
+        assertEquals("{a,b,c}", getNode("/test", -1, 0, -1));
+        assertEquals("{b,c}", getNode("/test", -1, 1, -1));
+        assertEquals("{c}", getNode("/test", -1, 2, -1));
+        assertEquals("{a}", getNode("/test", -1, 0, 1));
+        assertEquals("{a,b}", getNode("/test", -1, 0, 2));
+        assertEquals("{b}", getNode("/test", -1, 1, 1));
+    }
+
+    private void commit(String root, String diff) {
+        head = mk.commit(root, diff, head, null);
+    }
+
+    private String getNode(String node, int depth, long offset, int count) {
+        String s = mk.getNodes(node, mk.getHeadRevision(), depth, offset, count);
+        s = s.replaceAll("\"", "").replaceAll(":childNodeCount:.", "");
+        s = s.replaceAll("\\{\\,", "\\{").replaceAll("\\,\\}", "\\}");
+        s = s.replaceAll("\\:\\{\\}", "");
+        return s;
+    }
+
+}
\ No newline at end of file



Mime
View raw message