Skip to content

Commit 58df786

Browse files
[Java] Add example usage to RingBuffer and tryClaim for zero-copy writes
1 parent fd17a3e commit 58df786

1 file changed

Lines changed: 40 additions & 4 deletions

File tree

agrona/src/main/java/org/agrona/concurrent/ringbuffer/RingBuffer.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
/**
2222
* Ring-buffer for the concurrent exchanging of binary encoded messages from producer(s) to consumer(s)
2323
* in a FIFO manner.
24+
* <p>
25+
* Supports non-blocking writes via {@link #write(int, DirectBuffer, int, int)} and
26+
* zero-copy writes via {@link #tryClaim(int, int)}. Messages are read by consumers
27+
* using one of the {@code read} or {@code controlledRead} methods.
2428
*/
2529
public interface RingBuffer
2630
{
@@ -44,6 +48,25 @@ public interface RingBuffer
4448
/**
4549
* Non-blocking write of a message to an underlying ring-buffer.
4650
*
51+
* <p>
52+
* Example usage:
53+
* <pre>{@code
54+
* // Create a ring buffer with a direct buffer of 1024 bytes
55+
* ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
56+
* RingBuffer ringBuffer = new OneToOneRingBuffer(new UnsafeBuffer(byteBuffer));
57+
*
58+
* // Pre-allocate a reusable buffer (cached for the lifetime of the writer)
59+
* MutableDirectBuffer srcBuffer =
60+
* new UnsafeBuffer(ByteBuffer.allocateDirect(Long.BYTES));
61+
*
62+
* // Write a message using the cached buffer
63+
* srcBuffer.putLong(0, 12345L);
64+
* if (!ringBuffer.write(1, srcBuffer, 0, Long.BYTES))
65+
* {
66+
* // Handle insufficient capacity (e.g., retry, log, or drop message)
67+
* }
68+
* }</pre>
69+
*
4770
* @param msgTypeId type of the message encoding.
4871
* @param srcBuffer containing the encoded binary message.
4972
* @param offset at which the encoded message begins.
@@ -155,11 +178,24 @@ public interface RingBuffer
155178
int read(MessageHandler handler);
156179

157180
/**
158-
* Read as many messages as are available to end of the ring buffer to up a supplied maximum.
181+
* Read as many messages as are available to the end of the ring buffer.
182+
*
159183
* <p>
160-
* If the ring buffer wraps or encounters a type of record, such a padding record, then an implementation
161-
* may choose to return and expect the caller to try again. The {@link #size()} method may be called to
162-
* determine of a backlog of message bytes remains in the ring buffer.
184+
* Example usage:
185+
* <pre>{@code
186+
* ringBuffer.read(
187+
* (msgTypeId, buffer, index, length) ->
188+
* {
189+
* final long value = buffer.getLong(index);
190+
* // Process value
191+
* }
192+
* );
193+
* }</pre>
194+
*
195+
* <p>
196+
* If the ring buffer wraps or encounters a padding record, then an implementation
197+
* may choose to return and expect the caller to try again. The {@link #size()} method
198+
* may be called to determine if a backlog of message bytes remains.
163199
*
164200
* @param handler to be called for processing each message in turn.
165201
* @param messageCountLimit the number of messages will be read in a single invocation.

0 commit comments

Comments
 (0)