Class BufferBuilder
- java.lang.Object
-
- org.apache.flink.runtime.io.network.buffer.BufferBuilder
-
- All Implemented Interfaces:
AutoCloseable
@NotThreadSafe public class BufferBuilder extends Object implements AutoCloseable
Not thread safe class for filling in the content of theMemorySegment. To access written data please useBufferConsumerwhich allows to buildBufferinstances from the written data.
-
-
Constructor Summary
Constructors Constructor Description BufferBuilder(org.apache.flink.core.memory.MemorySegment memorySegment, BufferRecycler recycler)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intappend(ByteBuffer source)Append as many data as possible fromsource.intappendAndCommit(ByteBuffer source)Same asappend(ByteBuffer)but additionallycommit()the appending.voidclose()voidcommit()Make the change visible to the readers.BufferConsumercreateBufferConsumer()This method always creates aBufferConsumerstarting from the current writer offset.BufferConsumercreateBufferConsumerFromBeginning()This method always creates aBufferConsumerstarting from position 0 ofMemorySegment.intfinish()Mark thisBufferBuilderand associatedBufferConsumeras finished - no new data writes will be allowed.intgetCommittedBytes()Buffer.DataTypegetDataType()Gets the data type of the internal buffer.intgetMaxCapacity()intgetWritableBytes()booleanisFinished()booleanisFull()voidsetDataType(Buffer.DataType dataType)Sets the data type of the internal buffer.voidtrim(int newSize)The result capacity can not be greater than allocated memorySegment.
-
-
-
Constructor Detail
-
BufferBuilder
public BufferBuilder(org.apache.flink.core.memory.MemorySegment memorySegment, BufferRecycler recycler)
-
-
Method Detail
-
createBufferConsumer
public BufferConsumer createBufferConsumer()
This method always creates aBufferConsumerstarting from the current writer offset. Data written toBufferBuilderbefore creation ofBufferConsumerwon't be visible for thatBufferConsumer.- Returns:
- created matching instance of
BufferConsumerto thisBufferBuilder.
-
createBufferConsumerFromBeginning
public BufferConsumer createBufferConsumerFromBeginning()
This method always creates aBufferConsumerstarting from position 0 ofMemorySegment.- Returns:
- created matching instance of
BufferConsumerto thisBufferBuilder.
-
getDataType
public Buffer.DataType getDataType()
Gets the data type of the internal buffer.
-
setDataType
public void setDataType(Buffer.DataType dataType)
Sets the data type of the internal buffer.
-
appendAndCommit
public int appendAndCommit(ByteBuffer source)
Same asappend(ByteBuffer)but additionallycommit()the appending.
-
append
public int append(ByteBuffer source)
Append as many data as possible fromsource. Not everything might be copied if there is not enough space in the underlyingMemorySegment- Returns:
- number of copied bytes
-
commit
public void commit()
Make the change visible to the readers. This is costly operation (volatile access) thus in case of bulk writes it's better to commit them all together instead one by one.
-
finish
public int finish()
Mark thisBufferBuilderand associatedBufferConsumeras finished - no new data writes will be allowed.This method should be idempotent to handle failures and task interruptions. Check FLINK-8948 for more details.
- Returns:
- number of written bytes.
-
isFinished
public boolean isFinished()
-
isFull
public boolean isFull()
-
getWritableBytes
public int getWritableBytes()
-
getCommittedBytes
public int getCommittedBytes()
-
getMaxCapacity
public int getMaxCapacity()
-
trim
public void trim(int newSize)
The result capacity can not be greater than allocated memorySegment. It also can not be less than already written data.
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable
-
-