com.bigdata.cache
Class HardReferenceQueue<T>

java.lang.Object
  extended by com.bigdata.cache.RingBuffer<T>
      extended by com.bigdata.cache.HardReferenceQueue<T>
Type Parameters:
T - The reference type stored in the cache.
All Implemented Interfaces:
IHardReferenceQueue<T>, Iterable<T>, Collection<T>, Queue<T>

public class HardReferenceQueue<T>
extends RingBuffer<T>
implements IHardReferenceQueue<T>

A cache for hard references using an LRU policy. References are simply cached, but objects are not recoverable from their reference. In order to make an object recoverable, this cache must be wrapped by a weak reference cache that implements a hash map for discovery of objects using their persistent identifier. The HardReferenceQueue has a capacity that determines the #of hard references that may be cached before an object is evicted from the cache. Objects may be stored multiple times on the cache but the nscan most recent references are always tested before appending a reference to the cache in order to minimize cache churn when an object is touched repeatedly in close succession. Likewise, eviction does not mean that the object is no longer on the hard reference cache, nor does eviction mean that no hard references to the object exist within the VM. However, eviction is nevertheless used to drive persistence of the object. Since an object may be evicted multiple times without being updated in between evictions, this requires the object to implement a protocol for determining whether or not it is dirty. Eviction is then contingent on that protocol and the dirty state of the object is reset when it is serialized for eviction. In combination with the object's dirty protocol, the hard reference cache can substitute for a commit list.

Note: This implementation is NOT synchronized.

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

Field Summary
protected  int nscan
          The #of references to scan backwards from the LRU position when testing for whether or not a reference is already in the cache.
 
Fields inherited from class com.bigdata.cache.RingBuffer
capacity, size
 
Fields inherited from interface com.bigdata.cache.IHardReferenceQueue
DEFAULT_NSCAN
 
Constructor Summary
HardReferenceQueue(HardReferenceQueueEvictionListener<T> listener, int capacity)
          Uses the default #of references to scan on append requests.
HardReferenceQueue(HardReferenceQueueEvictionListener<T> listener, int capacity, int nscan)
          Fully specified ctor.
 
Method Summary
 boolean add(T ref)
          Add a reference to the cache.
protected  void beforeOffer(T ref)
          Extended to evict the element at the tail of the buffer iff the buffer is full.
 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()
          The listener specified to the constructor.
 int nscan()
          The #of references that are tested on append requests.
 
Methods inherited from class com.bigdata.cache.RingBuffer
addAll, capacity, clear, clear, contains, containsAll, element, get, isEmpty, isFull, iterator, offer, peek, poll, remove, remove, removeAll, retainAll, scanHead, scanTail, size, toArray, toArray
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.bigdata.cache.IHardReferenceQueue
capacity, clear, isEmpty, isFull, peek, size
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Field Detail

nscan

protected final int nscan
The #of references to scan backwards from the LRU position when testing for whether or not a reference is already in the cache.

Constructor Detail

HardReferenceQueue

public HardReferenceQueue(HardReferenceQueueEvictionListener<T> listener,
                          int capacity)
Uses the default #of references to scan on append requests.

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.

HardReferenceQueue

public HardReferenceQueue(HardReferenceQueueEvictionListener<T> listener,
                          int capacity,
                          int nscan)
Fully specified ctor.

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

getListener

public final HardReferenceQueueEvictionListener<T> getListener()
The listener specified to the constructor.


nscan

public final int nscan()
The #of references that are tested on append requests.

Specified by:
nscan in interface IHardReferenceQueue<T>

add

public boolean add(T ref)
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>
Specified by:
add in interface Collection<T>
Specified by:
add in interface Queue<T>
Overrides:
add in class RingBuffer<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.

beforeOffer

protected void beforeOffer(T ref)
Extended to evict the element at the tail of the buffer iff the buffer is full.

Note: This hook is further extended to realize the stale reference protocol in SynchronizedHardReferenceQueueWithTimeout.

Overrides:
beforeOffer in class RingBuffer<T>

evict

public final boolean evict()
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 final void evictAll(boolean clearRefs)
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.


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