From 5dc31d3d2dc3486cf17cdf8f511d6e9ae000326e Mon Sep 17 00:00:00 2001 From: Jukka Zitting Date: Thu, 18 Apr 2013 16:15:13 +0300 Subject: [PATCH] OAK-781: Clarify / fix effects of MISSING_NODE as base state of NodeBuilder Add exists(), getChild() and addChild() methods to NodeBuilder --- .../oak/plugins/memory/MemoryNodeBuilder.java | 17 +++++++++++- .../jackrabbit/oak/spi/state/NodeBuilder.java | 31 ++++++++++++++++++++++ .../jackrabbit/oak/spi/state/ReadOnlyBuilder.java | 15 +++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryNodeBuilder.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryNodeBuilder.java index 9faa23f..281e788 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryNodeBuilder.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryNodeBuilder.java @@ -41,6 +41,7 @@ import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry; import org.apache.jackrabbit.oak.spi.state.NodeBuilder; import org.apache.jackrabbit.oak.spi.state.NodeState; import org.apache.jackrabbit.oak.spi.state.NodeStateDiff; +import org.apache.jackrabbit.oak.spi.state.ReadOnlyBuilder; /** * In-memory node state builder. @@ -191,7 +192,8 @@ public class MemoryNodeBuilder implements NodeBuilder { * Determine whether this child exists at its direct parent. * @return {@code true} iff this child exists at its direct parent. */ - private boolean exists() { + @Override + public boolean exists() { if (isRoot()) { return true; } else if (parent.writeState == null) { @@ -502,6 +504,19 @@ public class MemoryNodeBuilder implements NodeBuilder { return builder; } + @Override @Nonnull + public NodeBuilder getChild(@Nonnull String name) { + read(); + return createChildBuilder(name); + } + + @Override @Nonnull + public NodeBuilder addChild(@Nonnull String name) { + NodeBuilder builder = child(name); + builder.reset(EMPTY_NODE); + return builder; + } + /** * The mutable state being built. Instances of this class * are never passed beyond the containing {@code MemoryNodeBuilder}, diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/state/NodeBuilder.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/state/NodeBuilder.java index 29a950e..b2fd1b8 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/state/NodeBuilder.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/state/NodeBuilder.java @@ -47,6 +47,13 @@ public interface NodeBuilder { NodeState getBaseState(); /** + * Checks whether this builder represents a node that exists. + * + * @return {@code true} if the node exists, {@code false} otherwise + */ + boolean exists(); + + /** * Check whether this builder represents a new node, which is not present in the base state. * @return {@code true} for a new node */ @@ -277,4 +284,28 @@ public interface NodeBuilder { @Nonnull NodeBuilder child(@Nonnull String name); + /** + * Returns a builder for constructing changes to the named child node. + * If the named child node does not already exist, the returned builder + * will refer to a non-existent node and trying to modify it will cause + * {@link IllegalStateException}s to be thrown. + * + * @since Oak 0.7 + * @param name name of the child node + * @return child builder, possibly non-existent + */ + @Nonnull + NodeBuilder getChild(@Nonnull String name); + + /** + * Adds the named child node and returns a builder for modifying it. + * Possible previous content in the named subtree is removed. + * + * @since Oak 0.7 + * @param name name of the child node + * @return child builder + */ + @Nonnull + NodeBuilder addChild(@Nonnull String name); + } diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/state/ReadOnlyBuilder.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/state/ReadOnlyBuilder.java index 4f964d6..408fb0c 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/state/ReadOnlyBuilder.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/state/ReadOnlyBuilder.java @@ -39,6 +39,11 @@ public class ReadOnlyBuilder implements NodeBuilder { } @Override + public boolean exists() { + return state.exists(); + } + + @Override public boolean isNew() { return false; } @@ -158,4 +163,14 @@ public class ReadOnlyBuilder implements NodeBuilder { } } + @Override @Nonnull + public NodeBuilder getChild(@Nonnull String name) { + return new ReadOnlyBuilder(state.getChildNode(name)); + } + + @Override @Nonnull + public NodeBuilder addChild(@Nonnull String name) { + throw unsupported(); + } + } -- 1.8.0.msysgit.0