~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/ehcache/constructs/nonstop/store/NonstopStoreImpl.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-05-06 14:53:07 UTC
  • mfrom: (1.1.7) (2.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130506145307-v5bhw5yu70re00l3
Tags: 2.6.7-1
* Team upload.
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
 
 *  Copyright 2003-2010 Terracotta, Inc.
 
2
 *  Copyright Terracotta, Inc.
3
3
 *
4
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
5
 *  you may not use this file except in compliance with the License.
34
34
import net.sf.ehcache.config.TimeoutBehaviorConfiguration.TimeoutBehaviorType;
35
35
import net.sf.ehcache.constructs.nonstop.ClusterOperation;
36
36
import net.sf.ehcache.constructs.nonstop.NonstopActiveDelegateHolder;
 
37
import net.sf.ehcache.constructs.nonstop.NonstopThread;
37
38
import net.sf.ehcache.constructs.nonstop.concurrency.ExplicitLockingContextThreadLocal;
38
39
import net.sf.ehcache.constructs.nonstop.concurrency.NonstopCacheLockProvider;
39
40
import net.sf.ehcache.search.Attribute;
80
81
            executorServiceStore = new ExecutorServiceStore(nonstopActiveDelegateHolder, nonstopConfig, this, cacheCluster,
81
82
                    explicitLockingContextThreadLocal);
82
83
        }
83
 
        this.nonstopCacheLockProvider = new NonstopCacheLockProvider(this, nonstopActiveDelegateHolder, explicitLockingContextThreadLocal);
 
84
        this.nonstopCacheLockProvider = new NonstopCacheLockProvider(this, nonstopActiveDelegateHolder, explicitLockingContextThreadLocal,
 
85
                nonstopConfig);
84
86
    }
85
87
 
86
88
    /**
220
222
        nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().expireElements();
221
223
    }
222
224
 
 
225
    private boolean isCurrentThreadNonstopThread() {
 
226
        return NonstopThread.isCurrentThreadNonstopThread();
 
227
    }
 
228
 
223
229
    // -------------------------------------------------------
224
230
    // All methods below delegate to the clusterAwareStore
225
231
    // -------------------------------------------------------
228
234
     * {@inheritDoc}
229
235
     */
230
236
    public void unpinAll() {
231
 
        executorServiceStore.unpinAll();
 
237
        if (isCurrentThreadNonstopThread()) {
 
238
            nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().unpinAll();
 
239
        } else {
 
240
            executorServiceStore.unpinAll();
 
241
        }
232
242
    }
233
243
 
234
244
    /**
235
245
     * {@inheritDoc}
236
246
     */
237
247
    public boolean isPinned(Object key) {
238
 
        return executorServiceStore.isPinned(key);
 
248
        if (isCurrentThreadNonstopThread()) {
 
249
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().isPinned(key);
 
250
        } else {
 
251
            return executorServiceStore.isPinned(key);
 
252
        }
239
253
    }
240
254
 
241
255
    /**
242
256
     * {@inheritDoc}
243
257
     */
244
258
    public void setPinned(Object key, boolean pinned) {
245
 
        executorServiceStore.setPinned(key, pinned);
 
259
        if (isCurrentThreadNonstopThread()) {
 
260
            nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().setPinned(key, pinned);
 
261
        } else {
 
262
            executorServiceStore.setPinned(key, pinned);
 
263
        }
246
264
    }
247
265
 
248
266
    /**
249
267
     * {@inheritDoc}
250
268
     */
251
269
    public void addStoreListener(StoreListener listener) {
252
 
        executorServiceStore.addStoreListener(listener);
 
270
        if (isCurrentThreadNonstopThread()) {
 
271
            nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().addStoreListener(listener);
 
272
        } else {
 
273
            executorServiceStore.addStoreListener(listener);
 
274
        }
253
275
    }
254
276
 
255
277
    /**
256
278
     * {@inheritDoc}
257
279
     */
258
280
    public void removeStoreListener(StoreListener listener) {
259
 
        executorServiceStore.removeStoreListener(listener);
 
281
        if (isCurrentThreadNonstopThread()) {
 
282
            nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().removeStoreListener(listener);
 
283
        } else {
 
284
            executorServiceStore.removeStoreListener(listener);
 
285
        }
260
286
    }
261
287
 
262
288
    /**
271
297
     * {@inheritDoc}
272
298
     */
273
299
    public boolean isCacheCoherent() {
274
 
        return executorServiceStore.isCacheCoherent();
 
300
        if (isCurrentThreadNonstopThread()) {
 
301
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().isCacheCoherent();
 
302
        } else {
 
303
            return executorServiceStore.isCacheCoherent();
 
304
        }
275
305
    }
276
306
 
277
307
    /**
278
308
     * {@inheritDoc}
279
309
     */
280
310
    public boolean isClusterCoherent() {
281
 
        return executorServiceStore.isClusterCoherent();
 
311
        if (isCurrentThreadNonstopThread()) {
 
312
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().isClusterCoherent();
 
313
        } else {
 
314
            return executorServiceStore.isClusterCoherent();
 
315
        }
282
316
    }
283
317
 
284
318
    /**
285
319
     * {@inheritDoc}
286
320
     */
287
321
    public boolean isNodeCoherent() {
288
 
        return executorServiceStore.isNodeCoherent();
 
322
        if (isCurrentThreadNonstopThread()) {
 
323
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().isNodeCoherent();
 
324
        } else {
 
325
            return executorServiceStore.isNodeCoherent();
 
326
        }
289
327
    }
290
328
 
291
329
    /**
292
330
     * {@inheritDoc}
293
331
     */
294
332
    public void setNodeCoherent(boolean coherent) throws UnsupportedOperationException {
295
 
        executorServiceStore.setNodeCoherent(coherent);
 
333
        if (isCurrentThreadNonstopThread()) {
 
334
            nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().setNodeCoherent(coherent);
 
335
        } else {
 
336
            executorServiceStore.setNodeCoherent(coherent);
 
337
        }
296
338
    }
297
339
 
298
340
    /**
299
341
     * {@inheritDoc}
300
342
     */
301
343
    public void dispose() {
302
 
        executorServiceStore.dispose();
 
344
        if (isCurrentThreadNonstopThread()) {
 
345
            nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().dispose();
 
346
        } else {
 
347
            executorServiceStore.dispose();
 
348
        }
303
349
    }
304
350
 
305
351
    /**
306
352
     * {@inheritDoc}
307
353
     */
308
354
    public boolean containsKey(Object key) {
309
 
        return executorServiceStore.containsKey(key);
 
355
        if (isCurrentThreadNonstopThread()) {
 
356
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().containsKey(key);
 
357
        } else {
 
358
            return executorServiceStore.containsKey(key);
 
359
        }
310
360
    }
311
361
 
312
362
    /**
313
363
     * {@inheritDoc}
314
364
     */
315
365
    public boolean containsKeyInMemory(Object key) {
316
 
        return executorServiceStore.containsKeyInMemory(key);
 
366
        if (isCurrentThreadNonstopThread()) {
 
367
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().containsKeyInMemory(key);
 
368
        } else {
 
369
            return executorServiceStore.containsKeyInMemory(key);
 
370
        }
317
371
    }
318
372
 
319
373
    /**
320
374
     * {@inheritDoc}
321
375
     */
322
376
    public Results executeQuery(StoreQuery query) {
323
 
        return executorServiceStore.executeQuery(query);
 
377
        if (isCurrentThreadNonstopThread()) {
 
378
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().executeQuery(query);
 
379
        } else {
 
380
            return executorServiceStore.executeQuery(query);
 
381
        }
324
382
    }
325
383
 
326
384
    /**
327
385
     * {@inheritDoc}
328
386
     */
329
387
    public <T> Attribute<T> getSearchAttribute(String attributeName) {
330
 
        return executorServiceStore.getSearchAttribute(attributeName);
 
388
        if (isCurrentThreadNonstopThread()) {
 
389
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().getSearchAttribute(attributeName);
 
390
        } else {
 
391
            return executorServiceStore.getSearchAttribute(attributeName);
 
392
        }
331
393
    }
332
394
 
333
395
    /**
334
396
     * {@inheritDoc}
335
397
     */
336
398
    public void flush() throws IOException {
337
 
        executorServiceStore.flush();
 
399
        if (isCurrentThreadNonstopThread()) {
 
400
            nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().flush();
 
401
        } else {
 
402
            executorServiceStore.flush();
 
403
        }
338
404
    }
339
405
 
340
406
    /**
341
407
     * {@inheritDoc}
342
408
     */
343
409
    public Element get(Object key) {
344
 
        return executorServiceStore.get(key);
 
410
        if (isCurrentThreadNonstopThread()) {
 
411
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().get(key);
 
412
        } else {
 
413
            return executorServiceStore.get(key);
 
414
        }
345
415
    }
346
416
 
347
417
    /**
348
418
     * {@inheritDoc}
349
419
     */
350
420
    public int getInMemorySize() {
351
 
        return executorServiceStore.getInMemorySize();
 
421
        if (isCurrentThreadNonstopThread()) {
 
422
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().getInMemorySize();
 
423
        } else {
 
424
            return executorServiceStore.getInMemorySize();
 
425
        }
352
426
    }
353
427
 
354
428
    /**
355
429
     * {@inheritDoc}
356
430
     */
357
431
    public long getInMemorySizeInBytes() {
358
 
        return executorServiceStore.getInMemorySizeInBytes();
 
432
        if (isCurrentThreadNonstopThread()) {
 
433
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().getInMemorySizeInBytes();
 
434
        } else {
 
435
            return executorServiceStore.getInMemorySizeInBytes();
 
436
        }
359
437
    }
360
438
 
361
439
    /**
362
440
     * {@inheritDoc}
363
441
     */
364
442
    public List getKeys() {
365
 
        return executorServiceStore.getKeys();
 
443
        if (isCurrentThreadNonstopThread()) {
 
444
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().getKeys();
 
445
        } else {
 
446
            return executorServiceStore.getKeys();
 
447
        }
366
448
    }
367
449
 
368
450
    /**
369
451
     * {@inheritDoc}
370
452
     */
371
453
    public Element getQuiet(Object key) {
372
 
        return executorServiceStore.getQuiet(key);
 
454
        if (isCurrentThreadNonstopThread()) {
 
455
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().getQuiet(key);
 
456
        } else {
 
457
            return executorServiceStore.getQuiet(key);
 
458
        }
373
459
    }
374
460
 
375
461
    /**
376
462
     * {@inheritDoc}
377
463
     */
378
464
    public Map<Object, Element> getAllQuiet(Collection<?> keys) {
379
 
        return executorServiceStore.getAllQuiet(keys);
 
465
        if (isCurrentThreadNonstopThread()) {
 
466
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().getAllQuiet(keys);
 
467
        } else {
 
468
            return executorServiceStore.getAllQuiet(keys);
 
469
        }
380
470
    }
381
471
 
382
472
    /**
383
473
     * {@inheritDoc}
384
474
     */
385
475
    public Map<Object, Element> getAll(Collection<?> keys) {
386
 
        return executorServiceStore.getAll(keys);
 
476
        if (isCurrentThreadNonstopThread()) {
 
477
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().getAll(keys);
 
478
        } else {
 
479
            return executorServiceStore.getAll(keys);
 
480
        }
387
481
    }
388
482
 
389
483
    /**
390
484
     * {@inheritDoc}
391
485
     */
392
486
    public int getSize() {
393
 
        return executorServiceStore.getSize();
 
487
        if (isCurrentThreadNonstopThread()) {
 
488
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().getSize();
 
489
        } else {
 
490
            return executorServiceStore.getSize();
 
491
        }
394
492
    }
395
493
 
396
494
    /**
397
495
     * {@inheritDoc}
398
496
     */
399
497
    public int getTerracottaClusteredSize() {
400
 
        return executorServiceStore.getTerracottaClusteredSize();
 
498
        if (isCurrentThreadNonstopThread()) {
 
499
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().getTerracottaClusteredSize();
 
500
        } else {
 
501
            return executorServiceStore.getTerracottaClusteredSize();
 
502
        }
401
503
    }
402
504
 
403
505
    /**
404
506
     * {@inheritDoc}
405
507
     */
406
508
    public boolean put(Element element) throws CacheException {
407
 
        return executorServiceStore.put(element);
 
509
        if (isCurrentThreadNonstopThread()) {
 
510
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().put(element);
 
511
        } else {
 
512
            return executorServiceStore.put(element);
 
513
        }
408
514
    }
409
515
 
410
516
    /**
411
517
     * {@inheritDoc}
412
518
     */
413
519
    public void putAll(Collection<Element> elements) throws CacheException {
414
 
        executorServiceStore.putAll(elements);
 
520
        if (isCurrentThreadNonstopThread()) {
 
521
            nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().putAll(elements);
 
522
        } else {
 
523
            executorServiceStore.putAll(elements);
 
524
        }
415
525
    }
416
526
 
417
527
    /**
418
528
     * {@inheritDoc}
419
529
     */
420
530
    public Element putIfAbsent(Element element) throws NullPointerException {
421
 
        return executorServiceStore.putIfAbsent(element);
 
531
        if (isCurrentThreadNonstopThread()) {
 
532
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().putIfAbsent(element);
 
533
        } else {
 
534
            return executorServiceStore.putIfAbsent(element);
 
535
        }
422
536
    }
423
537
 
424
538
    /**
425
539
     * {@inheritDoc}
426
540
     */
427
541
    public boolean putWithWriter(Element element, CacheWriterManager writerManager) throws CacheException {
428
 
        return executorServiceStore.putWithWriter(element, writerManager);
 
542
        if (isCurrentThreadNonstopThread()) {
 
543
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().putWithWriter(element, writerManager);
 
544
        } else {
 
545
            return executorServiceStore.putWithWriter(element, writerManager);
 
546
        }
429
547
    }
430
548
 
431
549
    /**
432
550
     * {@inheritDoc}
433
551
     */
434
552
    public Element remove(Object key) {
435
 
        return executorServiceStore.remove(key);
 
553
        if (isCurrentThreadNonstopThread()) {
 
554
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().remove(key);
 
555
        } else {
 
556
            return executorServiceStore.remove(key);
 
557
        }
436
558
    }
437
559
 
438
560
    /**
439
561
     * {@inheritDoc}
440
562
     */
441
563
    public void removeAll(final Collection<?> keys) {
442
 
        executorServiceStore.removeAll(keys);
 
564
        if (isCurrentThreadNonstopThread()) {
 
565
            nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().removeAll(keys);
 
566
        } else {
 
567
            executorServiceStore.removeAll(keys);
 
568
        }
443
569
    }
444
570
 
445
571
    /**
446
572
     * {@inheritDoc}
447
573
     */
448
574
    public void removeAll() throws CacheException {
449
 
        executorServiceStore.removeAll();
 
575
        if (isCurrentThreadNonstopThread()) {
 
576
            nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().removeAll();
 
577
        } else {
 
578
            executorServiceStore.removeAll();
 
579
        }
450
580
    }
451
581
 
452
582
    /**
453
583
     * {@inheritDoc}
454
584
     */
455
585
    public Element removeElement(Element element, ElementValueComparator comparator) throws NullPointerException {
456
 
        return executorServiceStore.removeElement(element, comparator);
 
586
        if (isCurrentThreadNonstopThread()) {
 
587
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().removeElement(element, comparator);
 
588
        } else {
 
589
            return executorServiceStore.removeElement(element, comparator);
 
590
        }
457
591
    }
458
592
 
459
593
    /**
460
594
     * {@inheritDoc}
461
595
     */
462
596
    public Element removeWithWriter(Object key, CacheWriterManager writerManager) throws CacheException {
463
 
        return executorServiceStore.removeWithWriter(key, writerManager);
 
597
        if (isCurrentThreadNonstopThread()) {
 
598
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().removeWithWriter(key, writerManager);
 
599
        } else {
 
600
            return executorServiceStore.removeWithWriter(key, writerManager);
 
601
        }
464
602
    }
465
603
 
466
604
    /**
468
606
     */
469
607
    public boolean replace(Element old, Element element, ElementValueComparator comparator) throws NullPointerException,
470
608
            IllegalArgumentException {
471
 
        return executorServiceStore.replace(old, element, comparator);
 
609
        if (isCurrentThreadNonstopThread()) {
 
610
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().replace(old, element, comparator);
 
611
        } else {
 
612
            return executorServiceStore.replace(old, element, comparator);
 
613
        }
472
614
    }
473
615
 
474
616
    /**
475
617
     * {@inheritDoc}
476
618
     */
477
619
    public Element replace(Element element) throws NullPointerException {
478
 
        return executorServiceStore.replace(element);
 
620
        if (isCurrentThreadNonstopThread()) {
 
621
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().replace(element);
 
622
        } else {
 
623
            return executorServiceStore.replace(element);
 
624
        }
479
625
    }
480
626
 
481
627
    /**
482
628
     * {@inheritDoc}
483
629
     */
484
630
    public Set getLocalKeys() {
485
 
        return executorServiceStore.getLocalKeys();
 
631
        if (isCurrentThreadNonstopThread()) {
 
632
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().getLocalKeys();
 
633
        } else {
 
634
            return executorServiceStore.getLocalKeys();
 
635
        }
486
636
    }
487
637
 
488
638
    /**
489
639
     * {@inheritDoc}
490
640
     */
491
641
    public CacheConfiguration.TransactionalMode getTransactionalMode() {
492
 
      return executorServiceStore.getTransactionalMode();
 
642
        if (isCurrentThreadNonstopThread()) {
 
643
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().getTransactionalMode();
 
644
        } else {
 
645
            return executorServiceStore.getTransactionalMode();
 
646
        }
493
647
    }
494
648
 
495
649
    /**
496
650
     * {@inheritDoc}
497
651
     */
498
652
    public Element unlockedGet(Object key) {
499
 
        return executorServiceStore.unlockedGet(key);
 
653
        if (isCurrentThreadNonstopThread()) {
 
654
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().unlockedGet(key);
 
655
        } else {
 
656
            return executorServiceStore.unlockedGet(key);
 
657
        }
500
658
    }
501
659
 
502
660
    /**
503
661
     * {@inheritDoc}
504
662
     */
505
663
    public Element unlockedGetQuiet(Object key) {
506
 
        return executorServiceStore.unlockedGetQuiet(key);
 
664
        if (isCurrentThreadNonstopThread()) {
 
665
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().unlockedGetQuiet(key);
 
666
        } else {
 
667
            return executorServiceStore.unlockedGetQuiet(key);
 
668
        }
507
669
    }
508
670
 
509
671
    /**
510
672
     * {@inheritDoc}
511
673
     */
512
674
    public Element unsafeGet(Object key) {
513
 
        return executorServiceStore.unsafeGet(key);
 
675
        if (isCurrentThreadNonstopThread()) {
 
676
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().unsafeGet(key);
 
677
        } else {
 
678
            return executorServiceStore.unsafeGet(key);
 
679
        }
514
680
    }
515
681
 
516
682
    /**
517
683
     * {@inheritDoc}
518
684
     */
519
685
    public Element unsafeGetQuiet(Object key) {
520
 
        return executorServiceStore.unsafeGetQuiet(key);
 
686
        if (isCurrentThreadNonstopThread()) {
 
687
            return nonstopActiveDelegateHolder.getUnderlyingTerracottaStore().unsafeGetQuiet(key);
 
688
        } else {
 
689
            return executorServiceStore.unsafeGetQuiet(key);
 
690
        }
521
691
    }
522
692
 
523
693
    /**
524
694
     * {@inheritDoc}
525
695
     */
526
696
    public <V> V executeClusterOperation(ClusterOperation<V> operation) {
527
 
        return executorServiceStore.executeClusterOperation(operation);
 
697
        if (isCurrentThreadNonstopThread()) {
 
698
            try {
 
699
                return operation.performClusterOperation();
 
700
            } catch (Exception e) {
 
701
                throw new RuntimeException(e);
 
702
            }
 
703
        } else {
 
704
            return executorServiceStore.executeClusterOperation(operation);
 
705
        }
528
706
    }
529
707
 
530
708
    /**