Class BatchArrayQueue<T>

java.lang.Object
org.cometd.server.ext.BatchArrayQueue<T>
Type Parameters:
T - the type of elements
All Implemented Interfaces:
Iterable<T>, Collection<T>, Queue<T>

public class BatchArrayQueue<T> extends Object implements Queue<T>

A concurrent queue that groups elements in batches.

The elements are stored in a growable circular array along with their batch number:


           head             tail
             |               |
 elements: [E1, E2, E3, E4, E5]
 batches : [ 1,  1,  2,  3,  3]
 

Elements E1 and E2 belong to batch 1, only element E3 belongs to batch 2, etc.

The batch can be "closed" and a new one "opened" by calling nextBatch().

The elements can be copied with exportMessagesToBatch(Queue, long) into a different queue, and removed with clearToBatch(long).

  • Constructor Details Link icon

    • BatchArrayQueue Link icon

      public BatchArrayQueue(int initial, Lock lock)
  • Method Details Link icon

    • offer Link icon

      public boolean offer(T t)

      Adds the given element to this queue, under the current batch as returned by getBatch().

      Specified by:
      offer in interface Queue<T>
      Parameters:
      t - the element to add
      Returns:
      true
    • add Link icon

      public boolean add(T t)
      Specified by:
      add in interface Collection<T>
      Specified by:
      add in interface Queue<T>
    • peek Link icon

      public T peek()
      Specified by:
      peek in interface Queue<T>
    • element Link icon

      public T element()
      Specified by:
      element in interface Queue<T>
    • poll Link icon

      public T poll()
      Specified by:
      poll in interface Queue<T>
    • remove Link icon

      public T remove()
      Specified by:
      remove in interface Queue<T>
    • remove Link icon

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<T>
    • addAll Link icon

      public boolean addAll(Collection<? extends T> items)
      Specified by:
      addAll in interface Collection<T>
    • removeAll Link icon

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<T>
    • retainAll Link icon

      public boolean retainAll(Collection<?> c)
      Specified by:
      retainAll in interface Collection<T>
    • containsAll Link icon

      public boolean containsAll(Collection<?> items)
      Specified by:
      containsAll in interface Collection<T>
    • contains Link icon

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<T>
    • iterator Link icon

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
    • isEmpty Link icon

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<T>
    • size Link icon

      public int size()
      Specified by:
      size in interface Collection<T>
    • toArray Link icon

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<T>
    • toArray Link icon

      public <E> E[] toArray(E[] a)
      Specified by:
      toArray in interface Collection<T>
    • clear Link icon

      public void clear()

      Removes all the elements from this queue and resets the batch number to 1.

      Specified by:
      clear in interface Collection<T>
    • getBatch Link icon

      public long getBatch()
      Returns:
      the current batch number
    • nextBatch Link icon

      public void nextBatch()

      Closes the current batch and starts a new batch.

      The next element offered to this queue will belong to the new batch.

    • clearToBatch Link icon

      public void clearToBatch(long batch)

      Removes all the elements up to the given batch number.

      For example, given:

      
                 head             tail
                   |               |
       elements: [E1, E2, E3, E4, E5]
       batches : [ 1,  1,  2,  3,  3]
       

      then calling clearToBatch(1) would leave the queue in this state:

      
                              head   tail
                               |       |
       elements: [null, null, E3, E4, E5]
       batches : [   0,    0,  2,  3,  3]
       
      Parameters:
      batch - the batch number
      See Also:
    • exportMessagesToBatch Link icon

      public void exportMessagesToBatch(Queue<T> target, long batch)

      Copies the elements of this queue into the given queue, up to the given batch number.

      For example, given:

      
       this queue:
                 head             tail
                   |               |
       elements: [E1, E2, E3, E4, E5]
       batches : [ 1,  1,  2,  3,  3]
      
       target queue: []
       

      then calling exportMessagesToBatch(2) would copy the elements belonging to batches up to 2 into the target queue, leaving this queue unchanged:

      
       this queue:
                 head             tail
                   |               |
       elements: [E1, E2, E3, E4, E5]
       batches : [ 1,  1,  2,  3,  3]
      
       target queue: [E1, E2, E3]
       
      Parameters:
      target - the target queue
      batch - the batch number
      See Also:
    • toString Link icon

      public String toString()
      Overrides:
      toString in class Object