Package io.airlift.concurrent
Class DynamicSizeBoundQueue<T>
java.lang.Object
io.airlift.concurrent.DynamicSizeBoundQueue<T>
Size constrained queue that utilizes a dynamic element size function. To prevent
starvation for adding large elements, the queue will only block new elements if
the total size has already been reached or exceeded. This means in the normal
case, the queue should be no larger than the max size plus the size of one element.
Callers also have the additional option to force insert further elements without
regard for size constraints. In the current implementation, elements are required
to have positive sizes (they cannot have zero size). This implementation is designed
to closely mirror the method signatures of
BlockingQueue.-
Constructor Summary
ConstructorsConstructorDescriptionDynamicSizeBoundQueue(long maxSize, ToLongFunction<T> elementSizeFunction) DynamicSizeBoundQueue(long maxSize, ToLongFunction<T> elementSizeFunction, com.google.common.base.Ticker ticker) -
Method Summary
Modifier and TypeMethodDescriptionvoidInsert without regard to the max size (potentially exceeding the max limit).longlonggetSize()Gets the current size of the queue.booleanbooleanofferWithBackoff(T element) Enqueue the element if there is space, otherwise returns a ListenableFuture that will complete when space becomes available for the element.poll()voidtake()
-
Constructor Details
-
DynamicSizeBoundQueue
-
DynamicSizeBoundQueue
public DynamicSizeBoundQueue(long maxSize, ToLongFunction<T> elementSizeFunction, com.google.common.base.Ticker ticker)
-
-
Method Details
-
getMaxSize
public long getMaxSize() -
getSize
public long getSize()Gets the current size of the queue. The size is guaranteed to be no larger than max size plus the size of one element ifforcePut(Object)is not used. -
offer
-
offer
- Throws:
InterruptedException
-
put
- Throws:
InterruptedException
-
offerWithBackoff
public Optional<com.google.common.util.concurrent.ListenableFuture<Void>> offerWithBackoff(T element) Enqueue the element if there is space, otherwise returns a ListenableFuture that will complete when space becomes available for the element. If a future is returned, the element was not inserted. -
forcePut
Insert without regard to the max size (potentially exceeding the max limit). This can throw anIllegalStateExceptionif the forced element triggers a numeric overflow, in which case the element is not inserted. -
poll
-
poll
- Throws:
InterruptedException
-
take
- Throws:
InterruptedException
-