1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.loader; |
8 |
| |
9 |
| import net.jcip.annotations.ThreadSafe; |
10 |
| import org.apache.commons.logging.Log; |
11 |
| import org.apache.commons.logging.LogFactory; |
12 |
| import org.jboss.cache.Fqn; |
13 |
| import org.jboss.cache.Modification; |
14 |
| import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig; |
15 |
| |
16 |
| import java.util.HashMap; |
17 |
| import java.util.HashSet; |
18 |
| import java.util.List; |
19 |
| import java.util.Map; |
20 |
| import java.util.Set; |
21 |
| import java.util.concurrent.ConcurrentHashMap; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| @ThreadSafe |
29 |
| public class DummyInMemoryCacheLoader extends AbstractCacheLoader |
30 |
| { |
31 |
| protected Map<Fqn, DummyNode> nodes = new ConcurrentHashMap<Fqn, DummyNode>(); |
32 |
| protected Log log = LogFactory.getLog(DummyInMemoryCacheLoader.class); |
33 |
| protected Map<Object, List<Modification>> transactions = new ConcurrentHashMap<Object, List<Modification>>(); |
34 |
| |
35 |
141
| public void setConfig(IndividualCacheLoaderConfig config)
|
36 |
| { |
37 |
| } |
38 |
| |
39 |
141
| public IndividualCacheLoaderConfig getConfig()
|
40 |
| { |
41 |
141
| return null;
|
42 |
| } |
43 |
| |
44 |
3328
| public Set getChildrenNames(Fqn fqn) throws Exception
|
45 |
| { |
46 |
0
| if (log.isDebugEnabled()) log.debug("Calling getChildrenNames on Fqn " + fqn + ". Data map = " + nodes);
|
47 |
3328
| if (!nodes.containsKey(fqn))
|
48 |
| { |
49 |
1413
| log.debug("node not in loader");
|
50 |
1413
| return null;
|
51 |
| } |
52 |
| |
53 |
1915
| Set children = findChildren(fqn);
|
54 |
1915
| log.debug("Fqn " + fqn + " has children " + children);
|
55 |
| |
56 |
1915
| return children.size() == 0 ? null : children;
|
57 |
| } |
58 |
| |
59 |
1915
| private Set findChildren(Fqn p)
|
60 |
| { |
61 |
1915
| Set c = new HashSet();
|
62 |
1915
| for (Fqn f : nodes.keySet())
|
63 |
| { |
64 |
181986
| if (!f.isRoot() && f.getParent().equals(p))
|
65 |
| { |
66 |
121
| c.add(f.getLastElement());
|
67 |
| } |
68 |
| } |
69 |
1915
| return c;
|
70 |
| } |
71 |
| |
72 |
4333
| public Map get(Fqn name) throws Exception
|
73 |
| { |
74 |
4333
| DummyNode dn = nodes.get(name);
|
75 |
4333
| Map d = dn != null ? dn.data : null;
|
76 |
| |
77 |
0
| if (log.isDebugEnabled()) log.debug("Getting data for fqn " + name + " = " + d);
|
78 |
4333
| return d;
|
79 |
| } |
80 |
| |
81 |
315392
| public boolean exists(Fqn name) throws Exception
|
82 |
| { |
83 |
315392
| return nodes.containsKey(name);
|
84 |
| } |
85 |
| |
86 |
2254
| public Object put(Fqn name, Object key, Object value) throws Exception
|
87 |
| { |
88 |
2254
| DummyNode n = nodes.get(name);
|
89 |
2254
| if (n == null)
|
90 |
| { |
91 |
204
| n = new DummyNode(name);
|
92 |
| } |
93 |
2254
| Object old = n.data.put(key, value);
|
94 |
2254
| nodes.put(name, n);
|
95 |
| |
96 |
2254
| recursivelyPutParentsIfNeeded(name);
|
97 |
0
| if (log.isDebugEnabled()) log.debug("Did a put on " + name + ", data is " + n.data);
|
98 |
2254
| return old;
|
99 |
| } |
100 |
| |
101 |
3409
| public void put(Fqn name, Map attributes) throws Exception
|
102 |
| { |
103 |
3409
| DummyNode n = nodes.get(name);
|
104 |
3409
| if (n == null)
|
105 |
| { |
106 |
1417
| n = new DummyNode(name);
|
107 |
| } |
108 |
3289
| if (attributes != null) n.data.putAll(attributes);
|
109 |
3409
| nodes.put(name, n);
|
110 |
| |
111 |
3409
| recursivelyPutParentsIfNeeded(name);
|
112 |
0
| if (log.isDebugEnabled()) log.debug("Did a put on " + name + ", data is " + n.data);
|
113 |
| } |
114 |
| |
115 |
6040
| private void recursivelyPutParentsIfNeeded(Fqn node)
|
116 |
| { |
117 |
6040
| Fqn parent = node.getParent();
|
118 |
5663
| if (nodes.containsKey(parent)) return;
|
119 |
| |
120 |
| |
121 |
377
| nodes.put(parent, new DummyNode(parent));
|
122 |
377
| recursivelyPutParentsIfNeeded(parent);
|
123 |
| } |
124 |
| |
125 |
2026
| public Object remove(Fqn fqn, Object key) throws Exception
|
126 |
| { |
127 |
2026
| log.debug("Removing data from " + fqn);
|
128 |
2026
| DummyNode n = nodes.get(fqn);
|
129 |
19
| if (n == null) n = new DummyNode(fqn);
|
130 |
2026
| Object old = n.data.remove(key);
|
131 |
2026
| nodes.put(fqn, n);
|
132 |
2026
| return old;
|
133 |
| } |
134 |
| |
135 |
3523
| public void remove(Fqn fqn) throws Exception
|
136 |
| { |
137 |
3523
| log.debug("Removing fqn " + fqn);
|
138 |
3523
| nodes.remove(fqn);
|
139 |
| |
140 |
3523
| recursivelyRemoveChildren(fqn);
|
141 |
| } |
142 |
| |
143 |
3835
| private void recursivelyRemoveChildren(Fqn removedParent)
|
144 |
| { |
145 |
3835
| for (Fqn f : nodes.keySet())
|
146 |
| { |
147 |
171405
| if (f.getParent().equals(removedParent))
|
148 |
| { |
149 |
| |
150 |
312
| nodes.remove(f);
|
151 |
| |
152 |
312
| recursivelyRemoveChildren(f);
|
153 |
| } |
154 |
| } |
155 |
| } |
156 |
| |
157 |
11
| public void removeData(Fqn fqn) throws Exception
|
158 |
| { |
159 |
11
| log.debug("Removing data from " + fqn);
|
160 |
11
| DummyNode n = nodes.get(fqn);
|
161 |
6
| if (n == null) n = new DummyNode(fqn);
|
162 |
11
| n.data.clear();
|
163 |
11
| nodes.put(fqn, n);
|
164 |
| } |
165 |
| |
166 |
| |
167 |
77
| public void prepare(Object tx, List<Modification> modifications, boolean one_phase) throws Exception
|
168 |
| { |
169 |
77
| if (one_phase)
|
170 |
| { |
171 |
3
| put(modifications);
|
172 |
| } |
173 |
| else |
174 |
| { |
175 |
74
| transactions.put(tx, modifications);
|
176 |
| } |
177 |
| } |
178 |
| |
179 |
72
| public void commit(Object tx) throws Exception
|
180 |
| { |
181 |
72
| List<Modification> modifications = transactions.remove(tx);
|
182 |
72
| if (modifications == null)
|
183 |
| { |
184 |
0
| throw new Exception("transaction " + tx + " not found in transaction table");
|
185 |
| } |
186 |
72
| put(modifications);
|
187 |
| } |
188 |
| |
189 |
2
| public void rollback(Object tx)
|
190 |
| { |
191 |
2
| transactions.remove(tx);
|
192 |
| } |
193 |
| |
194 |
141
| public void create() throws Exception
|
195 |
| { |
196 |
| } |
197 |
| |
198 |
141
| public void start() throws Exception
|
199 |
| { |
200 |
| } |
201 |
| |
202 |
133
| public void stop()
|
203 |
| { |
204 |
| } |
205 |
| |
206 |
133
| public void destroy()
|
207 |
| { |
208 |
| } |
209 |
| |
210 |
| |
211 |
| public class DummyNode |
212 |
| { |
213 |
| Map data = new HashMap(); |
214 |
| Fqn fqn; |
215 |
| |
216 |
2023
| public DummyNode(Fqn fqn)
|
217 |
| { |
218 |
2023
| this.fqn = fqn;
|
219 |
| } |
220 |
| |
221 |
24
| public String toString()
|
222 |
| { |
223 |
24
| return "Node{" +
|
224 |
| "data=" + data + |
225 |
| ", fqn=" + fqn + |
226 |
| '}'; |
227 |
| } |
228 |
| } |
229 |
| |
230 |
| |
231 |
8
| public String toString()
|
232 |
| { |
233 |
8
| return "DummyInMemoryCacheLoader{" +
|
234 |
| "nodes=" + nodes + |
235 |
| '}'; |
236 |
| } |
237 |
| } |