-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce interface(s) for hashing CommandObject (#3743)
- Loading branch information
Showing
11 changed files
with
194 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHashing.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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 AbstractCommandHashing implements CommandLongHashing { | ||
|
||
@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(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/redis/clients/jedis/csc/hash/CommandLongHashing.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package redis.clients.jedis.csc.hash; | ||
|
||
import redis.clients.jedis.CommandObject; | ||
|
||
/** | ||
* The interface for hashing a command object for client-side caching. | ||
*/ | ||
public interface CommandLongHashing { | ||
|
||
/** | ||
* Produce a 64-bit signed hash from a command object. | ||
* @param command the command object | ||
* @return 64-bit signed hash | ||
*/ | ||
long hash(CommandObject command); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/redis/clients/jedis/csc/hash/GuavaHashing.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package redis.clients.jedis.csc.hash; | ||
|
||
import com.google.common.hash.HashFunction; | ||
import com.google.common.hash.Hasher; | ||
import redis.clients.jedis.CommandObject; | ||
|
||
public class GuavaHashing implements CommandLongHashing { | ||
|
||
public static final HashFunction DEFAULT_HASH_FUNCTION = com.google.common.hash.Hashing.fingerprint2011(); | ||
|
||
private final HashFunction function; | ||
|
||
public GuavaHashing(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(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/redis/clients/jedis/csc/hash/OpenHftHashing.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package redis.clients.jedis.csc.hash; | ||
|
||
import net.openhft.hashing.LongHashFunction; | ||
|
||
public class OpenHftHashing extends PrimitiveArrayHashing implements CommandLongHashing { | ||
|
||
public static final LongHashFunction DEFAULT_HASH_FUNCTION = LongHashFunction.xx3(); | ||
|
||
private final LongHashFunction function; | ||
|
||
public OpenHftHashing(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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayHashing.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package redis.clients.jedis.csc.hash; | ||
|
||
import redis.clients.jedis.Builder; | ||
import redis.clients.jedis.args.Rawable; | ||
|
||
public abstract class PrimitiveArrayHashing extends AbstractCommandHashing { | ||
|
||
@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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.