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 Modifier and Type Class Description static classSegmentedRingByteBuffer.BufferModeBuffer mode.classSegmentedRingByteBuffer.ReadSegmentSegment available for data reading.classSegmentedRingByteBuffer.WriteSegmentSegment available for data writing.
-
Field Summary
Fields Modifier and Type Field Description ByteBufferbufBuffer.
-
Constructor Summary
Constructors Constructor 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 Modifier and Type Method Description voidclose()Closes the buffer.voidfree()Frees allocated memory in case of direct byte buffer.voidinit(long pos)Performs initialization of ring buffer state.SegmentedRingByteBuffer.BufferModemode()Returns buffer mode.SegmentedRingByteBuffer.WriteSegmentoffer(int size)Reservessizebytes inSegmentedRingByteBufferand returns instance ofSegmentedRingByteBuffer.WriteSegmentclass that points to wrappedByteBufferinstance with corresponding capacity.SegmentedRingByteBuffer.WriteSegmentofferSafe(int size)Behaves likeoffer(int)but in safe manner: there are no any concurrent threads and buffer in closed state.List<SegmentedRingByteBuffer.ReadSegment>poll()Retrieves list ofSegmentedRingByteBuffer.ReadSegmentinstances that point toByteBufferthat contains all data available for reading fromSegmentedRingByteBufferornullif there are no available data for reading.List<SegmentedRingByteBuffer.ReadSegment>poll(long pos)Retrieves list ofSegmentedRingByteBuffer.ReadSegmentinstances that point toByteBufferthat contains data available for reading fromSegmentedRingByteBufferlimited byposparameter ornullif there are no available data for reading.SegmentedRingByteBufferreset()Resets the state of the buffer and returns new instance but with the same underlying buffer.longtail()Returns actual buffer tail.
-
-
-
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-MappedByteBufferinstance.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)
Reservessizebytes inSegmentedRingByteBufferand returns instance ofSegmentedRingByteBuffer.WriteSegmentclass that points to wrappedByteBufferinstance with corresponding capacity. ThisByteBufferinstance should be used only for data writing toSegmentedRingByteBuffer.Returned result can be
nullin case of requested amount of bytes greater then available space inSegmentedRingByteBuffer. AlsoSegmentedRingByteBuffer.WriteSegment.buffer()can returnnullin case ofmaxSegmentSizevalue 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
ByteBufferinstance that mapped to ownSegmentedRingByteBufferslice.Once the data has been written into the
ByteBufferclient code must notifySegmentedRingByteBufferinstance usingSegmentedRingByteBuffer.WriteSegment.release()method in order to provide possibility to consumer get data for reading.- Parameters:
size- Amount of bytes for reserve.- Returns:
SegmentedRingByteBuffer.WriteSegmentinstance that point toByteBufferinstance with givensize.nullif buffer space is not enough.- Throws:
IgniteException- If the givensizeis greater then capacity of this buffer.
-
offerSafe
public SegmentedRingByteBuffer.WriteSegment offerSafe(int size)
Behaves likeoffer(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.WriteSegmentinstance that point toByteBufferinstance with givensize.nullif buffer space is not enough.- Throws:
IgniteException- If the givensizeis greater then capacity of this buffer.
-
close
public void close()
Closes the buffer.
-
poll
public List<SegmentedRingByteBuffer.ReadSegment> poll()
Retrieves list ofSegmentedRingByteBuffer.ReadSegmentinstances that point toByteBufferthat contains all data available for reading fromSegmentedRingByteBufferornullif 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.ReadSegmentclient code must notifySegmentedRingByteBufferinstance usingSegmentedRingByteBuffer.ReadSegment.release()method in order to release occupied space in theSegmentedRingByteBufferand make it available for writing.- Returns:
- List of
ReadSegmentinstances with all available data for reading ornullif there are no available data.
-
poll
public List<SegmentedRingByteBuffer.ReadSegment> poll(long pos)
Retrieves list ofSegmentedRingByteBuffer.ReadSegmentinstances that point toByteBufferthat contains data available for reading fromSegmentedRingByteBufferlimited byposparameter ornullif 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.ReadSegmentclient code must notifySegmentedRingByteBufferinstance usingSegmentedRingByteBuffer.ReadSegment.release()method in order to release occupied space in theSegmentedRingByteBufferand make it available for writing.- Parameters:
pos- End position in buffer.- Returns:
- List of
ReadSegmentinstances with all available data for reading ornullif 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.
-
-