Package org.apache.lucene.util
Class ByteBlockPool
- java.lang.Object
-
- org.apache.lucene.util.ByteBlockPool
-
public final class ByteBlockPool extends java.lang.ObjectClass 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classByteBlockPool.AllocatorAbstract class for allocating and freeing byte blocks.static classByteBlockPool.DirectAllocatorA simpleByteBlockPool.Allocatorthat never recycles.static classByteBlockPool.DirectTrackingAllocatorA simpleByteBlockPool.Allocatorthat never recycles, but tracks how much total RAM is in use.
-
Field Summary
Fields Modifier and Type Field Description byte[]bufferCurrent head bufferbyte[][]buffersarray of buffers currently used in the pool.static intBYTE_BLOCK_MASKstatic intBYTE_BLOCK_SHIFTstatic intBYTE_BLOCK_SIZEintbyteOffsetCurrent head offsetintbyteUptoWhere we are in head bufferstatic intFIRST_LEVEL_SIZEThe first level size for new slicesstatic int[]LEVEL_SIZE_ARRAYAn array holding the level sizes for byte slices.static int[]NEXT_LEVEL_ARRAYAn array holding the offset into theLEVEL_SIZE_ARRAYto quickly navigate to the next slice level.
-
Constructor Summary
Constructors Constructor Description ByteBlockPool(ByteBlockPool.Allocator allocator)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intallocSlice(byte[] slice, int upto)Creates a new byte slice with the given starting size and returns the slices offset in the pool.voidappend(BytesRef bytes)Appends the bytes in the providedBytesRefat the current position.intnewSlice(int size)Allocates a new slice with the given size.voidnextBuffer()Advances the pool to its next buffer.voidreadBytes(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.voidreset()Resets the pool to its initial state reusing the first buffer and fills all buffers with 0 bytes before they reused or passed toByteBlockPool.Allocator.recycleByteBlocks(byte[][], int, int).voidreset(boolean zeroFillBuffers, boolean reuseFirst)Expert: Resets the pool to its initial state reusing the first buffer.voidsetBytesRef(BytesRef term, int textStart)
-
-
-
Field Detail
-
BYTE_BLOCK_SHIFT
public static final int BYTE_BLOCK_SHIFT
- See Also:
- Constant Field Values
-
BYTE_BLOCK_SIZE
public static final int BYTE_BLOCK_SIZE
- See Also:
- Constant Field Values
-
BYTE_BLOCK_MASK
public static final int BYTE_BLOCK_MASK
- See Also:
- Constant Field Values
-
buffers
public byte[][] buffers
array of buffers currently used in the pool. Buffers are allocated if needed don't modify this outside of this class.
-
byteUpto
public int byteUpto
Where we are in head buffer
-
buffer
public byte[] buffer
Current head buffer
-
byteOffset
public int byteOffset
Current head offset
-
NEXT_LEVEL_ARRAY
public static final int[] NEXT_LEVEL_ARRAY
An array holding the offset into theLEVEL_SIZE_ARRAYto quickly navigate to the next slice level.
-
LEVEL_SIZE_ARRAY
public static final int[] LEVEL_SIZE_ARRAY
An array holding the level sizes for byte slices.
-
FIRST_LEVEL_SIZE
public static final int FIRST_LEVEL_SIZE
The first level size for new slices- See Also:
newSlice(int)
-
-
Constructor Detail
-
ByteBlockPool
public ByteBlockPool(ByteBlockPool.Allocator allocator)
-
-
Method Detail
-
reset
public void reset()
Resets the pool to its initial state reusing the first buffer and fills all buffers with 0 bytes before they reused or passed toByteBlockPool.Allocator.recycleByteBlocks(byte[][], int, int). CallingnextBuffer()is not needed after reset.
-
reset
public void reset(boolean zeroFillBuffers, boolean reuseFirst)Expert: Resets the pool to its initial state reusing the first buffer. CallingnextBuffer()is not needed after reset.- Parameters:
zeroFillBuffers- iftruethe buffers are filled with 0. This should be set totrueif this pool is used with slices.reuseFirst- iftruethe first buffer will be reused and callingnextBuffer()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 areset()call will advance the pool to its first buffer immediately.
-
newSlice
public int newSlice(int size)
Allocates a new slice with the given size.- See Also:
FIRST_LEVEL_SIZE
-
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.
-
setBytesRef
public void setBytesRef(BytesRef term, int textStart)
-
append
public void append(BytesRef bytes)
Appends the bytes in the providedBytesRefat 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.
-
-