KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > eviction > BaseSortedEvictionAlgorithm


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.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.Region;
13
14
15 /**
16  * An abstract SortedEvictionAlgorithm.
17  * <p/>
18  * This class supports early termination of the eviction queue processing. Because the eviction
19  * queue is sorted by first to evict to last to evict, when iterating the eviction queue, the first time
20  * a node is encountered that does not require eviction will terminate the loop early. This way we don't incur
21  * the full breadth of the O(n) = n operation everytime we need to check for eviction (defined by eviction poll time
22  * interval).
23  *
24  * @author Daniel Huang - dhuang@jboss.org - 10/2005
25  */

26 public abstract class BaseSortedEvictionAlgorithm extends BaseEvictionAlgorithm implements EvictionAlgorithm
27 {
28    private static final Log log = LogFactory.getLog(BaseSortedEvictionAlgorithm.class);
29
30    public void process(Region region) throws EvictionException
31    {
32       super.process(region);
33    }
34
35    protected void processQueues(Region region) throws EvictionException
36    {
37       boolean evictionNodesModified = false;
38
39       EvictedEventNode node;
40       int count = 0;
41       while ((node = region.takeLastEventNode()) != null)
42       {
43          Fqn fqn = node.getFqn();
44
45          count++;
46          switch (node.getEventType())
47          {
48             case ADD_NODE_EVENT:
49                this.processAddedNodes(fqn, node.getElementDifference(), node.isResetElementCount());
50                evictionNodesModified = true;
51                break;
52             case REMOVE_NODE_EVENT:
53                this.processRemovedNodes(fqn);
54                break;
55             case VISIT_NODE_EVENT:
56                this.processVisitedNodes(fqn);
57                evictionNodesModified = true;
58                break;
59             case ADD_ELEMENT_EVENT:
60                this.processAddedElement(fqn);
61                evictionNodesModified = true;
62                break;
63             case REMOVE_ELEMENT_EVENT:
64                this.processRemovedElement(fqn);
65                evictionNodesModified = true;
66                break;
67             default:
68                throw new RuntimeException JavaDoc("Illegal Eviction Event type " + node.getEventType());
69          }
70       }
71
72       if (log.isTraceEnabled())
73       {
74          log.trace("Eviction nodes visited or added requires resort of queue " + evictionNodesModified);
75       }
76
77       this.resortEvictionQueue(evictionNodesModified);
78
79
80       if (log.isTraceEnabled())
81       {
82          log.trace("processed " + count + " node events");
83       }
84
85    }
86
87    /**
88     * This method is called to resort the queue after add or visit events have occurred.
89     * <p/>
90     * If the parameter is true, the queue needs to be resorted. If it is false, the queue does not
91     * need resorting.
92     *
93     * @param evictionQueueModified True if the queue was added to or visisted during event processing.
94     */

95    protected void resortEvictionQueue(boolean evictionQueueModified)
96    {
97       if (!evictionQueueModified)
98       {
99          if (log.isDebugEnabled())
100          {
101             log.debug("Eviction queue not modified. Resort unnecessary.");
102          }
103          return;
104       }
105       long begin = System.currentTimeMillis();
106       ((SortedEvictionQueue) evictionQueue).resortEvictionQueue();
107       long end = System.currentTimeMillis();
108
109       if (log.isTraceEnabled())
110       {
111          long diff = end - begin;
112          log.trace("Took " + diff + "ms to sort queue with " + getEvictionQueue().getNumberOfNodes() + " elements");
113       }
114    }
115
116 }
117
Popular Tags