1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.constructs.nonstop.store;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import net.sf.ehcache.CacheException;
27 import net.sf.ehcache.Element;
28 import net.sf.ehcache.Status;
29 import net.sf.ehcache.config.TimeoutBehaviorConfiguration.TimeoutBehaviorType;
30 import net.sf.ehcache.constructs.nonstop.ClusterOperation;
31 import net.sf.ehcache.constructs.nonstop.NonstopActiveDelegateHolder;
32 import net.sf.ehcache.constructs.nonstop.NonstopTimeoutBehaviorFactory;
33 import net.sf.ehcache.search.Attribute;
34 import net.sf.ehcache.search.NullResults;
35 import net.sf.ehcache.search.Results;
36 import net.sf.ehcache.search.attribute.AttributeExtractor;
37 import net.sf.ehcache.store.ElementValueComparator;
38 import net.sf.ehcache.store.Policy;
39 import net.sf.ehcache.store.StoreListener;
40 import net.sf.ehcache.store.StoreQuery;
41 import net.sf.ehcache.writer.CacheWriterManager;
42
43 /***
44 * A {@link NonstopStore} implementation that returns the local value in the VM, if present, for get operations and no-op for put,
45 * remove and other operations
46 *
47 * @author Abhishek Sanoujam
48 *
49 */
50 public class LocalReadsOnTimeoutStore implements NonstopStore {
51
52 /***
53 * The {@link NonstopTimeoutBehaviorFactory} to create {@link LocalReadsOnTimeoutStore} stores
54 */
55 public static final NonstopTimeoutBehaviorFactory FACTORY = new NonstopTimeoutBehaviorFactory() {
56 public NonstopStore createNonstopTimeoutBehaviorStore(NonstopActiveDelegateHolder nonstopActiveDelegateHolder) {
57 return new LocalReadsOnTimeoutStore(nonstopActiveDelegateHolder);
58 }
59 };
60
61 private final NonstopActiveDelegateHolder nonstopActiveDelegateHolder;
62
63 /***
64 * Constructor accepting the {@link NonstopActiveDelegateHolder}
65 */
66 public LocalReadsOnTimeoutStore(NonstopActiveDelegateHolder nonstopActiveDelegateHolder) {
67 this.nonstopActiveDelegateHolder = nonstopActiveDelegateHolder;
68 }
69
70 /***
71 * {@inheritDoc}.
72 * <p>
73 * Uses the underlying store to get the local value present in the VM
74 */
75 public Element get(Object key) throws IllegalStateException, CacheException {
76 return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().unsafeGet(key);
77 }
78
79 /***
80 * {@inheritDoc}.
81 * <p>
82 * Uses the underlying store to get the local value present in the VM
83 */
84 public List getKeys() throws IllegalStateException, CacheException {
85 return Collections.unmodifiableList(new ArrayList(nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().getLocalKeys()));
86 }
87
88 /***
89 * {@inheritDoc}.
90 * <p>
91 * Uses the underlying store to get the local value present in the VM
92 */
93 public Element getQuiet(Object key) throws IllegalStateException, CacheException {
94 return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().unsafeGetQuiet(key);
95 }
96
97 /***
98 * {@inheritDoc}.
99 * <p>
100 * This is a no-op
101 */
102 public boolean put(Element element) throws IllegalArgumentException, IllegalStateException, CacheException {
103
104 return false;
105 }
106
107 /***
108 * {@inheritDoc}.
109 * <p>
110 * This is a no-op
111 */
112 public void putAll(Collection<Element> elements) throws CacheException {
113
114 }
115
116 /***
117 * {@inheritDoc}.
118 * <p>
119 * This is a no-op
120 */
121 public Element remove(Object key) throws IllegalStateException {
122
123 return null;
124 }
125
126 /***
127 * {@inheritDoc}.
128 * <p>
129 * This is a no-op
130 */
131 public void removeAll(final Collection<Object> keys) throws IllegalStateException {
132
133 }
134
135 /***
136 * {@inheritDoc}.
137 * <p>
138 * This is a no-op
139 */
140 public void removeAll() throws IllegalStateException, CacheException {
141
142 }
143
144 /***
145 * {@inheritDoc}.
146 * <p>
147 * This is a no-op
148 */
149 public void flush() throws IllegalStateException, CacheException {
150
151 }
152
153 /***
154 * {@inheritDoc}.
155 * <p>
156 * This is a no-op and always returns null
157 */
158 public Object getInternalContext() {
159 return null;
160 }
161
162 /***
163 * {@inheritDoc}.
164 * <p>
165 * This is a no-op and always returns zero
166 */
167 public int getSize() throws IllegalStateException, CacheException {
168 return getKeys().size();
169 }
170
171 /***
172 * {@inheritDoc}.
173 * <p>
174 * This is a no-op
175 */
176 public Element putIfAbsent(Element element) throws NullPointerException {
177 return null;
178 }
179
180 /***
181 * {@inheritDoc}.
182 * <p>
183 * This is a no-op
184 */
185 public Element replace(Element element) throws NullPointerException {
186 return null;
187 }
188
189 /***
190 * {@inheritDoc}.
191 * <p>
192 * This is a no-op
193 */
194 public void addStoreListener(StoreListener listener) {
195
196
197 }
198
199 /***
200 * {@inheritDoc}.
201 * <p>
202 * This is a no-op
203 */
204 public boolean bufferFull() {
205
206 return false;
207 }
208
209 /***
210 * {@inheritDoc}.
211 * <p>
212 * This is a no-op
213 */
214 public boolean containsKey(Object key) {
215
216 return false;
217 }
218
219 /***
220 * {@inheritDoc}.
221 * <p>
222 * This is a no-op
223 */
224 public boolean containsKeyInMemory(Object key) {
225
226 return false;
227 }
228
229 /***
230 * {@inheritDoc}.
231 * <p>
232 * This is a no-op
233 */
234 public boolean containsKeyOffHeap(Object key) {
235
236 return false;
237 }
238
239 /***
240 * {@inheritDoc}.
241 * <p>
242 * This is a no-op
243 */
244 public boolean containsKeyOnDisk(Object key) {
245
246 return false;
247 }
248
249 /***
250 * {@inheritDoc}.
251 * <p>
252 * This is a no-op
253 */
254 public void dispose() {
255
256
257 }
258
259 /***
260 * {@inheritDoc}.
261 */
262 public Results executeQuery(StoreQuery query) {
263 return NullResults.INSTANCE;
264 }
265
266 /***
267 * {@inheritDoc}.
268 * <p>
269 * This is a no-op
270 */
271 public void expireElements() {
272
273
274 }
275
276 /***
277 * {@inheritDoc}.
278 * <p>
279 * This is a no-op
280 */
281 public Policy getInMemoryEvictionPolicy() {
282
283 return null;
284 }
285
286 /***
287 * {@inheritDoc}.
288 * <p>
289 * This is a no-op
290 */
291 public int getInMemorySize() {
292
293 return 0;
294 }
295
296 /***
297 * {@inheritDoc}.
298 * <p>
299 * This is a no-op
300 */
301 public long getInMemorySizeInBytes() {
302
303 return 0;
304 }
305
306 /***
307 * {@inheritDoc}.
308 * <p>
309 * This is a no-op
310 */
311 public Object getMBean() {
312
313 return null;
314 }
315
316 /***
317 * {@inheritDoc}.
318 * <p>
319 * This is a no-op
320 */
321 public int getOffHeapSize() {
322
323 return 0;
324 }
325
326 /***
327 * {@inheritDoc}.
328 * <p>
329 * This is a no-op
330 */
331 public long getOffHeapSizeInBytes() {
332
333 return 0;
334 }
335
336 /***
337 * {@inheritDoc}.
338 * <p>
339 * This is a no-op
340 */
341 public int getOnDiskSize() {
342
343 return 0;
344 }
345
346 /***
347 * {@inheritDoc}.
348 * <p>
349 * This is a no-op
350 */
351 public long getOnDiskSizeInBytes() {
352
353 return 0;
354 }
355
356 /***
357 * {@inheritDoc}.
358 * <p>
359 * This is a no-op
360 */
361 public Status getStatus() {
362
363 return null;
364 }
365
366 /***
367 * {@inheritDoc}.
368 * <p>
369 * This is a no-op
370 */
371 public int getTerracottaClusteredSize() {
372
373 return 0;
374 }
375
376 /***
377 * {@inheritDoc}.
378 * <p>
379 * This is a no-op
380 */
381 public boolean isCacheCoherent() {
382
383 return false;
384 }
385
386 /***
387 * {@inheritDoc}.
388 * <p>
389 * This is a no-op
390 */
391 public boolean isClusterCoherent() {
392
393 return false;
394 }
395
396 /***
397 * {@inheritDoc}.
398 * <p>
399 * This is a no-op
400 */
401 public boolean isNodeCoherent() {
402
403 return false;
404 }
405
406 /***
407 * {@inheritDoc}.
408 * <p>
409 * This is a no-op
410 */
411 public boolean putWithWriter(Element element, CacheWriterManager writerManager) throws CacheException {
412
413 return false;
414 }
415
416 /***
417 * {@inheritDoc}.
418 * <p>
419 * This is a no-op
420 */
421 public Element removeElement(Element element, ElementValueComparator comparator) throws NullPointerException {
422
423 return null;
424 }
425
426 /***
427 * {@inheritDoc}.
428 * <p>
429 * This is a no-op
430 */
431 public void removeStoreListener(StoreListener listener) {
432
433
434 }
435
436 /***
437 * {@inheritDoc}.
438 * <p>
439 * This is a no-op
440 */
441 public Element removeWithWriter(Object key, CacheWriterManager writerManager) throws CacheException {
442
443 return null;
444 }
445
446 /***
447 * {@inheritDoc}.
448 * <p>
449 * This is a no-op
450 */
451 public boolean replace(Element old, Element element, ElementValueComparator comparator) throws NullPointerException,
452 IllegalArgumentException {
453
454 return false;
455 }
456
457 /***
458 * {@inheritDoc}.
459 * <p>
460 * This is a no-op
461 */
462 public void setAttributeExtractors(Map<String, AttributeExtractor> extractors) {
463
464
465 }
466
467 /***
468 * {@inheritDoc}.
469 * <p>
470 * This is a no-op
471 */
472 public void setInMemoryEvictionPolicy(Policy policy) {
473
474
475 }
476
477 /***
478 * {@inheritDoc}.
479 * <p>
480 * This is a no-op
481 */
482 public void setNodeCoherent(boolean coherent) throws UnsupportedOperationException {
483
484
485 }
486
487 /***
488 * {@inheritDoc}.
489 * <p>
490 * This is a no-op
491 */
492 public void waitUntilClusterCoherent() throws UnsupportedOperationException {
493
494
495 }
496
497 /***
498 * {@inheritDoc}.
499 */
500 public <T> Attribute<T> getSearchAttribute(String attributeName) {
501 return new Attribute<T>(attributeName);
502 }
503
504 /***
505 * {@inheritDoc}
506 */
507 public Set getLocalKeys() {
508 return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().getLocalKeys();
509 }
510
511 /***
512 * {@inheritDoc}
513 */
514 public Element unlockedGet(Object key) {
515 return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().unsafeGet(key);
516 }
517
518 /***
519 * {@inheritDoc}
520 */
521 public Element unlockedGetQuiet(Object key) {
522 return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().unsafeGet(key);
523 }
524
525 /***
526 * {@inheritDoc}
527 */
528 public Element unsafeGet(Object key) {
529 return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().unsafeGet(key);
530 }
531
532 /***
533 * {@inheritDoc}
534 */
535 public Element unsafeGetQuiet(Object key) {
536 return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().unsafeGetQuiet(key);
537 }
538
539 /***
540 * {@inheritDoc}
541 */
542 public <V> V executeClusterOperation(ClusterOperation<V> operation) {
543 return operation.performClusterOperationTimedOut(TimeoutBehaviorType.LOCAL_READS);
544 }
545 }