Added MurmureHash as sharding algo.

This commit is contained in:
Jonathan Leibiusky
2010-09-14 14:08:49 -03:00
parent a8ffacd30a
commit 8629360954
4 changed files with 119 additions and 16 deletions

View File

@@ -10,15 +10,19 @@ import java.util.TreeMap;
public abstract class Sharded<T> {
public static final int DEFAULT_WEIGHT = 1;
private static MessageDigest md5 = null; // avoid recurring construction
private TreeMap<Long, ShardInfo> nodes;
private int totalWeight;
private Map<ShardInfo, T> resources;
private Hashing algo = Hashing.MD5;
public Sharded(List<ShardInfo> shards) {
initialize(shards);
}
public Sharded(List<ShardInfo> shards, Hashing algo) {
initialize(shards);
}
private void initialize(List<ShardInfo> shards) {
nodes = new TreeMap<Long, ShardInfo>();
resources = new HashMap<ShardInfo, T>();
@@ -62,21 +66,7 @@ public abstract class Sharded<T> {
}
private Long calculateHash(String key) {
if (md5 == null) {
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("++++ no md5 algorythm found");
}
}
md5.reset();
md5.update(key.getBytes());
byte[] bKey = md5.digest();
long res = ((long) (bKey[3] & 0xFF) << 24)
| ((long) (bKey[2] & 0xFF) << 16)
| ((long) (bKey[1] & 0xFF) << 8) | (long) (bKey[0] & 0xFF);
return res;
return algo.hash(key);
}
private Long findPointFor(Long hashK) {