Skip to content

Commit

Permalink
Adapt HzCacheProvider to Guava Cache interface changes
Browse files Browse the repository at this point in the history
  • Loading branch information
groldan authored and jodygarnett committed Jan 15, 2015
1 parent 07de526 commit 134ca9e
Showing 1 changed file with 38 additions and 66 deletions.
104 changes: 38 additions & 66 deletions src/main/java/org/geoserver/cluster/hazelcast/HzCacheProvider.java
Expand Up @@ -99,7 +99,7 @@ private boolean available() {
} }


@Override @Override
public V getIfPresent(K key) { public V getIfPresent(Object key) {
if (available()) { if (available()) {
return hzMap.get(key); return hzMap.get(key);
} }
Expand All @@ -121,11 +121,11 @@ public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException
} }


@Override @Override
public ImmutableMap<K, V> getAllPresent(Iterable<? extends K> keys) { public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
if (available()) { if (available()) {
Set<K> set = new HashSet<K>(); Set<K> set = new HashSet<K>();
for (K k : keys) { for (Object k : keys) {
set.add(k); set.add((K) k);
} }
Map<K, V> allPresent = hzMap.getAll(set); Map<K, V> allPresent = hzMap.getAll(set);
return ImmutableMap.copyOf(allPresent); return ImmutableMap.copyOf(allPresent);
Expand Down Expand Up @@ -188,22 +188,9 @@ public void cleanUp() {
// //
} }


@Deprecated
@Override
public V get(K key) throws ExecutionException {
throw new UnsupportedOperationException();
}

@Deprecated
@Override
public V getUnchecked(K key) {
throw new UnsupportedOperationException();
}

@Deprecated
@Override @Override
public V apply(K key) { public void putAll(Map<? extends K, ? extends V> m) {
throw new UnsupportedOperationException(); hzMap.putAll(m);
} }
} }


Expand Down Expand Up @@ -247,7 +234,7 @@ private boolean available() {
} }


@Override @Override
public Info getIfPresent(String key) { public Info getIfPresent(Object key) {
Info info = null; Info info = null;
if (available()) { if (available()) {
byte[] serialForm = hzMap.get(key); byte[] serialForm = hzMap.get(key);
Expand Down Expand Up @@ -283,11 +270,11 @@ public Info get(String key, Callable<? extends Info> valueLoader) throws Executi
} }


@Override @Override
public ImmutableMap<String, Info> getAllPresent(Iterable<? extends String> keys) { public ImmutableMap<String, Info> getAllPresent(Iterable<?> keys) {
if (available()) { if (available()) {
Set<String> set = new HashSet<String>(); Set<String> set = new HashSet<String>();
for (String k : keys) { for (Object k : keys) {
set.add(k); set.add((String) k);
} }
Map<String, byte[]> allPresent = hzMap.getAll(set); Map<String, byte[]> allPresent = hzMap.getAll(set);
Function<byte[], Info> function = new Function<byte[], Info>() { Function<byte[], Info> function = new Function<byte[], Info>() {
Expand All @@ -305,17 +292,34 @@ public Info apply(byte[] input) {
@Override @Override
public void put(String key, Info value) { public void put(String key, Info value) {
if (available()) { if (available()) {
ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] serialForm = serialize(value);
try {
persister.save(value, out);
} catch (IOException e) {
throw Throwables.propagate(e);
}
byte[] serialForm = out.toByteArray();
hzMap.putTransient(key, serialForm, ttl, timeunit); hzMap.putTransient(key, serialForm, ttl, timeunit);
} }
} }


private byte[] serialize(Info value) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
persister.save(value, out);
} catch (IOException e) {
throw Throwables.propagate(e);
}
byte[] serialForm = out.toByteArray();
return serialForm;
}

@Override
public void putAll(Map<? extends String, ? extends Info> m) {
Function<Info, byte[]> f = new Function<Info, byte[]>() {
@Override
public byte[] apply(Info input) {
return serialize(input);
}
};
Map<? extends String, byte[]> map = Maps.transformValues(m, f);
hzMap.putAll(map);
}

@Override @Override
public void invalidate(Object key) { public void invalidate(Object key) {
if (available()) { if (available()) {
Expand Down Expand Up @@ -369,24 +373,6 @@ public Info apply(byte[] input) {
public void cleanUp() { public void cleanUp() {
// //
} }

@Deprecated
@Override
public Info get(String key) throws ExecutionException {
throw new UnsupportedOperationException();
}

@Deprecated
@Override
public Info getUnchecked(String key) {
throw new UnsupportedOperationException();
}

@Deprecated
@Override
public Info apply(String key) {
throw new UnsupportedOperationException();
}
} }


private static class NullCache<K, V> implements Cache<K, V> { private static class NullCache<K, V> implements Cache<K, V> {
Expand All @@ -402,16 +388,10 @@ public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException
} }


@Override @Override
public V getIfPresent(K key) { public V getIfPresent(Object key) {
return null; return null;
} }


@Deprecated
@Override
public V get(K key) throws ExecutionException {
throw new UnsupportedOperationException();
}

@Override @Override
public long size() { public long size() {
return 0L; return 0L;
Expand All @@ -433,7 +413,7 @@ public void put(K key, V value) {
} }


@Override @Override
public ImmutableMap<K, V> getAllPresent(Iterable<? extends K> keys) { public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
return ImmutableMap.of(); return ImmutableMap.of();
} }


Expand All @@ -457,17 +437,9 @@ public void cleanUp() {
// //
} }


@Deprecated
@Override @Override
public V getUnchecked(K key) { public void putAll(Map<? extends K, ? extends V> m) {
throw new UnsupportedOperationException(); //

}

@Deprecated
@Override
public V apply(K key) {
throw new UnsupportedOperationException();
} }
} }
} }

0 comments on commit 134ca9e

Please sign in to comment.