Beispiel #1
0
// get the node where a key should be stored
// it should be stored in the rightmost node less than 'key'
func (r *HashRing) GetNode(key *Key) Node {
	r.Lock()
	defer r.Unlock()
	var replica_idx = bisect.Bisect(r.sorted_keys, key) - 1
	if replica_idx == -1 {
		replica_idx = len(r.sorted_keys) - 1
	}
	return r.virtual_nodes[r.sorted_keys[replica_idx].(Key)]
}
Beispiel #2
0
// get the 'num' nodes where a key can be stored replicated
// since the key is stored in the preceding virtual-node, replicas
// can be store in the n previous buckets in case the primary fails
func (r *HashRing) GetNodes(key *Key, num int) (list []Node) {
	list = make([]Node, 0, 3)
	r.Lock()
	var base_idx = bisect.Bisect(r.sorted_keys, key)
	for i := 1; i <= num; i++ {
		var idx = (base_idx - i + (num * len(r.sorted_keys))) % len(r.sorted_keys)
		list = append(list, r.virtual_nodes[r.sorted_keys[idx].(Key)])
	}
	r.Unlock()
	return
}
Beispiel #3
0
// get the right-open range [start, end) of keys a virtual-node can hold
func (r *HashRing) NodeKeys(n Node, replica uint) (start, end Key) {
	var key = r.virtualNodeKey(n, replica)
	r.Lock()
	var idx = bisect.Bisect(r.sorted_keys, key)
	end = r.sorted_keys[idx%len(r.sorted_keys)].(Key)
	if idx == 0 {
		start = r.sorted_keys[len(r.sorted_keys)-1].(Key)
	} else {
		start = r.sorted_keys[idx-1].(Key)
	}
	r.Unlock()
	return
}