DirectWriter (Lucene 10.5.1 core API)
Description: declaration: package: org.apache.lucene.util.packed, class: DirectWriter

Package org.apache.lucene.util.packed

# Class DirectWriter

java.lang.Object
org.apache.lucene.util.packed.DirectWriter

public final class DirectWriter extends Object
Class for writing packed integers to be directly read from Directory. Integers can be read on-the-fly via `DirectReader`.

Unlike PackedInts, it optimizes for read i/o operations and supports > 2B values. Example usage:

       int bitsPerValue = DirectWriter.bitsRequired(100); // values up to and including 100
       IndexOutput output = dir.createOutput("packed", IOContext.DEFAULT);
       DirectWriter writer = DirectWriter.getInstance(output, numberOfValues, bitsPerValue);
       for (int i = 0; i < numberOfValues; i++) {
         writer.add(value);
       }
       writer.finish();
       output.close();

See Also:
  - `DirectReader`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `add(long l)`
  Adds a value to this writer
  `static int`
  `bitsRequired(long maxValue)`
  Returns how many bits are required to hold values up to and including maxValue
  `static long`
  `bytesRequired(long numValues, int bitsPerValue)`
  Returns how many bytes are written for encoding `numValues` using `bitsPerValue`.
  `void`
  `finish()`
  finishes writing
  `static DirectWriter`
  `getInstance(DataOutput output, long numValues, int bitsPerValue)`
  Returns an instance suitable for encoding `numValues` using `bitsPerValue`
  `static int`
  `unsignedBitsRequired(long maxValue)`
  Returns how many bits are required to hold values up to and including maxValue, interpreted as an unsigned value.

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

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

- ## Method Details

  - ### add

    public void add(long l) throws IOException
    Adds a value to this writer
    Throws:
      `IOException`

  - ### finish

    public void finish() throws IOException
    finishes writing
    Throws:
      `IOException`

  - ### getInstance

    public static DirectWriter getInstance(DataOutput output, long numValues, int bitsPerValue)
    Returns an instance suitable for encoding `numValues` using `bitsPerValue`

  - ### bitsRequired

    public static int bitsRequired(long maxValue)
    Returns how many bits are required to hold values up to and including maxValue
    Parameters:
      `maxValue` - the maximum value that should be representable.
    Returns:
      the amount of bits needed to represent values from 0 to maxValue.
    See Also:
      - `PackedInts.bitsRequired(long)`

  - ### unsignedBitsRequired

    public static int unsignedBitsRequired(long maxValue)
    Returns how many bits are required to hold values up to and including maxValue, interpreted as an unsigned value.
    Parameters:
      `maxValue` - the maximum value that should be representable.
    Returns:
      the amount of bits needed to represent values from 0 to maxValue.
    See Also:
      - `PackedInts.unsignedBitsRequired(long)`

  - ### bytesRequired

    public static long bytesRequired(long numValues, int bitsPerValue)
    Returns how many bytes are written for encoding `numValues` using `bitsPerValue`.
    Parameters:
      `numValues` - total number of values
      `bitsPerValue` - the number of bits required per value
    Returns:
      The amount of bytes written