This commit is contained in:
joyyizhang 2020-02-15 22:26:47 +08:00
parent 2570b67f5e
commit 18cc8265c5
3 changed files with 51 additions and 2 deletions

View File

@ -35,4 +35,10 @@ hash.AddNode("node3", 3)
//get key's node
node := hash.GetNode("key")
```
```
坑点:
// 一个9064767行的文件 每行做hashring结果不一致 原因hashBytes只取4位 529和83 一致 ,这个库是一对多的关系,非多对一
// node=1000 vitualSpots=20000
//9063340 key=49f57cf0 node=529
//9063340 key=49f57cf0 node=83

View File

@ -127,7 +127,6 @@ func (h *HashRing) GetNode(s string) string {
if len(h.nodes) == 0 {
return ""
}
hash := sha1.New()
hash.Write([]byte(s))
hashBytes := hash.Sum(nil)

View File

@ -1,6 +1,7 @@
package hashring
import (
"crypto/sha1"
"fmt"
"regexp"
"runtime"
@ -114,3 +115,46 @@ func TestSpeed(t *testing.T) {
hash.AddNodes(nodeWeight)
}
func TestManyTime(t *testing.T) {
// 一个9064767行的文件 每行做hashring结果不一致 原因hashBytes只取4位 529和83 一致 ,这个库是一对多的关系,非多对一
// node=1000 vitualSpots=20000
//9063340 key=49f57cf0 node=529
//9063340 key=49f57cf0 node=83
hash := sha1.New()
hash.Write([]byte("529" + ":" + strconv.Itoa(1)))
hashBytes := hash.Sum(nil)
a := hashBytes[6:10]
hash2 := sha1.New()
hash2.Write([]byte("83" + ":" + strconv.Itoa(1)))
hashBytes2 := hash.Sum(nil)
a2 := hashBytes2[6:10]
fmt.Printf("%s %s\n", a, a2)
//defer TimeTrack(time.Now())
//nodeWeight := make(map[string]int)
//for i := 0; i < 1000; i += 1 {
// nodeWeight[strconv.Itoa(i)] = 1
//}
//vitualSpots := 20000
//hash := NewHashRing(vitualSpots)
//hash.AddNodes(nodeWeight)
//for _, nn := range hash.nodes {
// if "529" != nn.nodeKey {
// fmt.Printf("529 %s\n", nn.nodeKey)
// }
// if "83" != nn.nodeKey {
// fmt.Printf("83 %s\n", nn.nodeKey)
// }
//}
//for i := 0; i <= 906476700; i++ {
// node := hash.GetNode("49f57cf0")
// if "83" != node {
// t.Errorf("err%s\n", node)
// return
// }
//}
}