add code comments

This commit is contained in:
g4zhuj 2018-04-09 11:33:06 +08:00
parent b84728bd22
commit 4f382af59b

View File

@ -9,7 +9,8 @@ import (
) )
const ( const (
DefaultVirualSpots = 40 //DefaultVirualSpots default virual spots
DefaultVirualSpots = 400
) )
type node struct { type node struct {
@ -24,12 +25,14 @@ func (p nodesArray) Less(i, j int) bool { return p[i].spotValue < p[j].spotValue
func (p nodesArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p nodesArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p nodesArray) Sort() { sort.Sort(p) } func (p nodesArray) Sort() { sort.Sort(p) }
//HashRing store nodes and weigths
type HashRing struct { type HashRing struct {
virualSpots int virualSpots int
nodes nodesArray nodes nodesArray
weights map[string]int weights map[string]int
} }
//NewHashRing create a hash ring with virual spots
func NewHashRing(spots int) *HashRing { func NewHashRing(spots int) *HashRing {
if spots == 0 { if spots == 0 {
spots = DefaultVirualSpots spots = DefaultVirualSpots
@ -42,6 +45,7 @@ func NewHashRing(spots int) *HashRing {
return h return h
} }
//AddNodes add nodes to hash ring
func (h *HashRing) AddNodes(nodeWeight map[string]int) { func (h *HashRing) AddNodes(nodeWeight map[string]int) {
for nodeKey, w := range nodeWeight { for nodeKey, w := range nodeWeight {
h.weights[nodeKey] = w h.weights[nodeKey] = w
@ -49,16 +53,19 @@ func (h *HashRing) AddNodes(nodeWeight map[string]int) {
h.generate() h.generate()
} }
//AddNode add node to hash ring
func (h *HashRing) AddNode(nodeKey string, weight int) { func (h *HashRing) AddNode(nodeKey string, weight int) {
h.weights[nodeKey] = weight h.weights[nodeKey] = weight
h.generate() h.generate()
} }
//RemoveNode remove node
func (h *HashRing) RemoveNode(nodeKey string) { func (h *HashRing) RemoveNode(nodeKey string) {
delete(h.weights, nodeKey) delete(h.weights, nodeKey)
h.generate() h.generate()
} }
//UpdateNode update node with weight
func (h *HashRing) UpdateNode(nodeKey string, weight int) { func (h *HashRing) UpdateNode(nodeKey string, weight int) {
h.weights[nodeKey] = weight h.weights[nodeKey] = weight
h.generate() h.generate()
@ -98,6 +105,7 @@ func genValue(bs []byte) uint32 {
return v return v
} }
//GetNode get node with key
func (h *HashRing) GetNode(s string) string { func (h *HashRing) GetNode(s string) string {
if len(h.nodes) == 0 { if len(h.nodes) == 0 {
return "" return ""