org.apache.ignite.internal.processors.cache.persistence.wal

## Class SegmentedRingByteBuffer

- java.lang.Object
- - org.apache.ignite.internal.processors.cache.persistence.wal.SegmentedRingByteBuffer

    public class SegmentedRingByteBuffer
    extends Object

Segmented ring byte buffer that represents multi producer/single consumer queue that can be used by multiple writer threads and one reader thread.

### Nested Class Summary

Nested Classes
`static class` `SegmentedRingByteBuffer.BufferMode` Buffer mode.
`class` `SegmentedRingByteBuffer.ReadSegment` Segment available for data reading.
`class` `SegmentedRingByteBuffer.WriteSegment` Segment available for data writing.

### Field Summary

Fields
`ByteBuffer` `buf` Buffer.

### Constructor Summary

Constructors
Constructor and Description
`SegmentedRingByteBuffer(int cap, long maxSegmentSize, SegmentedRingByteBuffer.BufferMode mode)` Creates ring buffer with given capacity.
`SegmentedRingByteBuffer(int cap, long maxSegmentSize, SegmentedRingByteBuffer.BufferMode mode, DataStorageMetricsImpl metrics)` Creates ring buffer with given capacity.
`SegmentedRingByteBuffer(MappedByteBuffer buf, DataStorageMetricsImpl metrics)` Creates ring buffer with given capacity which mapped to file.

### Method Summary

All Methods Instance Methods Concrete Methods
`void` `close()` Closes the buffer.
`void` `free()` Frees allocated memory in case of direct byte buffer.
`void` `init(long pos)` Performs initialization of ring buffer state.
`SegmentedRingByteBuffer.BufferMode` `mode()` Returns buffer mode.
`SegmentedRingByteBuffer.WriteSegment` `offer(int size)` Reserves `size` bytes in `SegmentedRingByteBuffer` and returns instance of `SegmentedRingByteBuffer.WriteSegment` class that points to wrapped `ByteBuffer` instance with corresponding capacity.
`SegmentedRingByteBuffer.WriteSegment` `offerSafe(int size)` Behaves like `offer(int)` but in safe manner: there are no any concurrent threads and buffer in closed state.
`List<SegmentedRingByteBuffer.ReadSegment>` `poll()` Retrieves list of `SegmentedRingByteBuffer.ReadSegment` instances that point to `ByteBuffer` that contains all data available for reading from `SegmentedRingByteBuffer` or `null` if there are no available data for reading.
`List<SegmentedRingByteBuffer.ReadSegment>` `poll(long pos)` Retrieves list of `SegmentedRingByteBuffer.ReadSegment` instances that point to `ByteBuffer` that contains data available for reading from `SegmentedRingByteBuffer` limited by `pos` parameter or `null` if there are no available data for reading.
`SegmentedRingByteBuffer` `reset()` Resets the state of the buffer and returns new instance but with the same underlying buffer.
`long` `tail()` Returns actual buffer tail.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### buf

    public final ByteBuffer buf

Buffer.

### Constructor Detail

#### SegmentedRingByteBuffer

    public SegmentedRingByteBuffer(int cap,
                                   long maxSegmentSize,
                                   SegmentedRingByteBuffer.BufferMode mode)

Creates ring buffer with given capacity.

Parameters:
  `cap` - Buffer's capacity.
  `maxSegmentSize` - Max segment size.
  `mode` - Buffer mode.

#### SegmentedRingByteBuffer

    public SegmentedRingByteBuffer(int cap,
                                   long maxSegmentSize,
                                   SegmentedRingByteBuffer.BufferMode mode,
                                   DataStorageMetricsImpl metrics)

Creates ring buffer with given capacity.

Parameters:
  `cap` - Buffer's capacity.
  `maxSegmentSize` - Max segment size.
  `mode` - Buffer mode.
  `metrics` - Metrics.

#### SegmentedRingByteBuffer

    public SegmentedRingByteBuffer(MappedByteBuffer buf,
                                   DataStorageMetricsImpl metrics)

Creates ring buffer with given capacity which mapped to file.

Parameters:
  `buf` - `MappedByteBuffer` instance.
  `metrics` - Metrics.

### Method Detail

#### init

    public void init(long pos)

Performs initialization of ring buffer state.

Parameters:
  `pos` - Position.

#### mode

    public SegmentedRingByteBuffer.BufferMode mode()

Returns buffer mode.

Returns:
  Buffer mode.

#### tail

    public long tail()

Returns actual buffer tail.

Returns:
  Buffer tail.

#### offer

    public SegmentedRingByteBuffer.WriteSegment offer(int size)

Reserves `size` bytes in `SegmentedRingByteBuffer` and returns instance of `SegmentedRingByteBuffer.WriteSegment` class that points to wrapped `ByteBuffer` instance with corresponding capacity. This `ByteBuffer` instance should be used only for data writing to `SegmentedRingByteBuffer`.

Returned result can be `null` in case of requested amount of bytes greater then available space in `SegmentedRingByteBuffer`. Also `SegmentedRingByteBuffer.WriteSegment.buffer()` can return `null` in case of `maxSegmentSize` value is exceeded. In this case buffer will be closed in order to prevent any concurrent threads from trying of reserve new segment.

This method can be invoked by many producer threads and each producer will get own `ByteBuffer` instance that mapped to own `SegmentedRingByteBuffer` slice.

Once the data has been written into the `ByteBuffer` client code must notify `SegmentedRingByteBuffer` instance using `SegmentedRingByteBuffer.WriteSegment.release()` method in order to provide possibility to consumer get data for reading.

Parameters:
  `size` - Amount of bytes for reserve.
Returns:
  `SegmentedRingByteBuffer.WriteSegment` instance that point to `ByteBuffer` instance with given `size`. `null` if buffer space is not enough.

#### offerSafe

    public SegmentedRingByteBuffer.WriteSegment offerSafe(int size)

Behaves like `offer(int)` but in safe manner: there are no any concurrent threads and buffer in closed state.

Parameters:
  `size` - Amount of bytes for reserve.
Returns:
  `SegmentedRingByteBuffer.WriteSegment` instance that point to `ByteBuffer` instance with given `size`. `null` if buffer space is not enough.

#### close

    public void close()

Closes the buffer.

#### poll

    public List<SegmentedRingByteBuffer.ReadSegment> poll()

Retrieves list of `SegmentedRingByteBuffer.ReadSegment` instances that point to `ByteBuffer` that contains all data available for reading from `SegmentedRingByteBuffer` or `null` if there are no available data for reading.

This method can be invoked only by one consumer thread.

Once the data has been read from the returned `SegmentedRingByteBuffer.ReadSegment` client code must notify `SegmentedRingByteBuffer` instance using `SegmentedRingByteBuffer.ReadSegment.release()` method in order to release occupied space in the `SegmentedRingByteBuffer` and make it available for writing.

Returns:
  List of `ReadSegment` instances with all available data for reading or `null` if there are no available data.

#### poll

    public List<SegmentedRingByteBuffer.ReadSegment> poll(long pos)

Retrieves list of `SegmentedRingByteBuffer.ReadSegment` instances that point to `ByteBuffer` that contains data available for reading from `SegmentedRingByteBuffer` limited by `pos` parameter or `null` if there are no available data for reading.

This method can be invoked only by one consumer thread.

Once the data has been read from the returned `SegmentedRingByteBuffer.ReadSegment` client code must notify `SegmentedRingByteBuffer` instance using `SegmentedRingByteBuffer.ReadSegment.release()` method in order to release occupied space in the `SegmentedRingByteBuffer` and make it available for writing.

Parameters:
  `pos` - End position in buffer.
Returns:
  List of `ReadSegment` instances with all available data for reading or `null` if there are no available data.

#### free

    public void free()

Frees allocated memory in case of direct byte buffer.

#### reset

    public SegmentedRingByteBuffer reset()

Resets the state of the buffer and returns new instance but with the same underlying buffer.

${current.year} Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Database and Caching Platform | : ver. 2.11.1
Release Date | : December 20 2021