Package org.jgroups.util
Class RequestTable<T>
java.lang.Object
org.jgroups.util.RequestTable<T>
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
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected 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) longAdds a new element and returns the sequence number at which it was inserted.protected voidintcapacity()clear()Removes all elements, compacts the buffer and sets low=high=0clear(long mark) booleancompact()Shrinks the underlying array to half its size _if_ the new array can hold all of the existing elements.booleanChecks if there is at least buffer.length/2 contiguous space in range [low+1 ..forEach(RequestTable.Visitor<T> visitor) forEachNonBlocking(RequestTable.Visitor<T> visitor) Non-blocking alternative toforEach(Visitor): iteration is performed on the array that exists at the time of this call.get(long seqno) grow(int new_capacity) Grows the array to at least new_capacity.longhigh()protected intintindex(long seqno) protected static intindex(long seqno, int length) longlow()remove(long seqno) Removes the element at the index matching seqno.removeMany(LongStream seqnos, Consumer<T> consumer) Removes all elements in the stream.intremovesTillCompaction(int rems) intsize()Returns the number of non-null elements in range [low ..toString()
-
Field Details
-
buffer
-
low
protected long low -
high
protected long high -
removes_till_compaction
protected int removes_till_compaction -
num_removes
protected int num_removes -
lock
-
-
Constructor Details
-
RequestTable
public RequestTable(int capacity) -
RequestTable
public RequestTable(int capacity, long low, long high)
-
-
Method Details
-
low
public long low() -
high
public long high() -
capacity
public int capacity() -
index
public int index(long seqno) -
removesTillCompaction
public int removesTillCompaction() -
removesTillCompaction
-
add
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
-
remove
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
Removes all elements in the stream. Calls the consumer (if not null) on non-null elements -
clear
Removes all elements, compacts the buffer and sets low=high=0 -
clear
-
forEach
-
forEachNonBlocking
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
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
-
_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
-
_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)
-