Class DynamicSizeBoundQueue<T>

java.lang.Object
io.airlift.concurrent.DynamicSizeBoundQueue<T>

@ThreadSafe public class DynamicSizeBoundQueue<T> extends Object
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 Details

    • DynamicSizeBoundQueue

      public DynamicSizeBoundQueue(long maxSize, ToLongFunction<T> elementSizeFunction)
    • 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 if forcePut(Object) is not used.
    • offer

      public boolean offer(T element)
    • offer

      public boolean offer(T element, long timeout, TimeUnit unit) throws InterruptedException
      Throws:
      InterruptedException
    • put

      public void put(T element) throws InterruptedException
      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

      public void forcePut(T element)
      Insert without regard to the max size (potentially exceeding the max limit). This can throw an IllegalStateException if the forced element triggers a numeric overflow, in which case the element is not inserted.
    • poll

      @Nullable public T poll()
    • poll

      public T poll(long timeout, TimeUnit unit) throws InterruptedException
      Throws:
      InterruptedException
    • take

      public T take() throws InterruptedException
      Throws:
      InterruptedException