Skip to content

Commit

Permalink
Merge branch 'develop' into gh-1422-swagger-ui-configurable-title
Browse files Browse the repository at this point in the history
  • Loading branch information
m55624 committed Nov 6, 2017
2 parents a2e7d23 + 7629db2 commit 63f9342
Show file tree
Hide file tree
Showing 74 changed files with 3,341 additions and 1,694 deletions.
5 changes: 5 additions & 0 deletions core/cache/pom.xml
Expand Up @@ -15,6 +15,11 @@
<artifactId>exception</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.gchq.gaffer</groupId>
<artifactId>serialisation</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>

</project>
Expand Up @@ -79,6 +79,13 @@ public static ICacheService getService() {
return service;
}

/**
* @return true if the cache is enabled
*/
public static boolean isEnabled() {
return null != service;
}

/**
* Gracefully shutdown and reset the cache service.
*/
Expand Down
25 changes: 15 additions & 10 deletions core/cache/src/main/java/uk/gov/gchq/gaffer/cache/ICache.java
Expand Up @@ -17,7 +17,7 @@
package uk.gov.gchq.gaffer.cache;

import uk.gov.gchq.gaffer.cache.exception.CacheOperationException;
import uk.gov.gchq.gaffer.core.exception.Status;
import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException;

import java.util.Collection;
import java.util.Set;
Expand All @@ -29,7 +29,7 @@
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
*/
public interface ICache <K, V> {
public interface ICache<K, V> {

/**
* Retrieve the value associated with the given key.
Expand All @@ -42,7 +42,7 @@ public interface ICache <K, V> {
/**
* Add a new key-value pair to the cache.
*
* @param key the key to add
* @param key the key to add
* @param value the value to add
* @throws CacheOperationException if there is an error adding the new key-value pair to the cache
*/
Expand All @@ -51,15 +51,20 @@ public interface ICache <K, V> {
/**
* Add a new key-value pair to the cache, but only if there is existing entry associated with the specified key.
*
* @param key the key to add
* @param key the key to add
* @param value the value to add
* @throws CacheOperationException if the specified key already exists in the cache with a non-null value
* @throws CacheOperationException if there is an error adding the new key-value pair to the cache
* @throws OverwritingException if the specified key already exists in the cache with a non-null value
*/
default void putSafe(final K key, final V value) throws CacheOperationException {
default void putSafe(final K key, final V value) throws OverwritingException, CacheOperationException {
if (null == get(key)) {
put(key, value);
try {
put(key, value);
} catch (final CacheOperationException e) {
throw e;
}
} else {
throw new CacheOperationException("Cache entry already exists for key: " + key, Status.CONFLICT);
throw new OverwritingException("Cache entry already exists for key: " + key);
}
}

Expand All @@ -73,14 +78,14 @@ default void putSafe(final K key, final V value) throws CacheOperationException
/**
* Get all values present in the cache.
*
* @return a {@link Collection} containing all of the cache values
* @return a {@link Collection} containing all of the cache values
*/
Collection<V> getAllValues();

/**
* Get all keys present in the cache.
*
* @return a {@link Set} containing all of the cache keys
* @return a {@link Set} containing all of the cache keys
*/
Set<K> getAllKeys();

Expand Down
Expand Up @@ -45,8 +45,8 @@ public interface ICacheService {
* Get the named cache from the cache service.
*
* @param cacheName the name of the cache to retrieve
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
* @return the requested cache object
*/
<K, V> ICache<K, V> getCache(final String cacheName);
Expand All @@ -55,9 +55,9 @@ public interface ICacheService {
* Get the value associated with the specified cache and key.
*
* @param cacheName the name of the cache to look in
* @param key the key of the entry to lookup
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
* @param key the key of the entry to lookup
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
* @return the requested cache object
*/
default <K, V> V getFromCache(final String cacheName, final K key) {
Expand All @@ -69,10 +69,10 @@ default <K, V> V getFromCache(final String cacheName, final K key) {
* Add a new key-value pair to the specified cache.
*
* @param cacheName the name of the cache
* @param key the key to add
* @param value the value to add
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
* @param key the key to add
* @param value the value to add
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
* @throws CacheOperationException if there is an error adding the new key-value pair to the cache
*/
default <K, V> void putInCache(final String cacheName, final K key, final V value) throws CacheOperationException {
Expand All @@ -81,14 +81,14 @@ default <K, V> void putInCache(final String cacheName, final K key, final V valu
}

/**
* Add a new key-value pair to the specified cache, but only if there is existing
* Add a new key-value pair to the specified cache, but only if there is no existing
* entry associated with the specified key.
*
* @param cacheName the name of the cache
* @param key the key to add
* @param value the value to add
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
* @param key the key to add
* @param value the value to add
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
* @throws CacheOperationException if the specified key already exists in the cache with a non-null value
*/
default <K, V> void putSafeInCache(final String cacheName, final K key, final V value) throws CacheOperationException {
Expand All @@ -100,9 +100,9 @@ default <K, V> void putSafeInCache(final String cacheName, final K key, final V
* Remove the entry associated with the specified key from the specified cache.
*
* @param cacheName the name of the cache to look in
* @param key the key of the entry to remove
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
* @param key the key of the entry to remove
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
*/
default <K, V> void removeFromCache(final String cacheName, final K key) {
final ICache<K, V> cache = getCache(cacheName);
Expand All @@ -113,8 +113,8 @@ default <K, V> void removeFromCache(final String cacheName, final K key) {
* Get all of the values associated with the specified cache.
*
* @param cacheName the name of the cache to look in
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
* @return the requested cache objects
*/
default <K, V> Collection<V> getAllValuesFromCache(final String cacheName) {
Expand All @@ -126,8 +126,8 @@ default <K, V> Collection<V> getAllValuesFromCache(final String cacheName) {
* Get all of the keys associated with the specified cache.
*
* @param cacheName the name of the cache to look in
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
* @param <K> The object type that acts as the key for the cache
* @param <V> The value that is stored in the cache
* @return the requested cache keys
*/
default <K, V> Set<K> getAllKeysFromCache(final String cacheName) {
Expand Down
Expand Up @@ -16,8 +16,13 @@

package uk.gov.gchq.gaffer.cache.impl;

import com.google.common.collect.Lists;

import uk.gov.gchq.gaffer.cache.ICache;
import uk.gov.gchq.gaffer.exception.SerialisationException;
import uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
Expand All @@ -29,18 +34,41 @@
* @param <K> The object type that acts as the key for the HashMap
* @param <V> The value that is stored in the HashMap
*/
public class HashMapCache <K, V> implements ICache<K, V> {
public class HashMapCache<K, V> implements ICache<K, V> {
private static final JavaSerialiser JAVA_SERIALISER = new JavaSerialiser();
private boolean useJavaSerialisation;
private HashMap<K, Object> cache = new HashMap<>();

public HashMapCache(final boolean useJavaSerialisation) {
this.useJavaSerialisation = useJavaSerialisation;
}

private final HashMap<K, V> cache = new HashMap<>();
public HashMapCache() {
this(false);
}

@Override
public V get(final K key) {
return cache.get(key);
try {
return (V) (useJavaSerialisation
? JAVA_SERIALISER.deserialise((byte[]) cache.get(key))
: cache.get(key));
} catch (final SerialisationException e) {
throw new RuntimeException(e);
}
}

@Override
public void put(final K key, final V value) {
cache.put(key, value);
if (useJavaSerialisation) {
try {
cache.put(key, JAVA_SERIALISER.serialise(value));
} catch (final SerialisationException e) {
throw new RuntimeException(e);
}
} else {
cache.<K, V>put(key, value);
}
}

@Override
Expand All @@ -50,7 +78,20 @@ public void remove(final K key) {

@Override
public Collection<V> getAllValues() {
return cache.values();
ArrayList<V> rtn = Lists.newArrayList();
if (useJavaSerialisation) {
cache.values()
.forEach((Object o) -> {
try {
rtn.add((V) JAVA_SERIALISER.deserialise((byte[]) o));
} catch (final SerialisationException e) {
throw new RuntimeException(e);
}
});
} else {
rtn.addAll((Collection<V>) cache.values());
}
return rtn;
}

@Override
Expand Down
Expand Up @@ -27,12 +27,25 @@
* {@link HashMapCache} as the cache implementation.
*/
public class HashMapCacheService implements ICacheService {
public static final String STATIC_CACHE = "gaffer.cache.hashmap.static";
public static final String JAVA_SERIALISATION_CACHE = "gaffer.cache.hashmap.useJavaSerialisation";
private static final HashMap<String, HashMapCache> STATIC_CACHES = new HashMap<>();
private final HashMap<String, HashMapCache> nonStaticCaches = new HashMap<>();
private boolean useJavaSerialisation = false;

private final HashMap<String, HashMapCache> caches = new HashMap<>();
private HashMap<String, HashMapCache> caches = nonStaticCaches;

@Override
public void initialise(final Properties properties) {
// do nothing
if (properties != null) {
useJavaSerialisation = Boolean.parseBoolean(properties.getProperty(JAVA_SERIALISATION_CACHE));
}

if (properties != null && Boolean.parseBoolean(properties.getProperty(STATIC_CACHE))) {
caches = STATIC_CACHES;
} else {
caches = nonStaticCaches;
}
}

@Override
Expand All @@ -42,7 +55,7 @@ public void shutdown() {

@Override
public <K, V> ICache<K, V> getCache(final String cacheName) {
HashMapCache<K, V> cache = caches.computeIfAbsent(cacheName, k -> new HashMapCache<>());
HashMapCache<K, V> cache = caches.computeIfAbsent(cacheName, k -> new HashMapCache<>(useJavaSerialisation));

return cache;
}
Expand Down
Expand Up @@ -24,6 +24,7 @@

import uk.gov.gchq.gaffer.cache.ICache;
import uk.gov.gchq.gaffer.cache.exception.CacheOperationException;
import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException;

import static org.junit.Assert.assertEquals;

Expand All @@ -37,14 +38,14 @@ public class HashMapCacheServiceTest {
public void before() {
service.initialise(null);
}

@After
public void after() {
service.shutdown();
}

@Test
public void shouldReturnInstanceOfHashMapCache() {

// when
ICache cache = service.getCache(CACHE_NAME);

Expand Down Expand Up @@ -92,11 +93,11 @@ public void shouldOnlyUpdateIfInstructed() throws CacheOperationException {
try {
service.putSafeInCache(CACHE_NAME, "test", 2);
Assert.fail("Expected an exception");
} catch (final CacheOperationException e) {
} catch (final OverwritingException e) {
assertEquals((Integer) 1, service.getFromCache(CACHE_NAME, "test"));
}

service.putInCache(CACHE_NAME,"test", 2);
service.putInCache(CACHE_NAME, "test", 2);

assertEquals((Integer) 2, service.getFromCache(CACHE_NAME, "test"));
}
Expand Down

0 comments on commit 63f9342

Please sign in to comment.