Package org.jgroups.util
Class RequestTable<T>
- java.lang.Object
-
- org.jgroups.util.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 inRequestCorrelator
. 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
RequestTable.Visitor<T>
-
Field Summary
Fields Modifier and Type Field Description protected T[]
buffer
protected long
high
protected java.util.concurrent.locks.Lock
lock
protected long
low
protected int
num_removes
protected int
removes_till_compaction
-
Constructor Summary
Constructors Constructor Description RequestTable(int capacity)
RequestTable(int capacity, long low, long high)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
_compact()
Shrinks the array to half of its current size if the current number of elements fit into half of the capacity.protected boolean
_contiguousSpaceAvailable(int space_needed)
Check if we have at least space_needed contiguous free slots available in range [low+1 ..protected void
_copy(int new_cap)
Copies elements from old into new arrayprotected void
_grow(long new_capacity)
long
add(T element)
Adds a new element and returns the sequence number at which it was inserted.protected void
advanceLow()
int
capacity()
RequestTable<T>
clear()
Removes all elements, compacts the buffer and sets low=high=0RequestTable<T>
clear(long mark)
boolean
compact()
Shrinks the underlying array to half its size _if_ the new array can hold all of the existing elements.boolean
contiguousSpaceAvailable()
Checks if there is at least buffer.length/2 contiguous space in range [low+1 ..java.lang.String
dumpContents()
RequestTable<T>
forEach(RequestTable.Visitor<T> visitor)
RequestTable<T>
forEachNonBlocking(RequestTable.Visitor<T> visitor)
Non-blocking alternative toforEach(Visitor)
: iteration is performed on the array that exists at the time of this call.T
get(long seqno)
RequestTable<T>
grow(int new_capacity)
Grows the array to at least new_capacity.long
high()
protected int
highestContiguousSpaceAvailable()
int
index(long seqno)
protected static int
index(long seqno, int length)
long
low()
T
remove(long seqno)
Removes the element at the index matching seqno.RequestTable<T>
removeMany(java.util.stream.LongStream seqnos, java.util.function.Consumer<T> consumer)
Removes all elements in the stream.int
removesTillCompaction()
RequestTable<T>
removesTillCompaction(int rems)
int
size()
Returns the number of non-null elements in range [low ..java.lang.String
toString()
-
-
-
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
-
-
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
-
clear
public RequestTable<T> clear(long mark)
-
forEach
public RequestTable<T> forEach(RequestTable.Visitor<T> visitor)
-
forEachNonBlocking
public RequestTable<T> forEachNonBlocking(RequestTable.Visitor<T> visitor)
Non-blocking alternative toforEach(Visitor)
: iteration is performed on the array that exists at the time of this call. Changes to the underlying array will not be reflected in the iteration.- Parameters:
visitor
- theRequestTable.Visitor
.
-
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 classjava.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)
-
-