jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1376901 - in /jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr: ItemDelegate.java NodeDelegate.java PropertyDelegate.java
Date Fri, 24 Aug 2012 13:05:31 GMT
Author: mduerig
Date: Fri Aug 24 13:05:30 2012
New Revision: 1376901

URL: http://svn.apache.org/viewvc?rev=1376901&view=rev
Log:
OAK-275 Introduce TreeLocation interface
refactor, cleanup

Modified:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.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-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=1376901&r1=1376900&r2=1376901&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
Fri Aug 24 13:05:30 2012
@@ -33,9 +33,9 @@ public abstract class ItemDelegate {
     protected final SessionDelegate sessionDelegate;
 
     /** The underlying {@link org.apache.jackrabbit.oak.api.TreeLocation} of this item. */
-    protected TreeLocation location;
+    private TreeLocation location;
 
-    protected ItemDelegate(SessionDelegate sessionDelegate, TreeLocation location) {
+    ItemDelegate(SessionDelegate sessionDelegate, TreeLocation location) {
         assert sessionDelegate != null;
         assert location != null;
 
@@ -58,8 +58,7 @@ public abstract class ItemDelegate {
      */
     @Nonnull
     public String getPath() throws InvalidItemStateException {
-        checkStale();
-        return location.getPath();
+        return getLocation().getPath();  // never null
     }
 
     /**
@@ -69,8 +68,7 @@ public abstract class ItemDelegate {
      */
     @CheckForNull
     public NodeDelegate getParent() throws InvalidItemStateException {
-        checkStale();
-        return NodeDelegate.create(sessionDelegate, location.getParent());
+        return NodeDelegate.create(sessionDelegate, getLocation().getParent());
     }
 
     /**
@@ -78,19 +76,7 @@ public abstract class ItemDelegate {
      * @return  {@code true} iff stale
      */
     public boolean isStale() {
-        if (location.getStatus() == Status.REMOVED) {
-            return true;
-        }
-        else {
-            resolve();
-            return location.getStatus() == Status.REMOVED;
-        }
-    }
-
-    public void checkStale() throws InvalidItemStateException {
-        if (isStale()) {
-            throw new InvalidItemStateException("Item is stale");
-        }
+        return getLocationOrNull() == null;
     }
 
     /**
@@ -99,8 +85,7 @@ public abstract class ItemDelegate {
      */
     @Nonnull
     public Status getStatus() throws InvalidItemStateException {
-        checkStale();
-        return location.getStatus();
+        return getLocation().getStatus();  // never null
     }
 
     /**
@@ -112,11 +97,44 @@ public abstract class ItemDelegate {
         return sessionDelegate;
     }
 
+    /**
+     * The underlying {@link org.apache.jackrabbit.oak.api.TreeLocation} of this item.
+     * @return  tree location of the underlying item
+     * @throws InvalidItemStateException if the location points to a stale item
+     */
+    @Nonnull
+    public final TreeLocation getLocation() throws InvalidItemStateException {
+        TreeLocation location = getLocationOrNull();
+        if (location == null) {
+            throw new InvalidItemStateException("Item is stale");
+        }
+        return location;
+    }
+
     @Override
     public String toString() {
-        // don't disturb the state: avoid resolving the tree
+        // don't disturb the state: avoid resolving location
         return getClass().getSimpleName() + '[' + location.getPath() + ']';
     }
 
-    protected abstract void resolve();
+    //------------------------------------------------------------< private >---
+
+    /**
+     * The underlying {@link org.apache.jackrabbit.oak.api.TreeLocation} of this item.
+     * @return  tree location of the underlying item or {@code null} if stale.
+     */
+    @CheckForNull
+    private synchronized TreeLocation getLocationOrNull() {
+        if (isStale(location)) {
+            return null;
+        }
+
+        location = sessionDelegate.getLocation(location.getPath());
+        return isStale(location) ? null : location;
+    }
+
+    private static boolean isStale(TreeLocation location) {
+        return location.getStatus() == Status.REMOVED || location.getPath() == null;
+    }
+
 }

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=1376901&r1=1376900&r2=1376901&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
Fri Aug 24 13:05:30 2012
@@ -98,7 +98,7 @@ public class NodeDelegate extends ItemDe
      * no such property exists
      */
     @CheckForNull
-    public PropertyDelegate getProperty(String relPath) {
+    public PropertyDelegate getProperty(String relPath) throws InvalidItemStateException
{
         TreeLocation propertyLocation = getChildLocation(relPath);
         PropertyState propertyState = propertyLocation.getProperty();
         return propertyState == null
@@ -131,7 +131,7 @@ public class NodeDelegate extends ItemDe
      * no such node exists
      */
     @CheckForNull
-    public NodeDelegate getChild(String relPath) {
+    public NodeDelegate getChild(String relPath) throws InvalidItemStateException {
         TreeLocation childLocation = getChildLocation(relPath);
         return create(sessionDelegate, childLocation);
     }
@@ -249,7 +249,8 @@ public class NodeDelegate extends ItemDe
      * @return  the set property
      */
     @Nonnull
-    public PropertyDelegate setProperty(String name, CoreValue value) throws InvalidItemStateException,
ValueFormatException {
+    public PropertyDelegate setProperty(String name, CoreValue value)
+            throws InvalidItemStateException, ValueFormatException {
         Tree tree = getTree();
         PropertyState old = tree.getProperty(name);
         if (old != null && old.isArray()) {
@@ -270,7 +271,8 @@ public class NodeDelegate extends ItemDe
      * @return  the set property
      */
     @Nonnull
-    public PropertyDelegate setProperty(String name, List<CoreValue> value) throws
InvalidItemStateException, ValueFormatException {
+    public PropertyDelegate setProperty(String name, List<CoreValue> value)
+            throws InvalidItemStateException, ValueFormatException {
         Tree tree = getTree();
         PropertyState old = tree.getProperty(name);
         if (old != null && ! old.isArray()) {
@@ -304,8 +306,7 @@ public class NodeDelegate extends ItemDe
 
     @Nonnull
     Tree getTree() throws InvalidItemStateException {
-        resolve();
-        Tree tree = location.getTree();
+        Tree tree = getLocation().getTree();
         if (tree == null) {
             throw new InvalidItemStateException("Node is stale");
         }
@@ -314,27 +315,10 @@ public class NodeDelegate extends ItemDe
 
     // -----------------------------------------------------------< private >---
 
-    private TreeLocation getChildLocation(String relPath) {
+    private TreeLocation getChildLocation(String relPath) throws InvalidItemStateException
{
         return getLocation().getChild(relPath);
     }
 
-    @Nonnull
-    private TreeLocation getLocation() {
-        resolve();
-        return location;
-    }
-
-    @Override
-    protected synchronized void resolve() {
-        String path = location.getPath();
-        if (path != null) {
-            Tree tree = sessionDelegate.getTree(path);
-            if (tree != null) {
-                location = tree.getLocation();
-            }
-        }
-    }
-
     private Iterator<NodeDelegate> nodeDelegateIterator(
             Iterator<Tree> children) {
         return Iterators.transform(
@@ -353,7 +337,8 @@ public class NodeDelegate extends ItemDe
     }
 
     private Iterator<PropertyDelegate> propertyDelegateIterator(
-            Iterator<? extends PropertyState> properties) {
+            Iterator<? extends PropertyState> properties) throws InvalidItemStateException
{
+        final TreeLocation location = getLocation();
         return Iterators.transform(
                 Iterators.filter(properties, new Predicate<PropertyState>() {
                     @Override

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=1376901&r1=1376900&r2=1376901&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
Fri Aug 24 13:05:30 2012
@@ -20,6 +20,7 @@ import java.util.List;
 
 import javax.annotation.Nonnull;
 import javax.jcr.InvalidItemStateException;
+import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.Value;
 import javax.jcr.nodetype.NodeType;
 import javax.jcr.nodetype.PropertyDefinition;
@@ -28,7 +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.TreeLocation;
-import org.apache.jackrabbit.oak.commons.PathUtils;
+import org.apache.jackrabbit.oak.util.TODO;
 
 /**
  * {@code PropertyDelegate} serve as internal representations of {@code Property}s.
@@ -77,96 +78,101 @@ public class PropertyDelegate extends It
      */
     @Nonnull
     public PropertyDefinition getDefinition() {
-        // TODO
-        return new PropertyDefinition() {
-
-            @Override
-            public int getRequiredType() {
-                return 0;
-            }
-
-            @Override
-            public String[] getValueConstraints() {
-                // TODO
-                return new String[0];
-            }
-
-            @Override
-            public Value[] getDefaultValues() {
-                // TODO
-                return new Value[0];
-            }
-
-            @Override
-            public boolean isMultiple() {
-                // TODO
-                try {
-                    return getPropertyState().isArray();
-                }
-                catch (InvalidItemStateException e) {
-                    return false;  // todo implement catch e
-                }
-            }
-
-            @Override
-            public String[] getAvailableQueryOperators() {
-                // TODO
-                return new String[0];
-            }
-
-            @Override
-            public boolean isFullTextSearchable() {
-                // TODO
-                return false;
-            }
-
-            @Override
-            public boolean isQueryOrderable() {
-                // TODO
-                return false;
-            }
-
-            @Override
-            public NodeType getDeclaringNodeType() {
-                // TODO
-                return null;
-            }
-
-            @Override
-            public String getName() {
-                // TODO
-                try {
-                    return getPropertyState().getName();
-                }
-                catch (InvalidItemStateException e) {
-                    return null;  // todo implement catch e
-                }
-            }
-
-            @Override
-            public boolean isAutoCreated() {
-                // TODO
-                return false;
-            }
-
-            @Override
-            public boolean isMandatory() {
-                // TODO
-                return false;
-            }
-
-            @Override
-            public int getOnParentVersion() {
-                // TODO
-                return 0;
-            }
-
-            @Override
-            public boolean isProtected() {
-                // TODO
-                return false;
-            }
-        };
+        try {
+            return TODO.dummyImplementation().returnValue(
+                new PropertyDefinition() {
+
+                    @Override
+                    public int getRequiredType() {
+                        return 0;
+                    }
+
+                    @Override
+                    public String[] getValueConstraints() {
+                        // TODO
+                        return new String[0];
+                    }
+
+                    @Override
+                    public Value[] getDefaultValues() {
+                        // TODO
+                        return new Value[0];
+                    }
+
+                    @Override
+                    public boolean isMultiple() {
+                        // TODO
+                        try {
+                            return getPropertyState().isArray();
+                        }
+                        catch (InvalidItemStateException e) {
+                            return false;  // todo implement catch e
+                        }
+                    }
+
+                    @Override
+                    public String[] getAvailableQueryOperators() {
+                        // TODO
+                        return new String[0];
+                    }
+
+                    @Override
+                    public boolean isFullTextSearchable() {
+                        // TODO
+                        return false;
+                    }
+
+                    @Override
+                    public boolean isQueryOrderable() {
+                        // TODO
+                        return false;
+                    }
+
+                    @Override
+                    public NodeType getDeclaringNodeType() {
+                        // TODO
+                        return null;
+                    }
+
+                    @Override
+                    public String getName() {
+                        // TODO
+                        try {
+                            return getPropertyState().getName();
+                        }
+                        catch (InvalidItemStateException e) {
+                            return null;  // todo implement catch e
+                        }
+                    }
+
+                    @Override
+                    public boolean isAutoCreated() {
+                        // TODO
+                        return false;
+                    }
+
+                    @Override
+                    public boolean isMandatory() {
+                        // TODO
+                        return false;
+                    }
+
+                    @Override
+                    public int getOnParentVersion() {
+                        // TODO
+                        return 0;
+                    }
+
+                    @Override
+                    public boolean isProtected() {
+                        // TODO
+                        return false;
+                    }
+                });
+        }
+        catch (UnsupportedRepositoryOperationException e) {
+            throw new UnsupportedOperationException(e);
+        }
     }
 
     /**
@@ -196,8 +202,7 @@ public class PropertyDelegate extends It
 
     @Nonnull
     private PropertyState getPropertyState() throws InvalidItemStateException {
-        resolve();
-        PropertyState property = location.getProperty();
+        PropertyState property = getLocation().getProperty();
         if (property == null) {
             throw new InvalidItemStateException("Property is stale");
         }
@@ -206,24 +211,11 @@ public class PropertyDelegate extends It
 
     @Nonnull
     private Tree getParentTree() throws InvalidItemStateException {
-        resolve();
-        Tree tree = location.getParent().getTree();
+        Tree tree = getLocation().getParent().getTree();
         if (tree == null) {
             throw new InvalidItemStateException("Parent node is stale");
         }
         return tree;
     }
 
-    @Override
-    protected 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