KickJava   Java API By Example, From Geeks To Geeks.

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


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 EDU.oswego.cs.dl.util.concurrent.BoundedBuffer;
11 import org.jboss.cache.Fqn;
12 import org.jboss.logging.Logger;
13
14 /**
15  * A region is a collection of tree cache nodes that share the same eviction
16  * policy configurations, e.g., maxNodes, etc. The region is specified via
17  * Fqn.
18  *
19  * @author Ben Wang 2-2004
20  */

21 public class Region
22 {
23
24    public static final long WAIT_TIME = 10000; // time to wait for the queue to free up
25

26    // Policy-specific config
27
private int maxNodes_ = 0; // 0 is no limit
28
private long timeToLiveSeconds_ = 0; // 0 is forever
29
// Region name
30
private String JavaDoc fqn_;
31    // Policy-spefic algorithm, e.g., LRU
32
private EvictionAlgorithm algorithm_;
33    // Parent policy provider
34
private EvictionPolicy policy_;
35    // These queues can do put and take concurrently.
36
protected BoundedBuffer nodeEventQueue_;
37    protected Logger log_ = Logger.getLogger(Region.class);
38
39    /**
40     * Default to package namespace on purpose so no one outside the package can instantiate it,
41     */

42    Region()
43    {
44       createQueue();
45    }
46
47    void createQueue()
48    {
49       nodeEventQueue_ = new BoundedBuffer(RegionManager.CAPACITY);
50    }
51
52    Region(String JavaDoc fqn, EvictionPolicy policy, EvictionAlgorithm algorithm) {
53       fqn_ = fqn;
54       policy_ = policy;
55       algorithm_ = algorithm;
56
57       createQueue();
58    }
59
60    public int getMaxNodes() { return maxNodes_; }
61
62    public void setMaxNodes(int maxNodes) { maxNodes_ = maxNodes; }
63
64 // public long getTimeToIdleSeconds() { return timeToLiveSeconds_; }
65

66 // public void setTimeToIdleSeconds(long secs) { timeToLiveSeconds_ = secs; }
67

68    public long getTimeToLiveSeconds() { return timeToLiveSeconds_; }
69
70    public void setTimeToLiveSeconds(long secs) { timeToLiveSeconds_ = secs; }
71
72    public EvictionAlgorithm getEvictionAlgorithm() { return algorithm_; }
73
74    public EvictionPolicy getEvictionPolicy() { return policy_; }
75
76    public String JavaDoc getFqn() { return fqn_; }
77
78    public void setAddedNode(Fqn fqn)
79    {
80       putNodeEvent(fqn, EvictedEventNode.ADD_EVENT);
81    }
82
83    public void setRemovedNode(Fqn fqn)
84    {
85       putNodeEvent(fqn, EvictedEventNode.REMOVE_EVENT);
86    }
87
88    public void setVisitedNode(Fqn fqn)
89    {
90       putNodeEvent(fqn, EvictedEventNode.VISIT_EVENT);
91    }
92
93    protected void putNodeEvent(Fqn fqn, Integer JavaDoc event) {
94       try {
95 // if(!nodeQueue_.offer(fqn, WAIT_TIME)) return;
96
if( nodeEventQueue_.size() > (100*RegionManager.CAPACITY)/98 ) {
97             log_.warn("putNodeEvent(): eviction node event queue size is at 98% threshold value of capacity: "
98                   +RegionManager.CAPACITY + " You will need to reduce the wakeUpIntervalSeconds parameter.");
99          }
100          nodeEventQueue_.put(new EvictedEventNode(fqn, event));
101       } catch (InterruptedException JavaDoc e) {
102          e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
103
}
104    }
105
106    /**
107     * Take the last node from node queue. It will also
108     * remove it from the queue.
109     * @return
110     */

111    public EvictedEventNode takeLastEventNode()
112    {
113       try {
114          return (EvictedEventNode)nodeEventQueue_.poll(0);
115       } catch (InterruptedException JavaDoc e) {
116          e.printStackTrace();
117       }
118       return null;
119    }
120
121    public int nodeEventQueueSize()
122    {
123       return nodeEventQueue_.size();
124    }
125
126    public void resetEvictionQueues()
127    {
128       BoundedBuffer q1 = nodeEventQueue_;
129       log_.info("reseteEvictionQueues(): node queue size: " +q1.size() +
130               " region name: " +fqn_);
131       createQueue();
132       // Help to recycle
133
for(int i=0; i < q1.size(); i++) {
134          try {
135             q1.take();
136          } catch (InterruptedException JavaDoc e) {
137             e.printStackTrace();
138          }
139       }
140    }
141
142    public String JavaDoc toString() {
143       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
144       buf.append("Regions--- fqn: ").append(getFqn()).append(" maxNodes " +getMaxNodes());
145       buf.append(" TimeToIdleSeconds " +getTimeToLiveSeconds());
146       return buf.toString();
147    }
148 }
149
Popular Tags