View Javadoc

1   package net.sf.ehcache.store.offheap;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * This class is simply a connector class into the EHCache for OffHeap.
46   * Until the issue (https://jira.terracotta.org/jira/browse/EHC-940) is fixed we need to use same package and class name
47   * as in ehcache
48   *
49   * @author michaelandrepearce
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; //need to implement disk backing to store.
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       * TODO create a real {@link SearchManager}
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             // no op
103         }
104 
105         @Override
106         public void remove( String s, Object o, int i, boolean b )
107         {
108             // no op
109         }
110 
111         @Override
112         public void clear( String s, int i )
113         {
114             // no op
115         }
116     }
117 
118     /**
119      * Creates a persitent-to-disk store for the given cache, using the given
120      * disk path. Heap and disk usage are not tracked by the returned store.
121      *
122      * @param cache         cache that fronts this store
123      * @param diskStorePath disk path to store data in
124      * @return a fully initialized store
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      * <b>do not use directly as can change</b>
138      * @param cache
139      * @return
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      * <b>do not use directly as can change</b>
153      * @param manager
154      * @return
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 }