jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1158695 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/util/PathUtils.java test/java/org/apache/jackrabbit/mk/PathTest.java test/java/org/apache/jackrabbit/mk/index/IndexTest.java
Date Wed, 17 Aug 2011 13:34:11 GMT
Author: thomasm
Date: Wed Aug 17 13:34:10 2011
New Revision: 1158695

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

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.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/util/PathUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java?rev=1158695&r1=1158694&r2=1158695&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
Wed Aug 17 13:34:10 2011
@@ -181,26 +181,69 @@ public class PathUtils {
         return array;
     }
 
-    public static String concat(String parentPath, String relativePath) {
+    /**
+     * Concatenate path elements.
+     *
+     * @param parentPath the parent path
+     * @param relativePaths the relative path elements to add
+     * @return the concatenated path
+     */
+    public static String concat(String parentPath, String... relativePaths) {
         assertValid(parentPath);
-        assertValid(relativePath);
-
-        if (isAbsolutePath(relativePath)) {
-            throw new IllegalArgumentException("Cannot append absolute path " + relativePath);
-        } else if (relativePath.length() == 0) {
-            return parentPath;
-        }
         int parentLen = parentPath.length();
-        StringBuilder buff = new StringBuilder(
-                parentLen + 1 + relativePath.length());
+        int len = parentLen;
+        for (String s : relativePaths) {
+            if (isAbsolutePath(s)) {
+                throw new IllegalArgumentException("Cannot append absolute path " + s);
+            }
+            int l = s.length();
+            if (l > 0) {
+                len += 1 + s.length();
+            }
+        }
+        StringBuilder buff = new StringBuilder(len);
         buff.append(parentPath);
-        if (parentLen > 0 && !denotesRootPath(parentPath)) {
-            buff.append('/');
+        boolean needSlash = parentLen > 0 && !denotesRootPath(parentPath);
+        for (int i=0, size = relativePaths.length; i < size; i++) {
+            String s = relativePaths[i];
+            if (s.length() > 0) {
+                if (needSlash) {
+                    buff.append('/');
+                }
+                buff.append(s);
+                needSlash = true;
+            }
         }
-        buff.append(relativePath);
         return buff.toString();
     }
 
+//    /**
+//     * Concatenate path elements.
+//     *
+//     * @param parentPath the parent path
+//     * @param relativePath the relative path to add
+//     * @return the concatenated path
+//     */
+//    public static String concat(String parentPath, String relativePath) {
+//        assertValid(parentPath);
+//        assertValid(relativePath);
+//
+////        if (isAbsolutePath(relativePath)) {
+////            throw new IllegalArgumentException("Cannot append absolute path " + relativePath);
+////        } else if (relativePath.length() == 0) {
+////            return parentPath;
+////        }
+////        int parentLen = parentPath.length();
+////        StringBuilder buff = new StringBuilder(
+////                parentLen + 1 + relativePath.length());
+////        buff.append(parentPath);
+////        if (parentLen > 0 && !denotesRootPath(parentPath)) {
+////            buff.append('/');
+////        }
+//        buff.append(relativePath);
+//        return buff.toString();
+//    }
+
     /**
      * Relativize a path wrt. a parent path such that
      * <code>relativize(parentPath, concat(parentPath, path)) == paths</code>

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java?rev=1158695&r1=1158694&r2=1158695&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java (original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java Wed
Aug 17 13:34:10 2011
@@ -118,7 +118,7 @@ public class PathTest extends TestCase {
         assertEquals(2, PathUtils.split("/" + parent + "/" + child).length);
 
         // concat
-        assertEquals(parent + "/" + child, PathUtils.concat(parent , child ));
+        assertEquals(parent + "/" + child, PathUtils.concat(parent, child));
         try {
             assertEquals(parent + "/" + child, PathUtils.concat(parent + "/", "/" + child));
             fail();
@@ -131,6 +131,7 @@ public class PathTest extends TestCase {
         } catch (IllegalArgumentException e) {
             // expected
         }
+        assertEquals(parent + "/" + child + "/" + child, PathUtils.concat(parent, child,
child));
 
         // denotesRoot
         assertTrue(PathUtils.denotesRoot("/"));

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=1158695&r1=1158694&r2=1158695&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
Wed Aug 17 13:34:10 2011
@@ -48,7 +48,7 @@ public class IndexTest {
         MicroKernel mk = MicroKernelFactory.getInstance(url);
         String head = mk.getHeadRevision();
         Index index = new Index(mk, "test", "id");
-        for (int i=0; i<1000; i++) {
+        for (int i = 0; i < 1000; i++) {
             NodeImpl n;
             n = new NodeImpl(0);
             n = n.cloneAndSetProperty("id", "" + i, 0);
@@ -127,17 +127,13 @@ public class IndexTest {
             return data2;
         }
 
-        IndexPage getPage(String parent, String path) {
-            String p;
-            if (parent == null) {
-                parent = "";
-            }
-            p = PathUtils.concat(parent, path);
-            String index = PathUtils.concat(indexRootNode, name);
+        IndexPage getPage(IndexNode parent, String name) {
+            String p = parent == null ? name : PathUtils.concat(parent.getPath(), name);
+            String index = PathUtils.concat(indexRootNode, this.name);
             String json = mk.getNodes("/" + PathUtils.concat(index, p), revision);
             IndexPage page;
             if (json == null) {
-                page = new IndexLeaf(this, parent);
+                page = new IndexLeaf(this, parent, name);
             } else {
                 JsopTokenizer t = new JsopTokenizer(json);
                 t.read('{');
@@ -146,18 +142,17 @@ public class IndexTest {
                 String paths = n.getProperty("paths");
                 String children = n.getProperty("children");
                 if (children != null) {
-                    IndexNode node = new IndexNode(this, parent);
+                    IndexNode node = new IndexNode(this, parent, name);
                     node.data = readArray(data);
                     node.children = readArray(children);
                     page = node;
                 } else {
-                    IndexLeaf leaf = new IndexLeaf(this, parent);
+                    IndexLeaf leaf = new IndexLeaf(this, parent, name);
                     leaf.data = readArray(data);
                     leaf.paths = readArray(paths);
                     page = leaf;
                 }
             }
-            page.path = p;
             return page;
         }
 
@@ -258,10 +253,10 @@ public class IndexTest {
                     int split = IndexPage.MIN_SIZE;
                     if (parent == null) {
                         // new root
-                        IndexNode root = new IndexNode(this, null);
+                        IndexNode root = new IndexNode(this, null, null);
                         root.children = new String[] { "0", "1" };
                         root.data = new String[] { n.data[split] };
-                        root.path = "";
+                        root.name = "";
                         n.split(root, "0", split, "1");
                         n = root;
                     } else {
@@ -331,24 +326,43 @@ public class IndexTest {
         final static int MIN_SIZE = 2;
         final static int MAX_SIZE = 2 * MIN_SIZE + 1;
         protected final Index index;
-
-        // TODO use the name, and keep the parent list in the cursor
-        // paths are not stable because of split / join operations,
-        // and use more space
-        String path;
+        protected IndexNode parent;
+        String name;
 
         String[] data;
 
-        IndexPage(Index index, String path) {
+        IndexPage(Index index, IndexNode parent, String name) {
             this.index = index;
-            this.path = path;
+            this.parent = parent;
+            this.name = name;
         }
 
-
         abstract void writeCreate();
-        abstract void split(IndexNode root, String newPath, int pos, String siblingPath);
+        abstract void split(IndexNode newParent, String newPath, int pos, String siblingPath);
         abstract IndexLeaf firstLeaf();
 
+        void setParent(IndexNode newParent, String newName) {
+            if (newParent != null) {
+                index.bufferMove(
+                        PathUtils.concat(index.getName(), getPath()),
+                        "temp");
+                newParent.writeCreate();
+                index.bufferMove(
+                        "temp",
+                        PathUtils.concat(index.getName(), getParentPath(), newName));
+                parent = newParent;
+                name = newName;
+            }
+        }
+
+        String getParentPath() {
+            return parent == null ? "" : parent.getPath();
+        }
+
+        String getPath() {
+            return PathUtils.concat(getParentPath(), name);
+        }
+
         int size() {
             return data.length;
         }
@@ -363,37 +377,21 @@ public class IndexTest {
 
         String[] paths = new String[0];
 
-        IndexLeaf(Index index, String path) {
-            super(index, path);
+        IndexLeaf(Index index, IndexNode parent, String path) {
+            super(index, parent, path);
         }
 
         IndexLeaf nextLeaf() {
-            String p = PathUtils.getParentPath(path);
-            String name = PathUtils.getName(p);
-            p = PathUtils.getParentPath(p);
-            IndexNode parent = (IndexNode) index.getPage(p, name);
-            if (parent == null) {
-                return null;
-            }
-            return parent.next(this);
+            return parent == null ? null : parent.next(this);
         }
 
         IndexLeaf firstLeaf() {
             return this;
         }
 
-        void split(IndexNode root, String newPath, int pos, String siblingPath) {
-            if (newPath != null) {
-                index.bufferMove(
-                        PathUtils.concat(index.getName(), path),
-                        "temp");
-                root.writeCreate();
-                index.bufferMove(
-                        "temp",
-                        PathUtils.concat(index.getName(), newPath));
-                path = newPath;
-            }
-            IndexLeaf n2 = new IndexLeaf(index, siblingPath);
+        void split(IndexNode newParent, String newName, int pos, String siblingName) {
+            setParent(newParent, newName);
+            IndexLeaf n2 = new IndexLeaf(index, parent, siblingName);
             n2.data = Arrays.copyOfRange(data, pos, data.length, String[].class);
             data = Arrays.copyOfRange(data, 0, pos, String[].class);
             n2.paths = Arrays.copyOfRange(paths, pos, paths.length, String[].class);
@@ -408,13 +406,13 @@ public class IndexTest {
         }
 
         void writeData() {
-            index.bufferSetArray(path, "data", data);
-            index.bufferSetArray(path, "paths", paths);
+            index.bufferSetArray(getPath(), "data", data);
+            index.bufferSetArray(getPath(), "paths", paths);
         }
 
         void writeCreate() {
             JsopBuilder jsop = new JsopBuilder();
-            jsop.append("+ ").key(PathUtils.concat(index.getName(), path)).object();
+            jsop.append("+ ").key(PathUtils.concat(index.getName(), getPath())).object();
             jsop.key("data").array();
             for (String d : data) {
                 jsop.value(d);
@@ -436,8 +434,8 @@ public class IndexTest {
 
         String[] children = new String[0];
 
-        IndexNode(Index index, String path) {
-            super(index, path);
+        IndexNode(Index index, IndexNode parent, String path) {
+            super(index, parent, path);
         }
 
         void insert(int pos, NodeImpl n) {
@@ -452,29 +450,16 @@ public class IndexTest {
                     max = x;
                 }
             }
-            String p = Integer.toString(max + 1);
-            if (path.length() == 0) {
-                return p;
-            }
-            return path + "/" + p;
+            return Integer.toString(max + 1);
         }
 
         IndexLeaf firstLeaf() {
-            return index.getPage(path, children[0]).firstLeaf();
+            return index.getPage(this, children[0]).firstLeaf();
         }
 
-        void split(IndexNode root, String newPath, int pos, String siblingPath) {
-            if (newPath != null) {
-                index.bufferMove(
-                        PathUtils.concat(index.getName(), path),
-                        "temp");
-                root.writeCreate();
-                index.bufferMove(
-                        "temp",
-                        PathUtils.concat(index.getName(), newPath));
-                path = newPath;
-            }
-            IndexNode n2 = new IndexNode(index, siblingPath);
+        void split(IndexNode newParent, String newName, int pos, String siblingName) {
+            setParent(newParent, newName);
+            IndexNode n2 = new IndexNode(index, parent, siblingName);
             n2.data = Arrays.copyOfRange(data, pos + 1, data.length, String[].class);
             data = Arrays.copyOfRange(data, 0, pos, String[].class);
             n2.children = Arrays.copyOfRange(children, pos + 1, children.length, String[].class);
@@ -482,8 +467,8 @@ public class IndexTest {
             n2.writeCreate();
             for (String c : n2.children) {
                 index.bufferMove(
-                        PathUtils.concat(index.getName(), PathUtils.concat(path, c)),
-                        PathUtils.concat(index.getName(), PathUtils.concat(siblingPath, c))
+                        PathUtils.concat(index.getName(), getPath(), c),
+                        PathUtils.concat(index.getName(), getParentPath(), siblingName, c)
                 );
             }
             writeData();
@@ -491,37 +476,30 @@ public class IndexTest {
 
         IndexLeaf next(IndexPage child) {
             int i=0;
-            String childName = PathUtils.getName(child.path);
+            String childName = child.name;
             for (; i<children.length; i++) {
                 if (children[i].equals(childName)) {
                     break;
                 }
             }
             if (i == children.length - 1) {
-                if (path.length() == 0) {
-                    return null;
-                }
-                String p = PathUtils.getParentPath(path);
-                String name = PathUtils.getName(p);
-                p = PathUtils.getParentPath(p);
-                IndexNode parent = (IndexNode) index.getPage(p, name);
-                return parent.next(this);
+                return parent == null ? null : parent.next(this);
             }
-            return index.getPage(path, children[i + 1]).firstLeaf();
+            return index.getPage(this, children[i + 1]).firstLeaf();
         }
 
         public IndexPage getChild(int pos) {
-            return index.getPage(path, children[pos]);
+            return index.getPage(this, children[pos]);
         }
 
         void writeData() {
-            index.bufferSetArray(path, "data", data);
-            index.bufferSetArray(path, "children", children);
+            index.bufferSetArray(getPath(), "data", data);
+            index.bufferSetArray(getPath(), "children", children);
         }
 
         void writeCreate() {
             JsopBuilder jsop = new JsopBuilder();
-            jsop.append("+ ").key(PathUtils.concat(index.getName(), path)).object();
+            jsop.append("+ ").key(PathUtils.concat(index.getName(), getPath())).object();
             jsop.key("data").array();
             for (String d : data) {
                 jsop.value(d);



Mime
View raw message