org.jboss.cache.eviction
Class BaseEvictionAlgorithm

java.lang.Object
  extended by org.jboss.cache.eviction.BaseEvictionAlgorithm
All Implemented Interfaces:
EvictionAlgorithm
Direct Known Subclasses:
BaseSortedEvictionAlgorithm, ExpirationAlgorithm, FIFOAlgorithm, LRUAlgorithm, MRUAlgorithm

public abstract class BaseEvictionAlgorithm
extends java.lang.Object
implements EvictionAlgorithm

Abstract Event Processing Eviction Algorithm. This class is used to implement basic event processing for Eviction Algorithms. To extend this abstract class to make an Eviction Algorithm, implement the abstract methods and a policy.

Version:
$Revision: 1.23 $
Author:
Daniel Huang - dhuang@jboss.org 10/2005

Field Summary
protected  EvictionQueue evictionQueue
          Contains NodeEntry instances.
protected  java.util.concurrent.BlockingQueue<Fqn> recycleQueue
          Contains Fqn instances.
protected  Region region
          Mapped region.
 
Constructor Summary
protected BaseEvictionAlgorithm()
           
 
Method Summary
protected  void emptyRecycleQueue()
          Empty the Recycle Queue.
protected  void evict(NodeEntry ne)
           
protected  boolean evictCacheNode(Fqn fqn)
          Evict a node from cache.
 EvictionQueue getEvictionQueue()
          Get the underlying EvictionQueue implementation.
protected  void initialize(Region region)
           
protected  boolean isNodeInUseAndNotTimedOut(NodeEntry ne)
           
 void process(Region region)
          Process the given region.
protected  void processAddedElement(Fqn fqn)
           
protected  void processAddedNodes(Fqn fqn, int numAddedElements, boolean resetElementCount)
           
protected  void processMarkInUseNodes(Fqn fqn, long inUseTimeout)
           
protected  void processQueues(Region region)
          Event processing for Evict/Add/Visiting of nodes.
protected  void processRemovedElement(Fqn fqn)
           
protected  void processRemovedNodes(Fqn fqn)
          Remove a node from cache.
protected  void processUnmarkInUseNodes(Fqn fqn)
           
protected  void processVisitedNodes(Fqn fqn)
          Visit a node in cache.
protected  void prune()
           
 void resetEvictionQueue(Region region)
          Reset the whole eviction queue.
protected abstract  EvictionQueue setupEvictionQueue(Region region)
          This method will create an EvictionQueue implementation and prepare it for use.
protected abstract  boolean shouldEvictNode(NodeEntry ne)
          This method will check whether the given node should be evicted or not.
 java.lang.String toString()
          Returns debug information.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

region

protected Region region
Mapped region.


recycleQueue

protected java.util.concurrent.BlockingQueue<Fqn> recycleQueue
Contains Fqn instances.


evictionQueue

protected EvictionQueue evictionQueue
Contains NodeEntry instances.

Constructor Detail

BaseEvictionAlgorithm

protected BaseEvictionAlgorithm()
Method Detail

setupEvictionQueue

protected abstract EvictionQueue setupEvictionQueue(Region region)
                                             throws EvictionException
This method will create an EvictionQueue implementation and prepare it for use.

Parameters:
region - MarshRegion to setup an eviction queue for.
Returns:
The created EvictionQueue to be used as the eviction queue for this algorithm.
Throws:
EvictionException
See Also:
EvictionQueue

shouldEvictNode

protected abstract boolean shouldEvictNode(NodeEntry ne)
This method will check whether the given node should be evicted or not.

Parameters:
ne - NodeEntry to test eviction for.
Returns:
True if the given node should be evicted. False if the given node should not be evicted.

initialize

protected void initialize(Region region)
                   throws EvictionException
Throws:
EvictionException

process

public void process(Region region)
             throws EvictionException
Process the given region.

Eviction Processing encompasses the following:

- Add/Remove/Visit Nodes - Prune according to Eviction Algorithm - Empty/Retry the recycle queue of previously evicted but locked (during actual cache eviction) nodes.

Specified by:
process in interface EvictionAlgorithm
Parameters:
region - Cache region to process for eviction.
Throws:
EvictionException

resetEvictionQueue

public void resetEvictionQueue(Region region)
Description copied from interface: EvictionAlgorithm
Reset the whole eviction queue. Queue may needs to be reset due to corrupted state, for example.

Specified by:
resetEvictionQueue in interface EvictionAlgorithm
Parameters:
region - MarshRegion that this algorithm will operate on.

getEvictionQueue

public EvictionQueue getEvictionQueue()
Get the underlying EvictionQueue implementation.

Specified by:
getEvictionQueue in interface EvictionAlgorithm
Returns:
the EvictionQueue used by this algorithm
See Also:
EvictionQueue

processQueues

protected void processQueues(Region region)
                      throws EvictionException
Event processing for Evict/Add/Visiting of nodes.

- On AddEvents a new element is added into the eviction queue - On RemoveEvents, the removed element is removed from the eviction queue. - On VisitEvents, the visited node has its eviction statistics updated (idleTime, numberOfNodeVisists, etc..)

Parameters:
region - Cache region to process for eviction.
Throws:
EvictionException

evict

protected void evict(NodeEntry ne)

evictCacheNode

protected boolean evictCacheNode(Fqn fqn)
Evict a node from cache.

Parameters:
fqn - node corresponds to this fqn
Returns:
True if successful

processMarkInUseNodes

protected void processMarkInUseNodes(Fqn fqn,
                                     long inUseTimeout)
                              throws EvictionException
Throws:
EvictionException

processUnmarkInUseNodes

protected void processUnmarkInUseNodes(Fqn fqn)
                                throws EvictionException
Throws:
EvictionException

processAddedNodes

protected void processAddedNodes(Fqn fqn,
                                 int numAddedElements,
                                 boolean resetElementCount)
                          throws EvictionException
Throws:
EvictionException

processRemovedNodes

protected void processRemovedNodes(Fqn fqn)
                            throws EvictionException
Remove a node from cache.

This method will remove the node from the eviction queue as well as evict the node from cache.

If a node cannot be removed from cache, this method will remove it from the eviction queue and place the element into the recycleQueue. Each node in the recycle queue will get retried until proper cache eviction has taken place.

Because EvictionQueues are collections, when iterating them from an iterator, use iterator.remove() to avoid ConcurrentModificationExceptions. Use the boolean parameter to indicate the calling context.

Parameters:
fqn - FQN of the removed node
Throws:
EvictionException

processVisitedNodes

protected void processVisitedNodes(Fqn fqn)
                            throws EvictionException
Visit a node in cache.

This method will update the numVisits and modifiedTimestamp properties of the Node. These properties are used as statistics to determine eviction (LRU, LFU, MRU, etc..)

*Note* that this method updates Node Entries by reference and does not put them back into the queue. For some sorted collections, a remove, and a re-add is required to maintain the sorted order of the elements.

Parameters:
fqn - FQN of the visited node.
Throws:
EvictionException

processRemovedElement

protected void processRemovedElement(Fqn fqn)
                              throws EvictionException
Throws:
EvictionException

processAddedElement

protected void processAddedElement(Fqn fqn)
                            throws EvictionException
Throws:
EvictionException

emptyRecycleQueue

protected void emptyRecycleQueue()
                          throws EvictionException
Empty the Recycle Queue.

This method will go through the recycle queue and retry to evict the nodes from cache.

Throws:
EvictionException

isNodeInUseAndNotTimedOut

protected boolean isNodeInUseAndNotTimedOut(NodeEntry ne)

prune

protected void prune()
              throws EvictionException
Throws:
EvictionException

toString

public java.lang.String toString()
Returns debug information.

Overrides:
toString in class java.lang.Object