1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.eviction; |
8 |
| |
9 |
| import org.jboss.cache.Fqn; |
10 |
| |
11 |
| import java.util.HashMap; |
12 |
| import java.util.Iterator; |
13 |
| import java.util.Map; |
14 |
| import java.util.NoSuchElementException; |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| public class MRUQueue implements EvictionQueue |
28 |
| { |
29 |
| |
30 |
| |
31 |
| |
32 |
| Map<Fqn, EvictionListEntry> nodeMap; |
33 |
| EvictionQueueList list; |
34 |
| private int numElements = 0; |
35 |
| |
36 |
14
| MRUQueue()
|
37 |
| { |
38 |
14
| nodeMap = new HashMap<Fqn, EvictionListEntry>();
|
39 |
14
| list = new EvictionQueueList();
|
40 |
| } |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
54
| void moveToTopOfStack(Fqn fqn)
|
50 |
| { |
51 |
54
| EvictionListEntry le = nodeMap.remove(fqn);
|
52 |
54
| if (le != null)
|
53 |
| { |
54 |
54
| list.remove(le);
|
55 |
54
| list.addToTop(le);
|
56 |
54
| nodeMap.put(le.node.getFqn(), le);
|
57 |
| } |
58 |
| } |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
10461
| public NodeEntry getFirstNodeEntry()
|
68 |
| { |
69 |
10461
| try
|
70 |
| { |
71 |
10461
| return list.getFirst().node;
|
72 |
| } |
73 |
| catch (NoSuchElementException e) |
74 |
| { |
75 |
| |
76 |
| } |
77 |
| |
78 |
19
| return null;
|
79 |
| } |
80 |
| |
81 |
21021
| public NodeEntry getNodeEntry(Fqn fqn)
|
82 |
| { |
83 |
21021
| EvictionListEntry le = nodeMap.get(fqn);
|
84 |
21021
| if (le != null)
|
85 |
77
| return le.node;
|
86 |
| |
87 |
20944
| return null;
|
88 |
| } |
89 |
| |
90 |
12
| public NodeEntry getNodeEntry(String fqn)
|
91 |
| { |
92 |
12
| return this.getNodeEntry(Fqn.fromString(fqn));
|
93 |
| } |
94 |
| |
95 |
10663
| public boolean containsNodeEntry(NodeEntry entry)
|
96 |
| { |
97 |
10663
| return nodeMap.containsKey(entry.getFqn());
|
98 |
| } |
99 |
| |
100 |
10444
| public void removeNodeEntry(NodeEntry entry)
|
101 |
| { |
102 |
10444
| EvictionListEntry le = nodeMap.remove(entry.getFqn());
|
103 |
10444
| if (le != null)
|
104 |
| { |
105 |
10444
| list.remove(le);
|
106 |
10444
| this.numElements -= le.node.getNumberOfElements();
|
107 |
| } |
108 |
| } |
109 |
| |
110 |
10663
| public void addNodeEntry(NodeEntry entry)
|
111 |
| { |
112 |
10663
| if (!this.containsNodeEntry(entry))
|
113 |
| { |
114 |
10663
| entry.queue = this;
|
115 |
10663
| EvictionListEntry le = new EvictionListEntry(entry);
|
116 |
10663
| list.addToBottom(le);
|
117 |
10663
| nodeMap.put(entry.getFqn(), le);
|
118 |
10663
| this.numElements += entry.getNumberOfElements();
|
119 |
| } |
120 |
| } |
121 |
| |
122 |
10412
| public int getNumberOfNodes()
|
123 |
| { |
124 |
10412
| return list.size();
|
125 |
| } |
126 |
| |
127 |
6
| public int getNumberOfElements()
|
128 |
| { |
129 |
6
| return this.numElements;
|
130 |
| } |
131 |
| |
132 |
3
| public void modifyElementCount(int difference)
|
133 |
| { |
134 |
3
| this.numElements += difference;
|
135 |
| } |
136 |
| |
137 |
2
| public void clear()
|
138 |
| { |
139 |
2
| nodeMap.clear();
|
140 |
2
| list.clear();
|
141 |
2
| this.numElements = 0;
|
142 |
| } |
143 |
| |
144 |
0
| public Iterator iterate()
|
145 |
| { |
146 |
0
| return list.iterator();
|
147 |
| } |
148 |
| |
149 |
1
| public String toString()
|
150 |
| { |
151 |
1
| return list.toString();
|
152 |
| } |
153 |
| } |