org.apache.lucene.util

Class ByteBlockPool



  • public final class ByteBlockPool
    extends Object
    Class that Posting and PostingVector use to write byte streams into shared fixed-size byte[] arrays. The idea is to allocate slices of increasing lengths For example, the first slice is 5 bytes, the next slice is 14, etc. We start by writing our bytes into the first 5 bytes. When we hit the end of the slice, we allocate the next slice and then write the address of the new slice into the last 4 bytes of the previous slice (the "forwarding address"). Each slice is filled with 0's initially, and we mark the end with a non-zero byte. This way the methods that are writing into the slice don't need to record its length and instead allocate a new slice once they hit a non-zero byte.
    • Method Detail

      • reset

        public void reset(boolean zeroFillBuffers,
                          boolean reuseFirst)
        Expert: Resets the pool to its initial state reusing the first buffer. Calling nextBuffer() is not needed after reset.
        Parameters:
        zeroFillBuffers - if true the buffers are filled with 0. This should be set to true if this pool is used with slices.
        reuseFirst - if true the first buffer will be reused and calling nextBuffer() is not needed after reset iff the block pool was used before ie. nextBuffer() was called before.
      • nextBuffer

        public void nextBuffer()
        Advances the pool to its next buffer. This method should be called once after the constructor to initialize the pool. In contrast to the constructor a reset() call will advance the pool to its first buffer immediately.
      • allocSlice

        public int allocSlice(byte[] slice,
                              int upto)
        Creates a new byte slice with the given starting size and returns the slices offset in the pool.
      • append

        public void append(BytesRef bytes)
        Appends the bytes in the provided BytesRef at the current position.
      • readBytes

        public void readBytes(long offset,
                              byte[] bytes,
                              int off,
                              int length)
        Reads bytes bytes out of the pool starting at the given offset with the given length into the given byte array at offset off.

        Note: this method allows to copy across block boundaries.

      • setRawBytesRef

        public void setRawBytesRef(BytesRef ref,
                                   long offset)
        Set the given BytesRef so that its content is equal to the ref.length bytes starting at offset. Most of the time this method will set pointers to internal data-structures. However, in case a value crosses a boundary, a fresh copy will be returned. On the contrary to setBytesRef(BytesRef, int), this does not expect the length to be encoded with the data.
      • readByte

        public byte readByte(long offset)
        Read a single byte at the given offset.