jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1161602 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/index/ main/java/org/apache/jackrabbit/mk/mem/ test/java/org/apache/jackrabbit/mk/index/
Date Thu, 25 Aug 2011 15:25:32 GMT
Author: mduerig
Date: Thu Aug 25 15:25:31 2011
New Revision: 1161602

URL: http://svn.apache.org/viewvc?rev=1161602&view=rev
Log:
A simple indexing mechanism (work in progress).

Added:
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/IdentifierIndexTest.java
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Index.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/IndexPage.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Indexer.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/IndexTest.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Index.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Index.java?rev=1161602&r1=1161601&r2=1161602&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Index.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Index.java
Thu Aug 25 15:25:31 2011
@@ -51,6 +51,16 @@ public class Index {
         }
     }
 
+    public String getPath(String propertyValue, String revision) {
+        indexer.updateUntil(revision);
+        Cursor c = findFirst(propertyValue);
+        if (!c.hasNext()) {
+            return null;
+        }
+        c.next();
+        return c.getPath();
+    }
+
     public void setMinSize(int minSize) {
         if (minSize < 2) {
             throw new IllegalArgumentException("minSize: " + minSize);
@@ -275,7 +285,7 @@ public class Index {
         commit();
     }
 
-    private void commit() {
+    void commit() {
         indexer.commit();
     }
 

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/IndexPage.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/IndexPage.java?rev=1161602&r1=1161601&r2=1161602&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/IndexPage.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/IndexPage.java
Thu Aug 25 15:25:31 2011
@@ -51,6 +51,9 @@ abstract class IndexPage {
                     PathUtils.concat(index.getName(), getParentPath(), newName));
             parent = newParent;
             name = newName;
+
+            int todoRequiredForMicroKernelImpl;
+            index.commit();
         }
     }
 
@@ -70,4 +73,4 @@ abstract class IndexPage {
         return index.find(n, data);
     }
 
-}
\ No newline at end of file
+}

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=1161602&r1=1161601&r2=1161602&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
Thu Aug 25 15:25:31 2011
@@ -16,13 +16,15 @@
  */
 package org.apache.jackrabbit.mk.index;
 
-import java.util.ArrayList;
 import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.json.JsopTokenizer;
 import org.apache.jackrabbit.mk.mem.NodeImpl;
 import org.apache.jackrabbit.mk.util.PathUtils;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+
 /**
  * A index mechanism. An index is bound to a certain repository, and supports
  * one or more indexes.
@@ -33,17 +35,32 @@ public class Indexer {
     private String revision;
     private String indexRootNode;
     private StringBuilder buffer;
+    private ArrayList<Index> indexes = new ArrayList<Index>();
+    private String readRevision;
 
-    public Indexer(MicroKernel mk) {
+    public Indexer(MicroKernel mk, String indexRootNode) {
         this.mk = mk;
-
+        this.indexRootNode = indexRootNode;
+        
         revision = mk.getHeadRevision();
-        indexRootNode = "index";
         if (!mk.nodeExists("/" + indexRootNode, revision)) {
             JsopBuilder jsop = new JsopBuilder();
             jsop.append("+ ").key(indexRootNode).append("{}");
             revision = mk.commit("/", jsop.toString(), revision, null);
         }
+        int todoReadIfAlreadyExists;
+        readRevision = mk.getHeadRevision();
+    }
+
+    public Indexer(MicroKernel mk) {
+        this(mk, "index");
+    }
+
+    public Index createUniqueIndex(String property) {
+        Index index = new Index(this, property, property, true);
+        index.setMinSize(10);
+        indexes.add(index);
+        return index;
     }
 
     boolean nodeExists(String name) {
@@ -117,4 +134,154 @@ public class Indexer {
         }
     }
 
+    void updateUntil(String toRevision) {
+        String journal = mk.getJournal(readRevision, toRevision);
+        JsopTokenizer t = new JsopTokenizer(journal);
+        t.read('[');
+        HashMap<String, String> map = new HashMap<String, String>();
+        do {
+            map.clear();
+            t.read('{');
+            do {
+                String key = t.readString();
+                t.read(':');
+                t.read();
+                String value = t.getToken();
+                map.put(key, value);
+            } while (t.matches(','));
+            updateWith(map.get("changes"));
+            t.read('}');
+        } while (t.matches(','));
+        int todoPersistReadRevision;
+        readRevision = toRevision;
+    }
+
+    private void updateWith(String jsop) {
+        JsopTokenizer t = new JsopTokenizer(jsop);
+        while (true) {
+            int r = t.read();
+            if (r == JsopTokenizer.END) {
+                break;
+            }
+            String path;
+            switch (r) {
+            case '+':
+                path = t.readString();
+                t.read(':');
+                if (t.matches('{')) {
+                    NodeImpl n = NodeImpl.parse(t, 0, path);
+                    addNodeRecursive(n);
+                } else {
+                    String value = t.readRawValue().trim();
+                    String nodePath = PathUtils.getParentPath(path);
+                    NodeImpl node = new NodeImpl(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 '^':
+                path = t.readString();
+                t.read(':');
+                String value;
+                NodeImpl node = new NodeImpl(0);
+                if (t.matches(JsopTokenizer.NULL)) {
+                    value = null;
+                } else {
+                    value = t.readRawValue().trim();
+                }
+                String nodePath = PathUtils.getParentPath(path);
+                node.setPath(nodePath);
+                String propertyName = PathUtils.getName(path);
+                node.cloneAndSetProperty(propertyName, value, 0);
+                addNodeRecursive(node);
+                break;
+            case '>':
+                int todoNotImplementedYet;
+                path = t.readString();
+                t.read(':');
+                String position, target;
+                boolean rename;
+                String to;
+                if (t.matches('{')) {
+                    rename = false;
+                    position = t.readString();
+                    t.read(':');
+                    target = t.readString();
+                    t.read('}');
+                } else {
+                    rename = true;
+                    position = null;
+                    target = t.readString();
+                }
+                boolean before = false;
+                if ("last".equals(position)) {
+                    // target = PathUtils.concat(target, name);
+                    position = null;
+                } else if ("first".equals(position)) {
+                    // target = PathUtils.concat(target, name);
+                    position = null;
+                    before = true;
+                } else if ("before".equals(position)) {
+                    position = PathUtils.getName(target);
+                    target = PathUtils.getParentPath(target);
+                    // target = PathUtils.concat(target, name);
+                    before = true;
+                } else if ("after".equals(position)) {
+                    position = PathUtils.getName(target);
+                    target = PathUtils.getParentPath(target);
+                    // target = PathUtils.concat(target, name);
+                } else if (position == null) {
+                    // move
+                } else {
+                    throw new AssertionError("position: " + position);
+                }
+                if (PathUtils.isAbsolute(target)) {
+                    to = PathUtils.relativize("/", target);
+                } else {
+                    // to = PathUtils.concat(fromRoot, target);
+                }
+//                boolean inPlaceRename = false;
+//                if (rename) {
+//                    if (PathUtils.getParentPath(from).equals(PathUtils.getParentPath(to)))
{
+//                        inPlaceRename = true;
+//                        position = PathUtils.getName(from);
+//                    }
+//                }
+//                NodeImpl node = headRoot.getNode(from);
+//                if (!inPlaceRename) {
+//                    headRoot = headRoot.cloneAndRemoveChildNode(from, headRevId);
+//                }
+//                headRoot = headRoot.cloneAndAddChildNode(to, before, position, node, headRevId);
+//                if (inPlaceRename) {
+//                    headRoot = headRoot.cloneAndRemoveChildNode(from, headRevId);
+//                }
+                break;
+            default:
+                throw new AssertionError("token: " + (char) t.getTokenType());
+            }
+        }
+    }
+
+    private void addNodeRecursive(NodeImpl n) {
+        for (Index index : indexes) {
+            index.remove(n);
+            index.add(n);
+        }
+        for (String c : n.getChildNodeNames()) {
+            addNodeRecursive(n.getNode(c));
+        }
+    }
+
+    private void removeNode(String path) {
+        int todo;
+        // ignore currently, because we don't know the property value
+        // (we need to read it separately)
+    }
+
 }

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=1161602&r1=1161601&r2=1161602&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 Aug 25 15:25:31 2011
@@ -261,13 +261,21 @@ public class NodeImpl {
     }
 
     public static NodeImpl parse(JsopTokenizer t, long revId) {
+        return parse(t, revId, null);
+    }
+
+    public static NodeImpl parse(JsopTokenizer t, long revId, String path) {
         NodeImpl node = new NodeImpl(revId);
+        if (path != null) {
+            node.setPath(path);
+        }
         if (!t.matches('}')) {
             do {
                 String key = t.readString();
                 t.read(':');
                 if (t.matches('{')) {
-                    node.addChildNode(key, false, null, parse(t, revId));
+                    String childPath = path == null || key == null ? null : PathUtils.concat(path,
key);
+                    node.addChildNode(key, false, null, parse(t, revId, childPath));
                 } else {
                     String value = t.readRawValue().trim();
                     if (!key.equals(":childNodeCount")) {

Added: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/IdentifierIndexTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/IdentifierIndexTest.java?rev=1161602&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/IdentifierIndexTest.java
(added)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/index/IdentifierIndexTest.java
Thu Aug 25 15:25:31 2011
@@ -0,0 +1,59 @@
+/*
+ * 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.index;
+
+import junit.framework.TestCase;
+import org.apache.jackrabbit.mk.MicroKernelFactory;
+import org.apache.jackrabbit.mk.api.MicroKernel;
+
+/**
+ * Test the indexer mechanism.
+ */
+public class IdentifierIndexTest extends TestCase {
+
+    private static final String URL = "fs:{homeDir}/target;clean";
+    // private static final String URL = "mem:fs:target/temp;clean";
+    // private static final String URL = "mem:";
+
+    public void test() {
+        test(URL);
+    }
+
+    private void test(String url) {
+        MicroKernel mk = MicroKernelFactory.getInstance(url);
+        Indexer indexer = new Indexer(mk);
+        Index index = indexer.createUniqueIndex("id");
+
+        String head = mk.getHeadRevision();
+
+        assertEquals(null, index.getPath("1", head));
+
+        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));
+
+        head = mk.commit("/", "-\"test2\"", head, null);
+        head = mk.commit("/test", "+\"test\" : {\"id\":\"3\"}", head, null);
+
+        assertEquals("/test/test", index.getPath("\"3\"", head));
+
+        mk.dispose();
+    }
+
+}

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=1161602&r1=1161601&r2=1161602&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
Thu Aug 25 15:25:31 2011
@@ -16,21 +16,22 @@
  */
 package org.apache.jackrabbit.mk.index;
 
-import java.util.Random;
-import java.util.TreeMap;
 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.Random;
+import java.util.TreeMap;
+
 /**
  * Tests the indexing mechanism.
  */
 public class IndexTest extends TestCase {
 
-    // private static final String URL = "fs:{homeDir}/target;clean";
-    private static final String URL = "mem:fs:target/temp;clean";
+    private static final String URL = "fs:{homeDir}/target;clean";
+    // private static final String URL = "mem:fs:target/temp;clean";
     // private static final String URL = "mem:";
 
     public void test() {
@@ -87,6 +88,8 @@ public class IndexTest extends TestCase 
         Index index = new Index(indexer, "test", "id", true);
         index.setUnique(unique);
         index.setMinSize(2);
+
+        // add
         NodeImpl n = new NodeImpl(0);
         n.setPath("p1");
         n.cloneAndSetProperty("id", "1", 0);
@@ -104,6 +107,8 @@ public class IndexTest extends TestCase 
         } else {
             index.add(n);
         }
+
+        // search
         Cursor c = index.findFirst("1");
         if (!unique) {
             assertEquals("1", c.next());
@@ -119,6 +124,13 @@ public class IndexTest extends TestCase 
         }
         assertFalse(c.hasNext());
         assertEquals(null, c.next());
+
+        // remove
+        n = new NodeImpl(0);
+        n.setPath("p1");
+        n.cloneAndSetProperty("id", "1", 0);
+        index.remove(n);
+
         mk.dispose();
     }
 



Mime
View raw message