ModeShape Distribution 3.0.0.Beta4

org.modeshape.jcr.cache.document
Class DocumentTranslator

java.lang.Object
  extended by org.modeshape.jcr.cache.document.DocumentTranslator

public class DocumentTranslator
extends Object

A utility class that encapsulates all the logic for reading from and writing to Document instances.


Nested Class Summary
static class DocumentTranslator.ChildReferencesInfo
           
 
Field Summary
static String BLOCK_SIZE
           
static String CHILDREN
           
static String CHILDREN_INFO
           
static String COUNT
           
static String KEY
           
static String LARGE_VALUE
           
static String LAST_BLOCK
           
static String NAME
           
static String NEXT_BLOCK
           
static String PARENT
           
static String PROPERTIES
           
static String REFERENCE_COUNT
           
static String REFERRERS
           
static String SHA1
           
static String STRONG
           
static String WEAK
           
 
Constructor Summary
DocumentTranslator(ExecutionContext context, SchematicDb database, long largeStringSize)
           
 
Method Summary
 void addPropertyValues(EditableDocument document, Name propertyName, boolean isMultiple, Collection<?> values, Set<BinaryKey> unusedBinaryKeys)
           
 void changeChildren(NodeKey key, EditableDocument document, SessionNode.ChangedChildren changedChildren, ChildReferences appended)
           
 void changeReferrers(EditableDocument document, SessionNode.ReferrerChanges changes)
           
protected  ChildReference childReferenceFrom(Object value)
           
 int countProperties(Document document)
           
protected  boolean decrementBinaryReferenceCount(Object fieldValue, Set<BinaryKey> unusedBinaryKeys)
          Decrement the reference count for the binary value.
 EditableDocument fromChildReference(ChildReference ref)
           
 ChildReferences getChildReferences(WorkspaceCache cache, Document document)
           
 DocumentTranslator.ChildReferencesInfo getChildReferencesInfo(Document document)
           
 NodeKey getParentKey(Document document, String primaryWorkspaceKey, String secondaryWorkspaceKey)
          Obtain the preferred key for the parent of this node.
 Set<NodeKey> getParentKeys(Document document, String primaryWorkspaceKey, String secondaryWorkspaceKey)
           
 void getProperties(Document document, Map<Name,Property> result)
           
 Property getProperty(Document document, Name propertyName)
           
 Set<NodeKey> getReferrers(Document document, CachedNode.ReferenceType type)
           
 boolean hasProperties(Document document)
           
 boolean hasProperty(Document document, Name propertyName)
           
protected  void incrementBinaryReferenceCount(BinaryKey binaryKey, Set<BinaryKey> unusedBinaryKeys)
          Increment the reference count for the stored binary value with the supplied SHA-1 hash.
protected  long insertChildren(EditableDocument document, Map<NodeKey,SessionNode.Insertions> insertionsByBeforeKey, Set<NodeKey> removals, Map<NodeKey,Name> newNames)
           
 boolean isLocked(EditableDocument doc)
          Checks if the given document is already locked
protected  String keyForBinaryReferenceDocument(String sha1)
           
protected  String mergeChildren(NodeKey key, EditableDocument document, EditableArray children, boolean isFirst, String nextBlock)
          Modify the supplied document (with the given key) to merge in all of the children from the next block.
protected  void optimizeChildrenBlocks(NodeKey key, EditableDocument document, int targetCountPerBlock, int tolerance)
           Note that this method changes the underlying db as well as the given document, so *it must* be called either from a transactional context or it must be followed by a session.save call, otherwise there might be inconsistencies between what a session sees as "persisted" state and the reality.
protected  Property propertyFor(Name propertyName, Object fieldValue)
           
 Property removeProperty(EditableDocument document, Name propertyName, Set<BinaryKey> unusedBinaryKeys)
           
 void removePropertyValues(EditableDocument document, Name propertyName, Collection<?> values, Set<BinaryKey> unusedBinaryKeys)
           
protected  Object resolveLargeValue(String sha1)
           
protected  List<Path.Segment> segmentsFrom(List<?> segmentValues)
           
 void setKey(EditableDocument document, NodeKey key)
           
 void setParents(EditableDocument document, NodeKey parent, NodeKey oldParent, SessionNode.ChangedAdditionalParents additionalParents)
           
 void setProperty(EditableDocument document, Property property, Set<BinaryKey> unusedBinaryKeys)
           
protected  boolean splitChildren(NodeKey key, EditableDocument document, EditableArray children, int targetCountPerBlock, int tolerance, boolean isFirst, String nextBlock)
          Split the children in the given document (with the given key) into two or more blocks, based upon the specified number of desired children per block and a tolerance.
protected  Object valueFromDocument(Object value)
           
protected  Object valueToDocument(Object value, Set<BinaryKey> unusedBinaryKeys)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SHA1

public static final String SHA1
See Also:
Constant Field Values

PARENT

public static final String PARENT
See Also:
Constant Field Values

LARGE_VALUE

public static final String LARGE_VALUE
See Also:
Constant Field Values

PROPERTIES

public static final String PROPERTIES
See Also:
Constant Field Values

CHILDREN

public static final String CHILDREN
See Also:
Constant Field Values

CHILDREN_INFO

public static final String CHILDREN_INFO
See Also:
Constant Field Values

COUNT

public static final String COUNT
See Also:
Constant Field Values

BLOCK_SIZE

public static final String BLOCK_SIZE
See Also:
Constant Field Values

NEXT_BLOCK

public static final String NEXT_BLOCK
See Also:
Constant Field Values

LAST_BLOCK

public static final String LAST_BLOCK
See Also:
Constant Field Values

NAME

public static final String NAME
See Also:
Constant Field Values

KEY

public static final String KEY
See Also:
Constant Field Values

REFERRERS

public static final String REFERRERS
See Also:
Constant Field Values

WEAK

public static final String WEAK
See Also:
Constant Field Values

STRONG

public static final String STRONG
See Also:
Constant Field Values

REFERENCE_COUNT

public static final String REFERENCE_COUNT
See Also:
Constant Field Values
Constructor Detail

DocumentTranslator

public DocumentTranslator(ExecutionContext context,
                          SchematicDb database,
                          long largeStringSize)
Method Detail

getParentKey

public NodeKey getParentKey(Document document,
                            String primaryWorkspaceKey,
                            String secondaryWorkspaceKey)
Obtain the preferred key for the parent of this node. Because a node can be used in more than once place, it may technically have more than one parent. Therefore, in such cases this method prefers the parent that is in the primaryWorkspaceKey and, if there is no such parent, the parent that is in the secondaryWorkspaceKey.

Parameters:
document - the document for the node; may not be null
primaryWorkspaceKey - the key for the workspace in which the parent should preferrably exist; may be null
secondaryWorkspaceKey - the key for the workspace in which the parent should exist if not in the primary workspace; may be null
Returns:
the key representing the preferred parent, or null if the document contains no parent reference or if the parent reference(s) do not have the specified workspace keys

getParentKeys

public Set<NodeKey> getParentKeys(Document document,
                                  String primaryWorkspaceKey,
                                  String secondaryWorkspaceKey)

getProperties

public void getProperties(Document document,
                          Map<Name,Property> result)

countProperties

public int countProperties(Document document)

hasProperties

public boolean hasProperties(Document document)

hasProperty

public boolean hasProperty(Document document,
                           Name propertyName)

getProperty

public Property getProperty(Document document,
                            Name propertyName)

propertyFor

protected Property propertyFor(Name propertyName,
                               Object fieldValue)

setProperty

public void setProperty(EditableDocument document,
                        Property property,
                        Set<BinaryKey> unusedBinaryKeys)

removeProperty

public Property removeProperty(EditableDocument document,
                               Name propertyName,
                               Set<BinaryKey> unusedBinaryKeys)

addPropertyValues

public void addPropertyValues(EditableDocument document,
                              Name propertyName,
                              boolean isMultiple,
                              Collection<?> values,
                              Set<BinaryKey> unusedBinaryKeys)

removePropertyValues

public void removePropertyValues(EditableDocument document,
                                 Name propertyName,
                                 Collection<?> values,
                                 Set<BinaryKey> unusedBinaryKeys)

setParents

public void setParents(EditableDocument document,
                       NodeKey parent,
                       NodeKey oldParent,
                       SessionNode.ChangedAdditionalParents additionalParents)

setKey

public void setKey(EditableDocument document,
                   NodeKey key)

changeChildren

public void changeChildren(NodeKey key,
                           EditableDocument document,
                           SessionNode.ChangedChildren changedChildren,
                           ChildReferences appended)

insertChildren

protected long insertChildren(EditableDocument document,
                              Map<NodeKey,SessionNode.Insertions> insertionsByBeforeKey,
                              Set<NodeKey> removals,
                              Map<NodeKey,Name> newNames)

getChildReferences

public ChildReferences getChildReferences(WorkspaceCache cache,
                                          Document document)

getChildReferencesInfo

public DocumentTranslator.ChildReferencesInfo getChildReferencesInfo(Document document)

childReferenceFrom

protected ChildReference childReferenceFrom(Object value)

fromChildReference

public EditableDocument fromChildReference(ChildReference ref)

getReferrers

public Set<NodeKey> getReferrers(Document document,
                                 CachedNode.ReferenceType type)

changeReferrers

public void changeReferrers(EditableDocument document,
                            SessionNode.ReferrerChanges changes)

valueToDocument

protected Object valueToDocument(Object value,
                                 Set<BinaryKey> unusedBinaryKeys)

keyForBinaryReferenceDocument

protected final String keyForBinaryReferenceDocument(String sha1)

incrementBinaryReferenceCount

protected void incrementBinaryReferenceCount(BinaryKey binaryKey,
                                             Set<BinaryKey> unusedBinaryKeys)
Increment the reference count for the stored binary value with the supplied SHA-1 hash.

Parameters:
binaryKey - the key for the binary value; never null
unusedBinaryKeys - the set of binary keys that are considered unused; may be null

decrementBinaryReferenceCount

protected boolean decrementBinaryReferenceCount(Object fieldValue,
                                                Set<BinaryKey> unusedBinaryKeys)
Decrement the reference count for the binary value.

Parameters:
fieldValue - the value in the document that may contain a binary value reference; may be null
unusedBinaryKeys - the set of binary keys that are considered unused; may be null
Returns:
true if the binary value is no longer referenced, or false otherwise

valueFromDocument

protected Object valueFromDocument(Object value)

segmentsFrom

protected List<Path.Segment> segmentsFrom(List<?> segmentValues)

resolveLargeValue

protected Object resolveLargeValue(String sha1)

optimizeChildrenBlocks

protected void optimizeChildrenBlocks(NodeKey key,
                                      EditableDocument document,
                                      int targetCountPerBlock,
                                      int tolerance)

Note that this method changes the underlying db as well as the given document, so *it must* be called either from a transactional context or it must be followed by a session.save call, otherwise there might be inconsistencies between what a session sees as "persisted" state and the reality.

Parameters:
key -
document -
targetCountPerBlock -
tolerance -

splitChildren

protected boolean splitChildren(NodeKey key,
                                EditableDocument document,
                                EditableArray children,
                                int targetCountPerBlock,
                                int tolerance,
                                boolean isFirst,
                                String nextBlock)
Split the children in the given document (with the given key) into two or more blocks, based upon the specified number of desired children per block and a tolerance. This method will create additional blocks and will modify the supplied document (with the smaller number of children and the pointer to the next block).

Note this method returns very quickly if the method determines that there is no work to do.

Note that this method changes the underlying db as well as the given document, so *it must* be called either from a transactional context or it must be followed by a session.save call, otherwise there might be inconsistencies between what a session sees as "persisted" state and the reality.

Parameters:
key - the key for the document whose children are to be split; may not be null
document - the document whose children are to be split; may not be null
children - the children that are to be split; may not be null
targetCountPerBlock - the goal for the number of children in each block; must be positive
tolerance - a tolerance that when added to and subtraced from the targetCountPerBlock gives an acceptable range for the number of children; must be positive but smaller than targetCountPerBlock
isFirst - true if the supplied document is the first node document, or false if it is a block document
nextBlock - the key for the next block of children; may be null if the supplied document is the last document and there is no next block
Returns:
true if the children were split, or false if no changes were made

mergeChildren

protected String mergeChildren(NodeKey key,
                               EditableDocument document,
                               EditableArray children,
                               boolean isFirst,
                               String nextBlock)
Modify the supplied document (with the given key) to merge in all of the children from the next block. If the next block is empty or contains no children, it will be deleted its next block merged. Note that this merging is performed, even if the resulting number of children is considered 'too-large' (as such 'too-large' blocks will be optimized at a subsequent optimization pass).

Note that this method changes the underlying db as well as the given document, so *it must* be called either from a transactional context or it must be followed by a session.save call, otherwise there might be inconsistencies between what a session sees as "persisted" state and the reality.

Parameters:
key - the key for the document whose children are to be merged with the next block; may not be null
document - the document to be modified with the next block's children; may not be null
children - the children into which are to be merged the next block's children; may not be null
isFirst - true if the supplied document is the first node document, or false if it is a block document
nextBlock - the key for the next block of children; may be null if the supplied document is the last document and there is no next block
Returns:
the key for the block of children that is after blocks that are removed; may be null if the supplied document is the last block

isLocked

public boolean isLocked(EditableDocument doc)
Checks if the given document is already locked

Parameters:
doc - the document
Returns:
true if the change was made successfully, or false otherwise

ModeShape Distribution 3.0.0.Beta4

Copyright © 2008-2012 JBoss, a division of Red Hat. All Rights Reserved.