Class UniformSplitTermsWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    STUniformSplitTermsWriter

    public class UniformSplitTermsWriter
    extends FieldsConsumer
    A block-based terms index and dictionary that assigns terms to nearly uniform length blocks. This technique is called Uniform Split.

    The block construction is driven by two parameters, targetNumBlockLines and deltaNumLines. Each block size (number of terms) is targetNumBlockLines+-deltaNumLines. The algorithm computes the minimal distinguishing prefix (MDP) between each term and its previous term (alphabetically ordered). Then it selects in the neighborhood of the targetNumBlockLines, and within the deltaNumLines, the term with the minimal MDP. This term becomes the first term of the next block and its MDP is the block key. This block key is added to the terms dictionary trie.

    We call dictionary the trie structure in memory, and block file the disk file containing the block lines, with one term and its corresponding term state details per line.

    When seeking a term, the dictionary seeks the floor leaf of the trie for the searched term and jumps to the corresponding file pointer in the block file. There, the block terms are scanned for the exact searched term.

    The terms inside a block do not need to share a prefix. Only the block key is used to find the block from the dictionary trie. And the block key is selected because it is the locally smallest MDP. This makes the dictionary trie very compact.

    An interesting property of the Uniform Split technique is the very linear balance between memory usage and lookup performance. By decreasing the target block size, the block scan becomes faster, and since there are more blocks, the dictionary trie memory usage increases. Additionally, small blocks are faster to read from disk. A good sweet spot for the target block size is 32 with delta of 3 (10%) (default values). This can be tuned in the constructor.

    There are additional optimizations:

    • Each block has a header that allows the lookup to jump directly to the middle term with a fast comparison. This reduces the linear scan by 2 for a small disk size increase.
    • Each block term is incrementally encoded according to its previous term. This both reduces the disk size and speeds up the block scan.
    • All term line details (the terms states) are written after all terms. This allows faster term scan without needing to decode the term states.
    • All file pointers are base-encoded. Their value is relative to the block base file pointer (not to the previous file pointer), this allows to read the term state of any term independently.

    Blocks can be compressed or encrypted with an optional BlockEncoder provided in the constructor.

    The block file contains all the term blocks for each field sequentially. It also contains the fields metadata at the end of the file.

    The dictionary file contains the trie (FST bytes) for each field sequentially.

    • Field Detail

      • DEFAULT_TARGET_NUM_BLOCK_LINES

        public static final int DEFAULT_TARGET_NUM_BLOCK_LINES
        Default value for the target block size (number of terms per block).
        See Also:
        Constant Field Values
      • DEFAULT_DELTA_NUM_LINES

        public static final int DEFAULT_DELTA_NUM_LINES
        Default value for the maximum allowed delta variation of the block size (delta of the number of terms per block). The block size will be [target block size]+-[allowed delta].
        See Also:
        Constant Field Values
      • MAX_NUM_BLOCK_LINES

        protected static final int MAX_NUM_BLOCK_LINES
        Upper limit of the block size (maximum number of terms per block).
        See Also:
        Constant Field Values
      • fieldInfos

        protected final FieldInfos fieldInfos
      • maxDoc

        protected final int maxDoc
      • targetNumBlockLines

        protected final int targetNumBlockLines
      • deltaNumLines

        protected final int deltaNumLines
      • dictionaryOutput

        protected final IndexOutput dictionaryOutput
    • Constructor Detail

      • UniformSplitTermsWriter

        public UniformSplitTermsWriter​(PostingsWriterBase postingsWriter,
                                       SegmentWriteState state,
                                       BlockEncoder blockEncoder)
                                throws java.io.IOException
        Parameters:
        blockEncoder - Optional block encoder, may be null if none. It can be used for compression or encryption.
        Throws:
        java.io.IOException
      • UniformSplitTermsWriter

        public UniformSplitTermsWriter​(PostingsWriterBase postingsWriter,
                                       SegmentWriteState state,
                                       int targetNumBlockLines,
                                       int deltaNumLines,
                                       BlockEncoder blockEncoder)
                                throws java.io.IOException
        Parameters:
        blockEncoder - Optional block encoder, may be null if none. It can be used for compression or encryption.
        Throws:
        java.io.IOException
      • UniformSplitTermsWriter

        protected UniformSplitTermsWriter​(PostingsWriterBase postingsWriter,
                                          SegmentWriteState state,
                                          int targetNumBlockLines,
                                          int deltaNumLines,
                                          BlockEncoder blockEncoder,
                                          java.lang.String codecName,
                                          int versionCurrent,
                                          java.lang.String termsBlocksExtension,
                                          java.lang.String dictionaryExtension)
                                   throws java.io.IOException
        Parameters:
        targetNumBlockLines - Target number of lines per block. Must be strictly greater than 0. The parameters can be pre-validated with validateSettings(int, int). There is one term per block line, with its corresponding details (TermState).
        deltaNumLines - Maximum allowed delta variation of the number of lines per block. Must be greater than or equal to 0 and strictly less than targetNumBlockLines. The block size will be targetNumBlockLines+-deltaNumLines. The block size must always be less than or equal to MAX_NUM_BLOCK_LINES.
        blockEncoder - Optional block encoder, may be null if none. It can be used for compression or encryption.
        Throws:
        java.io.IOException
    • Method Detail

      • validateSettings

        protected static void validateSettings​(int targetNumBlockLines,
                                               int deltaNumLines)
        Validates the constructor settings.
        Parameters:
        targetNumBlockLines - Target number of lines per block. Must be strictly greater than 0.
        deltaNumLines - Maximum allowed delta variation of the number of lines per block. Must be greater than or equal to 0 and strictly less than targetNumBlockLines. Additionally, targetNumBlockLines + deltaNumLines must be less than or equal to MAX_NUM_BLOCK_LINES.
      • write

        public void write​(Fields fields,
                          NormsProducer normsProducer)
                   throws java.io.IOException
        Description copied from class: FieldsConsumer
        Write all fields, terms and postings. This the "pull" API, allowing you to iterate more than once over the postings, somewhat analogous to using a DOM API to traverse an XML tree.

        Notes:

        • You must compute index statistics, including each Term's docFreq and totalTermFreq, as well as the summary sumTotalTermFreq, sumTotalDocFreq and docCount.
        • You must skip terms that have no docs and fields that have no terms, even though the provided Fields API will expose them; this typically requires lazily writing the field or term until you've actually seen the first term or document.
        • The provided Fields instance is limited: you cannot call any methods that return statistics/counts; you cannot pass a non-null live docs when pulling docs/positions enums.
        Specified by:
        write in class FieldsConsumer
        Throws:
        java.io.IOException
      • writeFieldsMetadata

        protected void writeFieldsMetadata​(int fieldsNumber,
                                           ByteBuffersDataOutput fieldsOutput)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • writeFieldTerms

        protected int writeFieldTerms​(BlockWriter blockWriter,
                                      DataOutput fieldsOutput,
                                      TermsEnum termsEnum,
                                      FieldInfo fieldInfo,
                                      NormsProducer normsProducer)
                               throws java.io.IOException
        Returns:
        1 if the field was written; 0 otherwise.
        Throws:
        java.io.IOException
      • writeDictionary

        protected void writeDictionary​(IndexDictionary.Builder dictionaryBuilder)
                                throws java.io.IOException
        Writes the dictionary index (FST) to disk.
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class FieldsConsumer
        Throws:
        java.io.IOException