From 5e6d0c9cb0daffdda645ddf12f8b01cd89a6b41b Mon Sep 17 00:00:00 2001
From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com>
Date: Fri, 5 Apr 2024 19:45:03 +0600
Subject: [PATCH] Remove openhft hashing from source dependency
---
pom.xml | 2 +-
.../jedis/csc/CaffeineClientSideCache.java | 10 +++++---
....java => AbstractSimpleCommandHasher.java} | 8 +++---
.../jedis/csc/hash/GuavaCommandHasher.java | 9 ++++++-
.../jedis/csc/hash/SimpleCommandHasher.java | 25 +++++++++++++++++++
.../jedis/csc/ClientSideCacheLibsTest.java | 9 +++----
.../clients/jedis/csc/MapClientSideCache.java | 20 +++------------
.../jedis/csc}/OpenHftCommandHasher.java | 9 +++++--
8 files changed, 58 insertions(+), 34 deletions(-)
rename src/main/java/redis/clients/jedis/csc/hash/{PrimitiveArrayCommandHasher.java => AbstractSimpleCommandHasher.java} (64%)
create mode 100644 src/main/java/redis/clients/jedis/csc/hash/SimpleCommandHasher.java
rename src/{main/java/redis/clients/jedis/csc/hash => test/java/redis/clients/jedis/csc}/OpenHftCommandHasher.java (70%)
diff --git a/pom.xml b/pom.xml
index 338847e0689..8d08a75c039 100644
--- a/pom.xml
+++ b/pom.xml
@@ -93,7 +93,7 @@
net.openhft
zero-allocation-hashing
0.16
- true
+ test
diff --git a/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java b/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java
index 8ff993d6c75..f0f6d98cb21 100644
--- a/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java
+++ b/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java
@@ -5,18 +5,22 @@
import java.util.concurrent.TimeUnit;
import redis.clients.jedis.csc.hash.CommandLongHasher;
-import redis.clients.jedis.csc.hash.OpenHftCommandHasher;
+import redis.clients.jedis.csc.hash.SimpleCommandHasher;
public class CaffeineClientSideCache extends ClientSideCache {
private final Cache cache;
public CaffeineClientSideCache(Cache caffeineCache) {
- this(caffeineCache, DefaultClientSideCacheable.INSTANCE);
+ this(caffeineCache, new SimpleCommandHasher());
+ }
+
+ public CaffeineClientSideCache(Cache caffeineCache, CommandLongHasher commandHasher) {
+ this(caffeineCache, commandHasher, DefaultClientSideCacheable.INSTANCE);
}
public CaffeineClientSideCache(Cache caffeineCache, ClientSideCacheable cacheable) {
- this(caffeineCache, new OpenHftCommandHasher(OpenHftCommandHasher.DEFAULT_HASH_FUNCTION), cacheable);
+ this(caffeineCache, new SimpleCommandHasher(), cacheable);
}
public CaffeineClientSideCache(Cache caffeineCache, CommandLongHasher commandHasher, ClientSideCacheable cacheable) {
diff --git a/src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayCommandHasher.java b/src/main/java/redis/clients/jedis/csc/hash/AbstractSimpleCommandHasher.java
similarity index 64%
rename from src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayCommandHasher.java
rename to src/main/java/redis/clients/jedis/csc/hash/AbstractSimpleCommandHasher.java
index ed4a9ff97c1..3b782c23414 100644
--- a/src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayCommandHasher.java
+++ b/src/main/java/redis/clients/jedis/csc/hash/AbstractSimpleCommandHasher.java
@@ -4,11 +4,11 @@
import redis.clients.jedis.args.Rawable;
/**
- * It is possible to extend {@link PrimitiveArrayCommandHasher this abstract class} in order to implement
- * {@link CommandLongHasher} as {@link PrimitiveArrayCommandHasher#hashLongs(long[])} and
- * {@link PrimitiveArrayCommandHasher#hashBytes(byte[])} can be supported by almost all Java hashing libraries.
+ * 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 PrimitiveArrayCommandHasher extends AbstractCommandHasher {
+public abstract class AbstractSimpleCommandHasher extends AbstractCommandHasher {
@Override
protected final long hashRawable(Rawable raw) {
diff --git a/src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHasher.java b/src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHasher.java
index 8c072452730..956d1f3628d 100644
--- a/src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHasher.java
+++ b/src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHasher.java
@@ -4,12 +4,19 @@
import com.google.common.hash.Hasher;
import redis.clients.jedis.CommandObject;
-public class GuavaCommandHasher implements CommandLongHasher {
+/**
+ * An implementation of {@link CommandLongHasher} based on {@link HashFunction} of 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;
}
diff --git a/src/main/java/redis/clients/jedis/csc/hash/SimpleCommandHasher.java b/src/main/java/redis/clients/jedis/csc/hash/SimpleCommandHasher.java
new file mode 100644
index 00000000000..1414d972952
--- /dev/null
+++ b/src/main/java/redis/clients/jedis/csc/hash/SimpleCommandHasher.java
@@ -0,0 +1,25 @@
+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/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java
index 189332b2602..b01449c9cf1 100644
--- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java
+++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java
@@ -7,8 +7,6 @@
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.cache.CacheBuilder;
import java.util.function.Supplier;
-import net.openhft.hashing.LongHashFunction;
-
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.hamcrest.Matchers;
import org.junit.After;
@@ -23,7 +21,6 @@
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.JedisPooled;
-import redis.clients.jedis.csc.hash.OpenHftCommandHasher;
public class ClientSideCacheLibsTest {
@@ -92,8 +89,7 @@ public void guavaMore() {
@Test
public void caffeineSimple() {
- CaffeineClientSideCache caffeine = CaffeineClientSideCache.builder().maximumSize(10).ttl(10)
- .commandHasher(new OpenHftCommandHasher(LongHashFunction.xx())).build();
+ CaffeineClientSideCache caffeine = CaffeineClientSideCache.builder().maximumSize(10).ttl(10).build();
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), caffeine)) {
control.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
@@ -107,7 +103,8 @@ public void caffeineMore() {
com.github.benmanes.caffeine.cache.Cache caffeine = Caffeine.newBuilder().recordStats().build();
- try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CaffeineClientSideCache(caffeine),
+ try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(),
+ new CaffeineClientSideCache(caffeine, new OpenHftCommandHasher()),
singleConnectionPoolConfig.get())) {
control.set("foo", "bar");
assertEquals(0, caffeine.estimatedSize());
diff --git a/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java b/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java
index 4ff4e4367a2..421eed8b42a 100644
--- a/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java
+++ b/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java
@@ -1,26 +1,12 @@
package redis.clients.jedis.csc;
-import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
-import redis.clients.jedis.csc.hash.PrimitiveArrayCommandHasher;
+import redis.clients.jedis.csc.hash.SimpleCommandHasher;
public class MapClientSideCache extends ClientSideCache {
- private static final PrimitiveArrayCommandHasher HASHING = new PrimitiveArrayCommandHasher() {
-
- @Override
- protected long hashLongs(long[] longs) {
- return Arrays.hashCode(longs);
- }
-
- @Override
- protected long hashBytes(byte[] bytes) {
- return Arrays.hashCode(bytes);
- }
- };
-
private final Map cache;
public MapClientSideCache() {
@@ -28,12 +14,12 @@ public MapClientSideCache() {
}
public MapClientSideCache(Map map) {
- super(HASHING);
+ super(SimpleCommandHasher.INSTANCE);
this.cache = map;
}
public MapClientSideCache(Map cache, ClientSideCacheable cacheable) {
- super(HASHING, cacheable);
+ super(SimpleCommandHasher.INSTANCE, cacheable);
this.cache = cache;
}
diff --git a/src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHasher.java b/src/test/java/redis/clients/jedis/csc/OpenHftCommandHasher.java
similarity index 70%
rename from src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHasher.java
rename to src/test/java/redis/clients/jedis/csc/OpenHftCommandHasher.java
index e2a7bced3db..147ad42e8fa 100644
--- a/src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHasher.java
+++ b/src/test/java/redis/clients/jedis/csc/OpenHftCommandHasher.java
@@ -1,13 +1,18 @@
-package redis.clients.jedis.csc.hash;
+package redis.clients.jedis.csc;
import net.openhft.hashing.LongHashFunction;
+import redis.clients.jedis.csc.hash.AbstractSimpleCommandHasher;
-public class OpenHftCommandHasher extends PrimitiveArrayCommandHasher implements CommandLongHasher {
+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;
}