ModeShape Distribution 3.2.0.Final

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 CACHE_TTL_SECONDS
          A constant that can be used by a connector implementation as a supplementary document field, that indicates the maximum number of seconds that particular document should be stored in the workspace cache.
static String CHILDREN
           
static String CHILDREN_INFO
           
static String COUNT
           
static String EMBEDDED_DOCUMENTS
          A constant that is used as the name for a nested document in which additional, embedded documents can be placed.
static String EXTERNAL_BINARY_ID_FIELD
           
static String FEDERATED_SEGMENTS
           
static String KEY
           
static String LARGE_VALUE
           
static String LAST_BLOCK
           
static String LENGTH
           
static String LENGTH_FIELD
           
static String NAME
           
static String NEXT_BLOCK
           
static String PARENT
           
static String PROPERTIES
           
static String QUERYABLE_FIELD
           
static String REFERENCE_COUNT
           
static String REFERRERS
           
static String SHA1
           
static String SHA1_FIELD
           
static String SOURCE_NAME_FIELD
           
static String STRONG
           
static String WEAK
           
 
Constructor Summary
DocumentTranslator(ExecutionContext context, DocumentStore documentStore, long largeStringSize)
           
 
Method Summary
protected  void addFederatedSegment(EditableDocument document, String documentKey, String sourceName, String externalPath, String alias)
          Given an existing document adds a new federated segment with the given alias pointing to the external document located at externalPath
 void addPropertyValues(EditableDocument document, Name propertyName, boolean isMultiple, Collection<?> values, Set<BinaryKey> unusedBinaryKeys)
           
 void changeChildren(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)
           
protected  Integer getCacheTtlSeconds(Document document)
          Returns the value of the CACHE_TTL_SECONDS field, if such a value exists.
 ChildReferences getChildReferences(WorkspaceCache cache, Document document)
           
 ChildReferences getChildReferencesFromBlock(Document block)
          Reads the children of the given block and returns a ChildReferences instance.
 DocumentTranslator.ChildReferencesInfo getChildReferencesInfo(Document document)
           
 String getKey(Document document)
           
 Set<String> getMixinTypeNames(Document document)
           
 Set<Name> getMixinTypes(Document document)
           
 NameFactory getNameFactory()
           
 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)
           
 Name getPrimaryType(Document document)
           
 String getPrimaryTypeName(Document document)
           
 void getProperties(Document document, Map<Name,Property> result)
           
 Property getProperty(Document document, Name propertyName)
           
 Property getProperty(Document document, String propertyName)
           
 PropertyFactory getPropertyFactory()
           
 ReferenceFactory getReferenceFactory()
           
 Set<NodeKey> getReferrers(Document document, CachedNode.ReferenceType type)
           
 ValueFactory<String> getStringFactory()
           
 ReferenceFactory getWeakReferenceFactory()
           
 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)
           
protected  boolean isFederatedDocument(Document document)
           
 boolean isLocked(EditableDocument doc)
          Checks if the given document is already locked
protected  boolean isQueryable(Document document)
           
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)
           
protected  void removeFederatedSegments(EditableDocument federatedDocument, Set<String> externalNodeKeys)
           
protected  void removeFederatedSegments(EditableDocument federatedDocument, String... externalNodeKeys)
           
 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 setKey(EditableDocument document, String key)
           
 void setParents(EditableDocument document, NodeKey parent, NodeKey oldParent, SessionNode.ChangedAdditionalParents additionalParents)
           
 void setProperty(EditableDocument document, Property property, Set<BinaryKey> unusedBinaryKeys)
           
 void setQueryable(EditableDocument document, boolean queryable)
          Marks the given document as queryable, by setting a flag.
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.
 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

EXTERNAL_BINARY_ID_FIELD

public static final String EXTERNAL_BINARY_ID_FIELD
See Also:
Constant Field Values

SOURCE_NAME_FIELD

public static final String SOURCE_NAME_FIELD
See Also:
Constant Field Values

SHA1_FIELD

public static final String SHA1_FIELD
See Also:
Constant Field Values

LENGTH

public static final String LENGTH
See Also:
Constant Field Values

LENGTH_FIELD

public static final String LENGTH_FIELD
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

FEDERATED_SEGMENTS

public static final String FEDERATED_SEGMENTS
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

QUERYABLE_FIELD

public static final String QUERYABLE_FIELD
See Also:
Constant Field Values

EMBEDDED_DOCUMENTS

public static final String EMBEDDED_DOCUMENTS
A constant that is used as the name for a nested document in which additional, embedded documents can be placed. Each of these documents represents a separate node and will be automatically extracted from the containing document prior to usage.

See Also:
Constant Field Values

CACHE_TTL_SECONDS

public static final String CACHE_TTL_SECONDS
A constant that can be used by a connector implementation as a supplementary document field, that indicates the maximum number of seconds that particular document should be stored in the workspace cache.

See Also:
Constant Field Values
Constructor Detail

DocumentTranslator

public DocumentTranslator(ExecutionContext context,
                          DocumentStore documentStore,
                          long largeStringSize)
Method Detail

getStringFactory

public final ValueFactory<String> getStringFactory()

getNameFactory

public final NameFactory getNameFactory()

getReferenceFactory

public final ReferenceFactory getReferenceFactory()

getWeakReferenceFactory

public final ReferenceFactory getWeakReferenceFactory()

getPropertyFactory

public final PropertyFactory getPropertyFactory()

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 preferably 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,
                            String propertyName)

getProperty

public Property getProperty(Document document,
                            Name propertyName)

getPrimaryType

public Name getPrimaryType(Document document)

getPrimaryTypeName

public String getPrimaryTypeName(Document document)

getMixinTypes

public Set<Name> getMixinTypes(Document document)

getMixinTypeNames

public Set<String> getMixinTypeNames(Document document)

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)

setKey

public void setKey(EditableDocument document,
                   String key)

getKey

public String getKey(Document document)

changeChildren

public void changeChildren(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)

getChildReferencesFromBlock

public ChildReferences getChildReferencesFromBlock(Document block)
Reads the children of the given block and returns a ChildReferences instance.

Parameters:
block - a non-null Document representing a block of children
Returns:
a non-null child references instance

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

public 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

isFederatedDocument

protected boolean isFederatedDocument(Document document)

removeFederatedSegments

protected void removeFederatedSegments(EditableDocument federatedDocument,
                                       Set<String> externalNodeKeys)

removeFederatedSegments

protected void removeFederatedSegments(EditableDocument federatedDocument,
                                       String... externalNodeKeys)

isQueryable

protected boolean isQueryable(Document document)

setQueryable

public void setQueryable(EditableDocument document,
                         boolean queryable)
Marks the given document as queryable, by setting a flag.

Parameters:
document - a EditableDocument instance; never null
queryable - a boolean which indicates whether the document should be queryable or not.

addFederatedSegment

protected void addFederatedSegment(EditableDocument document,
                                   String documentKey,
                                   String sourceName,
                                   String externalPath,
                                   String alias)
Given an existing document adds a new federated segment with the given alias pointing to the external document located at externalPath

Parameters:
document - a non-null EditableDocument representing the document of a local node to which the federated segment should be appended.
documentKey - a non-null String representing the key of the document. This is passed from the outside as the document may not have a KEY property (e.g. root node)
sourceName - a non-null string, the name of the source where externalPath will be resolved
externalPath - a non-null string the location in the external source which points to an external node
alias - an optional string representing the name under which the federated segment will be linked. In effect, this represents the name of a child reference. If not present, the externalPath will be used. Note that the name of the federated segment (either coming from externalPath or alias) should not contain the "/" character. If it does, this will be removed.

getCacheTtlSeconds

protected Integer getCacheTtlSeconds(Document document)
Returns the value of the CACHE_TTL_SECONDS field, if such a value exists.

Parameters:
document - a non-null document
Returns:
either the value of the above field, or null if such a value doesn't exist.

ModeShape Distribution 3.2.0.Final

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