Class Buffer<T>

java.lang.Object
org.jgroups.util.Buffer<T>
Type Parameters:
T - T
All Implemented Interfaces:
Closeable, AutoCloseable, Iterable<T>
Direct Known Subclasses:
DynamicBuffer, FixedBuffer

public abstract class Buffer<T> extends Object implements Iterable<T>, Closeable
Base 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
  • Field Details

    • lock

      protected final Lock lock
    • adders

      protected final 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
  • Constructor Details

    • Buffer

      public Buffer()
  • Method Details

    • lock

      public Lock lock()
    • getAdders

      public 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:
      close in interface AutoCloseable
      Specified by:
      close in interface 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.
      Returns:
      True if the element at the computed index was null, else false
    • add

      public abstract boolean add(long seqno, T element, 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 element
      element - The element to be added
      remove_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, Function<T,Long> seqno_getter, boolean remove_from_batch, T const_value)
    • add

      public abstract boolean add(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 list
      remove_added_elements - If true, elements that could not be added to the table are removed from list
      const_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 List<T> removeMany(boolean nullify, int max_results)
    • removeMany

      public abstract List<T> removeMany(boolean nullify, int max_results, Predicate<T> filter)
    • removeMany

      public abstract <R> R removeMany(boolean nullify, int max_results, Predicate<T> filter, Supplier<R> result_creator, 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 Iterator<T> iterator(long from, long to)
    • stream

      public abstract Stream<T> stream()
    • stream

      public abstract 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 than highestDelivered(), 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 String dump()
      Dumps all non-null messages (used for testing)
    • toString

      public String toString()
      Overrides:
      toString in class Object