jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1376710 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/core/ oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/
Date Thu, 23 Aug 2012 21:36:50 GMT
Author: mduerig
Date: Thu Aug 23 21:36:50 2012
New Revision: 1376710

URL: http://svn.apache.org/viewvc?rev=1376710&view=rev
Log:
OAK-275 Introduce TreeLocation interface
OAK-212 Move status of property to PropertyState
OAK-214 PropertyImpl#getParent violates JCR API
OAK-220 PropertyState can only be accessed from parent tree
OAK-226 Property#getPath() relies on accessibility of the parent node

Modified:
    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/NodeDelegate.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java

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=1376710&r1=1376709&r2=1376710&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 Aug 23 21:36:50 2012
@@ -570,12 +570,15 @@ public class TreeImpl implements Tree, P
             String parentPath = PathUtils.getParentPath(relPath);
             for (String name : PathUtils.elements(parentPath)) {
                 child = child.internalGetChild(name);
+                if (child == null) {
+                    return NullLocation.INSTANCE;
+                }
             }
 
             String name = PathUtils.getName(relPath);
             PropertyState property = child.internalGetProperty(name);
             if (property != null) {
-                return new PropertyLocation(this, property);
+                return new PropertyLocation(new NodeLocation(child), property);
             }
             else {
                 child = child.internalGetChild(name);
@@ -651,7 +654,7 @@ public class TreeImpl implements Tree, P
     }
 
     private static class NullLocation implements TreeLocation {
-        static NullLocation INSTANCE = new NullLocation();
+        public static final NullLocation INSTANCE = new NullLocation();
 
         @Override
         public TreeLocation getParent() {

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java?rev=1376710&r1=1376709&r2=1376710&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java
Thu Aug 23 21:36:50 2012
@@ -40,7 +40,6 @@ 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;
 
 /**
  * {@code NodeDelegate} serve as internal representations of {@code Node}s.
@@ -51,11 +50,11 @@ import org.apache.jackrabbit.oak.commons
 public class NodeDelegate extends ItemDelegate {
 
     /** The underlying {@link TreeLocation} of this node. */
-    private TreeLocation treeLocation;
+    private TreeLocation location;
 
     public NodeDelegate(SessionDelegate sessionDelegate, Tree tree) {
         super(sessionDelegate);
-        this.treeLocation = tree.getLocation();
+        this.location = tree.getLocation();
     }
 
     @Override
@@ -76,7 +75,7 @@ public class NodeDelegate extends ItemDe
 
     @Override
     public boolean isStale() {
-        return treeLocation.getStatus() == Status.REMOVED;
+        return location.getStatus() == Status.REMOVED;
     }
 
     @Override
@@ -87,7 +86,7 @@ public class NodeDelegate extends ItemDe
     @Override
     public String toString() {
         // don't disturb the state: avoid resolving the tree
-        return "NodeDelegate[" + treeLocation.getPath() + ']';
+        return "NodeDelegate[" + location.getPath() + ']';
     }
 
     @Nonnull
@@ -119,17 +118,12 @@ public class NodeDelegate extends ItemDe
      * no such property exists
      */
     @CheckForNull
-    public PropertyDelegate getProperty(String relPath) throws InvalidItemStateException
{
-        Tree parent = getTree(PathUtils.getParentPath(relPath));
-        if (parent == null) {
-            return null;
-        }
-
-        String name = PathUtils.getName(relPath);
-        PropertyState propertyState = parent.getProperty(name);
+    public PropertyDelegate getProperty(String relPath) {
+        TreeLocation propertyLocation = getLocation().getChild(relPath);
+        PropertyState propertyState = propertyLocation.getProperty();
         return propertyState == null
-            ? null
-            : new PropertyDelegate(sessionDelegate, parent, propertyState);
+                ? null
+                : new PropertyDelegate(sessionDelegate, propertyLocation);
     }
 
     /**
@@ -157,8 +151,8 @@ public class NodeDelegate extends ItemDe
      * no such node exists
      */
     @CheckForNull
-    public NodeDelegate getChild(String relPath) throws InvalidItemStateException {
-        Tree tree = getTree(relPath);
+    public NodeDelegate getChild(String relPath) {
+        Tree tree = getLocation().getChild(relPath).getTree();
         return tree == null ? null : new NodeDelegate(sessionDelegate, tree);
     }
 
@@ -276,12 +270,13 @@ public class NodeDelegate extends ItemDe
      */
     @Nonnull
     public PropertyDelegate setProperty(String name, CoreValue value) throws InvalidItemStateException,
ValueFormatException {
-        PropertyState old = getTree().getProperty(name);
+        Tree tree = getTree();
+        PropertyState old = tree.getProperty(name);
         if (old != null && old.isArray()) {
             throw new ValueFormatException("Attempt to set a single value to multi-valued
property.");
         }
-        PropertyState propertyState = getTree().setProperty(name, value);
-        return new PropertyDelegate(sessionDelegate, getTree(), propertyState);
+        PropertyState propertyState = tree.setProperty(name, value);
+        return new PropertyDelegate(sessionDelegate, tree, propertyState);
     }
 
     public void removeProperty(String name) throws InvalidItemStateException {
@@ -296,12 +291,13 @@ public class NodeDelegate extends ItemDe
      */
     @Nonnull
     public PropertyDelegate setProperty(String name, List<CoreValue> value) throws
InvalidItemStateException, ValueFormatException {
-        PropertyState old = getTree().getProperty(name);
+        Tree tree = getTree();
+        PropertyState old = tree.getProperty(name);
         if (old != null && ! old.isArray()) {
             throw new ValueFormatException("Attempt to set multiple values to single valued
property.");
         }
-        PropertyState propertyState = getTree().setProperty(name, value);
-        return new PropertyDelegate(sessionDelegate, getTree(), propertyState);
+        PropertyState propertyState = tree.setProperty(name, value);
+        return new PropertyDelegate(sessionDelegate, tree, propertyState);
     }
 
     /**
@@ -326,22 +322,32 @@ public class NodeDelegate extends ItemDe
 
     // -----------------------------------------------------------< private >---
 
-    private Tree getTree(String relPath) throws InvalidItemStateException {
-        String absPath = PathUtils.concat(getPath(), relPath);
-        return sessionDelegate.getTree(absPath);
+    @Nonnull
+    Tree getTree() throws InvalidItemStateException {
+        resolve();
+        Tree tree = location.getTree();
+        if (tree == null) {
+            throw new InvalidItemStateException("Node is stale");
+        }
+        else {
+            return tree;
+        }
     }
 
     @Nonnull
-    synchronized Tree getTree() throws InvalidItemStateException {
-        String path = treeLocation.getPath();
+    private TreeLocation getLocation() {
+        resolve();
+        return location;
+    }
+
+    private synchronized void resolve() {
+        String path = location.getPath();
         if (path != null) {
             Tree tree = sessionDelegate.getTree(path);
             if (tree != null) {
-                treeLocation = tree.getLocation();
-                return tree;
+                location = tree.getLocation();
             }
         }
-        throw new InvalidItemStateException("Node is stale");
     }
 
     private Iterator<NodeDelegate> nodeDelegateIterator(
@@ -373,7 +379,7 @@ public class NodeDelegate extends ItemDe
                 new Function<PropertyState, PropertyDelegate>() {
                     @Override
                     public PropertyDelegate apply(PropertyState propertyState) {
-                        return new PropertyDelegate(sessionDelegate, treeLocation.getTree(),
+                        return new PropertyDelegate(sessionDelegate, location.getTree(),
                                 propertyState);
                     }
                 });

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java?rev=1376710&r1=1376709&r2=1376710&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java
Thu Aug 23 21:36:50 2012
@@ -29,6 +29,7 @@ import org.apache.jackrabbit.oak.api.Cor
 import org.apache.jackrabbit.oak.api.PropertyState;
 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;
 
 /**
@@ -39,27 +40,21 @@ import org.apache.jackrabbit.oak.commons
  */
 public class PropertyDelegate extends ItemDelegate {
 
-    /**
-     * The underlying {@link Tree} of the parent node. In order to ensure the
-     * instance is up to date, this field <em>should not be accessed directly</em>
-     * but rather the {@link #getParentTree()} Tree()} method should be used.
-     */
-    private Tree parent;
-
-    /**
-     * The underlying {@link PropertyState}. In order to ensure the instance is up
-     * to date, this field <em>should not be accessed directly</em> but rather
the
-     * {@link #getPropertyState()} method should be used.
-     */
-    private PropertyState propertyState;
+    /** The underlying {@link TreeLocation} of this node. */
+    private TreeLocation location;
 
     PropertyDelegate(SessionDelegate sessionDelegate, Tree parent, PropertyState propertyState)
{
         super(sessionDelegate);
 
         assert parent != null;
         assert propertyState != null;
-        this.parent = parent;
-        this.propertyState = propertyState;
+        this.location = parent.getLocation().getChild(propertyState.getName());
+    }
+
+    public PropertyDelegate(SessionDelegate sessionDelegate, TreeLocation location) {
+        super(sessionDelegate);
+        assert location != null;
+        this.location = location;
     }
 
     @Override
@@ -81,7 +76,7 @@ public class PropertyDelegate extends It
     @Override
     public boolean isStale() {
         resolve();
-        return parent == null;
+        return location.getStatus() == Status.REMOVED;
     }
 
     @Override
@@ -97,7 +92,7 @@ public class PropertyDelegate extends It
     @Override
     public String toString() {
         // don't disturb the state: avoid calling resolve()
-        return "PropertyDelegate[" + parent.getPath() + '/' + propertyState.getName() + ']';
+        return "PropertyDelegate[" + location.getPath() + ']';
     }
 
     /**
@@ -255,36 +250,32 @@ public class PropertyDelegate extends It
     @Nonnull
     private PropertyState getPropertyState() throws InvalidItemStateException {
         resolve();
-        if (parent == null) {
+        PropertyState property = location.getProperty();
+        if (property == null) {
             throw new InvalidItemStateException("Property is stale");
         }
-
-        return propertyState;
+        return property;
     }
 
     @Nonnull
     private Tree getParentTree() throws InvalidItemStateException {
         resolve();
-        if (parent == null) {
-            throw new InvalidItemStateException("Property is stale");
+        Tree tree = location.getParent().getTree();
+        if (tree == null) {
+            throw new InvalidItemStateException("Parent node is stale");
         }
-
-        return parent;
+        return tree;
     }
 
-    private synchronized void resolve() {
-        if (parent != null) {
-            parent = parent.getStatus() == Status.REMOVED
-                ? null
-                : sessionDelegate.getTree(parent.getPath());
-
-            if (parent == null) {
-                propertyState = null;
-            } else {
-                String path = PathUtils.concat(parent.getPath(), propertyState.getName());
-                propertyState = parent.getProperty(PathUtils.getName(path));
+    synchronized void resolve() {
+        String path = location.getPath();
+        if (path != null) {
+            Tree parent = sessionDelegate.getTree(PathUtils.getParentPath(path));
+            if (parent != null) {
+                location = parent.getLocation().getChild(PathUtils.getName(path));
             }
         }
     }
 
+
 }



Mime
View raw message