org.terracotta.modules.ehcache.collections.SerializedToolkitCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ehcache Show documentation
Show all versions of ehcache Show documentation
Ehcache is an open source, standards-based cache used to boost performance,
offload the database and simplify scalability. Ehcache is robust, proven and full-featured and
this has made it the most widely-used Java-based cache.
/*
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. Licensed under the Apache
* License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain
* a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in
* writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
* OF ANY KIND, either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.terracotta.modules.ehcache.collections;
import org.terracotta.toolkit.cache.ToolkitCache;
import org.terracotta.toolkit.cache.ToolkitCacheListener;
import org.terracotta.toolkit.concurrent.locks.ToolkitReadWriteLock;
import org.terracotta.toolkit.config.Configuration;
import org.terracotta.toolkit.search.QueryBuilder;
import org.terracotta.toolkit.search.attribute.ToolkitAttributeExtractor;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* An implementation of {@link ToolkitCache} that supports serializable keys
*/
public class SerializedToolkitCache implements ToolkitCache {
private final ToolkitCache toolkitCache;
public SerializedToolkitCache(ToolkitCache toolkitMap) {
this.toolkitCache = toolkitMap;
}
@Override
public int size() {
return this.toolkitCache.size();
}
@Override
public boolean isEmpty() {
return this.toolkitCache.isEmpty();
}
private static String serializeToString(Object key) {
try {
return SerializationHelper.serializeToString(key);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static Object deserializeFromString(String key) {
try {
return SerializationHelper.deserializeFromString(key);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean containsKey(Object key) {
return this.toolkitCache.containsKey(serializeToString(key));
}
@Override
public V get(Object key) {
return this.toolkitCache.get(serializeToString(key));
}
@Override
public V put(K key, V value) {
return this.toolkitCache.put(serializeToString(key), value);
}
@Override
public V remove(Object key) {
return this.toolkitCache.remove(serializeToString(key));
}
@Override
public void putAll(Map extends K, ? extends V> m) {
Map tempMap = new HashMap();
for (Entry extends K, ? extends V> entry : m.entrySet()) {
tempMap.put(serializeToString(entry.getKey()), entry.getValue());
}
toolkitCache.putAll(tempMap);
}
@Override
public void clear() {
toolkitCache.clear();
}
@Override
public Set keySet() {
return new ToolkitKeySet(toolkitCache.keySet());
}
@Override
public boolean isDestroyed() {
return toolkitCache.isDestroyed();
}
@Override
public void destroy() {
toolkitCache.destroy();
}
@Override
public String getName() {
return toolkitCache.getName();
}
@Override
public ToolkitReadWriteLock createLockForKey(K key) {
return toolkitCache.createLockForKey(serializeToString(key));
}
@Override
public void removeNoReturn(Object key) {
toolkitCache.removeNoReturn(serializeToString(key));
}
@Override
public void putNoReturn(K key, V value) {
toolkitCache.putNoReturn(serializeToString(key), value);
}
@Override
public Map getAll(Collection extends K> keys) {
HashSet tempSet = new HashSet();
for (K key : keys) {
tempSet.add(serializeToString(key));
}
Map m = toolkitCache.getAll(tempSet);
Map tempMap = m.isEmpty() ? Collections.EMPTY_MAP : new HashMap();
for (Entry entry : m.entrySet()) {
tempMap.put((K) deserializeFromString(entry.getKey()), entry.getValue());
}
return tempMap;
}
@Override
public Configuration getConfiguration() {
return toolkitCache.getConfiguration();
}
@Override
public void setConfigField(String name, Serializable value) {
toolkitCache.setConfigField(name, value);
}
@Override
public boolean containsValue(Object value) {
return toolkitCache.containsValue(value);
}
@Override
public V putIfAbsent(K key, V value) {
return toolkitCache.putIfAbsent(serializeToString(key), value);
}
@Override
public Set> entrySet() {
return new ToolkitEntrySet(this.toolkitCache.entrySet());
}
@Override
public Collection values() {
return this.toolkitCache.values();
}
@Override
public boolean remove(Object key, Object value) {
return this.toolkitCache.remove(serializeToString(key), value);
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
return this.toolkitCache.replace(serializeToString(key), oldValue, newValue);
}
@Override
public V replace(K key, V value) {
return this.toolkitCache.replace(serializeToString(key), value);
}
private static class ToolkitEntrySet implements Set> {
private final Set> set;
public ToolkitEntrySet(Set> set) {
this.set = set;
}
@Override
public int size() {
return set.size();
}
@Override
public boolean isEmpty() {
return set.isEmpty();
}
@Override
public boolean contains(Object o) {
if (!(o instanceof Map.Entry)) { return false; }
Map.Entry entry = (java.util.Map.Entry) o;
ToolkitCacheEntry toolkitEntry = null;
toolkitEntry = new ToolkitCacheEntry(serializeToString(entry.getKey()), entry.getValue());
return this.set.contains(toolkitEntry);
}
@Override
public Iterator> iterator() {
return new ToolkitEntryIterator(set.iterator());
}
@Override
public Object[] toArray() {
throw new UnsupportedOperationException();
}
@Override
public T[] toArray(T[] a) {
throw new UnsupportedOperationException();
}
@Override
public boolean add(java.util.Map.Entry e) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean containsAll(Collection> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection extends java.util.Map.Entry> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection> c) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
}
private static class ToolkitEntryIterator implements Iterator> {
private final Iterator> iter;
public ToolkitEntryIterator(Iterator> iter) {
this.iter = iter;
}
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public java.util.Map.Entry next() {
Map.Entry entry = iter.next();
if (entry == null) { return null; }
return new ToolkitCacheEntry(deserializeFromString(entry.getKey()), entry.getValue());
}
@Override
public void remove() {
iter.remove();
}
}
private static class ToolkitCacheEntry implements Map.Entry {
private final K k;
private final V v;
public ToolkitCacheEntry(K k, V v) {
this.k = k;
this.v = v;
}
@Override
public K getKey() {
return k;
}
@Override
public V getValue() {
return v;
}
@Override
public V setValue(V value) {
throw new UnsupportedOperationException();
}
}
private static class ToolkitKeySet implements Set {
private final Set set;
public ToolkitKeySet(Set set) {
this.set = set;
}
@Override
public int size() {
return set.size();
}
@Override
public boolean isEmpty() {
return set.isEmpty();
}
@Override
public boolean contains(Object o) {
return set.contains(serializeToString(o));
}
@Override
public Iterator iterator() {
return new ToolkitKeyIterator(set.iterator());
}
@Override
public Object[] toArray() {
throw new UnsupportedOperationException();
}
@Override
public T[] toArray(T[] a) {
throw new UnsupportedOperationException();
}
@Override
public boolean add(K e) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean containsAll(Collection> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection extends K> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection> c) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
}
private static class ToolkitKeyIterator implements Iterator {
private final Iterator iter;
public ToolkitKeyIterator(Iterator iter) {
this.iter = iter;
}
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public K next() {
String k = iter.next();
if (k == null) { return null; }
return (K) deserializeFromString(k);
}
@Override
public void remove() {
iter.remove();
}
}
@Override
public Map getAllQuiet(Collection keys) {
HashSet tempSet = new HashSet();
for (K key : keys) {
tempSet.add(serializeToString(key));
}
Map m = toolkitCache.getAllQuiet(tempSet);
Map tempMap = m.isEmpty() ? Collections.EMPTY_MAP : new HashMap();
for (Entry entry : m.entrySet()) {
tempMap.put((K) deserializeFromString(entry.getKey()), entry.getValue());
}
return tempMap;
}
@Override
public V getQuiet(Object key) {
return this.toolkitCache.get(serializeToString(key));
}
@Override
public void putNoReturn(K key, V value, long createTimeInSecs, int customMaxTTISeconds, int customMaxTTLSeconds) {
this.toolkitCache.putNoReturn(serializeToString(key), value, createTimeInSecs, customMaxTTISeconds,
customMaxTTLSeconds);
}
@Override
public V putIfAbsent(K key, V value, long createTimeInSecs, int customMaxTTISeconds, int customMaxTTLSeconds) {
return this.toolkitCache.putIfAbsent(serializeToString(key), value, createTimeInSecs, customMaxTTISeconds,
customMaxTTLSeconds);
}
@Override
public void addListener(ToolkitCacheListener listener) {
throw new UnsupportedOperationException();
}
@Override
public void removeListener(ToolkitCacheListener listener) {
throw new UnsupportedOperationException();
}
@Override
public void setAttributeExtractor(ToolkitAttributeExtractor attrExtractor) {
this.toolkitCache.setAttributeExtractor(attrExtractor);
}
@Override
public QueryBuilder createQueryBuilder() {
return this.toolkitCache.createQueryBuilder();
}
@Override
public boolean isBulkLoadEnabled() {
return toolkitCache.isBulkLoadEnabled();
}
@Override
public boolean isNodeBulkLoadEnabled() {
return toolkitCache.isNodeBulkLoadEnabled();
}
@Override
public void setNodeBulkLoadEnabled(boolean enabledBulkLoad) {
toolkitCache.setNodeBulkLoadEnabled(enabledBulkLoad);
}
@Override
public void waitUntilBulkLoadComplete() throws InterruptedException {
toolkitCache.waitUntilBulkLoadComplete();
}
}