Skip to content

Commit

Permalink
Remove openhft hashing from source dependency (#3800)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 authored Apr 5, 2024
1 parent 767fc01 commit 3bd45a4
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 37 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
<groupId>net.openhft</groupId>
<artifactId>zero-allocation-hashing</artifactId>
<version>0.16</version>
<optional>true</optional>
<scope>test</scope>
</dependency>

<!-- UNIX socket connection support -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long, Object> cache;

public CaffeineClientSideCache(Cache<Long, Object> caffeineCache) {
this(caffeineCache, DefaultClientSideCacheable.INSTANCE);
this(caffeineCache, SimpleCommandHasher.INSTANCE);
}

Check warning on line 16 in src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java#L15-L16

Added lines #L15 - L16 were not covered by tests

public CaffeineClientSideCache(Cache<Long, Object> caffeineCache, CommandLongHasher commandHasher) {
this(caffeineCache, commandHasher, DefaultClientSideCacheable.INSTANCE);
}

public CaffeineClientSideCache(Cache<Long, Object> caffeineCache, ClientSideCacheable cacheable) {
this(caffeineCache, new OpenHftCommandHasher(OpenHftCommandHasher.DEFAULT_HASH_FUNCTION), cacheable);
this(caffeineCache, SimpleCommandHasher.INSTANCE, cacheable);
}

Check warning on line 24 in src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java#L23-L24

Added lines #L23 - L24 were not covered by tests

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

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

private ClientSideCacheable cacheable = DefaultClientSideCacheable.INSTANCE;

Expand Down Expand Up @@ -88,8 +92,7 @@ public CaffeineClientSideCache build() {

cb.expireAfterWrite(expireTime, expireTimeUnit);

return commandHasher != null ? new CaffeineClientSideCache(cb.build(), commandHasher, cacheable)
: new CaffeineClientSideCache(cb.build(), cacheable);
return new CaffeineClientSideCache(cb.build(), commandHasher, cacheable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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} 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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"));
Expand All @@ -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());
Expand Down
20 changes: 3 additions & 17 deletions src/test/java/redis/clients/jedis/csc/MapClientSideCache.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
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<Long, Object> cache;

public MapClientSideCache() {
this(new HashMap<>());
}

public MapClientSideCache(Map<Long, Object> map) {
super(HASHING);
super(SimpleCommandHasher.INSTANCE);
this.cache = map;
}

public MapClientSideCache(Map<Long, Object> cache, ClientSideCacheable cacheable) {
super(HASHING, cacheable);
super(SimpleCommandHasher.INSTANCE, cacheable);
this.cache = cache;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down

0 comments on commit 3bd45a4

Please sign in to comment.