Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 188   Methods: 19
NCLOC: 137   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LRUQueue.java 56.2% 77.6% 84.2% 75%
coverage coverage
 1    /*
 2    * JBoss, the OpenSource J2EE webOS
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache.eviction;
 8   
 9    import org.jboss.cache.Fqn;
 10   
 11    import java.util.Iterator;
 12    import java.util.LinkedHashMap;
 13    import java.util.Map;
 14   
 15    /**
 16    * LRU Eviction Queue implementation.
 17    * <p/>
 18    * This eviction queue will iterate properly through two sorted lists.
 19    * One sorted by maxAge and the other sorted by idleTime.
 20    *
 21    * @author Daniel Huang (dhuang@jboss.org)
 22    * @version $Revision: 1.5 $
 23    */
 24    public class LRUQueue implements EvictionQueue
 25    {
 26    private Map<Fqn, NodeEntry> maxAgeQueue;
 27    private Map<Fqn, NodeEntry> lruQueue;
 28    private long alternatingCount = 0;
 29    private int numElements = 0;
 30   
 31  209 LRUQueue()
 32    {
 33  209 maxAgeQueue = new LinkedHashMap<Fqn, NodeEntry>();
 34  209 lruQueue = new LinkedHashMap<Fqn, NodeEntry>(16, 0.75f, true);
 35    }
 36   
 37  300 void reorderByLRU(Fqn fqn)
 38    {
 39    // leave the max age queue alone - it is like a fifo.
 40   
 41    // the lru queue is access ordered. meaning the most recently read item is moved to the bottom of the queue.
 42    // simply calling get against it visits it and will cause LinkedHashMap to move it to the bottom of the queue.
 43  300 lruQueue.get(fqn);
 44    }
 45   
 46  1 public NodeEntry getFirstNodeEntry()
 47    {
 48    // because the underlying queue is in two differently sorted queues, we alternate between them when calling
 49    // a generic getFirstNodeEntry.
 50    // we must alternate to keep things balanced when evicting nodes based on the maxNodes attribute. We don't
 51    // want to just prune from one queue but rather we want to be able to prune from both.
 52  1 NodeEntry ne;
 53  1 if (alternatingCount % 2 == 0)
 54    {
 55  1 ne = this.getFirstLRUNodeEntry();
 56  1 if (ne == null)
 57    {
 58  1 ne = this.getFirstMaxAgeNodeEntry();
 59    }
 60    }
 61    else
 62    {
 63  0 ne = this.getFirstMaxAgeNodeEntry();
 64  0 if (ne == null)
 65    {
 66  0 ne = this.getFirstLRUNodeEntry();
 67    }
 68    }
 69  1 alternatingCount++;
 70  1 return ne;
 71    }
 72   
 73  204 public NodeEntry getFirstLRUNodeEntry()
 74    {
 75  204 if (lruQueue.size() > 0)
 76    {
 77  201 return lruQueue.values().iterator().next();
 78    }
 79   
 80  3 return null;
 81    }
 82   
 83  204 public NodeEntry getFirstMaxAgeNodeEntry()
 84    {
 85  204 if (maxAgeQueue.size() > 0)
 86    {
 87  201 return maxAgeQueue.values().iterator().next();
 88    }
 89   
 90  3 return null;
 91    }
 92   
 93  906552 public NodeEntry getNodeEntry(Fqn fqn)
 94    {
 95  906552 return lruQueue.get(fqn);
 96    }
 97   
 98  12 public NodeEntry getNodeEntry(String fqn)
 99    {
 100  12 return this.getNodeEntry(Fqn.fromString(fqn));
 101    }
 102   
 103  91417 public boolean containsNodeEntry(NodeEntry entry)
 104    {
 105  91417 return this.maxAgeQueue.containsKey(entry.getFqn());
 106    }
 107   
 108  0 void removeNodeEntryFromLRU(NodeEntry entry)
 109    {
 110  0 Fqn fqn = entry.getFqn();
 111  0 lruQueue.remove(fqn);
 112    }
 113   
 114  48333 void removeNodeEntryFromMaxAge(NodeEntry entry)
 115    {
 116  48333 Fqn fqn = entry.getFqn();
 117  48333 maxAgeQueue.remove(fqn);
 118    }
 119   
 120  10683 public void removeNodeEntry(NodeEntry entry)
 121    {
 122  10683 if (!this.containsNodeEntry(entry))
 123    {
 124  0 return;
 125    }
 126  10683 Fqn fqn = entry.getFqn();
 127  10683 NodeEntry ne1 = lruQueue.remove(fqn);
 128  10683 NodeEntry ne2 = maxAgeQueue.remove(fqn);
 129   
 130  10683 if (ne1 == null || ne2 == null)
 131    {
 132  0 throw new RuntimeException("The queues are out of sync.");
 133    }
 134   
 135  10683 this.numElements -= ne1.getNumberOfElements();
 136   
 137    }
 138   
 139  80732 public void addNodeEntry(NodeEntry entry)
 140    {
 141  80732 if (!this.containsNodeEntry(entry))
 142    {
 143  80732 Fqn fqn = entry.getFqn();
 144  80732 entry.queue = this;
 145  80732 maxAgeQueue.put(fqn, entry);
 146  80732 lruQueue.put(fqn, entry);
 147  80732 this.numElements += entry.getNumberOfElements();
 148    }
 149    }
 150   
 151  43349 public int getNumberOfNodes()
 152    {
 153  43349 return maxAgeQueue.size();
 154    }
 155   
 156  6 public int getNumberOfElements()
 157    {
 158  6 return this.numElements;
 159    }
 160   
 161  0 public void clear()
 162    {
 163  0 maxAgeQueue.clear();
 164  0 lruQueue.clear();
 165  0 this.numElements = 0;
 166    }
 167   
 168  119972 public void modifyElementCount(int difference)
 169    {
 170  119972 this.numElements += difference;
 171    }
 172   
 173  0 public Iterator<NodeEntry> iterate()
 174    {
 175  0 return lruQueue.values().iterator();
 176    }
 177   
 178  798 final Iterator<NodeEntry> iterateMaxAgeQueue()
 179    {
 180  798 return maxAgeQueue.values().iterator();
 181    }
 182   
 183  1581 final Iterator<NodeEntry> iterateLRUQueue()
 184    {
 185  1581 return lruQueue.values().iterator();
 186    }
 187   
 188    }