View Javadoc

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.store;
18  
19  import net.sf.ehcache.Ehcache;
20  import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
21  
22  /***
23   * Abstract class for BootstrapCacheLoader implementers that should alter their load behavior (probably stop loading)
24   * whenever the cache being bootstrapped has reached its in-memory limit (off- or on-heap)
25   *
26   * @author Alex Snaps
27   */
28  public abstract class MemoryLimitedCacheLoader implements BootstrapCacheLoader, Cloneable {
29  
30      /***
31       * Checks whether the cache has reached the limit configured for in-memory storage
32       *
33       * @param cache          The cache being loaded and to be checked for limit being reached
34       * @param loadedElements amounts of elements loaded so far
35       * @return true if on-heap or off-heap limit has been reached, false otherwise
36       */
37      protected boolean isInMemoryLimitReached(final Ehcache cache, final int loadedElements) {
38  
39          long maxBytesInMem;
40          long maxElementsInMem;
41          final boolean overflowToOffHeap = cache.getCacheConfiguration().isOverflowToOffHeap();
42          if (overflowToOffHeap) {
43              maxBytesInMem = cache.getCacheConfiguration().getMaxBytesLocalHeap();
44              maxElementsInMem = cache.getCacheConfiguration().getMaxEntriesLocalHeap() == 0
45                  ? Integer.MAX_VALUE : cache.getCacheConfiguration().getMaxEntriesLocalHeap();
46          } else {
47              maxBytesInMem = cache.getCacheConfiguration().getMaxBytesLocalOffHeap();
48              maxElementsInMem = cache.getCacheConfiguration().getMaxEntriesLocalHeap() == 0
49                  ? Integer.MAX_VALUE : cache.getCacheConfiguration().getMaxEntriesLocalHeap();
50          }
51  
52          if (maxBytesInMem != 0) {
53              final long inMemorySizeInBytes = overflowToOffHeap ? cache.calculateOffHeapSize() : cache.calculateInMemorySize();
54              final long avgSize = inMemorySizeInBytes / (overflowToOffHeap ? cache.getOffHeapStoreSize() : cache.getMemoryStoreSize());
55              return inMemorySizeInBytes + (avgSize * 2) >= maxBytesInMem;
56          } else {
57              return loadedElements >= maxElementsInMem;
58          }
59      }
60  
61      /***
62       * {@inheritDoc}
63       */
64      @Override
65      public Object clone() throws CloneNotSupportedException {
66          return super.clone();
67      }
68  }