Package org.jgroups.util
Class FixedBuffer<T>
- java.lang.Object
-
- org.jgroups.util.Buffer<T>
-
- org.jgroups.util.FixedBuffer<T>
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.lang.Iterable<T>
public class FixedBuffer<T> extends Buffer<T> implements java.io.Closeable
Ring buffer of fixed capacity. Indices low and high point to the beginning and end of the buffer. Sequence numbers (seqnos) are mapped to an index byseqno % capacity
. High can never pass low, and drops the element or blocks when that's the case.
Note that 'null' is not a valid element, but signifies a missing element
The design is described in doc/design/FixedBuffer.txt.- Since:
- 5.4
- Author:
- Bela Ban
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
FixedBuffer.FixedBufferIterator
-
Nested classes/interfaces inherited from class org.jgroups.util.Buffer
Buffer.HighestDeliverable, Buffer.Missing, Buffer.NumDeliverable, Buffer.Options, Buffer.Remover<R>, Buffer.Visitor<T>
-
-
Field Summary
Fields Modifier and Type Field Description protected AverageMinMax
avg_time_blocked
protected T[]
buf
Holds the elementsprotected java.util.concurrent.locks.Condition
buffer_full
protected java.util.concurrent.atomic.LongAdder
num_blockings
protected boolean
open
Used to unblock blocked senders on close()
-
Constructor Summary
Constructors Constructor Description FixedBuffer()
FixedBuffer(int capacity, long offset)
Creates a RingBufferFixedBuffer(long offset)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description T
_get(long seqno)
Only used for testing !!boolean
add(long seqno, T element, java.util.function.Predicate<T> remove_filter, Buffer.Options opts)
Adds a new element to the bufferboolean
add(MessageBatch batch, java.util.function.Function<T,java.lang.Long> seqno_getter, boolean remove_from_batch, T const_value)
AverageMinMax
avgTimeBlocked()
protected boolean
block(long seqno)
int
capacity()
Returns the current capacity in the buffer.void
close()
void
forEach(long from, long to, Buffer.Visitor<T> visitor, boolean nullify)
void
forEach(long from, long to, Buffer.Visitor<T> visitor, boolean nullify, boolean respect_stop)
T
get(long seqno)
protected int
index(long seqno)
java.util.Iterator<T>
iterator()
Returns an iterator over the elements of the ring buffer in the range [LOW+1 ..java.util.Iterator<T>
iterator(long from, long to)
long
numBlockings()
int
purge(long seqno, boolean force)
Purges (nulls) all elements <= seqno.T
remove(boolean nullify)
Removes the next non-null element and advances hdjava.util.List<T>
removeMany(boolean nullify, int max_results, java.util.function.Predicate<T> filter)
<R> R
removeMany(boolean nullify, int max_results, java.util.function.Predicate<T> filter, java.util.function.Supplier<R> result_creator, java.util.function.BiConsumer<R,T> accumulator)
void
resetStats()
java.util.stream.Stream<T>
stream()
java.util.stream.Stream<T>
stream(long from, long to)
-
Methods inherited from class org.jgroups.util.Buffer
add, computeSize, dump, forEach, getAdders, getDigest, getHighestDeliverable, getMissing, getMissing, getNumDeliverable, hd, high, highestDelivered, highestDelivered, isEmpty, lock, low, numMissing, offset, purge, remove, removeMany, size, toString
-
-
-
-
Field Detail
-
buf
protected final T[] buf
Holds the elements
-
buffer_full
protected final java.util.concurrent.locks.Condition buffer_full
-
open
protected boolean open
Used to unblock blocked senders on close()
-
num_blockings
protected final java.util.concurrent.atomic.LongAdder num_blockings
-
avg_time_blocked
protected final AverageMinMax avg_time_blocked
-
-
Constructor Detail
-
FixedBuffer
public FixedBuffer()
-
FixedBuffer
public FixedBuffer(long offset)
-
FixedBuffer
public FixedBuffer(int capacity, long offset)
Creates a RingBuffer- Parameters:
capacity
- The number of elements the ring buffer's array should hold.offset
- The offset. The first element to be added has to be offset +1.
-
-
Method Detail
-
capacity
public int capacity()
Description copied from class:Buffer
Returns the current capacity in the buffer. This value is fixed in a fixed-size buffer (e.g.FixedBuffer
), but can change in a dynamic buffer (DynamicBuffer
)
-
numBlockings
public long numBlockings()
-
avgTimeBlocked
public AverageMinMax avgTimeBlocked()
-
add
public boolean add(long seqno, T element, java.util.function.Predicate<T> remove_filter, Buffer.Options opts)
Adds a new element to the buffer
-
add
public boolean add(MessageBatch batch, java.util.function.Function<T,java.lang.Long> seqno_getter, boolean remove_from_batch, T const_value)
-
remove
public T remove(boolean nullify)
Removes the next non-null element and advances hd
-
removeMany
public java.util.List<T> removeMany(boolean nullify, int max_results, java.util.function.Predicate<T> filter)
- Specified by:
removeMany
in classBuffer<T>
-
removeMany
public <R> R removeMany(boolean nullify, int max_results, java.util.function.Predicate<T> filter, java.util.function.Supplier<R> result_creator, java.util.function.BiConsumer<R,T> accumulator)
- Specified by:
removeMany
in classBuffer<T>
-
purge
public int purge(long seqno, boolean force)
Description copied from class:Buffer
Purges (nulls) all elements <= seqno.
-
forEach
public void forEach(long from, long to, Buffer.Visitor<T> visitor, boolean nullify)
-
forEach
public void forEach(long from, long to, Buffer.Visitor<T> visitor, boolean nullify, boolean respect_stop)
-
resetStats
public void resetStats()
- Overrides:
resetStats
in classBuffer<T>
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
-
iterator
public java.util.Iterator<T> iterator()
Returns an iterator over the elements of the ring buffer in the range [LOW+1 .. HIGH]- Specified by:
iterator
in interfacejava.lang.Iterable<T>
- Returns:
- FixedBufferIterator
- Throws:
java.util.NoSuchElementException
- is HD is moved forward during the iteration
-
iterator
public java.util.Iterator<T> iterator(long from, long to)
-
stream
public java.util.stream.Stream<T> stream(long from, long to)
-
index
protected int index(long seqno)
-
block
protected boolean block(long seqno)
-
-