org.archive.queue
Class TieredQueue

java.lang.Object
  extended by org.archive.queue.TieredQueue
All Implemented Interfaces:
Queue

public class TieredQueue
extends java.lang.Object
implements Queue

A queue with multiple internal queues, numbered 0 to n. Dequeues come from lower-numbered queues before higher-numbered queues. The standard enqueue() places an item at the back of the highest-numbered queue. Other variants of enqueue place items on specific queues. A peek() guarantees that the item returned by peek() will be returned by the next dequeue(), even if other items are enqueued to lower-numbered internal queues in the meantime. (The unpeek() may be used to release the TieredQueue from this guarantee; the next peek()/dequeue() will then return the item available from the lowest-numbered queue.)

Author:
gojomo

Field Summary
(package private)  java.lang.Object headObject
           
(package private)  int headSource
           
(package private)  Queue[] innerQueues
           
(package private)  int lastQueue
           
(package private)  long length
           
 
Constructor Summary
TieredQueue(int tiers)
          Create a TieredQueue with the given number of internal queue slots.
 
Method Summary
 long deleteMatchedItems(org.apache.commons.collections.Predicate matcher)
          All objects in the queue where matcher.match(object) returns true will be deleted from the queue.
 java.lang.Object dequeue()
          remove an entry from the start of the queue
 void enqueue(java.lang.Object obj)
          Add an entry to the end of queue
 void enqueue(java.lang.Object obj, int tier)
          Enqueue the object to the given tier.
 java.util.Iterator getIterator(boolean inCacheOnly)
          Returns an iterator for the queue.
 void initializeDiskBackedQueues(java.io.File scratchDir, java.lang.String tmpName, int inMemCap)
          Initialize all innerQueues to be DiskBackedQueues in the given scratch directory, using the tmpName prefix.
 boolean isEmpty()
          is the queue empty?
 long length()
          get the number of elements in the queue
 java.lang.Object peek()
          Give the top object in the queue, leaving it in place to be returned by future peek() or dequeue() invocations.
 void release()
          release any OS/IO resources associated with Queue
 void setMemoryResidentQueueCap(int inMemCap)
          Set the total number of in-memory items, assuming the underlying subqueues are DiskBackedQueue instances.
 void setQueue(int tier, Queue q)
          Set the internal queue tier to be the supplied Queue instance.
 void unpeek()
          Releases queue from the obligation to return in the next peek()/dequeue() the same object as returned by any previous peek().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

headObject

java.lang.Object headObject

headSource

int headSource

innerQueues

Queue[] innerQueues

lastQueue

int lastQueue

length

long length
Constructor Detail

TieredQueue

public TieredQueue(int tiers)
Create a TieredQueue with the given number of internal queue slots.

Method Detail

enqueue

public void enqueue(java.lang.Object obj)
Description copied from interface: Queue
Add an entry to the end of queue

Specified by:
enqueue in interface Queue
Parameters:
obj - the entry to queue

enqueue

public void enqueue(java.lang.Object obj,
                    int tier)
Enqueue the object to the given tier.


isEmpty

public boolean isEmpty()
Description copied from interface: Queue
is the queue empty?

Specified by:
isEmpty in interface Queue
Returns:
true if the queue has no elements

dequeue

public java.lang.Object dequeue()
                         throws java.util.NoSuchElementException
Description copied from interface: Queue
remove an entry from the start of the queue

Specified by:
dequeue in interface Queue
Returns:
the object
Throws:
java.util.NoSuchElementException

length

public long length()
Description copied from interface: Queue
get the number of elements in the queue

Specified by:
length in interface Queue
Returns:
the number of elements in the queue

release

public void release()
Description copied from interface: Queue
release any OS/IO resources associated with Queue

Specified by:
release in interface Queue

peek

public java.lang.Object peek()
Description copied from interface: Queue
Give the top object in the queue, leaving it in place to be returned by future peek() or dequeue() invocations.

Specified by:
peek in interface Queue
Returns:
top object, without removing it

unpeek

public void unpeek()
Description copied from interface: Queue
Releases queue from the obligation to return in the next peek()/dequeue() the same object as returned by any previous peek().

Specified by:
unpeek in interface Queue

setQueue

public void setQueue(int tier,
                     Queue q)
Set the internal queue tier to be the supplied Queue instance.

Parameters:
tier - Tier level to set.
q - Queue to use.

getIterator

public java.util.Iterator getIterator(boolean inCacheOnly)
Description copied from interface: Queue
Returns an iterator for the queue.

The returned iterator's remove method is considered unsafe.

Editing the queue while using the iterator is not safe.

Specified by:
getIterator in interface Queue
Returns:
an iterator for the queue

deleteMatchedItems

public long deleteMatchedItems(org.apache.commons.collections.Predicate matcher)
Description copied from interface: Queue
All objects in the queue where matcher.match(object) returns true will be deleted from the queue.

Making other changes to the queue while this method is being processed is not safe.

Specified by:
deleteMatchedItems in interface Queue
Parameters:
matcher - a predicate
Returns:
the number of deleted items

initializeDiskBackedQueues

public void initializeDiskBackedQueues(java.io.File scratchDir,
                                       java.lang.String tmpName,
                                       int inMemCap)
                                throws java.io.IOException
Initialize all innerQueues to be DiskBackedQueues in the given scratch directory, using the tmpName prefix.

Parameters:
scratchDir - Directory to put tmp files in.
tmpName - Prefix to use in tmpnames.
Throws:
java.io.IOException

setMemoryResidentQueueCap

public void setMemoryResidentQueueCap(int inMemCap)
Set the total number of in-memory items, assuming the underlying subqueues are DiskBackedQueue instances.

Parameters:
inMemCap - Memory cap.


Copyright © 2003-2005 Internet Archive. All Rights Reserved.