nodes = new TreeMap<Long, Node>(); for (int i = 0; i != shards.size(); ++i) { final Node shardInfo = shards.get(i); for (int n = 0; n < 160 ; n++) { nodes.put(MurmurHash.hash("SHARD-" + i + "-NODE-" + n), shardInfo); }
publicstaticlonghash64A(ByteBuffer buf, int seed){ ByteOrder byteOrder = buf.order(); buf.order(ByteOrder.LITTLE_ENDIAN);
long m = 0xc6a4a7935bd1e995L; int r = 47;
long h = seed ^ (buf.remaining() * m);
long k; while (buf.remaining() >= 8) { k = buf.getLong();
k *= m; k ^= k >>> r; k *= m;
h ^= k; h *= m; }
if (buf.remaining() > 0) { ByteBuffer finish = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN); // for big-endian version, do this first: // finish.position(8-buf.remaining()); finish.put(buf).rewind(); h ^= finish.getLong(); h *= m; }
h ^= h >>> r; h *= m; h ^= h >>> r;
buf.order(byteOrder); return h; }
初始化的TreeMap的内容:
2.根据缓存key找具体的数据节点
1 2 3 4 5 6 7
public Node getNodeByKey(byte[] key){ //取大于或等于hash key的列表 SortedMap<Long, Node> tail = nodes.tailMap(MurmurHash.hash(key)); if (tail.isEmpty()) return nodes.get(nodes.firstKey()); return tail.get(tail.firstKey()); }