jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1443368 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/api/ oak-core/src/main/java/org/apache/jackrabbit/oak/core/ oak-core/src/test/java/org/apache/jackrabbit/oak/core/ oak-jcr/src/main/java/org/apache/jackra...
Date Thu, 07 Feb 2013 09:37:15 GMT
Author: mduerig
Date: Thu Feb  7 09:37:14 2013
New Revision: 1443368

URL: http://svn.apache.org/viewvc?rev=1443368&view=rev
Log:
OAK-605: NullLocation.getParent() should return to parent instead of returning this instance

Added:
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/TreeLocationTest.java
  (with props)
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/TreeLocation.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/AbstractPropertyLocation.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/NullLocation.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/ReadOnlyTree.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/RootImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/TreeImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/TreeLocation.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/TreeLocation.java?rev=1443368&r1=1443367&r2=1443368&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/TreeLocation.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/TreeLocation.java
Thu Feb  7 09:37:14 2013
@@ -25,8 +25,9 @@ import org.apache.jackrabbit.oak.api.Tre
 
 /**
  * A {@code TreeLocation} denotes a location inside a tree.
- * It can either refer to a inner node (that is a {@link org.apache.jackrabbit.oak.api.Tree})
- * or to a leaf (that is a {@link org.apache.jackrabbit.oak.api.PropertyState}).
+ * It can either refer to a inner node (that is a {@link org.apache.jackrabbit.oak.api.Tree}),
+ * to a leaf (that is a {@link org.apache.jackrabbit.oak.api.PropertyState}) or to an
+ * invalid location which refers to neither of the former.
  * {@code TreeLocation} instances provide methods for navigating trees. {@code TreeLocation}
  * instances are immutable and navigating a tree always results in new {@code TreeLocation}
  * instances. Navigation never fails. Errors are deferred until the underlying item itself
is
@@ -37,7 +38,7 @@ import org.apache.jackrabbit.oak.api.Tre
 public interface TreeLocation {
 
     /**
-     * Navigate to the parent
+     * Navigate to the parent or an invalid location for the root of the hierarchy.
      * @return  a {@code TreeLocation} for the parent of this location.
      */
     @Nonnull
@@ -76,18 +77,18 @@ public interface TreeLocation {
     PropertyState getProperty();
 
     /**
-     * {@link org.apache.jackrabbit.oak.api.Tree.Status} of the underlying item or {@code
null} if no
-     * such item exists.
-     * @return
+     * Get the {@link org.apache.jackrabbit.oak.api.Tree.Status} of the underlying item for
this
+     * {@code TreeLocation}.
+     * @return  underlying status or {@code null} if not available.
      */
     @CheckForNull
     Status getStatus();
 
     /**
-     * The path of the underlying item or {@code null} if no such item exists.
+     * The path of this location
      * @return  path
      */
-    @CheckForNull
+    @Nonnull
     String getPath();
 
     /**

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/AbstractPropertyLocation.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/AbstractPropertyLocation.java?rev=1443368&r1=1443367&r2=1443368&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/AbstractPropertyLocation.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/AbstractPropertyLocation.java
Thu Feb  7 09:37:14 2013
@@ -21,6 +21,7 @@ import org.apache.jackrabbit.oak.api.Tre
 import org.apache.jackrabbit.oak.api.TreeLocation;
 import org.apache.jackrabbit.oak.commons.PathUtils;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
@@ -43,7 +44,14 @@ abstract class AbstractPropertyLocation<
 
     @Override
     public TreeLocation getChild(String relPath) {
-        return NullLocation.NULL;
+        checkArgument(!PathUtils.isAbsolute(relPath), "Not a relative path: " + relPath);
+
+        TreeLocation child = this;
+        for (String name : PathUtils.elements(relPath)) {
+            child = new NullLocation(child, name);
+        }
+
+        return child;
     }
 
     @Override

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/NullLocation.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/NullLocation.java?rev=1443368&r1=1443367&r2=1443368&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/NullLocation.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/NullLocation.java
Thu Feb  7 09:37:14 2013
@@ -22,31 +22,45 @@ import org.apache.jackrabbit.oak.api.Pro
 import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.api.Tree.Status;
 import org.apache.jackrabbit.oak.api.TreeLocation;
+import org.apache.jackrabbit.oak.commons.PathUtils;
+
+import static com.google.common.base.Preconditions.checkArgument;
 
 /**
  * This {@code TreeLocation} refers to an invalid location in a tree. That is
  * to a location where no item resides.
  */
 final class NullLocation implements TreeLocation {
-    static final TreeLocation NULL = new NullLocation();
+    public static final NullLocation NULL = new NullLocation();
+
+    private final TreeLocation parent;
+    private final String name;
+
+    public NullLocation(TreeLocation parent, String name) {
+        this.parent = parent;
+        this.name = name;
+    }
 
     private NullLocation() {
+        this.parent = this;
+        this.name = "";
     }
 
-    /**
-     * @return  {@code NULL}
-     */
     @Override
     public TreeLocation getParent() {
-        return NULL;
+        return parent;
     }
 
-    /**
-     * @return  {@code NULL}
-     */
     @Override
     public TreeLocation getChild(String relPath) {
-        return NULL;
+        checkArgument(!PathUtils.isAbsolute(relPath), "Not a relative path: " + relPath);
+
+        TreeLocation child = this;
+        for (String e : PathUtils.elements(relPath)) {
+            child = new NullLocation(child, e);
+        }
+
+        return child;
     }
 
     /**
@@ -57,12 +71,9 @@ final class NullLocation implements Tree
         return false;
     }
 
-    /**
-     * @return  {@code null}
-     */
     @Override
     public String getPath() {
-        return null;
+        return parent == this ? "" : PathUtils.concat(parent.getPath(), name);
     }
 
     /**

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/ReadOnlyTree.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/ReadOnlyTree.java?rev=1443368&r1=1443367&r2=1443368&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/ReadOnlyTree.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/ReadOnlyTree.java
Thu Feb  7 09:37:14 2013
@@ -255,29 +255,33 @@ public class ReadOnlyTree implements Tre
 
         @Override
         public TreeLocation getChild(String relPath) {
-            checkArgument(!relPath.startsWith("/"));
+            checkArgument(!PathUtils.isAbsolute(relPath), "Not a relative path: " + relPath);
             if (relPath.isEmpty()) {
                 return this;
             }
 
-            ReadOnlyTree child = tree;
-            String parentPath = PathUtils.getParentPath(relPath);
-            for (String name : PathUtils.elements(parentPath)) {
-                child = child.getChild(name);
-                if (child == null) {
-                    return NullLocation.NULL;
+            int slash = PathUtils.getNextSlash(relPath, 0);
+            if (slash == -1) {
+                ReadOnlyTree child = tree.getChild(relPath);
+                if (child != null) {
+                    return new NodeLocation(child);
                 }
-            }
 
-            String name = PathUtils.getName(relPath);
-            PropertyState property = child.getProperty(name);
-            if (property != null) {
-                return new PropertyLocation(new NodeLocation(child), name);
-            } else {
-                child = child.getChild(name);
-                return child == null
-                        ? NullLocation.NULL
-                        : new NodeLocation(child);
+                PropertyState prop = tree.getProperty(relPath);
+                if (prop != null) {
+                    return new PropertyLocation(this, relPath);
+                }
+                return new NullLocation(this, relPath);
+            }
+            else {
+                String name = relPath.substring(0, slash);
+                String tail = relPath.substring(slash + 1, relPath.length());
+
+                ReadOnlyTree child = tree.getChild(name);
+                if (child != null) {
+                    return new NodeLocation(child).getChild(tail);
+                }
+                return new NullLocation(this, name).getChild(tail);
             }
         }
 

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/RootImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/RootImpl.java?rev=1443368&r1=1443367&r2=1443368&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/RootImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/RootImpl.java
Thu Feb  7 09:37:14 2013
@@ -32,6 +32,7 @@ import org.apache.jackrabbit.oak.api.Com
 import org.apache.jackrabbit.oak.api.QueryEngine;
 import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.api.TreeLocation;
+import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.plugins.index.diffindex.UUIDDiffIndexProviderWrapper;
 import org.apache.jackrabbit.oak.query.QueryEngineImpl;
 import org.apache.jackrabbit.oak.spi.observation.ChangeExtractor;
@@ -199,7 +200,7 @@ public class RootImpl implements Root {
     @Override
     public TreeLocation getLocation(String path) {
         checkLive();
-        checkArgument(path.startsWith("/"));
+        checkArgument(PathUtils.isAbsolute(path), "Not an absolute path: " + path);
         return rootTree.getLocation().getChild(path.substring(1));
     }
 

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/TreeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/TreeImpl.java?rev=1443368&r1=1443367&r2=1443368&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/TreeImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/TreeImpl.java
Thu Feb  7 09:37:14 2013
@@ -599,29 +599,33 @@ public class TreeImpl implements Tree {
 
         @Override
         public TreeLocation getChild(String relPath) {
-            checkArgument(!relPath.startsWith("/"));
+            checkArgument(!PathUtils.isAbsolute(relPath), "Not a relative path: " + relPath);
             if (relPath.isEmpty()) {
                 return this;
             }
 
-            TreeImpl child = tree;
-            String parentPath = PathUtils.getParentPath(relPath);
-            for (String name : PathUtils.elements(parentPath)) {
-                child = child.internalGetChild(name);
-                if (child == null) {
-                    return NullLocation.NULL;
+            int slash = PathUtils.getNextSlash(relPath, 0);
+            if (slash == -1) {
+                TreeImpl child = tree.internalGetChild(relPath);
+                if (child != null) {
+                    return new NodeLocation(child);
                 }
-            }
 
-            String name = PathUtils.getName(relPath);
-            PropertyState property = child.internalGetProperty(name);
-            if (property != null) {
-                return new PropertyLocation(new NodeLocation(child), name);
-            } else {
-                child = child.internalGetChild(name);
-                return child == null
-                        ? NullLocation.NULL
-                        : new NodeLocation(child);
+                PropertyState prop = tree.internalGetProperty(relPath);
+                if (prop != null) {
+                    return new PropertyLocation(this, relPath);
+                }
+                return new NullLocation(this, relPath);
+            }
+            else {
+                String name = relPath.substring(0, slash);
+                String tail = relPath.substring(slash + 1, relPath.length());
+
+                TreeImpl child = tree.internalGetChild(name);
+                if (child != null) {
+                    return new NodeLocation(child).getChild(tail);
+                }
+                return new NullLocation(this, name).getChild(tail);
             }
         }
 

Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/TreeLocationTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/TreeLocationTest.java?rev=1443368&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/TreeLocationTest.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/TreeLocationTest.java
Thu Feb  7 09:37:14 2013
@@ -0,0 +1,111 @@
+/*
+ * 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.oak.core;
+
+import org.apache.jackrabbit.oak.Oak;
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.api.ContentSession;
+import org.apache.jackrabbit.oak.api.Root;
+import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.api.TreeLocation;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.apache.jackrabbit.oak.core.NullLocation.NULL;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class TreeLocationTest {
+
+    private Root root;
+
+    @Before
+    public void setUp() throws CommitFailedException {
+        ContentSession session = new Oak().createContentSession();
+
+        // Add test content
+        root = session.getLatestRoot();
+        Tree tree = root.getTree("/");
+        tree.setProperty("a", 1);
+        tree.setProperty("b", 2);
+        tree.setProperty("c", 3);
+        tree.addChild("x");
+        tree.addChild("y");
+        tree.addChild("z");
+        root.commit();
+
+        // Acquire a fresh new root to avoid problems from lingering state
+        root = session.getLatestRoot();
+    }
+
+    @After
+    public void tearDown() {
+        root = null;
+    }
+
+    @Test
+    public void testNullLocation() {
+        TreeLocation xyz = NULL.getChild("x/y/z");
+        Assert.assertEquals("x/y/z", xyz.getPath());
+        assertEquals("x/y", xyz.getParent().getPath());
+        assertEquals("x", xyz.getParent().getParent().getPath());
+        assertEquals(NULL, xyz.getParent().getParent().getParent());
+    }
+
+    @Test
+    public void testParentOfRoot() {
+        TreeLocation rootLocation = root.getLocation("/");
+        assertEquals(NULL, rootLocation.getParent());
+    }
+
+    @Test
+    public void testNodeLocation() {
+        TreeLocation x = root.getLocation("/x");
+        assertNotNull(x.getTree());
+
+        TreeLocation xyz = x.getChild("y/z");
+        assertEquals("/x/y/z", xyz.getPath());
+        assertNull(xyz.getTree());
+
+        TreeLocation xy = xyz.getParent();
+        assertEquals("/x/y", xy.getPath());
+        assertNull(xy.getTree());
+
+        assertEquals(x.getTree(), xy.getParent().getTree());
+    }
+
+    @Test
+    public void testPropertyLocation() {
+        TreeLocation a = root.getLocation("/a");
+        assertNotNull(a.getProperty());
+
+        TreeLocation abc = a.getChild("b/c");
+        assertEquals("/a/b/c", abc.getPath());
+        assertNull(abc.getProperty());
+
+        TreeLocation ab = abc.getParent();
+        assertEquals("/a/b", ab.getPath());
+        assertNull(ab.getProperty());
+
+        assertEquals(a.getProperty(), ab.getParent().getProperty());
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/TreeLocationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/TreeLocationTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java?rev=1443368&r1=1443367&r2=1443368&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java
Thu Feb  7 09:37:14 2013
@@ -135,7 +135,7 @@ public abstract class ItemDelegate {
      * The underlying {@link org.apache.jackrabbit.oak.api.TreeLocation} of this item.
      * The location is only re-resolved when the revision of this item does not match
      * the revision of the session.
-     * @return  tree location of the underlying item or {@link org.apache.jackrabbit.oak.core.NullLocation#NULL}
if stale.
+     * @return  tree location of the underlying item.
      */
     @Nonnull
     private synchronized TreeLocation getLocationOrNull() {



Mime
View raw message