You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by md...@apache.org on 2012/08/23 15:27:16 UTC
svn commit: r1376481 - in
/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak:
api/Tree.java api/TreeLocation.java core/ReadOnlyTree.java core/TreeImpl.java
Author: mduerig
Date: Thu Aug 23 13:27:16 2012
New Revision: 1376481
URL: http://svn.apache.org/viewvc?rev=1376481&view=rev
Log:
OAK-275: Introduce TreeLocation interface
initial version and implementations
Added:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/TreeLocation.java
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Tree.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/TreeImpl.java
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Tree.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Tree.java?rev=1376481&r1=1376480&r2=1376481&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Tree.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Tree.java Thu Aug 23 13:27:16 2012
@@ -97,6 +97,12 @@ public interface Tree {
Status getStatus();
/**
+ * @return the current location
+ */
+ @Nonnull
+ TreeLocation getLocation();
+
+ /**
* @return the parent of this {@code Tree} instance. This method returns
* {@code null} if the parent is not accessible or if no parent exists (root
* node).
Added: 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=1376481&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/TreeLocation.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/TreeLocation.java Thu Aug 23 13:27:16 2012
@@ -0,0 +1,65 @@
+package org.apache.jackrabbit.oak.api;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+
+import org.apache.jackrabbit.oak.api.Tree.Status;
+
+/**
+ * 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}).
+ * {@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
+ * accessed. That is, if a {@code TreeLocation} points to an item which does not exist or
+ * is unavailable otherwise (i.e. due to access control restrictions) accessing the tree
+ * will return {@code null} at this point.
+ */
+public interface TreeLocation {
+
+ /**
+ * Navigate to the parent
+ * @return a {@code TreeLocation} for the parent of this location.
+ */
+ @Nonnull
+ TreeLocation getParent();
+
+ /**
+ * Navigate to a child
+ * @param name name of the child
+ * @return a {@code TreeLocation} for a child with the given {@code name}.
+ */
+ @Nonnull
+ TreeLocation getChild(String name);
+
+ /**
+ * Get the underlying {@link org.apache.jackrabbit.oak.api.Tree} for this {@code TreeLocation}.
+ * @return underlying {@code Tree} instance or {@code null} if not available.
+ */
+ @CheckForNull
+ Tree getTree();
+
+ /**
+ * Get the underlying {@link org.apache.jackrabbit.oak.api.PropertyState} for this {@code TreeLocation}.
+ * @return underlying {@code PropertyState} instance or {@code null} if not available.
+ */
+ @CheckForNull
+ PropertyState getProperty();
+
+ /**
+ * {@link org.apache.jackrabbit.oak.api.Tree.Status} of the underlying item or {@code null} if no
+ * such item exists.
+ * @return
+ */
+ @CheckForNull
+ Status getStatus();
+
+ /**
+ * The path of the underlying item or {@code null} if no such item exists.
+ * @return path
+ */
+ @CheckForNull
+ String getPath();
+
+}
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=1376481&r1=1376480&r2=1376481&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 Aug 23 13:27:16 2012
@@ -24,6 +24,7 @@ import java.util.List;
import org.apache.jackrabbit.oak.api.CoreValue;
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.spi.state.ChildNodeEntry;
import org.apache.jackrabbit.oak.spi.state.NodeState;
@@ -131,6 +132,11 @@ public class ReadOnlyTree implements Tre
}
@Override
+ public TreeLocation getLocation() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
public boolean hasChild(String name) {
return state.getChildNode(name) != null;
}
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=1376481&r1=1376480&r2=1376481&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 13:27:16 2012
@@ -35,6 +35,7 @@ import com.google.common.collect.Iterabl
import org.apache.jackrabbit.oak.api.CoreValue;
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.core.RootImpl.PurgeListener;
import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
@@ -134,49 +135,11 @@ public class TreeImpl implements Tree, P
@Override
public Status getPropertyStatus(String name) {
// TODO: see OAK-212
- if (!canReadProperty(name)) {
- return null;
+ if (canReadProperty(name)) {
+ return internalGetPropertyStatus(name);
}
-
- NodeState baseState = getBaseState();
- boolean exists = internalGetProperty(name) != null;
- if (baseState == null) {
- // This instance is NEW...
- if (exists) {
- // ...so all children are new
- return Status.NEW;
- } else {
- // ...unless they don't exist.
- return null;
- }
- } else {
- if (exists) {
- // We have the property...
- if (baseState.getProperty(name) == null) {
- // ...but didn't have it before. So its NEW.
- return Status.NEW;
- } else {
- // ... and did have it before. So...
- PropertyState base = baseState.getProperty(name);
- PropertyState head = getProperty(name);
- if (base == null ? head == null : base.equals(head)) {
- // ...it's EXISTING if it hasn't changed
- return Status.EXISTING;
- } else {
- // ...and MODIFIED otherwise.
- return Status.MODIFIED;
- }
- }
- } else {
- // We don't have the property
- if (baseState.getProperty(name) == null) {
- // ...and didn't have it before. So it doesn't exist.
- return null;
- } else {
- // ...but did have it before. So it's REMOVED
- return Status.REMOVED;
- }
- }
+ else {
+ return null;
}
}
@@ -326,6 +289,11 @@ public class TreeImpl implements Tree, P
root.purge();
}
+ @Override
+ public TreeLocation getLocation() {
+ return new NodeLocation(this);
+ }
+
//--------------------------------------------------< RootImpl.Listener >---
@Override
@@ -421,6 +389,49 @@ public class TreeImpl implements Tree, P
return getNodeBuilder().getProperty(propertyName);
}
+ private Status internalGetPropertyStatus(String name) {
+ NodeState baseState = getBaseState();
+ boolean exists = internalGetProperty(name) != null;
+ if (baseState == null) {
+ // This instance is NEW...
+ if (exists) {
+ // ...so all children are new
+ return Status.NEW;
+ } else {
+ // ...unless they don't exist.
+ return null;
+ }
+ } else {
+ if (exists) {
+ // We have the property...
+ if (baseState.getProperty(name) == null) {
+ // ...but didn't have it before. So its NEW.
+ return Status.NEW;
+ } else {
+ // ... and did have it before. So...
+ PropertyState base = baseState.getProperty(name);
+ PropertyState head = getProperty(name);
+ if (base == null ? head == null : base.equals(head)) {
+ // ...it's EXISTING if it hasn't changed
+ return Status.EXISTING;
+ } else {
+ // ...and MODIFIED otherwise.
+ return Status.MODIFIED;
+ }
+ }
+ } else {
+ // We don't have the property
+ if (baseState.getProperty(name) == null) {
+ // ...and didn't have it before. So it doesn't exist.
+ return null;
+ } else {
+ // ...but did have it before. So it's REMOVED
+ return Status.REMOVED;
+ }
+ }
+ }
+ }
+
private boolean isRemoved() {
return parent == this;
}
@@ -532,4 +543,142 @@ public class TreeImpl implements Tree, P
}
}
+ //------------------------------------------------------------< TreeLocation >---
+
+ private class NodeLocation implements TreeLocation {
+ private final TreeImpl tree;
+
+ public NodeLocation(TreeImpl tree) {
+ assert tree != null;
+ this.tree = tree;
+ }
+
+ @Nonnull
+ @Override
+ public TreeLocation getParent() {
+ return tree.parent == null
+ ? NullLocation.INSTANCE
+ : new NodeLocation(tree.parent);
+ }
+
+ @Nonnull
+ @Override
+ public TreeLocation getChild(String name) {
+ PropertyState property = tree.internalGetProperty(name);
+ if (property != null) {
+ return new PropertyLocation(this, property);
+ }
+ else {
+ TreeImpl node = tree.internalGetChild(name);
+ return node == null
+ ? NullLocation.INSTANCE
+ : new NodeLocation(node);
+ }
+ }
+
+ @Override
+ public String getPath() {
+ return tree.getPath();
+ }
+
+ @Override
+ public Tree getTree() {
+ return canRead(tree) ? tree : null;
+ }
+
+ @Override
+ public PropertyState getProperty() {
+ return null;
+ }
+
+ @Override
+ public Status getStatus() {
+ return tree.getStatus();
+ }
+ }
+
+ private class PropertyLocation implements TreeLocation {
+ private final NodeLocation parent;
+ private final PropertyState property;
+
+ public PropertyLocation(NodeLocation parent, PropertyState property) {
+ assert parent != null;
+ assert property != null;
+ this.parent = parent;
+ this.property = property;
+ }
+
+ @Nonnull
+ @Override
+ public TreeLocation getParent() {
+ return parent;
+ }
+
+ @Nonnull
+ @Override
+ public TreeLocation getChild(String name) {
+ return NullLocation.INSTANCE;
+ }
+
+ @Override
+ public String getPath() {
+ return PathUtils.concat(parent.getPath(), property.getName());
+ }
+
+ @Override
+ public Tree getTree() {
+ return null;
+ }
+
+ @Override
+ public PropertyState getProperty() {
+ return root.getPermissions().canRead(getPath(), true)
+ ? property
+ : null;
+ }
+
+ @Override
+ public Status getStatus() {
+ return parent.tree.internalGetPropertyStatus(property.getName());
+ }
+ }
+
+ private static class NullLocation implements TreeLocation {
+ static NullLocation INSTANCE = new NullLocation();
+
+ @Nonnull
+ @Override
+ public TreeLocation getParent() {
+ return this;
+ }
+
+ @Nonnull
+ @Override
+ public TreeLocation getChild(String name) {
+ return this;
+ }
+
+ @Override
+ public String getPath() {
+ return null;
+ }
+
+ @Override
+ public Tree getTree() {
+ return null;
+ }
+
+ @Override
+ public PropertyState getProperty() {
+ return null;
+ }
+
+ @Override
+ public Status getStatus() {
+ return null;
+ }
+ }
+
}
+
+