Class MemoryNodeBuilder
- java.lang.Object
-
- org.apache.jackrabbit.oak.plugins.memory.MemoryNodeBuilder
-
- All Implemented Interfaces:
NodeBuilder
public class MemoryNodeBuilder extends java.lang.Object implements NodeBuilder
In-memory node state builder.A
MemoryNodeBuilderinstance tracks uncommitted changes without relying on weak references or requiring hard references on the entire accessed subtree. It does this by relying onMutableNodeStateinstances for tracking uncommitted changes and onHeadinstances for tracking the connectedness of the builder. A builder keeps a reference to the parent builder and knows its own name, which is used to check for relevant changes in its parent builder and update its state accordingly.A builder is in one of three possible states, which is tracked within its
Headinstance:- unconnected
- A child builder with no content changes starts in this state. Before each access the unconnected builder checks its parent for relevant changes.
- connected
-
Once a builder is first modified, it switches to the connected state
and records all modification in a shared
MutableNodeStateinstance. Before each access the connected builder checks whether its parents base state has been reset and if so, resets its own base state accordingly. - root
- Same as the connected state but only the root of the builder hierarchy can have this state.
-
-
Constructor Summary
Constructors Constructor Description MemoryNodeBuilder(@NotNull NodeState base)Creates a new in-memory node state builder rooted at and based on the passedbasestate.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description @NotNull NodeBuilderchild(@NotNull java.lang.String name)Returns a builder for constructing changes to the named child node.BlobcreateBlob(java.io.InputStream stream)booleanexists()Checks whether this builder represents a node that exists.@NotNull NodeStategetBaseState()Returns the original base state that this builder is modifying.booleangetBoolean(@NotNull java.lang.String name)Returns the boolean value of the named property.@NotNull NodeBuildergetChildNode(@NotNull java.lang.String name)Returns a builder for constructing changes to the named child node.longgetChildNodeCount(long max)Returns the current number of child nodes.@NotNull java.lang.Iterable<java.lang.String>getChildNodeNames()Returns the names of current child nodes.@Nullable java.lang.StringgetName(@NotNull java.lang.String name)Returns the name value of the named property.@NotNull java.lang.Iterable<java.lang.String>getNames(@NotNull java.lang.String name)Returns the name values of the named property.@NotNull NodeStategetNodeState()Returns an immutable node state that matches the current state of the builder.java.lang.StringgetPath()@NotNull java.lang.Iterable<? extends PropertyState>getProperties()Returns the current properties.PropertyStategetProperty(java.lang.String name)Returns the current state of the named property, ornullif the property is not set.longgetPropertyCount()Returns the current number of properties.@Nullable java.lang.StringgetString(@NotNull java.lang.String name)Returns the name value of the named property.booleanhasChildNode(@NotNull java.lang.String name)Checks whether the named child node currently exists.booleanhasProperty(java.lang.String name)Checks whether the named property exists.booleanisModified()Check whether this builder represents a modified node, which has either modified properties or removed or added child nodes.booleanisNew()Check whether this builder represents a new node, which is not present in the base state.booleanisNew(java.lang.String name)Check whether the named property is new, i.e.booleanisReplaced()Check whether this builder represents a node that used to exist but was then replaced with other content, for example as a result of aNodeBuilder.setChildNode(String)call.booleanisReplaced(java.lang.String name)Check whether the named property exists in the base state but is replaced with other content, for example as a result of aNodeBuilder.setProperty(PropertyState)call.booleanisRoot()booleanmoveTo(@NotNull NodeBuilder newParent, @NotNull java.lang.String newName)This implementation has the same semantics as adding this node with namenewNameas a new child ofnewParentfollowed by removing this node.booleanremove()Remove this child node from its parent.@NotNull NodeBuilderremoveProperty(java.lang.String name)Remove the named property.voidreset(@NotNull NodeState newBase)Throws away all changes in this builder and resets the base to the given node state.@NotNull NodeBuildersetChildNode(@NotNull java.lang.String name)Adds the named child node and returns a builder for modifying it.@NotNull NodeBuildersetChildNode(@NotNull java.lang.String name, @NotNull NodeState state)Adds or replaces a subtree.@NotNull NodeBuildersetProperty(@NotNull PropertyState property)Set a property state<T> @NotNull NodeBuildersetProperty(java.lang.String name, T value)Set a property state<T> @NotNull NodeBuildersetProperty(java.lang.String name, T value, Type<T> type)Set a property statejava.lang.StringtoString()
-
-
-
Constructor Detail
-
MemoryNodeBuilder
public MemoryNodeBuilder(@NotNull @NotNull NodeState base)Creates a new in-memory node state builder rooted at and based on the passedbasestate.- Parameters:
base- base state of the new builder
-
-
Method Detail
-
isRoot
public final boolean isRoot()
- Returns:
trueiff this is the root builder
-
reset
public void reset(@NotNull @NotNull NodeState newBase)Throws away all changes in this builder and resets the base to the given node state.- Parameters:
newBase- new base state
-
getNodeState
@NotNull public @NotNull NodeState getNodeState()
Description copied from interface:NodeBuilderReturns an immutable node state that matches the current state of the builder.- Specified by:
getNodeStatein interfaceNodeBuilder- Returns:
- immutable node state
-
getBaseState
@NotNull public @NotNull NodeState getBaseState()
Description copied from interface:NodeBuilderReturns the original base state that this builder is modifying. The return value may be non-existent (i.e. itsexistsmethod returnsfalse) if this builder represents a new node that didn't exist in the base content tree.- Specified by:
getBaseStatein interfaceNodeBuilder- Returns:
- base node state, possibly non-existent
-
exists
public boolean exists()
Description copied from interface:NodeBuilderChecks whether this builder represents a node that exists.- Specified by:
existsin interfaceNodeBuilder- Returns:
trueif the node exists,falseotherwise
-
isNew
public boolean isNew()
Description copied from interface:NodeBuilderCheck whether this builder represents a new node, which is not present in the base state.- Specified by:
isNewin interfaceNodeBuilder- Returns:
truefor a new node
-
isNew
public boolean isNew(java.lang.String name)
Description copied from interface:NodeBuilderCheck whether the named property is new, i.e. not present in the base state.- Specified by:
isNewin interfaceNodeBuilder- Parameters:
name- property name- Returns:
truefor a new property
-
isModified
public boolean isModified()
Description copied from interface:NodeBuilderCheck whether this builder represents a modified node, which has either modified properties or removed or added child nodes.- Specified by:
isModifiedin interfaceNodeBuilder- Returns:
truefor a modified node
-
isReplaced
public boolean isReplaced()
Description copied from interface:NodeBuilderCheck whether this builder represents a node that used to exist but was then replaced with other content, for example as a result of aNodeBuilder.setChildNode(String)call.- Specified by:
isReplacedin interfaceNodeBuilder- Returns:
truefor a replaced node
-
isReplaced
public boolean isReplaced(java.lang.String name)
Description copied from interface:NodeBuilderCheck whether the named property exists in the base state but is replaced with other content, for example as a result of aNodeBuilder.setProperty(PropertyState)call.- Specified by:
isReplacedin interfaceNodeBuilder- Parameters:
name- property name- Returns:
truefor a replaced property
-
getChildNodeCount
public long getChildNodeCount(long max)
Description copied from interface:NodeBuilderReturns the current number of child nodes.If an implementation does know the exact value, it returns it (even if the value is higher than max). If the implementation does not know the exact value, and the child node count is higher than max, it may return Long.MAX_VALUE. The cost of the operation is at most O(max).
- Specified by:
getChildNodeCountin interfaceNodeBuilder- Parameters:
max- the maximum value- Returns:
- number of child nodes
-
getChildNodeNames
@NotNull public @NotNull java.lang.Iterable<java.lang.String> getChildNodeNames()
Description copied from interface:NodeBuilderReturns the names of current child nodes.- Specified by:
getChildNodeNamesin interfaceNodeBuilder- Returns:
- child node names
-
hasChildNode
public boolean hasChildNode(@NotNull @NotNull java.lang.String name)Description copied from interface:NodeBuilderChecks whether the named child node currently exists.- Specified by:
hasChildNodein interfaceNodeBuilder- Parameters:
name- child node name- Returns:
trueif the named child node exists,falseotherwise
-
child
@NotNull public @NotNull NodeBuilder child(@NotNull @NotNull java.lang.String name)
Description copied from interface:NodeBuilderReturns a builder for constructing changes to the named child node. If the named child node does not already exist, a new empty child node is automatically created as the base state of the returned child builder. Otherwise the existing child node state is used as the base state of the returned builder.All updates to the returned child builder will implicitly affect also this builder, as if a
setNode(name, childBuilder.getNodeState())method call had been made after each update. Repeated calls to this method with the same name will return the same child builder instance until an explicitNodeBuilder.setChildNode(String, NodeState)orNodeBuilder.remove()call is made, at which point the link between this builder and a previously returned child builder for that child node name will get broken.- Specified by:
childin interfaceNodeBuilder- Parameters:
name- name of the child node- Returns:
- child builder
-
getChildNode
@NotNull public @NotNull NodeBuilder getChildNode(@NotNull @NotNull java.lang.String name)
Description copied from interface:NodeBuilderReturns 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 causeIllegalStateExceptions to be thrown.- Specified by:
getChildNodein interfaceNodeBuilder- Parameters:
name- name of the child node- Returns:
- child builder, possibly non-existent
-
setChildNode
@NotNull public @NotNull NodeBuilder setChildNode(@NotNull @NotNull java.lang.String name)
Description copied from interface:NodeBuilderAdds the named child node and returns a builder for modifying it. Possible previous content in the named subtree is removed.- Specified by:
setChildNodein interfaceNodeBuilder- Parameters:
name- name of the child node- Returns:
- child builder
-
setChildNode
@NotNull public @NotNull NodeBuilder setChildNode(@NotNull @NotNull java.lang.String name, @NotNull @NotNull NodeState state)
Description copied from interface:NodeBuilderAdds or replaces a subtree.- Specified by:
setChildNodein interfaceNodeBuilder- Parameters:
name- name of the child node containing the new subtreestate- subtree- Returns:
- child builder
-
remove
public boolean remove()
Description copied from interface:NodeBuilderRemove this child node from its parent.- Specified by:
removein interfaceNodeBuilder- Returns:
truefor existing nodes,falseotherwise
-
moveTo
public boolean moveTo(@NotNull @NotNull NodeBuilder newParent, @NotNull @NotNull java.lang.String newName) throws java.lang.IllegalArgumentExceptionThis implementation has the same semantics as adding this node with namenewNameas a new child ofnewParentfollowed by removing this node. As a consequence this implementation allows moving this node into the subtree rooted here, the result of which is the same as removing this node.See also
the general contractforMoveTo.- Specified by:
moveToin interfaceNodeBuilder- Parameters:
newParent- builder for the new parent.newName- name of this child at the new parent- Returns:
trueon success,falseotherwise- Throws:
java.lang.IllegalArgumentException- if the given name string is empty or contains the forward slash character
-
getPropertyCount
public long getPropertyCount()
Description copied from interface:NodeBuilderReturns the current number of properties.- Specified by:
getPropertyCountin interfaceNodeBuilder- Returns:
- number of properties
-
getProperties
@NotNull public @NotNull java.lang.Iterable<? extends PropertyState> getProperties()
Description copied from interface:NodeBuilderReturns the current properties.- Specified by:
getPropertiesin interfaceNodeBuilder- Returns:
- current properties
-
hasProperty
public boolean hasProperty(java.lang.String name)
Description copied from interface:NodeBuilderChecks whether the named property exists. The implementation is equivalent togetProperty(name) != null, but may be optimized to avoid having to load the property value.- Specified by:
hasPropertyin interfaceNodeBuilder- Parameters:
name- property name- Returns:
trueif the named property exists,falseotherwise
-
getProperty
public PropertyState getProperty(java.lang.String name)
Description copied from interface:NodeBuilderReturns the current state of the named property, ornullif the property is not set.- Specified by:
getPropertyin interfaceNodeBuilder- Parameters:
name- property name- Returns:
- property state
-
getBoolean
public boolean getBoolean(@NotNull @NotNull java.lang.String name)Description copied from interface:NodeBuilderReturns the boolean value of the named property. The implementation is equivalent to the following code, but may be optimized.PropertyState property = builder.getProperty(name); return property != null && property.getType() == Type.BOOLEAN && property.getValue(Type.BOOLEAN);- Specified by:
getBooleanin interfaceNodeBuilder- Parameters:
name- property name- Returns:
- boolean value of the named property, or
false
-
getString
@Nullable public @Nullable java.lang.String getString(@NotNull @NotNull java.lang.String name)Description copied from interface:NodeBuilderReturns the name value of the named property. The implementation is equivalent to the following code, but may be optimized.PropertyState property = builder.getProperty(name); if (property != null && property.getType() == Type.STRING) { return property.getValue(Type.STRING); } else { return null; }- Specified by:
getStringin interfaceNodeBuilder- Parameters:
name- property name- Returns:
- string value of the named property, or
null
-
getName
@Nullable public @Nullable java.lang.String getName(@NotNull @NotNull java.lang.String name)Description copied from interface:NodeBuilderReturns the name value of the named property. The implementation is equivalent to the following code, but may be optimized.PropertyState property = builder.getProperty(name); if (property != null && property.getType() == Type.NAME) { return property.getValue(Type.NAME); } else { return null; }- Specified by:
getNamein interfaceNodeBuilder- Parameters:
name- property name- Returns:
- name value of the named property, or
null
-
getNames
@NotNull public @NotNull java.lang.Iterable<java.lang.String> getNames(@NotNull @NotNull java.lang.String name)Description copied from interface:NodeBuilderReturns the name values of the named property. The implementation is equivalent to the following code, but may be optimized.PropertyState property = builder.getProperty(name); if (property != null && property.getType() == Type.NAMES) { return property.getValue(Type.NAMES); } else { return Collections.emptyList(); }- Specified by:
getNamesin interfaceNodeBuilder- Parameters:
name- property name- Returns:
- name values of the named property, or an empty collection
-
setProperty
@NotNull public @NotNull NodeBuilder setProperty(@NotNull @NotNull PropertyState property)
Description copied from interface:NodeBuilderSet a property state- Specified by:
setPropertyin interfaceNodeBuilder- Parameters:
property- The property state to set- Returns:
- this builder
-
setProperty
@NotNull public <T> @NotNull NodeBuilder setProperty(java.lang.String name, @NotNull T value)
Description copied from interface:NodeBuilderSet a property state- Specified by:
setPropertyin interfaceNodeBuilder- Type Parameters:
T- The type of this property. Must be one ofString, Blob, byte[], Long, Integer, Double, Boolean, BigDecimal- Parameters:
name- The name of this propertyvalue- The value of this property- Returns:
- this builder
-
setProperty
@NotNull public <T> @NotNull NodeBuilder setProperty(java.lang.String name, @NotNull T value, Type<T> type)
Description copied from interface:NodeBuilderSet a property state- Specified by:
setPropertyin interfaceNodeBuilder- Type Parameters:
T- The type of this property.- Parameters:
name- The name of this propertyvalue- The value of this property- Returns:
- this builder
-
removeProperty
@NotNull public @NotNull NodeBuilder removeProperty(java.lang.String name)
Description copied from interface:NodeBuilderRemove the named property. This method has no effect if a property of the givennamedoes not exist.- Specified by:
removePropertyin interfaceNodeBuilder- Parameters:
name- name of the property
-
createBlob
public Blob createBlob(java.io.InputStream stream) throws java.io.IOException
- Specified by:
createBlobin interfaceNodeBuilder- Throws:
java.io.IOException
-
getPath
public final java.lang.String getPath()
- Returns:
- path of this builder.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-