1 package net.sf.ehcache.store.offheap;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import net.sf.ehcache.CacheManager;
23 import net.sf.ehcache.Ehcache;
24 import net.sf.ehcache.Element;
25 import net.sf.ehcache.config.CacheConfiguration;
26 import net.sf.ehcache.pool.Pool;
27 import net.sf.ehcache.pool.PoolableStore;
28 import net.sf.ehcache.pool.impl.UnboundedPool;
29 import net.sf.ehcache.search.NullResults;
30 import net.sf.ehcache.search.Results;
31 import net.sf.ehcache.search.attribute.AttributeExtractor;
32 import net.sf.ehcache.search.attribute.DynamicAttributesExtractor;
33 import net.sf.ehcache.search.impl.SearchManager;
34 import net.sf.ehcache.store.FrontEndCacheTier;
35 import net.sf.ehcache.store.MemoryStore;
36 import net.sf.ehcache.store.Store;
37 import net.sf.ehcache.store.StoreQuery;
38 import net.sf.ehcache.store.disk.DiskStore;
39 import org.apache.directmemory.ehcache.DirectMemoryStore;
40
41 import java.util.Map;
42 import java.util.WeakHashMap;
43
44
45
46
47
48
49
50
51 public class OffHeapStore
52 {
53
54 private static final WeakHashMap<CacheManager, Pool<PoolableStore>> OFFHEAP_POOLS =
55 new WeakHashMap<CacheManager, Pool<PoolableStore>>();
56
57 public static Store create( Ehcache cache, String diskStorePath, Pool<PoolableStore> onHeapPool,
58 Pool<PoolableStore> onDiskPool )
59 {
60
61 CacheConfiguration config = cache.getCacheConfiguration();
62 MemoryStore memoryStore = createMemoryStore( cache, onHeapPool );
63 DirectMemoryStore offHeapStore = createOffHeapStore( cache );
64 DiskStore diskStore = null;
65 Store store = null;
66 if ( diskStore == null )
67 {
68 store = new FrontEndCacheTier<MemoryStore, DirectMemoryStore>( memoryStore, offHeapStore,
69 config.getCopyStrategy(),
70 new MockSearchManager(),
71 config.isCopyOnWrite(),
72 config.isCopyOnRead() )
73 {
74
75 @Override
76 public Object getMBean()
77 {
78 return this.authority.getMBean();
79 }
80
81 };
82 }
83 return store;
84 }
85
86
87
88
89 private static class MockSearchManager implements SearchManager
90 {
91 @Override
92 public Results executeQuery( String s, StoreQuery storeQuery,
93 Map<String, AttributeExtractor> stringAttributeExtractorMap )
94 {
95 return new NullResults();
96 }
97
98 @Override
99 public void put( String s, int i, Element element, Map<String, AttributeExtractor> stringAttributeExtractorMap,
100 DynamicAttributesExtractor dynamicAttributesExtractor )
101 {
102
103 }
104
105 @Override
106 public void remove( String s, Object o, int i, boolean b )
107 {
108
109 }
110
111 @Override
112 public void clear( String s, int i )
113 {
114
115 }
116 }
117
118
119
120
121
122
123
124
125
126 public static Store create( Ehcache cache, String diskStorePath )
127 {
128 return create( cache, diskStorePath, new UnboundedPool(), new UnboundedPool() );
129 }
130
131 private static MemoryStore createMemoryStore( Ehcache cache, Pool<PoolableStore> onHeapPool )
132 {
133 return MemoryStore.create( cache, onHeapPool );
134 }
135
136
137
138
139
140
141 public static DirectMemoryStore createOffHeapStore( Ehcache cache )
142 {
143 Pool<PoolableStore> offHeapPool = null;
144 if ( cache.getCacheConfiguration().getMaxBytesLocalOffHeap() == 0L )
145 {
146 offHeapPool = getOffHeapPool( cache.getCacheManager() );
147 }
148 return new DirectMemoryStore( cache, offHeapPool );
149 }
150
151
152
153
154
155
156 public static Pool<PoolableStore> getOffHeapPool( CacheManager manager )
157 {
158 Pool<PoolableStore> pool;
159 synchronized ( OFFHEAP_POOLS )
160 {
161 pool = OFFHEAP_POOLS.get( manager );
162 if ( pool == null )
163 {
164 pool = new UnboundedPool();
165 OFFHEAP_POOLS.put( manager, pool );
166 }
167 }
168 return pool;
169 }
170
171 }