ByteBlockPool

This class enables the allocation of fixed-size buffers and their management as part of a buffer array. Allocation is done through the use of an Allocator which can be customized, e.g. to allow recycling old buffers. There are methods for writing (.append and reading from the buffers (e.g. .readBytes, which handle read/write operations across buffer boundaries.

Constructors

Link copied to clipboard
constructor(allocator: ByteBlockPool.Allocator)

Types

Link copied to clipboard
abstract class Allocator

Abstract class for allocating and freeing byte blocks.

Link copied to clipboard
object Companion
Link copied to clipboard

A simple Allocator that never recycles.

Link copied to clipboard

A simple Allocator that never recycles, but tracks how much total RAM is in use.

Properties

Link copied to clipboard

Current head buffer.

Link copied to clipboard

Offset from the start of the first buffer to the start of the current buffer, which is bufferUpto * BYTE_BLOCK_SIZE. The buffer pool maintains this offset because it is the first to overflow if there are too many allocated blocks.

Link copied to clipboard

Where we are in the head buffer.

Link copied to clipboard

Returns nested resources of this class. The result should be a point-in-time snapshot (to avoid race conditions).

Link copied to clipboard

Functions

Link copied to clipboard
fun append(bytes: BytesRef)

Appends the bytes in the provided BytesRef at the current position.

fun append(bytes: ByteArray, offset: Int = 0, length: Int = bytes.size)

Append the provided byte array at the current position.

fun append(srcPool: ByteBlockPool, srcOffset: Long, length: Int)

Append the bytes from a source ByteBlockPool at a given offset and length

Link copied to clipboard
fun getBuffer(bufferIndex: Int): ByteArray

Retrieve the buffer at the specified index from the buffer pool.

Link copied to clipboard

Allocates a new buffer and advances the pool to it. This method should be called once after the constructor to initialize the pool. In contrast to the constructor, a call will advance the pool to its first buffer immediately.

Link copied to clipboard
open override fun ramBytesUsed(): Long

Return the memory usage of this object in bytes. Negative values are illegal.

Link copied to clipboard
fun readByte(offset: Long): Byte

Read a single byte at the given offset

Link copied to clipboard
fun readBytes(offset: Long, bytes: ByteArray, bytesOffset: Int, bytesLength: Int)

Reads bytes out of the pool starting at the given offset with the given length into the given byte array at offset off.

Link copied to clipboard
fun reset(zeroFillBuffers: Boolean, reuseFirst: Boolean)

Expert: Resets the pool to its initial state, while optionally reusing the first buffer. Buffers that are not reused are reclaimed by Allocator.recycleByteBlocks. Buffers can be filled with zeros before recycling them. This is useful if a slice pool works on top of this byte pool and relies on the buffers being filled with zeros to find the non-zero end of slices.

Link copied to clipboard
fun setBytesRef(builder: BytesRefBuilder, result: BytesRef, offset: Long, length: Int)

Fill the provided BytesRef with the bytes at the specified offset and length. This will avoid copying the bytes if the slice fits into a single block; otherwise, it uses the provided BytesRefBuilder to copy bytes over.