001 /*
002 * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003 *
004 * This file is part of the Java Data Mining Package (JDMP).
005 * See the NOTICE file distributed with this work for additional
006 * information regarding copyright ownership and licensing.
007 *
008 * JDMP is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * JDMP is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with JDMP; if not, write to the
020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021 * Boston, MA 02110-1301 USA
022 */
023
024 package org.jdmp.ehcache;
025
026 import java.io.Closeable;
027 import java.io.File;
028 import java.io.Flushable;
029 import java.io.IOException;
030 import java.util.Collection;
031 import java.util.HashSet;
032 import java.util.Map;
033 import java.util.Set;
034
035 import net.sf.ehcache.Cache;
036 import net.sf.ehcache.CacheManager;
037 import net.sf.ehcache.Element;
038 import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
039 import net.sf.ehcache.config.CacheConfiguration;
040 import net.sf.ehcache.config.Configuration;
041 import net.sf.ehcache.config.DiskStoreConfiguration;
042 import net.sf.ehcache.event.RegisteredEventListeners;
043 import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
044
045 import org.ujmp.core.exceptions.MatrixException;
046
047 public class EhcacheMap<K, V> implements Map<K, V>, Flushable, Closeable {
048 private static final long serialVersionUID = -2405059234958626645L;
049
050 private final int maxElementsInMemory = 1000;
051
052 private final int maxElementsOnDisk = Integer.MAX_VALUE - 1;
053
054 private final boolean overflowToDisk = true;
055
056 private final boolean eternal = true;
057
058 private final boolean diskPersistent = true;
059
060 private transient File diskStorePath = null;
061
062 private transient String name = null;
063
064 private transient final RegisteredEventListeners registeredEventListeners = null;
065
066 private final int timeToLiveSeconds = Integer.MAX_VALUE - 1;
067
068 private final int timeToIdleSeconds = 120;
069
070 private final int diskSpoolBufferSizeMB = 16;
071
072 private final int diskExpiryThreadIntervalSeconds = 300;
073
074 private final MemoryStoreEvictionPolicy memoryStoreEvictionPolicy = MemoryStoreEvictionPolicy.LFU;
075
076 private transient CacheManager manager = null;
077
078 private transient Cache cache = null;
079
080 private final BootstrapCacheLoader bootstrapCacheLoader = null;
081
082 public EhcacheMap() throws IOException {
083 this(null);
084 }
085
086 public EhcacheMap(String name) throws IOException {
087 this(name, null);
088 }
089
090 public EhcacheMap(String name, File path) throws IOException {
091 System.setProperty("net.sf.ehcache.enableShutdownHook", "true");
092 this.diskStorePath = path;
093 this.name = name;
094 }
095
096 public String getName() {
097 if (name == null) {
098 name = "ehcache" + System.nanoTime();
099 }
100 return name;
101 }
102
103 public File getPath() {
104 if (diskStorePath == null) {
105 try {
106 diskStorePath = File.createTempFile(getName(), ".tmp");
107 diskStorePath.delete();
108 diskStorePath.mkdir();
109 diskStorePath.deleteOnExit();
110 new File(diskStorePath + System.getProperty("file.separator") + getName() + ".data")
111 .deleteOnExit();
112 new File(diskStorePath + System.getProperty("file.separator") + getName()
113 + ".index").deleteOnExit();
114 } catch (Exception e) {
115 e.printStackTrace();
116 throw new RuntimeException(e);
117 }
118 }
119 return diskStorePath;
120 }
121
122 private CacheManager getCacheManager() {
123 if (manager == null) {
124
125 Configuration config = new Configuration();
126 CacheConfiguration cacheconfig = new CacheConfiguration();
127 cacheconfig.setDiskExpiryThreadIntervalSeconds(diskExpiryThreadIntervalSeconds);
128 cacheconfig.setDiskPersistent(diskPersistent);
129 cacheconfig.setEternal(eternal);
130 cacheconfig.setMaxElementsInMemory(maxElementsInMemory);
131 cacheconfig.setMaxElementsOnDisk(maxElementsOnDisk);
132 cacheconfig.setMemoryStoreEvictionPolicyFromObject(memoryStoreEvictionPolicy);
133 cacheconfig.setOverflowToDisk(overflowToDisk);
134 cacheconfig.setTimeToIdleSeconds(timeToIdleSeconds);
135 cacheconfig.setTimeToLiveSeconds(timeToLiveSeconds);
136
137 DiskStoreConfiguration diskStoreConfigurationParameter = new DiskStoreConfiguration();
138 diskStoreConfigurationParameter.setPath(getPath().getAbsolutePath());
139 config.addDiskStore(diskStoreConfigurationParameter);
140 config.setDefaultCacheConfiguration(cacheconfig);
141 manager = new CacheManager(config);
142 }
143 return manager;
144 }
145
146 private Cache getCache() {
147 if (cache == null) {
148 Cache c = new Cache(getName(), maxElementsInMemory, memoryStoreEvictionPolicy,
149 overflowToDisk, getPath().getAbsolutePath(), eternal, timeToLiveSeconds,
150 timeToIdleSeconds, diskPersistent, diskExpiryThreadIntervalSeconds,
151 registeredEventListeners, bootstrapCacheLoader, maxElementsOnDisk,
152 diskSpoolBufferSizeMB);
153 getCacheManager().addCache(c);
154 cache = getCacheManager().getCache(getName());
155 }
156 return cache;
157 }
158
159 public void clear() {
160 getCache().flush();
161 }
162
163 public boolean containsKey(Object key) {
164 return getCache().isKeyInCache(key);
165 }
166
167 public boolean containsValue(Object value) {
168 return getCache().isValueInCache(value);
169 }
170
171 public Set<java.util.Map.Entry<K, V>> entrySet() {
172 throw new MatrixException("not implemented");
173 }
174
175 public V get(Object key) {
176 Element e = getCache().get(key);
177 if (e != null) {
178 return (V) e.getValue();
179 } else {
180 return null;
181 }
182 }
183
184 public boolean isEmpty() {
185 return getCache().getSize() == 0;
186 }
187
188 public Set<K> keySet() {
189 return new HashSet<K>(getCache().getKeys());
190 }
191
192 public V put(K key, V value) {
193 Element e = new Element(key, value);
194 getCache().put(e);
195 return null;
196 }
197
198 public void putAll(Map<? extends K, ? extends V> m) {
199 for (K key : m.keySet()) {
200 V value = m.get(key);
201 put(key, value);
202 }
203 }
204
205 public V remove(Object key) {
206 getCache().remove(key);
207 return null;
208 }
209
210 public int size() {
211 return getCache().getSize();
212 }
213
214 public Collection<V> values() {
215 throw new MatrixException("not implemented");
216 }
217
218 @Override
219 public void finalize() {
220 getCacheManager().removeCache(getCache().getName());
221 }
222
223 public void flush() throws IOException {
224 getCache().flush();
225 }
226
227 public void close() throws IOException {
228 getCache().dispose();
229 getCacheManager().removeCache(getCache().getName());
230 }
231 }