org.modeshape.jcr.cache.document

## Class DocumentOptimizer

- java.lang.Object
- - org.modeshape.jcr.cache.document.DocumentOptimizer

- All Implemented Interfaces:
    DocumentConstants

      public class DocumentOptimizer
      extends Object
      implements DocumentConstants

  A component that can optimize the document for a node.

- - ### Field Summary

    - ### Fields inherited from interface org.modeshape.jcr.cache.document.DocumentConstants

      `BLOCK_SIZE, CACHE_TTL_SECONDS, CHILDREN, CHILDREN_INFO, COUNT, EMBEDDED_DOCUMENTS, EXTERNAL_BINARY_ID_FIELD, FEDERATED_SEGMENTS, KEY, LARGE_VALUE, LAST_BLOCK, LENGTH, LENGTH_FIELD, NAME, NEXT_BLOCK, PARENT, PROPERTIES, QUERYABLE_FIELD, REFERENCE_COUNT, REFERENCE_FIELD, REFERRERS, SHA1, SHA1_FIELD, SIMPLE_REFERENCE_FIELD, SOURCE_NAME_FIELD, STRONG, WEAK, WEAK_REFERENCE_FIELD`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `DocumentOptimizer(Cache<String,SchematicEntry> store)`
    `DocumentOptimizer(DocumentStore documentStore)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected SchematicEntry` | `lookup(String key)`
    `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.
    `boolean` | `optimizeChildrenBlocks(NodeKey key, EditableDocument document, int targetCountPerBlock, int tolerance)` Optimize the children in the supplied node document 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 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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### DocumentOptimizer

          public DocumentOptimizer(DocumentStore documentStore)

    - #### DocumentOptimizer

          public DocumentOptimizer(Cache<String,SchematicEntry> store)

  - ### Method Detail

    - #### optimizeChildrenBlocks

          public boolean optimizeChildrenBlocks(NodeKey key,
                                       EditableDocument document,
                                       int targetCountPerBlock,
                                       int tolerance)

      Optimize the children in the supplied node document

      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 node
        `document` - the node's document representation that is to be optimized
        `targetCountPerBlock` - the target number of children per block
        `tolerance` - the allowed tolerance between the target and actual number of children per block
      Returns:
        true if the document was changed, or false otherwise

    - #### lookup

          protected SchematicEntry lookup(String key)

    - #### 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

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