Package org.agrona
Class ExpandableRingBuffer
java.lang.Object
org.agrona.ExpandableRingBuffer
Ring-buffer for storing messages which can expand to accommodate the messages written into it. Message are written
and read in a FIFO order with capacity up to
maxCapacity(). Messages can be iterated via for-each methods
without consuming and having the option to begin iteration an offset from the current head() position.
Note: This class is not thread safe.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceConsumers of messages implement this interface and pass it toconsume(MessageConsumer, int). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intAlignment in bytes for the beginning of message header.static final intLength of encapsulating header.static final intMaximum capacity to which the ring buffer can grow which is 1 GB. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new ring buffer which is initially compact and empty, has potential forMAX_CAPACITY, and using a directByteBuffer.ExpandableRingBuffer(int initialCapacity, int maxCapacity, boolean isDirect) Create a new ring buffer providing configuration for initial and max capacity, plus whether it is direct or not. -
Method Summary
Modifier and TypeMethodDescriptionbooleanappend(DirectBuffer srcBuffer, int srcOffset, int srcLength) Append a message into the ring buffer, expanding the buffer if required.intcapacity()Current capacity of the ring buffer in bytes.intconsume(ExpandableRingBuffer.MessageConsumer messageConsumer, int messageLimit) Consume messages up to a limit and pass them to theExpandableRingBuffer.MessageConsumer.intforEach(int headOffset, ExpandableRingBuffer.MessageConsumer messageConsumer, int limit) Iterate encoded contents and pass messages to theExpandableRingBuffer.MessageConsumerwhich can stop by returning false.intforEach(ExpandableRingBuffer.MessageConsumer messageConsumer, int limit) Iterate encoded contents and pass messages to theExpandableRingBuffer.MessageConsumerwhich can stop by returning false.longhead()Head position in the buffer from which bytes are consumed forward toward thetail().booleanisDirect()Is theByteBufferused for backing storage direct, that is off Java heap, or not.booleanisEmpty()Is the ring buffer currently empty.intThe maximum capacity to which the buffer can expand.voidreset(int requiredCapacity) Reset the buffer with a new capacity and empty state.intsize()Size of the ring buffer currently populated in bytes.longtail()Tail position in the buffer at which new bytes are appended.
-
Field Details
-
MAX_CAPACITY
public static final int MAX_CAPACITYMaximum capacity to which the ring buffer can grow which is 1 GB.- See Also:
-
HEADER_ALIGNMENT
public static final int HEADER_ALIGNMENTAlignment in bytes for the beginning of message header.- See Also:
-
HEADER_LENGTH
public static final int HEADER_LENGTHLength of encapsulating header.- See Also:
-
-
Constructor Details
-
ExpandableRingBuffer
public ExpandableRingBuffer()Create a new ring buffer which is initially compact and empty, has potential forMAX_CAPACITY, and using a directByteBuffer. -
ExpandableRingBuffer
public ExpandableRingBuffer(int initialCapacity, int maxCapacity, boolean isDirect) Create a new ring buffer providing configuration for initial and max capacity, plus whether it is direct or not.- Parameters:
initialCapacity- required in the buffer.maxCapacity- the buffer can expand to.isDirect- is theByteBufferallocated direct or heap based.
-
-
Method Details
-
isDirect
public boolean isDirect()Is theByteBufferused for backing storage direct, that is off Java heap, or not.- Returns:
- return true if direct
ByteBufferor false for heap basedByteBuffer.
-
maxCapacity
public int maxCapacity()The maximum capacity to which the buffer can expand.- Returns:
- maximum capacity to which the buffer can expand.
-
capacity
public int capacity()Current capacity of the ring buffer in bytes.- Returns:
- the current capacity of the ring buffer in bytes.
-
size
public int size()Size of the ring buffer currently populated in bytes.- Returns:
- size of the ring buffer currently populated in bytes.
-
isEmpty
public boolean isEmpty()Is the ring buffer currently empty.- Returns:
- true if the ring buffer is empty otherwise false.
-
head
public long head()Head position in the buffer from which bytes are consumed forward toward thetail().- Returns:
- head position in the buffer from which bytes are consumed forward towards the
tail(). - See Also:
-
tail
public long tail()Tail position in the buffer at which new bytes are appended.- Returns:
- tail position in the buffer at which new bytes are appended.
- See Also:
-
reset
public void reset(int requiredCapacity) Reset the buffer with a new capacity and empty state. Buffer capacity will grow or shrink as necessary.- Parameters:
requiredCapacity- for the ring buffer. If the same as exiting capacity then no adjustment is made.
-
forEach
Iterate encoded contents and pass messages to theExpandableRingBuffer.MessageConsumerwhich can stop by returning false.- Parameters:
messageConsumer- to which the encoded messages are passed.limit- for the number of entries to iterate over.- Returns:
- count of bytes iterated.
- See Also:
-
forEach
Iterate encoded contents and pass messages to theExpandableRingBuffer.MessageConsumerwhich can stop by returning false.- Parameters:
headOffset- offset fromheadwhich must be <=tail(), and be the start a message.messageConsumer- to which the encoded messages are passed.limit- for the number of entries to iterate over.- Returns:
- count of bytes iterated.
- See Also:
-
consume
Consume messages up to a limit and pass them to theExpandableRingBuffer.MessageConsumer.- Parameters:
messageConsumer- to which the encoded messages are passed.messageLimit- on the number of messages to consume per read operation.- Returns:
- the number of bytes consumed
- See Also:
-
append
Append a message into the ring buffer, expanding the buffer if required.- Parameters:
srcBuffer- containing the encoded message.srcOffset- within the buffer at which the message begins.srcLength- of the encoded message in the buffer.- Returns:
- true if successful otherwise false if
maxCapacity()is reached.
-