Skip to content

Commit 3bd45a4

Browse files
authored
Remove openhft hashing from source dependency (#3800)
1 parent 767fc01 commit 3bd45a4

File tree

8 files changed

+60
-37
lines changed

8 files changed

+60
-37
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
<groupId>net.openhft</groupId>
9494
<artifactId>zero-allocation-hashing</artifactId>
9595
<version>0.16</version>
96-
<optional>true</optional>
96+
<scope>test</scope>
9797
</dependency>
9898

9999
<!-- UNIX socket connection support -->

src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
55
import java.util.concurrent.TimeUnit;
66

77
import redis.clients.jedis.csc.hash.CommandLongHasher;
8-
import redis.clients.jedis.csc.hash.OpenHftCommandHasher;
8+
import redis.clients.jedis.csc.hash.SimpleCommandHasher;
99

1010
public class CaffeineClientSideCache extends ClientSideCache {
1111

1212
private final Cache<Long, Object> cache;
1313

1414
public CaffeineClientSideCache(Cache<Long, Object> caffeineCache) {
15-
this(caffeineCache, DefaultClientSideCacheable.INSTANCE);
15+
this(caffeineCache, SimpleCommandHasher.INSTANCE);
16+
}
17+
18+
public CaffeineClientSideCache(Cache<Long, Object> caffeineCache, CommandLongHasher commandHasher) {
19+
this(caffeineCache, commandHasher, DefaultClientSideCacheable.INSTANCE);
1620
}
1721

1822
public CaffeineClientSideCache(Cache<Long, Object> caffeineCache, ClientSideCacheable cacheable) {
19-
this(caffeineCache, new OpenHftCommandHasher(OpenHftCommandHasher.DEFAULT_HASH_FUNCTION), cacheable);
23+
this(caffeineCache, SimpleCommandHasher.INSTANCE, cacheable);
2024
}
2125

2226
public CaffeineClientSideCache(Cache<Long, Object> caffeineCache, CommandLongHasher commandHasher, ClientSideCacheable cacheable) {
@@ -55,7 +59,7 @@ public static class Builder {
5559
private final TimeUnit expireTimeUnit = TimeUnit.SECONDS;
5660

5761
// not using a default value to avoid an object creation like 'new OpenHftHashing(hashFunction)'
58-
private CommandLongHasher commandHasher = null;
62+
private CommandLongHasher commandHasher = SimpleCommandHasher.INSTANCE;
5963

6064
private ClientSideCacheable cacheable = DefaultClientSideCacheable.INSTANCE;
6165

@@ -88,8 +92,7 @@ public CaffeineClientSideCache build() {
8892

8993
cb.expireAfterWrite(expireTime, expireTimeUnit);
9094

91-
return commandHasher != null ? new CaffeineClientSideCache(cb.build(), commandHasher, cacheable)
92-
: new CaffeineClientSideCache(cb.build(), cacheable);
95+
return new CaffeineClientSideCache(cb.build(), commandHasher, cacheable);
9396
}
9497
}
9598
}

src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayCommandHasher.java renamed to src/main/java/redis/clients/jedis/csc/hash/AbstractSimpleCommandHasher.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import redis.clients.jedis.args.Rawable;
55

66
/**
7-
* It is possible to extend {@link PrimitiveArrayCommandHasher this abstract class} in order to implement
8-
* {@link CommandLongHasher} as {@link PrimitiveArrayCommandHasher#hashLongs(long[])} and
9-
* {@link PrimitiveArrayCommandHasher#hashBytes(byte[])} can be supported by almost all Java hashing libraries.
7+
* It is possible to extend {@link AbstractSimpleCommandHasher this abstract class} in order to implement
8+
* {@link CommandLongHasher} as {@link AbstractSimpleCommandHasher#hashLongs(long[])} and
9+
* {@link AbstractSimpleCommandHasher#hashBytes(byte[])} are supported by almost all Java hashing libraries.
1010
*/
11-
public abstract class PrimitiveArrayCommandHasher extends AbstractCommandHasher {
11+
public abstract class AbstractSimpleCommandHasher extends AbstractCommandHasher {
1212

1313
@Override
1414
protected final long hashRawable(Rawable raw) {

src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHasher.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
import com.google.common.hash.Hasher;
55
import redis.clients.jedis.CommandObject;
66

7-
public class GuavaCommandHasher implements CommandLongHasher {
7+
/**
8+
* An implementation of {@link CommandLongHasher} based on {@link HashFunction} from Google Guava library.
9+
*/
10+
public final class GuavaCommandHasher implements CommandLongHasher {
811

912
public static final HashFunction DEFAULT_HASH_FUNCTION = com.google.common.hash.Hashing.fingerprint2011();
1013

1114
private final HashFunction function;
1215

16+
/**
17+
* It is advised to use a {@link HashFunction} capable of producing 64-bit hash.
18+
* @param function an implementation of hash function
19+
*/
1320
public GuavaCommandHasher(HashFunction function) {
1421
this.function = function;
1522
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package redis.clients.jedis.csc.hash;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* This {@link CommandLongHasher} implementation is simply based on {@link Arrays#hashCode(long[])}
7+
* and {@link Arrays#hashCode(byte[])}. These methods actually produce 32-bit hash codes. It is
8+
* advised to use proper 64-bit hash codes in production.
9+
*/
10+
public final class SimpleCommandHasher extends AbstractSimpleCommandHasher {
11+
12+
public static final SimpleCommandHasher INSTANCE = new SimpleCommandHasher();
13+
14+
public SimpleCommandHasher() { }
15+
16+
@Override
17+
protected long hashLongs(long[] longs) {
18+
return Arrays.hashCode(longs);
19+
}
20+
21+
@Override
22+
protected long hashBytes(byte[] bytes) {
23+
return Arrays.hashCode(bytes);
24+
}
25+
}

src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import com.github.benmanes.caffeine.cache.Caffeine;
88
import com.google.common.cache.CacheBuilder;
99
import java.util.function.Supplier;
10-
import net.openhft.hashing.LongHashFunction;
11-
1210
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
1311
import org.hamcrest.Matchers;
1412
import org.junit.After;
@@ -23,7 +21,6 @@
2321
import redis.clients.jedis.Jedis;
2422
import redis.clients.jedis.JedisClientConfig;
2523
import redis.clients.jedis.JedisPooled;
26-
import redis.clients.jedis.csc.hash.OpenHftCommandHasher;
2724

2825
public class ClientSideCacheLibsTest {
2926

@@ -92,8 +89,7 @@ public void guavaMore() {
9289

9390
@Test
9491
public void caffeineSimple() {
95-
CaffeineClientSideCache caffeine = CaffeineClientSideCache.builder().maximumSize(10).ttl(10)
96-
.commandHasher(new OpenHftCommandHasher(LongHashFunction.xx())).build();
92+
CaffeineClientSideCache caffeine = CaffeineClientSideCache.builder().maximumSize(10).ttl(10).build();
9793
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), caffeine)) {
9894
control.set("foo", "bar");
9995
assertEquals("bar", jedis.get("foo"));
@@ -107,7 +103,8 @@ public void caffeineMore() {
107103

108104
com.github.benmanes.caffeine.cache.Cache caffeine = Caffeine.newBuilder().recordStats().build();
109105

110-
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CaffeineClientSideCache(caffeine),
106+
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(),
107+
new CaffeineClientSideCache(caffeine, new OpenHftCommandHasher()),
111108
singleConnectionPoolConfig.get())) {
112109
control.set("foo", "bar");
113110
assertEquals(0, caffeine.estimatedSize());

src/test/java/redis/clients/jedis/csc/MapClientSideCache.java

+3-17
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,25 @@
11
package redis.clients.jedis.csc;
22

3-
import java.util.Arrays;
43
import java.util.HashMap;
54
import java.util.Map;
65

7-
import redis.clients.jedis.csc.hash.PrimitiveArrayCommandHasher;
6+
import redis.clients.jedis.csc.hash.SimpleCommandHasher;
87

98
public class MapClientSideCache extends ClientSideCache {
109

11-
private static final PrimitiveArrayCommandHasher HASHING = new PrimitiveArrayCommandHasher() {
12-
13-
@Override
14-
protected long hashLongs(long[] longs) {
15-
return Arrays.hashCode(longs);
16-
}
17-
18-
@Override
19-
protected long hashBytes(byte[] bytes) {
20-
return Arrays.hashCode(bytes);
21-
}
22-
};
23-
2410
private final Map<Long, Object> cache;
2511

2612
public MapClientSideCache() {
2713
this(new HashMap<>());
2814
}
2915

3016
public MapClientSideCache(Map<Long, Object> map) {
31-
super(HASHING);
17+
super(SimpleCommandHasher.INSTANCE);
3218
this.cache = map;
3319
}
3420

3521
public MapClientSideCache(Map<Long, Object> cache, ClientSideCacheable cacheable) {
36-
super(HASHING, cacheable);
22+
super(SimpleCommandHasher.INSTANCE, cacheable);
3723
this.cache = cache;
3824
}
3925

src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHasher.java renamed to src/test/java/redis/clients/jedis/csc/OpenHftCommandHasher.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
package redis.clients.jedis.csc.hash;
1+
package redis.clients.jedis.csc;
22

33
import net.openhft.hashing.LongHashFunction;
4+
import redis.clients.jedis.csc.hash.AbstractSimpleCommandHasher;
45

5-
public class OpenHftCommandHasher extends PrimitiveArrayCommandHasher implements CommandLongHasher {
6+
public class OpenHftCommandHasher extends AbstractSimpleCommandHasher {
67

78
public static final LongHashFunction DEFAULT_HASH_FUNCTION = LongHashFunction.xx3();
89

910
private final LongHashFunction function;
1011

12+
OpenHftCommandHasher() {
13+
this(DEFAULT_HASH_FUNCTION);
14+
}
15+
1116
public OpenHftCommandHasher(LongHashFunction function) {
1217
this.function = function;
1318
}

0 commit comments

Comments
 (0)