Package org.jgroups.util
Class Buffer<T>
- java.lang.Object
-
- org.jgroups.util.Buffer<T>
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Iterable<T>
- Direct Known Subclasses:
DynamicBuffer,FixedBuffer
public abstract class Buffer<T> extends java.lang.Object implements java.lang.Iterable<T>, java.io.CloseableBase class for message buffers. Used on the senders (keeping track of sent messages and purging delivered messages) and receivers (delivering messages in the correct order and asking senders for missing messages).- Since:
- 5.4
- Author:
- Bela Ban
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classBuffer.HighestDeliverableprotected classBuffer.Missingprotected classBuffer.NumDeliverableprotected classBuffer.Remover<R>static interfaceBuffer.Visitor<T>
-
Field Summary
Fields Modifier and Type Field Description protected java.util.concurrent.atomic.AtomicIntegeraddersprotected longhdThe highest delivered (=removed) seqnoprotected longhighThe highest received/sent seqno.protected java.util.concurrent.locks.Locklockprotected longlowsender: highest seqno seen by everyone, receiver: highest delivered seqnoprotected longoffsetprotected intsizeThe number of non-null elements
-
Constructor Summary
Constructors Constructor Description Buffer()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract T_get(long seqno)booleanadd(long seqno, T element)Adds an element if the element at the given index is null.abstract booleanadd(long seqno, T element, java.util.function.Predicate<T> remove_filter, boolean block_if_full)Adds an element if the element at the given index is null.abstract booleanadd(java.util.List<LongTuple<T>> list, boolean remove_added_elements, T const_value)Adds elements from the listabstract booleanadd(MessageBatch batch, java.util.function.Function<T,java.lang.Long> seqno_getter, boolean remove_from_batch, T const_value)abstract intcapacity()Returns the current capacity in the buffer.voidclose()intcomputeSize()Iterates from hd to high and adds up non-null values.java.lang.Stringdump()Dumps all non-null messages (used for testing)abstract voidforEach(long from, long to, Buffer.Visitor<T> visitor, boolean nullify)voidforEach(Buffer.Visitor<T> visitor, boolean nullify)abstract Tget(long seqno)java.util.concurrent.atomic.AtomicIntegergetAdders()long[]getDigest()longgetHighestDeliverable()Returns the highest deliverable (= removable) seqno.SeqnoListgetMissing()Returns a list of missing (= null) elementsSeqnoListgetMissing(int max_msgs)Returns a list of missing messagesintgetNumDeliverable()Returns the number of messages that can be deliveredlonghd()longhigh()longhighestDelivered()Buffer<T>highestDelivered(long seqno)Only used internally on a state transfer (setting the digest).booleanisEmpty()abstract java.util.Iterator<T>iterator(long from, long to)java.util.concurrent.locks.Locklock()longlow()intnumMissing()Returns the number of null elements in the range [hd+1 ..longoffset()voidopen(boolean b)intpurge(long seqno)Removes all elements <= seqno from the buffer.abstract intpurge(long seqno, boolean force)Purges (nulls) all elements <= seqno.Tremove()abstract Tremove(boolean nullify)java.util.List<T>removeMany(boolean nullify, int max_results)abstract java.util.List<T>removeMany(boolean nullify, int max_results, java.util.function.Predicate<T> filter)abstract <R> RremoveMany(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)voidresetStats()intsize()abstract java.util.stream.Stream<T>stream()abstract java.util.stream.Stream<T>stream(long from, long to)java.lang.StringtoString()
-
-
-
Field Detail
-
lock
protected final java.util.concurrent.locks.Lock lock
-
adders
protected final java.util.concurrent.atomic.AtomicInteger adders
-
offset
protected long offset
-
low
protected long low
sender: highest seqno seen by everyone, receiver: highest delivered seqno
-
hd
protected long hd
The highest delivered (=removed) seqno
-
high
protected long high
The highest received/sent seqno. Moved forward by add(). The next message to be added is high+1. low <= hd <= high always holds
-
size
protected int size
The number of non-null elements
-
-
Method Detail
-
lock
public java.util.concurrent.locks.Lock lock()
-
getAdders
public java.util.concurrent.atomic.AtomicInteger getAdders()
-
offset
public long offset()
-
low
public long low()
-
highestDelivered
public long highestDelivered()
-
hd
public long hd()
-
high
public long high()
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
capacity
public abstract int capacity()
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)
-
resetStats
public void resetStats()
-
open
public void open(boolean b)
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
add
public boolean add(long seqno, T element)Adds an element if the element at the given index is null. Returns true if no element existed at the given index, else returns false and doesn't set the element.- Parameters:
seqno-element-- Returns:
- True if the element at the computed index was null, else false
-
add
public abstract boolean add(long seqno, T element, java.util.function.Predicate<T> remove_filter, boolean block_if_full)Adds an element if the element at the given index is null. Returns true if no element existed at the given index, else returns false and doesn't set the element.- Parameters:
seqno- The seqno of the elementelement- The element to be addedremove_filter- A filter used to remove all consecutive messages passing the filter (and non-null). This doesn't necessarily null a removed message, but may simply advance an index (e.g. highest delivered). Ignored if null.block_if_full- If true: blocks when an element is to be added to the buffer, else drops the element- Returns:
- True if the element at the computed index was null, else false
-
add
public abstract boolean add(MessageBatch batch, java.util.function.Function<T,java.lang.Long> seqno_getter, boolean remove_from_batch, T const_value)
-
add
public abstract boolean add(java.util.List<LongTuple<T>> list, boolean remove_added_elements, T const_value)
Adds elements from the list- Parameters:
list- The list of tuples of seqnos and elements. If remove_added_elements is true, if elements could not be added (e.g. because they were already present or the seqno was < HD), those elements will be removed from listremove_added_elements- If true, elements that could not be added to the table are removed from listconst_value- If non-null, this value should be used rather than the values of the list tuples- Returns:
- True if at least 1 element was added successfully, false otherwise.
-
get
public abstract T get(long seqno)
-
_get
public abstract T _get(long seqno)
-
remove
public T remove()
-
remove
public abstract T remove(boolean nullify)
-
removeMany
public java.util.List<T> removeMany(boolean nullify, int max_results)
-
removeMany
public abstract java.util.List<T> removeMany(boolean nullify, int max_results, java.util.function.Predicate<T> filter)
-
removeMany
public abstract <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)
-
purge
public int purge(long seqno)
Removes all elements <= seqno from the buffer. Does this by nulling all elements < index(seqno)
-
purge
public abstract int purge(long seqno, boolean force)Purges (nulls) all elements <= seqno.- Parameters:
seqno- All elements <= seqno will be purged.force- If false, seqno is max(seqno,hd), else max(seqno,high). In the latter case (seqno > hd), we might purge elements that have not yet been received- Returns:
- 0. The number of purged elements
-
forEach
public void forEach(Buffer.Visitor<T> visitor, boolean nullify)
-
forEach
public abstract void forEach(long from, long to, Buffer.Visitor<T> visitor, boolean nullify)
-
iterator
public abstract java.util.Iterator<T> iterator(long from, long to)
-
stream
public abstract java.util.stream.Stream<T> stream()
-
stream
public abstract java.util.stream.Stream<T> stream(long from, long to)
-
computeSize
public int computeSize()
Iterates from hd to high and adds up non-null values. Caller must hold the lock.
-
numMissing
public int numMissing()
Returns the number of null elements in the range [hd+1 .. hr-1] excluding hd and hr
-
getMissing
public SeqnoList getMissing()
Returns a list of missing (= null) elements- Returns:
- A SeqnoList of missing messages, or null if no messages are missing
-
getMissing
public SeqnoList getMissing(int max_msgs)
Returns a list of missing messages- Parameters:
max_msgs- If > 0, the max number of missing messages to be returned (oldest first), else no limit- Returns:
- A SeqnoList of missing messages, or null if no messages are missing
-
getNumDeliverable
public int getNumDeliverable()
Returns the number of messages that can be delivered
-
getHighestDeliverable
public long getHighestDeliverable()
Returns the highest deliverable (= removable) seqno. This may be higher thanhighestDelivered(), e.g. if elements have been added but not yet removed
-
getDigest
public long[] getDigest()
-
highestDelivered
public Buffer<T> highestDelivered(long seqno)
Only used internally on a state transfer (setting the digest). Don't use this in application code!
-
dump
public java.lang.String dump()
Dumps all non-null messages (used for testing)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-