1 /**
2  *  Copyright 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.store;
18
19 import net.sf.ehcache.CacheException;
20 import net.sf.ehcache.Ehcache;
21 import net.sf.ehcache.config.CacheConfiguration;
22 import net.sf.ehcache.pool.Pool;
23 import net.sf.ehcache.search.impl.SearchManager;
24 import net.sf.ehcache.store.disk.DiskStore;
25
26 import java.io.Serializable;
27
28 /**
29  * A tiered store using an in-memory cache of elements stored on disk.
30  *
31  * @author Ludovic Orban
32  */

33 public final class DiskBackedMemoryStore extends FrontEndCacheTier<MemoryStore, DiskStore> {
34
35     private DiskBackedMemoryStore(CacheConfiguration cacheConfiguration, MemoryStore cache, DiskStore authority, SearchManager searchManager) {
36         super(cache, authority, cacheConfiguration.getCopyStrategy(), searchManager,
37               cacheConfiguration.isCopyOnWrite(), cacheConfiguration.isCopyOnRead());
38     }
39
40     /**
41      * Create a DiskBackedMemoryStore instance
42      * @param cache the cache
43      * @param onHeapPool the pool tracking on-heap usage
44      * @param onDiskPool the pool tracking on-disk usage
45      * @return a DiskBackedMemoryStore instance
46      */

47     public static Store create(Ehcache cache, Pool onHeapPool, Pool onDiskPool) {
48         final MemoryStore memoryStore = createMemoryStore(cache, onHeapPool);
49         DiskStore diskStore = createDiskStore(cache, onHeapPool, onDiskPool);
50
51         return new DiskBackedMemoryStore(cache.getCacheConfiguration(), memoryStore, diskStore, null);
52     }
53
54     private static MemoryStore createMemoryStore(Ehcache cache, Pool onHeapPool) {
55         return MemoryStore.create(cache, onHeapPool);
56     }
57
58     private static DiskStore createDiskStore(Ehcache cache, Pool onHeapPool, Pool onDiskPool) {
59         CacheConfiguration config = cache.getCacheConfiguration();
60         if (config.isOverflowToDisk()) {
61             return DiskStore.create(cache, onHeapPool, onDiskPool);
62         } else {
63             throw new CacheException("DiskBackedMemoryStore can only be used when cache overflows to disk or is disk persistent");
64         }
65     }
66
67     /**
68      * {@inheritDoc}
69      */

70     public Object getMBean() {
71         return null;
72     }
73
74     /**
75      * {@inheritDoc}
76      */

77     @Override
78     public boolean notifyEvictionFromCache(final Serializable key) {
79         return authority.cleanUpFailedMarker(key);
80     }
81 }
82