Class Buffer<T>

  • All Implemented Interfaces:
    java.lang.Iterable<T>
    Direct Known Subclasses:
    DynamicBuffer, FixedBuffer

    public abstract class Buffer<T>
    extends java.lang.Object
    implements java.lang.Iterable<T>
    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 Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.concurrent.atomic.AtomicInteger adders  
      protected long hd
      The highest delivered (=removed) seqno
      protected long high
      The highest received/sent seqno.
      protected java.util.concurrent.locks.Lock lock  
      protected long low
      sender: highest seqno seen by everyone, receiver: highest delivered seqno
      protected long offset  
      protected int size
      The 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)  
      boolean add​(long seqno, T element)
      Adds an element if the element at the given index is null.
      abstract boolean add​(long seqno, T element, java.util.function.Predicate<T> remove_filter, Buffer.Options options)
      Adds an element if the element at the given index is null.
      abstract boolean add​(MessageBatch batch, java.util.function.Function<T,​java.lang.Long> seqno_getter, boolean remove_from_batch, T const_value)  
      abstract int capacity()
      Returns the current capacity in the buffer.
      int computeSize()
      Iterates from hd to high and adds up non-null values.
      java.lang.String dump()
      Dumps all non-null messages (used for testing)
      abstract void forEach​(long from, long to, Buffer.Visitor<T> visitor, boolean nullify)  
      void forEach​(Buffer.Visitor<T> visitor, boolean nullify)  
      abstract T get​(long seqno)  
      java.util.concurrent.atomic.AtomicInteger getAdders()  
      long[] getDigest()  
      long getHighestDeliverable()
      Returns the highest deliverable (= removable) seqno.
      SeqnoList getMissing()
      Returns a list of missing (= null) elements
      SeqnoList getMissing​(int max_msgs)
      Returns a list of missing messages
      int getNumDeliverable()
      Returns the number of messages that can be delivered
      long hd()  
      long high()  
      long highestDelivered()  
      Buffer<T> highestDelivered​(long seqno)
      Only used internally on a state transfer (setting the digest).
      boolean isEmpty()  
      abstract java.util.Iterator<T> iterator​(long from, long to)  
      java.util.concurrent.locks.Lock lock()  
      long low()  
      int numMissing()
      Returns the number of null elements in the range [hd+1 ..
      long offset()  
      int purge​(long seqno)
      Removes all elements <= seqno from the buffer.
      abstract int purge​(long seqno, boolean force)
      Purges (nulls) all elements <= seqno.
      T remove()  
      abstract T remove​(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> 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()  
      int size()  
      abstract java.util.stream.Stream<T> stream()  
      abstract java.util.stream.Stream<T> stream​(long from, long to)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, iterator, spliterator
    • 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
    • Constructor Detail

      • Buffer

        public Buffer()
    • 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()
      • 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,
                                    Buffer.Options options)
        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 -
        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.
        options -
        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)
      • 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 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 java.lang.String dump()
        Dumps all non-null messages (used for testing)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object