KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.jboss.cache.aop.AOPInstance;
11 import org.jboss.cache.aop.TreeCacheAop;
12
13 import java.util.Set JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 /**
17  * LRUAlgorithm specific to TreeCacheAop. Overriding couple of hooks to customize
18  * the algorithm such that it works correctly when using TreeCacheAop.
19  * The basic strategy for the AOP-specific case are:
20  * <ul>
21  * <li>When a node is visited, it will check if it is an AOPInstance node. If it
22  * is, then it is an AOP node. In that case, we will update all children nodes'
23  * time stamp to synchronize with parent node.</li>
24  * <li>When a node is to be evicted, it will check if it an AOP node. If it is,
25  * we will traverse through the children nodes to see if their timestamp is younger.
26  * If it is younger, then we must not evict the whol aop node (i.e., parent node is
27  * not evicted either). Furthermore, we should synchronize the whole tree.
28  * </ul>
29  *
30  * @author Ben Wang, Feb 17, 2004
31  */

32 public class AopLRUAlgorithm extends LRUAlgorithm
33 {
34    static final long TOLERANCE = 10; // millis
35

36
37    /**
38     * Hook for evict.
39     * @param fqn
40     * @return Set of associated node to evict as well in string
41     */

42    protected Set JavaDoc getAssociatedEvictNode(Fqn fqn) {
43       // That means even is child is touched, once the parent
44
// node is evicted, it will evict the children as well regardless of
45
// the time stamp.
46
return getChildrenNames(fqn);
47    }
48
49    /**
50     * Hook for processAddedNodes.
51     */

52    protected boolean preAddedNodes(Fqn fqn) {
53       // If this is a JBoss_Internal nodes. Don't evict.
54
if(fqn.getFqnChild(0).equals(TreeCacheAop.JBOSS_INTERNAL)) return false;
55       return true;
56    }
57
58    private boolean isAopNode(Fqn fqn) {
59       EvictionPolicy policy = region_.getEvictionPolicy();
60       return (policy.getCacheData(fqn, AOPInstance.KEY) == null);
61    }
62
63    /**
64     *
65     * @param fqn
66     * @return Set of Objects
67     */

68    private Set JavaDoc getChildrenNames(Fqn fqn) {
69       EvictionPolicy policy = region_.getEvictionPolicy();
70       return policy.getChildrenNames(fqn);
71    }
72
73    /**
74     * Hook for processVisitedNodes
75     * @param fqn
76     * @return true if it is successful
77     */

78    protected boolean preVisitedNodes(Fqn fqn, long stamp) {
79       // If AOPInstance exists, update all the children's time stamp.
80
if(isAopNode(fqn)) {
81          Set JavaDoc set = getChildrenNames(fqn);
82          Iterator JavaDoc it = set.iterator();
83          while(it.hasNext()) {
84             Object JavaDoc child = it.next();
85             Fqn childFqn = new Fqn(child);
86             if( !preVisitedNodes(childFqn, stamp) ) return false;
87             try {
88                updateChildTimeStamp(childFqn, stamp);
89             } catch (EvictionException e) {
90                e.printStackTrace();
91                return false;
92             }
93          }
94       }
95       return true;
96    }
97
98    private void updateChildTimeStamp(Fqn childFqn, long stamp) throws EvictionException
99    {
100       NodeEntry ne = (NodeEntry)nodeMap_.get(childFqn);
101       ne.setModifiedTimeStamp(stamp);
102       demote(childFqn);
103    }
104 }
105
Popular Tags