com.bigdata.cache
Class SynchronizedHardReferenceQueue<T>

java.lang.Object
  extended by com.bigdata.cache.SynchronizedHardReferenceQueue<T>
All Implemented Interfaces:
IHardReferenceQueue<T>

public class SynchronizedHardReferenceQueue<T>
extends Object
implements IHardReferenceQueue<T>

Thread-safe version.

Version:
$Id: SynchronizedHardReferenceQueue.java 2265 2009-10-26 12:51:06Z thompsonbry $
Author:
Bryan Thompson

Field Summary
protected  HardReferenceQueue<T> queue
          Note: Synchronization for the inner queue is realized using the outer reference!
 
Fields inherited from interface com.bigdata.cache.IHardReferenceQueue
DEFAULT_NSCAN
 
Constructor Summary
SynchronizedHardReferenceQueue(HardReferenceQueueEvictionListener<T> listener, int capacity)
          Defaults the #of references to scan on append requests to 10.
SynchronizedHardReferenceQueue(HardReferenceQueueEvictionListener<T> listener, int capacity, int nscan)
          Core impl.
 
Method Summary
 boolean add(T ref)
          Add a reference to the cache.
protected  void beforeOffer(T t)
          All attempts to add an element to the buffer invoke this hook before checking the remaining capacity in the buffer.
 int capacity()
          The cache capacity.
 void clear(boolean clearRefs)
          Clears the cache (sets the head, tail and count to zero) without generating eviction notices.
 boolean evict()
          Evict the LRU reference.
 void evictAll(boolean clearRefs)
          Evict all references, starting with the LRU reference and proceeding to the MRU reference.
 HardReferenceQueueEvictionListener<T> getListener()
           
 boolean isEmpty()
          True iff the cache is empty.
 boolean isFull()
          True iff the cache is full.
 int nscan()
          The #of references that are tested on append requests.
 T peek()
          The reference at the tail of the queue.
 boolean scanHead(int nscan, T ref)
           
 boolean scanTail(int nscan, T ref)
           
 int size()
          The #of references in the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

queue

protected final HardReferenceQueue<T> queue
Note: Synchronization for the inner queue is realized using the outer reference!

Constructor Detail

SynchronizedHardReferenceQueue

public SynchronizedHardReferenceQueue(HardReferenceQueueEvictionListener<T> listener,
                                      int capacity)
Defaults the #of references to scan on append requests to 10.

Parameters:
listener - The listener on which cache evictions are reported.
capacity - The maximum #of references that can be stored on the cache. There is no guarantee that all stored references are distinct.

SynchronizedHardReferenceQueue

public SynchronizedHardReferenceQueue(HardReferenceQueueEvictionListener<T> listener,
                                      int capacity,
                                      int nscan)
Core impl.

Parameters:
listener - The listener on which cache evictions are reported (optional).
capacity - The maximum #of references that can be stored on the cache. There is no guarantee that all stored references are distinct.
nscan - The #of references to scan from the MRU position before appended a reference to the cache. Scanning is used to reduce the chance that references that are touched several times in near succession from entering the cache more than once. The #of reference tests trades off against the latency of adding a reference to the cache.
Method Detail

beforeOffer

protected void beforeOffer(T t)
All attempts to add an element to the buffer invoke this hook before checking the remaining capacity in the buffer. The caller will be synchronized on this when this method is invoked.


capacity

public final int capacity()
Description copied from interface: IHardReferenceQueue
The cache capacity.

Specified by:
capacity in interface IHardReferenceQueue<T>

getListener

public HardReferenceQueueEvictionListener<T> getListener()

nscan

public int nscan()
Description copied from interface: IHardReferenceQueue
The #of references that are tested on append requests.

Specified by:
nscan in interface IHardReferenceQueue<T>

add

public boolean add(T ref)
Description copied from interface: IHardReferenceQueue
Add a reference to the cache. If the reference was recently added to the cache then this is a NOP. Otherwise the reference is appended to the cache. If a reference is appended to the cache and then cache is at capacity, then the LRU reference is first evicted from the cache.

Specified by:
add in interface IHardReferenceQueue<T>
Parameters:
ref - The reference to be added.
Returns:
True iff the reference was added to the cache and false iff the reference was found in a scan of the nscan MRU cache entries.

clear

public void clear(boolean clearRefs)
Description copied from interface: IHardReferenceQueue
Clears the cache (sets the head, tail and count to zero) without generating eviction notices.

Specified by:
clear in interface IHardReferenceQueue<T>
Parameters:
clearRefs - When true the references are explicitly set to null which can facilitate garbage collection.

evict

public boolean evict()
Description copied from interface: IHardReferenceQueue
Evict the LRU reference. This is a NOP iff the cache is empty.

Specified by:
evict in interface IHardReferenceQueue<T>
Returns:
true iff a reference was evicted.
See Also:
HardReferenceQueueEvictionListener

evictAll

public void evictAll(boolean clearRefs)
Description copied from interface: IHardReferenceQueue
Evict all references, starting with the LRU reference and proceeding to the MRU reference.

Specified by:
evictAll in interface IHardReferenceQueue<T>
Parameters:
clearRefs - When true, the reference are actually cleared from the cache. This may be false to force persistence of the references in the cache without actually clearing the cache.

peek

public T peek()
Description copied from interface: IHardReferenceQueue
The reference at the tail of the queue. This is the next reference that will be evicted from the queue.

Specified by:
peek in interface IHardReferenceQueue<T>

isEmpty

public boolean isEmpty()
Description copied from interface: IHardReferenceQueue
True iff the cache is empty.

Specified by:
isEmpty in interface IHardReferenceQueue<T>

isFull

public boolean isFull()
Description copied from interface: IHardReferenceQueue
True iff the cache is full.

Specified by:
isFull in interface IHardReferenceQueue<T>

scanHead

public boolean scanHead(int nscan,
                        T ref)

scanTail

public boolean scanTail(int nscan,
                        T ref)

size

public int size()
Description copied from interface: IHardReferenceQueue
The #of references in the cache. Note that there is no guarantee that the references are distinct.

Specified by:
size in interface IHardReferenceQueue<T>


Copyright © 2006-2009 SYSTAP, LLC. All Rights Reserved.