Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 181   Methods: 6
NCLOC: 145   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LRUAlgorithm.java 79.5% 81.5% 100% 81.7%
coverage coverage
 1    /*
 2    * JBoss, the OpenSource J2EE webOS
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    * Created on March 25 2003
 7    */
 8    package org.jboss.cache.eviction;
 9   
 10    import org.apache.commons.logging.Log;
 11    import org.apache.commons.logging.LogFactory;
 12    import org.jboss.cache.Region;
 13   
 14    import java.util.Iterator;
 15   
 16    /**
 17    * Least recently Used algorithm to purge old data.
 18    * Note that this algorithm is not thread-safe.
 19    *
 20    * @author Ben Wang 02-2004
 21    * @author Daniel Huang - dhuang@jboss.org
 22    */
 23    public class LRUAlgorithm extends BaseEvictionAlgorithm implements EvictionAlgorithm
 24    {
 25    private static final Log log = LogFactory.getLog(LRUAlgorithm.class);
 26   
 27  4045 public LRUAlgorithm()
 28    {
 29  4045 super();
 30    }
 31   
 32  204 protected EvictionQueue setupEvictionQueue(Region region) throws EvictionException
 33    {
 34  204 return new LRUQueue();
 35    }
 36   
 37  6456 protected boolean shouldEvictNode(NodeEntry entry)
 38    {
 39  6456 LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig();
 40    // no idle or max time limit
 41  92 if (config.getTimeToLiveSeconds() == 0 && config.getMaxAgeSeconds() == 0) return false;
 42   
 43  6364 long currentTime = System.currentTimeMillis();
 44  6364 if (config.getTimeToLiveSeconds() != 0)
 45    {
 46  6352 long idleTime = currentTime - entry.getModifiedTimeStamp();
 47  6352 if (log.isTraceEnabled())
 48    {
 49  0 log.trace("Node " + entry.getFqn() + " has been idle for " + idleTime + "ms");
 50    }
 51  6352 if ((idleTime >= (config.getTimeToLiveSeconds() * 1000)))
 52    {
 53  5994 if (log.isTraceEnabled())
 54    {
 55  0 log.trace("Node " + entry.getFqn() + " should be evicted because of idle time");
 56    }
 57  5994 return true;
 58    }
 59    }
 60   
 61  370 if (config.getMaxAgeSeconds() != 0)
 62    {
 63  34 long objectLifeTime = currentTime - entry.getCreationTimeStamp();
 64  34 if (log.isTraceEnabled())
 65    {
 66  0 log.trace("Node " + entry.getFqn() + " has been alive for " + objectLifeTime + "ms");
 67    }
 68  34 if ((objectLifeTime >= (config.getMaxAgeSeconds() * 1000)))
 69    {
 70  8 if (log.isTraceEnabled())
 71    {
 72  0 log.trace("Node " + entry.getFqn() + " should be evicted because of max age");
 73    }
 74  8 return true;
 75    }
 76    }
 77   
 78  362 if (log.isTraceEnabled())
 79    {
 80  0 log.trace("Node " + entry.getFqn() + " should not be evicted");
 81    }
 82  362 return false;
 83    }
 84   
 85  48333 protected void evict(NodeEntry ne)
 86    {
 87    // NodeEntry ne = evictionQueue.getNodeEntry(fqn);
 88  48333 if (ne != null)
 89    {
 90    // evictionQueue.removeNodeEntry(ne);
 91  48333 if (!this.evictCacheNode(ne.getFqn()))
 92    {
 93  0 try
 94    {
 95  0 recycleQueue.put(ne.getFqn());
 96    }
 97    catch (InterruptedException e)
 98    {
 99  0 log.debug("InterruptedException", e);
 100    }
 101    }
 102    }
 103    }
 104   
 105  797 protected void prune() throws EvictionException
 106    {
 107  797 LRUQueue lruQueue = (LRUQueue) evictionQueue;
 108  797 NodeEntry ne;
 109  797 Iterator it = lruQueue.iterateLRUQueue();
 110  797 while (it.hasNext())
 111    {
 112  6230 ne = (NodeEntry) it.next();
 113  6230 if (isNodeInUseAndNotTimedOut(ne))
 114    {
 115  1 continue;
 116    }
 117   
 118  6229 if (this.shouldEvictNode(ne))
 119    {
 120  6002 it.remove();
 121  6002 lruQueue.removeNodeEntryFromMaxAge(ne);
 122  6002 this.evict(ne);
 123    }
 124    else
 125    {
 126  227 break;
 127    }
 128    }
 129   
 130  797 it = lruQueue.iterateMaxAgeQueue();
 131  797 while (it.hasNext())
 132    {
 133  228 ne = (NodeEntry) it.next();
 134  228 if (isNodeInUseAndNotTimedOut(ne))
 135    {
 136  1 continue;
 137    }
 138   
 139  227 if (this.shouldEvictNode(ne))
 140    {
 141  0 it.remove();
 142  0 lruQueue.removeNodeEntryFromLRU(ne);
 143  0 this.evict(ne);
 144    }
 145    else
 146    {
 147  227 break;
 148    }
 149    }
 150   
 151  797 int maxNodes = this.getConfiguration().getMaxNodes();
 152  797 if (maxNodes <= 0)
 153    {
 154  14 return;
 155    }
 156   
 157  783 it = lruQueue.iterateLRUQueue();
 158  783 while (evictionQueue.getNumberOfNodes() > maxNodes)
 159    {
 160  42332 ne = (NodeEntry) it.next();
 161  42332 if (log.isTraceEnabled())
 162    {
 163  0 log.trace("Node " + ne.getFqn() + " will be evicted because of exceeding the maxNode limit." +
 164    " maxNode: " + maxNodes + " but current queue size is: " + evictionQueue.getNumberOfNodes());
 165    }
 166   
 167  42332 if (!this.isNodeInUseAndNotTimedOut(ne))
 168    {
 169  42331 it.remove();
 170  42331 lruQueue.removeNodeEntryFromMaxAge(ne);
 171  42331 this.evict(ne);
 172    }
 173    }
 174    }
 175   
 176  797 protected LRUConfiguration getConfiguration()
 177    {
 178  797 return (LRUConfiguration) region.getEvictionPolicyConfig();
 179    }
 180   
 181    }