diff --git a/pom.xml b/pom.xml index 33a1c9bd36..f6b6fdab98 100644 --- a/pom.xml +++ b/pom.xml @@ -89,12 +89,6 @@ 2.9.3 true - - net.openhft - zero-allocation-hashing - 0.16 - test - diff --git a/src/main/java/redis/clients/jedis/CommandObject.java b/src/main/java/redis/clients/jedis/CommandObject.java index b4931f2634..c44a0be7de 100644 --- a/src/main/java/redis/clients/jedis/CommandObject.java +++ b/src/main/java/redis/clients/jedis/CommandObject.java @@ -1,5 +1,8 @@ package redis.clients.jedis; +import java.util.Iterator; +import redis.clients.jedis.args.Rawable; + public class CommandObject { private final CommandArguments arguments; @@ -17,4 +20,39 @@ public CommandArguments getArguments() { public Builder getBuilder() { return builder; } + + @Override + public int hashCode() { + int hashCode = 1; + for (Rawable e : arguments) { + hashCode = 31 * hashCode + e.hashCode(); + } + hashCode = 31 * hashCode + builder.hashCode(); + return hashCode; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + if (!(o instanceof CommandObject)) { + return false; + } + + Iterator e1 = arguments.iterator(); + Iterator e2 = ((CommandObject) o).arguments.iterator(); + while (e1.hasNext() && e2.hasNext()) { + Rawable o1 = e1.next(); + Rawable o2 = e2.next(); + if (!(o1 == null ? o2 == null : o1.equals(o2))) { + return false; + } + } + if (e1.hasNext() || e2.hasNext()) { + return false; + } + + return builder == ((CommandObject) o).builder; + } } diff --git a/src/main/java/redis/clients/jedis/args/Rawable.java b/src/main/java/redis/clients/jedis/args/Rawable.java index 7515386861..be266f58aa 100644 --- a/src/main/java/redis/clients/jedis/args/Rawable.java +++ b/src/main/java/redis/clients/jedis/args/Rawable.java @@ -10,4 +10,10 @@ public interface Rawable { * @return binary */ byte[] getRaw(); + + @Override + int hashCode(); + + @Override + boolean equals(Object o); } diff --git a/src/main/java/redis/clients/jedis/args/RawableFactory.java b/src/main/java/redis/clients/jedis/args/RawableFactory.java index 813ddd021b..4a2ec782a7 100644 --- a/src/main/java/redis/clients/jedis/args/RawableFactory.java +++ b/src/main/java/redis/clients/jedis/args/RawableFactory.java @@ -96,17 +96,12 @@ public int hashCode() { /** * A {@link Rawable} wrapping a {@link String}. */ - public static class RawString implements Rawable { + public static class RawString extends Raw { - private final byte[] raw; + // TODO: private final String str; ^ implements Rawable public RawString(String str) { - this.raw = SafeEncoder.encode(str); - } - - @Override - public byte[] getRaw() { - return raw; + super(SafeEncoder.encode(str)); } } diff --git a/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java b/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java index 0fa05919e0..0750c9fff7 100644 --- a/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java @@ -3,41 +3,34 @@ import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import java.util.concurrent.TimeUnit; - -import redis.clients.jedis.csc.hash.CommandLongHasher; -import redis.clients.jedis.csc.hash.SimpleCommandHasher; +import redis.clients.jedis.CommandObject; public class CaffeineClientSideCache extends ClientSideCache { - private final Cache cache; - - public CaffeineClientSideCache(Cache caffeineCache) { - this(caffeineCache, SimpleCommandHasher.INSTANCE); - } + private final Cache cache; - public CaffeineClientSideCache(Cache caffeineCache, CommandLongHasher commandHasher) { - super(commandHasher); + public CaffeineClientSideCache(Cache caffeineCache) { this.cache = caffeineCache; } @Override - protected final void invalidateAllHashes() { + protected final void invalidateFullCache() { cache.invalidateAll(); } @Override - protected void invalidateHashes(Iterable hashes) { - cache.invalidateAll(hashes); + protected void invalidateCache(Iterable> commands) { + cache.invalidateAll(commands); } @Override - protected void putValue(long hash, Object value) { - cache.put(hash, value); + protected void putValue(CommandObject command, T value) { + cache.put(command, value); } @Override - protected Object getValue(long hash) { - return cache.getIfPresent(hash); + protected T getValue(CommandObject command) { + return (T) cache.getIfPresent(command); } public static Builder builder() { @@ -50,9 +43,6 @@ public static class Builder { private long expireTime = DEFAULT_EXPIRE_SECONDS; private final TimeUnit expireTimeUnit = TimeUnit.SECONDS; - // not using a default value to avoid an object creation like 'new OpenHftHashing(hashFunction)' - private CommandLongHasher commandHasher = SimpleCommandHasher.INSTANCE; - private Builder() { } public Builder maximumSize(int size) { @@ -65,11 +55,6 @@ public Builder ttl(int seconds) { return this; } - public Builder commandHasher(CommandLongHasher commandHasher) { - this.commandHasher = commandHasher; - return this; - } - public CaffeineClientSideCache build() { Caffeine cb = Caffeine.newBuilder(); @@ -77,7 +62,7 @@ public CaffeineClientSideCache build() { cb.expireAfterWrite(expireTime, expireTimeUnit); - return new CaffeineClientSideCache(cb.build(), commandHasher); + return new CaffeineClientSideCache(cb.build()); } } } diff --git a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java index 48a23d5ead..31b1239821 100644 --- a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java @@ -1,7 +1,6 @@ package redis.clients.jedis.csc; import java.nio.ByteBuffer; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; @@ -11,7 +10,6 @@ import redis.clients.jedis.CommandObject; import redis.clients.jedis.annots.Experimental; -import redis.clients.jedis.csc.hash.CommandLongHasher; import redis.clients.jedis.util.SafeEncoder; /** @@ -25,25 +23,23 @@ public abstract class ClientSideCache { protected static final int DEFAULT_MAXIMUM_SIZE = 10_000; protected static final int DEFAULT_EXPIRE_SECONDS = 100; - private final Map> keyToCommandHashes = new ConcurrentHashMap<>(); - private final CommandLongHasher commandHasher; + private final Map>> keyToCommandHashes = new ConcurrentHashMap<>(); private ClientSideCacheable cacheable = DefaultClientSideCacheable.INSTANCE; // TODO: volatile - protected ClientSideCache(CommandLongHasher commandHasher) { - this.commandHasher = commandHasher; + protected ClientSideCache() { } public void setCacheable(ClientSideCacheable cacheable) { this.cacheable = Objects.requireNonNull(cacheable, "'cacheable' must not be null"); } - protected abstract void invalidateAllHashes(); + protected abstract void invalidateFullCache(); - protected abstract void invalidateHashes(Iterable hashes); + protected abstract void invalidateCache(Iterable> commands); - protected abstract void putValue(long hash, Object value); + protected abstract void putValue(CommandObject command, T value); - protected abstract Object getValue(long hash); + protected abstract T getValue(CommandObject command); public final void clear() { invalidateAllKeysAndCommandHashes(); @@ -63,7 +59,7 @@ public final void invalidate(List list) { } private void invalidateAllKeysAndCommandHashes() { - invalidateAllHashes(); + invalidateFullCache(); keyToCommandHashes.clear(); } @@ -76,9 +72,9 @@ private void invalidateKeyAndRespectiveCommandHashes(Object key) { // final ByteBuffer mapKey = makeKeyForKeyToCommandHashes((byte[]) key); final ByteBuffer mapKey = makeKeyForKeyToCommandHashes(key); - Set hashes = keyToCommandHashes.get(mapKey); - if (hashes != null) { - invalidateHashes(hashes); + Set> commands = keyToCommandHashes.get(mapKey); + if (commands != null) { + invalidateCache(commands); keyToCommandHashes.remove(mapKey); } } @@ -89,23 +85,21 @@ public final T get(Function, T> loader, CommandObject co return loader.apply(command); } - final long hash = commandHasher.hash(command); - - T value = (T) getValue(hash); + T value = getValue(command); if (value != null) { return value; } value = loader.apply(command); if (value != null) { - putValue(hash, value); + putValue(command, value); for (Object key : keys) { ByteBuffer mapKey = makeKeyForKeyToCommandHashes(key); if (keyToCommandHashes.containsKey(mapKey)) { - keyToCommandHashes.get(mapKey).add(hash); + keyToCommandHashes.get(mapKey).add(command); } else { - Set set = new HashSet<>(); - set.add(hash); + Set> set = ConcurrentHashMap.newKeySet(); + set.add(command); keyToCommandHashes.put(mapKey, set); } } diff --git a/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java b/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java index 5f5aee86eb..ccde8af60c 100644 --- a/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java @@ -2,47 +2,36 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; -import com.google.common.hash.HashFunction; import java.util.concurrent.TimeUnit; - -import redis.clients.jedis.csc.hash.CommandLongHasher; -import redis.clients.jedis.csc.hash.GuavaCommandHasher; +import redis.clients.jedis.CommandObject; public class GuavaClientSideCache extends ClientSideCache { - private final Cache cache; - - public GuavaClientSideCache(Cache guavaCache) { - this(guavaCache, GuavaCommandHasher.DEFAULT_HASH_FUNCTION); - } - - public GuavaClientSideCache(Cache guavaCache, HashFunction hashFunction) { - this(guavaCache, new GuavaCommandHasher(hashFunction)); - } + private final Cache cache; - public GuavaClientSideCache(Cache guavaCache, CommandLongHasher commandHasher) { - super(commandHasher); + public GuavaClientSideCache(Cache guavaCache) { + super(); this.cache = guavaCache; } @Override - protected final void invalidateAllHashes() { + protected final void invalidateFullCache() { cache.invalidateAll(); } @Override - protected void invalidateHashes(Iterable hashes) { - cache.invalidateAll(hashes); + protected void invalidateCache(Iterable> commands) { + cache.invalidateAll(commands); } @Override - protected void putValue(long hash, Object value) { - cache.put(hash, value); + protected void putValue(CommandObject command, T value) { + cache.put(command, value); } @Override - protected Object getValue(long hash) { - return cache.getIfPresent(hash); + protected T getValue(CommandObject command) { + return (T) cache.getIfPresent(command); } public static Builder builder() { @@ -55,10 +44,6 @@ public static class Builder { private long expireTime = DEFAULT_EXPIRE_SECONDS; private final TimeUnit expireTimeUnit = TimeUnit.SECONDS; - // not using a default value to avoid an object creation like 'new GuavaHashing(hashFunction)' - private HashFunction hashFunction = null; - private CommandLongHasher commandHasher = null; - private Builder() { } public Builder maximumSize(int size) { @@ -71,18 +56,6 @@ public Builder ttl(int seconds) { return this; } - public Builder hashFunction(HashFunction function) { - this.hashFunction = function; - this.commandHasher = null; - return this; - } - - public Builder commandHasher(CommandLongHasher commandHasher) { - this.commandHasher = commandHasher; - this.hashFunction = null; - return this; - } - public GuavaClientSideCache build() { CacheBuilder cb = CacheBuilder.newBuilder(); @@ -90,9 +63,7 @@ public GuavaClientSideCache build() { cb.expireAfterWrite(expireTime, expireTimeUnit); - return hashFunction != null ? new GuavaClientSideCache(cb.build(), new GuavaCommandHasher(hashFunction)) - : commandHasher != null ? new GuavaClientSideCache(cb.build(), commandHasher) - : new GuavaClientSideCache(cb.build()); + return new GuavaClientSideCache(cb.build()); } } } diff --git a/src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHasher.java b/src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHasher.java deleted file mode 100644 index 2c71e4ea80..0000000000 --- a/src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHasher.java +++ /dev/null @@ -1,27 +0,0 @@ -package redis.clients.jedis.csc.hash; - -import redis.clients.jedis.Builder; -import redis.clients.jedis.CommandObject; -import redis.clients.jedis.args.Rawable; - -public abstract class AbstractCommandHasher implements CommandLongHasher { - - @Override - public final long hash(CommandObject command) { - long[] nums = new long[command.getArguments().size() + 1]; - int idx = 0; - for (Rawable raw : command.getArguments()) { - nums[idx++] = hashRawable(raw); - } - nums[idx] = hashBuilder(command.getBuilder()); - return hashLongs(nums); - } - - protected abstract long hashLongs(long[] longs); - - protected abstract long hashRawable(Rawable raw); - - protected long hashBuilder(Builder builder) { - return builder.hashCode(); - } -} diff --git a/src/main/java/redis/clients/jedis/csc/hash/AbstractSimpleCommandHasher.java b/src/main/java/redis/clients/jedis/csc/hash/AbstractSimpleCommandHasher.java deleted file mode 100644 index 3b782c2341..0000000000 --- a/src/main/java/redis/clients/jedis/csc/hash/AbstractSimpleCommandHasher.java +++ /dev/null @@ -1,28 +0,0 @@ -package redis.clients.jedis.csc.hash; - -import redis.clients.jedis.Builder; -import redis.clients.jedis.args.Rawable; - -/** - * It is possible to extend {@link AbstractSimpleCommandHasher this abstract class} in order to implement - * {@link CommandLongHasher} as {@link AbstractSimpleCommandHasher#hashLongs(long[])} and - * {@link AbstractSimpleCommandHasher#hashBytes(byte[])} are supported by almost all Java hashing libraries. - */ -public abstract class AbstractSimpleCommandHasher extends AbstractCommandHasher { - - @Override - protected final long hashRawable(Rawable raw) { - return hashBytes(raw.getRaw()); - } - - @Override - protected final long hashBuilder(Builder builder) { - return hashInt(builder.hashCode()); - } - - protected abstract long hashBytes(byte[] bytes); - - protected long hashInt(int hashCode) { - return hashCode; - } -} diff --git a/src/main/java/redis/clients/jedis/csc/hash/CommandLongHasher.java b/src/main/java/redis/clients/jedis/csc/hash/CommandLongHasher.java deleted file mode 100644 index bb0b03e072..0000000000 --- a/src/main/java/redis/clients/jedis/csc/hash/CommandLongHasher.java +++ /dev/null @@ -1,16 +0,0 @@ -package redis.clients.jedis.csc.hash; - -import redis.clients.jedis.CommandObject; - -/** - * The interface for hashing a command object to support client-side caching. - */ -public interface CommandLongHasher { - - /** - * Produce a 64-bit signed hash value from a command object. - * @param command the command object - * @return 64-bit signed hash value - */ - long hash(CommandObject command); -} diff --git a/src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHasher.java b/src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHasher.java deleted file mode 100644 index 3208049e3a..0000000000 --- a/src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHasher.java +++ /dev/null @@ -1,31 +0,0 @@ -package redis.clients.jedis.csc.hash; - -import com.google.common.hash.HashFunction; -import com.google.common.hash.Hasher; -import redis.clients.jedis.CommandObject; - -/** - * An implementation of {@link CommandLongHasher} based on {@link HashFunction} from Google Guava library. - */ -public final class GuavaCommandHasher implements CommandLongHasher { - - public static final HashFunction DEFAULT_HASH_FUNCTION = com.google.common.hash.Hashing.fingerprint2011(); - - private final HashFunction function; - - /** - * It is advised to use a {@link HashFunction} capable of producing 64-bit hash. - * @param function an implementation of hash function - */ - public GuavaCommandHasher(HashFunction function) { - this.function = function; - } - - @Override - public long hash(CommandObject command) { - Hasher hasher = function.newHasher(); - command.getArguments().forEach(raw -> hasher.putBytes(raw.getRaw())); - hasher.putInt(command.getBuilder().hashCode()); - return hasher.hash().asLong(); - } -} diff --git a/src/main/java/redis/clients/jedis/csc/hash/SimpleCommandHasher.java b/src/main/java/redis/clients/jedis/csc/hash/SimpleCommandHasher.java deleted file mode 100644 index 1414d97295..0000000000 --- a/src/main/java/redis/clients/jedis/csc/hash/SimpleCommandHasher.java +++ /dev/null @@ -1,25 +0,0 @@ -package redis.clients.jedis.csc.hash; - -import java.util.Arrays; - -/** - * This {@link CommandLongHasher} implementation is simply based on {@link Arrays#hashCode(long[])} - * and {@link Arrays#hashCode(byte[])}. These methods actually produce 32-bit hash codes. It is - * advised to use proper 64-bit hash codes in production. - */ -public final class SimpleCommandHasher extends AbstractSimpleCommandHasher { - - public static final SimpleCommandHasher INSTANCE = new SimpleCommandHasher(); - - public SimpleCommandHasher() { } - - @Override - protected long hashLongs(long[] longs) { - return Arrays.hashCode(longs); - } - - @Override - protected long hashBytes(byte[] bytes) { - return Arrays.hashCode(bytes); - } -} diff --git a/src/main/java/redis/clients/jedis/csc/hash/package-info.java b/src/main/java/redis/clients/jedis/csc/hash/package-info.java deleted file mode 100644 index 0667e1f98b..0000000000 --- a/src/main/java/redis/clients/jedis/csc/hash/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * This package contains the classes and interface for hashing command arguments to support - * Server-assisted Client-side Caching. - */ -@Experimental -package redis.clients.jedis.csc.hash; - -import redis.clients.jedis.annots.Experimental; \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/csc/AllowAndDenyListClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/AllowAndDenyListClientSideCacheTest.java index d31626ca90..21ad307668 100644 --- a/src/test/java/redis/clients/jedis/csc/AllowAndDenyListClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/AllowAndDenyListClientSideCacheTest.java @@ -9,13 +9,14 @@ import org.hamcrest.Matchers; import org.junit.Test; +import redis.clients.jedis.CommandObject; import redis.clients.jedis.JedisPooled; import redis.clients.jedis.Protocol; import redis.clients.jedis.csc.util.AllowAndDenyListWithStringKeys; public class AllowAndDenyListClientSideCacheTest extends ClientSideCacheTestBase { - private static MapClientSideCache createMapClientSideCache(Map map, ClientSideCacheable cacheable) { + private static MapClientSideCache createMapClientSideCache(Map map, ClientSideCacheable cacheable) { MapClientSideCache mapCache = new MapClientSideCache(map); mapCache.setCacheable(cacheable); return mapCache; @@ -23,7 +24,7 @@ private static MapClientSideCache createMapClientSideCache(Map map @Test public void none() { - HashMap map = new HashMap<>(); + HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), createMapClientSideCache(map, new AllowAndDenyListWithStringKeys(null, null, null, null)), singleConnectionPoolConfig.get())) { @@ -36,7 +37,7 @@ public void none() { @Test public void whiteListCommand() { - HashMap map = new HashMap<>(); + HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), createMapClientSideCache(map, new AllowAndDenyListWithStringKeys(singleton(Protocol.Command.GET), null, null, null)), singleConnectionPoolConfig.get())) { @@ -49,7 +50,7 @@ public void whiteListCommand() { @Test public void blackListCommand() { - HashMap map = new HashMap<>(); + HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), createMapClientSideCache(map, new AllowAndDenyListWithStringKeys(null, singleton(Protocol.Command.GET), null, null)), singleConnectionPoolConfig.get())) { @@ -62,7 +63,7 @@ public void blackListCommand() { @Test public void whiteListKey() { - HashMap map = new HashMap<>(); + HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), createMapClientSideCache(map, new AllowAndDenyListWithStringKeys(null, null, singleton("foo"), null)), singleConnectionPoolConfig.get())) { @@ -75,7 +76,7 @@ public void whiteListKey() { @Test public void blackListKey() { - HashMap map = new HashMap<>(); + HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), createMapClientSideCache(map, new AllowAndDenyListWithStringKeys(null, null, null, singleton("foo"))), singleConnectionPoolConfig.get())) { diff --git a/src/test/java/redis/clients/jedis/csc/CaffeineClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/CaffeineClientSideCacheTest.java index 7fc2d6a8a7..8c1074f097 100644 --- a/src/test/java/redis/clients/jedis/csc/CaffeineClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/CaffeineClientSideCacheTest.java @@ -34,8 +34,7 @@ public void individualCommandsAndThenStats() { Cache caffeine = Caffeine.newBuilder().recordStats().build(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), - new CaffeineClientSideCache(caffeine, new OpenHftCommandHasher()), - singleConnectionPoolConfig.get())) { + new CaffeineClientSideCache(caffeine), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertEquals(0, caffeine.estimatedSize()); assertEquals("bar", jedis.get("foo")); diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java index 6670da47d5..a44f026fc5 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java @@ -12,6 +12,8 @@ import java.util.HashMap; import java.util.LinkedHashMap; import org.junit.Test; + +import redis.clients.jedis.CommandObject; import redis.clients.jedis.JedisPooled; import redis.clients.jedis.util.JedisURIHelper; @@ -24,7 +26,7 @@ public void flushEntireCache() { control.set("k" + i, "v" + i); } - HashMap map = new HashMap<>(); + HashMap map = new HashMap<>(); ClientSideCache clientSideCache = new MapClientSideCache(map); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache)) { for (int i = 0; i < count; i++) { @@ -45,7 +47,7 @@ public void removeSpecificKey() { } // By using LinkedHashMap, we can get the hashes (map keys) at the same order of the actual keys. - LinkedHashMap map = new LinkedHashMap<>(); + LinkedHashMap map = new LinkedHashMap<>(); ClientSideCache clientSideCache = new MapClientSideCache(map); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache)) { for (int i = 0; i < count; i++) { @@ -53,14 +55,14 @@ public void removeSpecificKey() { } } - ArrayList commandHashes = new ArrayList<>(map.keySet()); + ArrayList commandHashes = new ArrayList<>(map.keySet()); assertEquals(count, map.size()); for (int i = 0; i < count; i++) { String key = "k" + i; - Long hash = commandHashes.get(i); - assertTrue(map.containsKey(hash)); + CommandObject command = commandHashes.get(i); + assertTrue(map.containsKey(command)); clientSideCache.removeKey(key); - assertFalse(map.containsKey(hash)); + assertFalse(map.containsKey(command)); } } @@ -69,7 +71,7 @@ public void multiKeyOperation() { control.set("k1", "v1"); control.set("k2", "v2"); - HashMap map = new HashMap<>(); + HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapClientSideCache(map))) { jedis.mget("k1", "k2"); assertEquals(1, map.size()); diff --git a/src/test/java/redis/clients/jedis/csc/GuavaClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/GuavaClientSideCacheTest.java index 33a23c55c0..2bc1ab766f 100644 --- a/src/test/java/redis/clients/jedis/csc/GuavaClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/GuavaClientSideCacheTest.java @@ -7,7 +7,6 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheStats; -import com.google.common.hash.Hashing; import java.net.URI; import java.util.concurrent.TimeUnit; @@ -20,8 +19,7 @@ public class GuavaClientSideCacheTest extends ClientSideCacheTestBase { @Test public void simple() { - GuavaClientSideCache guava = GuavaClientSideCache.builder().maximumSize(10).ttl(10) - .hashFunction(Hashing.farmHashFingerprint64()).build(); + GuavaClientSideCache guava = GuavaClientSideCache.builder().maximumSize(10).ttl(10).build(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), guava)) { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); diff --git a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java index 63e543cc1c..158a83055e 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java @@ -15,6 +15,7 @@ import org.junit.Before; import org.junit.Test; +import redis.clients.jedis.CommandObject; import redis.clients.jedis.Connection; import redis.clients.jedis.ConnectionPoolConfig; import redis.clients.jedis.DefaultJedisClientConfig; @@ -62,7 +63,7 @@ public void simple() { @Test public void simpleWithSimpleMap() { - HashMap map = new HashMap<>(); + HashMap map = new HashMap<>(); try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapClientSideCache(map), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); @@ -92,7 +93,7 @@ public void flushAll() { @Test public void flushAllWithSimpleMap() { - HashMap map = new HashMap<>(); + HashMap map = new HashMap<>(); try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapClientSideCache(map), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); diff --git a/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java index 2d1889fbc8..e26c0b4d61 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java @@ -13,6 +13,7 @@ import org.junit.Before; import org.junit.Test; +import redis.clients.jedis.CommandObject; import redis.clients.jedis.Connection; import redis.clients.jedis.ConnectionPoolConfig; import redis.clients.jedis.DefaultJedisClientConfig; @@ -64,7 +65,7 @@ public void simple() { @Test public void simpleWithSimpleMap() { - HashMap map = new HashMap<>(); + HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapClientSideCache(map), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); @@ -94,7 +95,7 @@ public void flushAll() { @Test public void flushAllWithSimpleMap() { - HashMap map = new HashMap<>(); + HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapClientSideCache(map), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); diff --git a/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java index b6eb6bec9f..f00cc27ea9 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java @@ -14,6 +14,7 @@ import org.junit.Before; import org.junit.Test; +import redis.clients.jedis.CommandObject; import redis.clients.jedis.DefaultJedisClientConfig; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.HostAndPorts; @@ -59,7 +60,7 @@ public void simple() { @Test public void simpleWithSimpleMap() { - HashMap map = new HashMap<>(); + HashMap map = new HashMap<>(); try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapClientSideCache(map), sentinels, sentinelClientConfig)) { control.set("foo", "bar"); @@ -90,7 +91,7 @@ public void flushAll() { @Test public void flushAllWithSimpleMap() { - HashMap map = new HashMap<>(); + HashMap map = new HashMap<>(); try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapClientSideCache(map), sentinels, sentinelClientConfig)) { control.set("foo", "bar"); diff --git a/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java b/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java index e94930cb4a..e8bc975231 100644 --- a/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java +++ b/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java @@ -3,38 +3,38 @@ import java.util.HashMap; import java.util.Map; -import redis.clients.jedis.csc.hash.SimpleCommandHasher; +import redis.clients.jedis.CommandObject; public class MapClientSideCache extends ClientSideCache { - private final Map cache; + private final Map cache; public MapClientSideCache() { this(new HashMap<>()); } - public MapClientSideCache(Map map) { - super(SimpleCommandHasher.INSTANCE); + public MapClientSideCache(Map map) { + super(); this.cache = map; } @Override - protected final void invalidateAllHashes() { + protected final void invalidateFullCache() { cache.clear(); } @Override - protected void invalidateHashes(Iterable hashes) { - hashes.forEach(hash -> cache.remove(hash)); + protected void invalidateCache(Iterable> commands) { + commands.forEach(hash -> cache.remove(hash)); } @Override - protected void putValue(long hash, Object value) { - cache.put(hash, value); + protected void putValue(CommandObject command, T value) { + cache.put(command, value); } @Override - protected Object getValue(long hash) { - return cache.get(hash); + protected T getValue(CommandObject command) { + return (T) cache.get(command); } } diff --git a/src/test/java/redis/clients/jedis/csc/OpenHftCommandHasher.java b/src/test/java/redis/clients/jedis/csc/OpenHftCommandHasher.java deleted file mode 100644 index 147ad42e8f..0000000000 --- a/src/test/java/redis/clients/jedis/csc/OpenHftCommandHasher.java +++ /dev/null @@ -1,34 +0,0 @@ -package redis.clients.jedis.csc; - -import net.openhft.hashing.LongHashFunction; -import redis.clients.jedis.csc.hash.AbstractSimpleCommandHasher; - -public class OpenHftCommandHasher extends AbstractSimpleCommandHasher { - - public static final LongHashFunction DEFAULT_HASH_FUNCTION = LongHashFunction.xx3(); - - private final LongHashFunction function; - - OpenHftCommandHasher() { - this(DEFAULT_HASH_FUNCTION); - } - - public OpenHftCommandHasher(LongHashFunction function) { - this.function = function; - } - - @Override - protected long hashLongs(long[] longs) { - return function.hashLongs(longs); - } - - @Override - protected long hashBytes(byte[] bytes) { - return function.hashBytes(bytes); - } - - @Override - protected long hashInt(int hashCode) { - return function.hashInt(hashCode); - } -}