Class RequestTable<T>


  • public class RequestTable<T>
    extends java.lang.Object
    Table for storing requests associated with monotonically increasing sequence numbers (seqnos).

    Could be used for example in RequestCorrelator. Grows and shrinks when needed. Addition is always at the end, yielding monotonically increasing seqnos. Removal is done by nulling the element(s) between low and high and advancing the low pointer whenever possible.

    See JGRP-1982 for details.

    Since:
    3.6.7
    Author:
    Bela Ban
    • Field Detail

      • buffer

        protected T[] buffer
      • low

        protected long low
      • high

        protected long high
      • removes_till_compaction

        protected int removes_till_compaction
      • num_removes

        protected int num_removes
      • lock

        protected final java.util.concurrent.locks.Lock lock
    • Constructor Detail

      • RequestTable

        public RequestTable​(int capacity)
      • RequestTable

        public RequestTable​(int capacity,
                            long low,
                            long high)
    • Method Detail

      • low

        public long low()
      • high

        public long high()
      • capacity

        public int capacity()
      • index

        public int index​(long seqno)
      • removesTillCompaction

        public int removesTillCompaction()
      • removesTillCompaction

        public RequestTable<T> removesTillCompaction​(int rems)
      • add

        public long add​(T element)
        Adds a new element and returns the sequence number at which it was inserted. Advances the high pointer and grows the buffer if needed.
        Parameters:
        element - the element to be added. Must not be null or an exception will be thrown
        Returns:
        the seqno at which element was added
      • get

        public T get​(long seqno)
      • remove

        public T remove​(long seqno)
        Removes the element at the index matching seqno. If seqno == low, tries to advance low until a non-null element is encountered, up to high
        Parameters:
        seqno -
        Returns:
      • removeMany

        public RequestTable<T> removeMany​(java.util.stream.LongStream seqnos,
                                          java.util.function.Consumer<T> consumer)
        Removes all elements in the stream. Calls the consumer (if not null) on non-null elements
      • clear

        public RequestTable<T> clear()
        Removes all elements, compacts the buffer and sets low=high=0
      • grow

        public RequestTable<T> grow​(int new_capacity)
        Grows the array to at least new_capacity. This method is mainly used for testing and is not typically called directly, but indirectly when adding elements and the underlying array has no space left.
        Parameters:
        new_capacity - the new capacity of the underlying array. Will be rounded up to the nearest power of 2 value. A value smaller than the current capacity is ignored.
      • compact

        public boolean compact()
        Shrinks the underlying array to half its size _if_ the new array can hold all of the existing elements.
        Returns:
        true if the compaction succeeded, or false if it failed (e.g. not enough space)
      • contiguousSpaceAvailable

        public boolean contiguousSpaceAvailable()
        Checks if there is at least buffer.length/2 contiguous space in range [low+1 .. high-1] available
      • size

        public int size()
        Returns the number of non-null elements in range [low .. high-1]
        Returns:
      • toString

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

        protected void _grow​(long new_capacity)
      • _compact

        protected boolean _compact()
        Shrinks the array to half of its current size if the current number of elements fit into half of the capacity.
        Returns:
        true if the compaction succeeded, else false (e.g. when the current elements would not fit)
      • dumpContents

        public java.lang.String dumpContents()
      • _copy

        protected void _copy​(int new_cap)
        Copies elements from old into new array
      • _contiguousSpaceAvailable

        protected boolean _contiguousSpaceAvailable​(int space_needed)
        Check if we have at least space_needed contiguous free slots available in range [low+1 .. high-1]
        Parameters:
        space_needed - the number of contiguous free slots required to do compaction, usually half of the current buffer size
        Returns:
        true if a contiguous space was found, false otherwise
      • highestContiguousSpaceAvailable

        protected int highestContiguousSpaceAvailable()
      • advanceLow

        protected void advanceLow()
      • index

        protected static int index​(long seqno,
                                   int length)