1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.internal.cache; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.HashSet; |
25 |
|
import java.util.List; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
|
29 |
|
import org.xwiki.bridge.event.DocumentCreatedEvent; |
30 |
|
import org.xwiki.bridge.event.DocumentDeletedEvent; |
31 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
32 |
|
import org.xwiki.cache.Cache; |
33 |
|
import org.xwiki.cache.CacheException; |
34 |
|
import org.xwiki.cache.CacheManager; |
35 |
|
import org.xwiki.cache.config.CacheConfiguration; |
36 |
|
import org.xwiki.component.annotation.Component; |
37 |
|
import org.xwiki.component.annotation.InstantiationStrategy; |
38 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
39 |
|
import org.xwiki.model.reference.DocumentReference; |
40 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
41 |
|
import org.xwiki.observation.EventListener; |
42 |
|
import org.xwiki.observation.ObservationManager; |
43 |
|
import org.xwiki.observation.event.Event; |
44 |
|
|
45 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
@param |
53 |
|
@version |
54 |
|
@since |
55 |
|
|
56 |
|
@Component |
57 |
|
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) |
|
|
| 70.8% |
Uncovered Elements: 19 (65) |
Complexity: 16 |
Complexity Density: 0.38 |
|
58 |
|
public class DefaultDocumentCache<C> implements DocumentCache<C> |
59 |
|
{ |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private static final List<Event> EVENTS = Arrays.<Event>asList(new DocumentCreatedEvent(), |
64 |
|
new DocumentUpdatedEvent(), new DocumentDeletedEvent()); |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
@version |
70 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 3 |
Complexity Density: 0.75 |
|
71 |
|
protected class Listener implements EventListener |
72 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
24 |
@Override... |
74 |
|
public String getName() |
75 |
|
{ |
76 |
24 |
return DefaultDocumentCache.this.name; |
77 |
|
} |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
3 |
@Override... |
80 |
|
public List<Event> getEvents() |
81 |
|
{ |
82 |
3 |
return EVENTS; |
83 |
|
} |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
85 |
2 |
@Override... |
86 |
|
public void onEvent(Event event, Object source, Object data) |
87 |
|
{ |
88 |
2 |
XWikiDocument doc = (XWikiDocument) source; |
89 |
2 |
removeAll(doc.getDocumentReference()); |
90 |
|
} |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
protected Listener listener = new Listener(); |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
@Inject |
102 |
|
private CacheManager cacheManager; |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
@Inject |
108 |
|
private EntityReferenceSerializer<String> serializer; |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
@Inject |
114 |
|
private ObservationManager observationManager; |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
private Cache<C> cache; |
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
private Cache<Collection<String>> mappingCache; |
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
private String name; |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
131 |
3 |
@Override... |
132 |
|
public void create(CacheConfiguration cacheConfiguration) throws CacheException |
133 |
|
{ |
134 |
3 |
this.name = cacheConfiguration.getConfigurationId(); |
135 |
|
|
136 |
3 |
this.cache = this.cacheManager.createNewCache(cacheConfiguration); |
137 |
|
|
138 |
3 |
CacheConfiguration mappingCacheConfiguration = (CacheConfiguration) cacheConfiguration.clone(); |
139 |
3 |
mappingCacheConfiguration.setConfigurationId(cacheConfiguration.getConfigurationId() + ".mapping"); |
140 |
|
|
141 |
3 |
this.mappingCache = this.cacheManager.createNewCache(mappingCacheConfiguration); |
142 |
|
|
143 |
3 |
this.observationManager.addListener(this.listener); |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
148 |
8 |
@Override... |
149 |
|
public C get(DocumentReference documentReference, Object... extensions) |
150 |
|
{ |
151 |
8 |
return this.cache.get(getKey(documentReference, extensions)); |
152 |
|
} |
153 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
154 |
5 |
@Override... |
155 |
|
public void set(C data, DocumentReference documentReference, Object... extensions) |
156 |
|
{ |
157 |
5 |
String key = getKey(documentReference, extensions); |
158 |
5 |
this.cache.set(key, data); |
159 |
|
|
160 |
5 |
String documentReferenceString = this.serializer.serialize(documentReference); |
161 |
|
|
162 |
5 |
Collection<String> keys = this.mappingCache.get(documentReferenceString); |
163 |
|
|
164 |
5 |
if (keys == null) { |
165 |
3 |
keys = new HashSet<String>(); |
166 |
3 |
this.mappingCache.set(documentReferenceString, keys); |
167 |
|
} |
168 |
|
|
169 |
5 |
keys.add(key); |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
@param |
176 |
|
@param |
177 |
|
@return |
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
179 |
13 |
protected String getKey(DocumentReference documentReference, Object... extensions)... |
180 |
|
{ |
181 |
13 |
StringBuilder buffer = new StringBuilder(); |
182 |
|
|
183 |
13 |
if (extensions.length > 0) { |
184 |
9 |
buffer.append(escape(this.serializer.serialize(documentReference))); |
185 |
9 |
for (Object extension : extensions) { |
186 |
28 |
buffer.append(':'); |
187 |
28 |
buffer.append(escape(extension != null ? extension.toString() : "")); |
188 |
|
} |
189 |
|
} else { |
190 |
4 |
buffer.append(this.serializer.serialize(documentReference)); |
191 |
|
} |
192 |
|
|
193 |
13 |
return buffer.toString(); |
194 |
|
} |
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
@param |
200 |
|
@return |
201 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
202 |
37 |
private String escape(String str)... |
203 |
|
{ |
204 |
37 |
return str.replace("\\", "\\\\").replace(":", "\\:"); |
205 |
|
} |
206 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
207 |
0 |
@Override... |
208 |
|
public void remove(C data, DocumentReference documentReference, Object... extensions) |
209 |
|
{ |
210 |
0 |
String key = getKey(documentReference, extensions); |
211 |
0 |
this.cache.remove(key); |
212 |
|
|
213 |
0 |
String documentReferenceString = this.serializer.serialize(documentReference); |
214 |
|
|
215 |
0 |
Collection<String> keys = this.mappingCache.get(documentReferenceString); |
216 |
|
|
217 |
0 |
if (keys != null) { |
218 |
0 |
keys.remove(key); |
219 |
|
} |
220 |
|
} |
221 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
222 |
0 |
@Override... |
223 |
|
public void removeAll() |
224 |
|
{ |
225 |
0 |
if (this.cache != null) { |
226 |
0 |
this.cache.removeAll(); |
227 |
|
} |
228 |
0 |
if (this.mappingCache != null) { |
229 |
0 |
this.mappingCache.removeAll(); |
230 |
|
} |
231 |
|
} |
232 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
233 |
2 |
@Override... |
234 |
|
public void removeAll(DocumentReference documentReference) |
235 |
|
{ |
236 |
2 |
String documentReferenceString = this.serializer.serialize(documentReference); |
237 |
|
|
238 |
2 |
Collection<String> keys = this.mappingCache.get(documentReferenceString); |
239 |
|
|
240 |
2 |
if (keys != null) { |
241 |
2 |
for (String key : keys) { |
242 |
3 |
this.cache.remove(key); |
243 |
|
} |
244 |
|
|
245 |
2 |
this.mappingCache.remove(documentReferenceString); |
246 |
|
} |
247 |
|
} |
248 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
249 |
2 |
@Override... |
250 |
|
public void dispose() |
251 |
|
{ |
252 |
2 |
this.cache.dispose(); |
253 |
2 |
this.mappingCache.dispose(); |
254 |
|
} |
255 |
|
} |