Binary key & values seems to be implemented now
This commit is contained in:
@@ -3,13 +3,19 @@ package redis.clients.util;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import redis.clients.jedis.Protocol;
|
||||
|
||||
public interface Hashing {
|
||||
public static final Hashing MURMUR_HASH = new MurmurHash();
|
||||
|
||||
public static final Hashing MD5 = new Hashing() {
|
||||
private MessageDigest md5 = null; // avoid recurring construction
|
||||
|
||||
|
||||
public long hash(String key) {
|
||||
return hash(key.getBytes(Protocol.UTF8));
|
||||
}
|
||||
|
||||
public long hash(byte[] key) {
|
||||
if (md5 == null) {
|
||||
try {
|
||||
md5 = MessageDigest.getInstance("MD5");
|
||||
@@ -20,7 +26,7 @@ public interface Hashing {
|
||||
}
|
||||
|
||||
md5.reset();
|
||||
md5.update(key.getBytes());
|
||||
md5.update(key);
|
||||
byte[] bKey = md5.digest();
|
||||
long res = ((long) (bKey[3] & 0xFF) << 24)
|
||||
| ((long) (bKey[2] & 0xFF) << 16)
|
||||
@@ -30,4 +36,5 @@ public interface Hashing {
|
||||
};
|
||||
|
||||
public long hash(String key);
|
||||
public long hash(byte[] key);
|
||||
}
|
||||
@@ -20,6 +20,8 @@ package redis.clients.util;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
import redis.clients.jedis.Protocol;
|
||||
|
||||
/**
|
||||
* This is a very fast, non-cryptographic hash suitable for general hash-based
|
||||
* lookup. See http://murmurhash.googlepages.com/ for more details.
|
||||
@@ -156,7 +158,11 @@ public class MurmurHash implements Hashing {
|
||||
return h;
|
||||
}
|
||||
|
||||
public long hash(byte[] key) {
|
||||
return hash64A(key, 0x1234ABCD);
|
||||
}
|
||||
|
||||
public long hash(String key) {
|
||||
return hash64A(key.getBytes(), 0x1234ABCD);
|
||||
return hash(key.getBytes(Protocol.UTF8));
|
||||
}
|
||||
}
|
||||
@@ -69,10 +69,19 @@ public class Sharded<R, S extends ShardInfo<R>> {
|
||||
}
|
||||
}
|
||||
|
||||
public R getShard(String key) {
|
||||
return nodes.floorEntry(algo.hash(getKeyTag(key))).getValue()
|
||||
.getResource();
|
||||
}
|
||||
public R getShard(byte[] key) {
|
||||
return nodes
|
||||
.floorEntry(algo.hash(key))
|
||||
.getValue()
|
||||
.getResource();
|
||||
}
|
||||
|
||||
public R getShard(String key) {
|
||||
return nodes
|
||||
.floorEntry(algo.hash(getKeyTag(key)))
|
||||
.getValue()
|
||||
.getResource();
|
||||
}
|
||||
|
||||
public S getShardInfo(String key) {
|
||||
return nodes.floorEntry(algo.hash(getKeyTag(key))).getValue();
|
||||
|
||||
Reference in New Issue
Block a user