1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.eviction; |
8 |
| |
9 |
| import org.jboss.cache.Fqn; |
10 |
| |
11 |
| import java.util.Collections; |
12 |
| import java.util.Comparator; |
13 |
| import java.util.HashMap; |
14 |
| import java.util.HashSet; |
15 |
| import java.util.Iterator; |
16 |
| import java.util.LinkedList; |
17 |
| import java.util.List; |
18 |
| import java.util.Map; |
19 |
| import java.util.NoSuchElementException; |
20 |
| import java.util.Set; |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| public class ElementSizeQueue implements SortedEvictionQueue |
27 |
| { |
28 |
| private Map<Fqn, NodeEntry> nodeMap; |
29 |
| private LinkedList<NodeEntry> evictionList; |
30 |
| private Comparator<NodeEntry> comparator; |
31 |
| |
32 |
| private Set<NodeEntry> removalQueue; |
33 |
| private int numElements = 0; |
34 |
| |
35 |
19
| ElementSizeQueue()
|
36 |
| { |
37 |
19
| nodeMap = new HashMap<Fqn, NodeEntry>();
|
38 |
19
| evictionList = new LinkedList<NodeEntry>();
|
39 |
19
| comparator = new MaxElementComparator();
|
40 |
19
| removalQueue = new HashSet<NodeEntry>();
|
41 |
| } |
42 |
| |
43 |
18
| public void resortEvictionQueue()
|
44 |
| { |
45 |
18
| Collections.sort(evictionList, comparator);
|
46 |
| } |
47 |
| |
48 |
7625
| public NodeEntry getFirstNodeEntry()
|
49 |
| { |
50 |
7625
| try
|
51 |
| { |
52 |
7625
| NodeEntry ne;
|
53 |
?
| while ((ne = evictionList.getFirst()) != null)
|
54 |
| { |
55 |
15196
| if (removalQueue.contains(ne))
|
56 |
| { |
57 |
7593
| evictionList.removeFirst();
|
58 |
7593
| removalQueue.remove(ne);
|
59 |
| } |
60 |
| else |
61 |
| { |
62 |
7603
| break;
|
63 |
| } |
64 |
| } |
65 |
7603
| return ne;
|
66 |
| } |
67 |
| catch (NoSuchElementException e) |
68 |
| { |
69 |
| |
70 |
| } |
71 |
| |
72 |
22
| return null;
|
73 |
| } |
74 |
| |
75 |
47914
| public NodeEntry getNodeEntry(Fqn fqn)
|
76 |
| { |
77 |
47914
| return nodeMap.get(fqn);
|
78 |
| } |
79 |
| |
80 |
13
| public NodeEntry getNodeEntry(String fqn)
|
81 |
| { |
82 |
13
| return this.getNodeEntry(Fqn.fromString(fqn));
|
83 |
| } |
84 |
| |
85 |
20639
| public boolean containsNodeEntry(NodeEntry entry)
|
86 |
| { |
87 |
20639
| Fqn fqn = entry.getFqn();
|
88 |
20639
| return this.getNodeEntry(fqn) != null;
|
89 |
| } |
90 |
| |
91 |
10104
| public void removeNodeEntry(NodeEntry entry)
|
92 |
| { |
93 |
10104
| NodeEntry ne = nodeMap.remove(entry.getFqn());
|
94 |
10104
| if (ne != null)
|
95 |
| { |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
10104
| this.removalQueue.add(ne);
|
102 |
| |
103 |
| |
104 |
| |
105 |
10104
| this.numElements -= ne.getNumberOfElements();
|
106 |
| } |
107 |
| } |
108 |
| |
109 |
18138
| public void addNodeEntry(NodeEntry entry)
|
110 |
| { |
111 |
18138
| if (!this.containsNodeEntry(entry))
|
112 |
| { |
113 |
18138
| Fqn fqn = entry.getFqn();
|
114 |
18138
| entry.queue = this;
|
115 |
18138
| nodeMap.put(fqn, entry);
|
116 |
18138
| evictionList.add(entry);
|
117 |
18138
| this.numElements += entry.getNumberOfElements();
|
118 |
| } |
119 |
| } |
120 |
| |
121 |
7127
| public int getNumberOfNodes()
|
122 |
| { |
123 |
7127
| return nodeMap.size();
|
124 |
| } |
125 |
| |
126 |
9
| public int getNumberOfElements()
|
127 |
| { |
128 |
9
| return this.numElements;
|
129 |
| } |
130 |
| |
131 |
953
| public void modifyElementCount(int difference)
|
132 |
| { |
133 |
953
| this.numElements += difference;
|
134 |
| } |
135 |
| |
136 |
0
| public void clear()
|
137 |
| { |
138 |
0
| nodeMap.clear();
|
139 |
0
| evictionList.clear();
|
140 |
0
| removalQueue.clear();
|
141 |
0
| this.numElements = 0;
|
142 |
| } |
143 |
| |
144 |
1
| final List<NodeEntry> getEvictionList()
|
145 |
| { |
146 |
1
| return evictionList;
|
147 |
| } |
148 |
| |
149 |
1
| final Set<NodeEntry> getRemovalQueue()
|
150 |
| { |
151 |
1
| return removalQueue;
|
152 |
| } |
153 |
| |
154 |
32
| final void prune()
|
155 |
| { |
156 |
32
| Iterator<NodeEntry> it = evictionList.iterator();
|
157 |
32
| while (it.hasNext() && removalQueue.size() > 0)
|
158 |
| { |
159 |
4999
| if (removalQueue.remove(it.next()))
|
160 |
| { |
161 |
2500
| it.remove();
|
162 |
| } |
163 |
| } |
164 |
| } |
165 |
| |
166 |
4
| public Iterator<NodeEntry> iterate()
|
167 |
| { |
168 |
4
| return evictionList.iterator();
|
169 |
| } |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
| static class MaxElementComparator implements Comparator<NodeEntry> |
183 |
| { |
184 |
19
| MaxElementComparator()
|
185 |
| { |
186 |
| } |
187 |
| |
188 |
30616
| public int compare(NodeEntry ne1, NodeEntry ne2)
|
189 |
| { |
190 |
30616
| if (ne1.equals(ne2))
|
191 |
| { |
192 |
0
| return 0;
|
193 |
| } |
194 |
| |
195 |
30616
| int neNumElements = ne1.getNumberOfElements();
|
196 |
30616
| int neNumElements2 = ne2.getNumberOfElements();
|
197 |
| |
198 |
30616
| if (neNumElements > neNumElements2)
|
199 |
| { |
200 |
1044
| return -1;
|
201 |
| } |
202 |
29572
| else if (neNumElements < neNumElements2)
|
203 |
| { |
204 |
2833
| return 1;
|
205 |
| } |
206 |
26739
| else if (neNumElements == neNumElements2)
|
207 |
| { |
208 |
26739
| return 0;
|
209 |
| } |
210 |
| |
211 |
0
| throw new RuntimeException("Should never reach this condition");
|
212 |
| } |
213 |
| |
214 |
| } |
215 |
| |
216 |
| } |
217 |
| |
218 |
| |