Skip to content

Commit d174f21

Browse files
committed
update
1 parent ba07bcd commit d174f21

File tree

6 files changed

+27
-184
lines changed

6 files changed

+27
-184
lines changed

redis/bloom.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,20 @@ func (c *Client) SetBloom(ctx context.Context, bloomname string, groupnum uint64
160160
if bitnum < 1024 {
161161
bitnum = 1024
162162
}
163-
key := bloomname + "_" + strconv.FormatUint(common.BkdrhashString(userkey, uint64(groupnum)), 10)
163+
key := bloomname + "_" + strconv.FormatUint(common.Bkdrhash(common.STB(userkey), uint64(groupnum)), 10)
164164
keybkdr := "{" + key + "}_bkdr"
165165
keydjb := "{" + key + "}_djb"
166166
keyfnv := "{" + key + "}_fnv"
167167
keydek := "{" + key + "}_dek"
168168
keyrs := "{" + key + "}_rs"
169169
keysdbm := "{" + key + "}_sdbm"
170170
keyexist := "{" + key + "}_exist"
171-
bit1 := common.BkdrhashString(userkey, bitnum)
172-
bit2 := common.DjbhashString(userkey, bitnum)
173-
bit3 := common.FnvhashString(userkey, bitnum)
174-
bit4 := common.DekhashString(userkey, bitnum)
175-
bit5 := common.RshashString(userkey, bitnum)
176-
bit6 := common.SdbmhashString(userkey, bitnum)
171+
bit1 := common.Bkdrhash(common.STB(userkey), bitnum)
172+
bit2 := common.Djbhash(common.STB(userkey), bitnum)
173+
bit3 := common.Fnvhash(common.STB(userkey), bitnum)
174+
bit4 := common.Dekhash(common.STB(userkey), bitnum)
175+
bit5 := common.Rshash(common.STB(userkey), bitnum)
176+
bit6 := common.Sdbmhash(common.STB(userkey), bitnum)
177177
r, e := setBloom.Run(ctx, c, []string{keybkdr, keydjb, keyfnv, keydek, keyrs, keysdbm, keyexist}, bit1, bit2, bit3, bit4, bit5, bit6).Int()
178178
if e != nil {
179179
return false, e
@@ -192,20 +192,20 @@ func (c *Client) CheckBloom(ctx context.Context, bloomname string, groupnum uint
192192
if bitnum < 1024 {
193193
bitnum = 1024
194194
}
195-
key := bloomname + "_" + strconv.FormatUint(common.BkdrhashString(userkey, uint64(groupnum)), 10)
195+
key := bloomname + "_" + strconv.FormatUint(common.Bkdrhash(common.STB(userkey), uint64(groupnum)), 10)
196196
keybkdr := "{" + key + "}_bkdr"
197197
keydjb := "{" + key + "}_djb"
198198
keyfnv := "{" + key + "}_fnv"
199199
keydek := "{" + key + "}_dek"
200200
keyrs := "{" + key + "}_rs"
201201
keysdbm := "{" + key + "}_sdbm"
202202
keyexist := "{" + key + "}_exist"
203-
bit1 := common.BkdrhashString(userkey, bitnum)
204-
bit2 := common.DjbhashString(userkey, bitnum)
205-
bit3 := common.FnvhashString(userkey, bitnum)
206-
bit4 := common.DekhashString(userkey, bitnum)
207-
bit5 := common.RshashString(userkey, bitnum)
208-
bit6 := common.SdbmhashString(userkey, bitnum)
203+
bit1 := common.Bkdrhash(common.STB(userkey), bitnum)
204+
bit2 := common.Djbhash(common.STB(userkey), bitnum)
205+
bit3 := common.Fnvhash(common.STB(userkey), bitnum)
206+
bit4 := common.Dekhash(common.STB(userkey), bitnum)
207+
bit5 := common.Rshash(common.STB(userkey), bitnum)
208+
bit6 := common.Sdbmhash(common.STB(userkey), bitnum)
209209
r, e := checkBloom.Run(ctx, c, []string{keybkdr, keydjb, keyfnv, keydek, keyrs, keysdbm, keyexist}, bit1, bit2, bit3, bit4, bit5, bit6).Int()
210210
if e != nil {
211211
return false, e

redis/broadcast.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (c *Client) PubBroadcast(ctx context.Context, broadcast string, shard uint8
6060
if key == "" {
6161
stream = "broadcast_" + broadcast + "_" + strconv.FormatUint(rand.Uint64()%uint64(shard), 10)
6262
} else {
63-
stream = "broadcast_" + broadcast + "_" + strconv.FormatUint(common.BkdrhashString(key, uint64(shard)), 10)
63+
stream = "broadcast_" + broadcast + "_" + strconv.FormatUint(common.Bkdrhash(common.STB(key), uint64(shard)), 10)
6464
}
6565
args := &gredis.XAddArgs{
6666
Stream: stream,

redis/unicast.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (c *Client) PubUnicast(ctx context.Context, unicast string, shard uint8, ke
108108
if key == "" {
109109
list = "unicast_" + unicast + "_" + strconv.FormatUint(rand.Uint64()%uint64(shard), 10)
110110
} else {
111-
list = "unicast_" + unicast + "_" + strconv.FormatUint(common.BkdrhashString(key, uint64(shard)), 10)
111+
list = "unicast_" + unicast + "_" + strconv.FormatUint(common.Bkdrhash(common.STB(key), uint64(shard)), 10)
112112
}
113113
exist := "{" + list + "}_exist"
114114
return pubunicast.Run(ctx, c, []string{list, exist}, values...).Err()

stream/connmng.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ var errClosing = errors.New("instance closing")
8585

8686
func (m *connmng) AddPeer(p *Peer) error {
8787
peeraddr := p.c.RemoteAddr().String()
88-
tw := m.timewheels[common.BkdrhashString(peeraddr, uint64(len(m.timewheels)))]
88+
tw := m.timewheels[common.Bkdrhash(common.STB(peeraddr), uint64(len(m.timewheels)))]
8989
g := tw.groups[(atomic.LoadUint64(&tw.index)+rand.Uint64()%50+5)%60] //rand is used to reduce race
9090
g.Lock()
9191
if _, ok := g.peers[peeraddr]; ok {

util/common/common_test.go

+4-97
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,14 @@ package common
22

33
import (
44
"bytes"
5-
"fmt"
6-
"math/rand"
7-
"sync"
85
"testing"
96
)
107

11-
func Test_RandCode(t *testing.T) {
12-
t.Log(MakeRandCode(100))
13-
}
14-
15-
func Test_Nil(t *testing.T) {
16-
a := ([]byte)(nil)
17-
if BTS(a) != "" {
18-
panic("nil")
19-
}
20-
b := make([]byte, 0)
21-
if BTS(b) != "" {
22-
panic("empty")
23-
}
24-
}
258
func Test_Common(t *testing.T) {
269
var sempty string = ""
2710
var s string = "abc"
28-
var bempty []byte = nil
11+
var bnil []byte = nil
12+
var bempty []byte = []byte{}
2913
var b []byte = []byte{'a', 'b', 'c'}
3014

3115
if !bytes.Equal(STB(s), b) {
@@ -41,84 +25,7 @@ func Test_Common(t *testing.T) {
4125
if BTS(bempty) != sempty {
4226
panic("empty byte to str failed")
4327
}
44-
wg := &sync.WaitGroup{}
45-
wg.Add(6)
46-
go func() {
47-
defer wg.Done()
48-
fmt.Println("bkdr: ", testhash(BkdrhashString, BkdrhashByte))
49-
}()
50-
go func() {
51-
defer wg.Done()
52-
fmt.Println("fnv: ", testhash(FnvhashString, FnvhashByte))
53-
}()
54-
go func() {
55-
defer wg.Done()
56-
fmt.Println("rs: ", testhash(RshashString, RshashByte))
57-
}()
58-
go func() {
59-
defer wg.Done()
60-
fmt.Println("sdbm: ", testhash(SdbmhashString, SdbmhashByte))
61-
}()
62-
go func() {
63-
defer wg.Done()
64-
fmt.Println("dek: ", testhash(DekhashString, DekhashByte))
65-
}()
66-
go func() {
67-
defer wg.Done()
68-
fmt.Println("djb: ", testhash(DjbhashString, DjbhashByte))
69-
}()
70-
wg.Wait()
71-
}
72-
73-
// capacity 1024*1024*8
74-
// test data 1024*1024*4
75-
func testhash(strf func(string, uint64) uint64, bytef func([]byte, uint64) uint64) int {
76-
total := 1024 * 1024 * 8
77-
conflict := 0
78-
bit := make(map[uint64]struct{}, total)
79-
for i := 0; i < total/2; i++ {
80-
keyb := makerandkeybyte()
81-
keys := BTS(keyb)
82-
indexs := strf(keys, uint64(total))
83-
indexb := bytef(keyb, uint64(total))
84-
if indexs != indexb {
85-
panic("string hash caculate different with byteslice hash")
86-
}
87-
if _, ok := bit[indexs]; ok {
88-
conflict++
89-
} else {
90-
bit[indexs] = struct{}{}
91-
}
92-
}
93-
return conflict
94-
}
95-
96-
// key contain [0-9][a-z][A-Z][_:-|]
97-
func makerandkeystr() string {
98-
return BTS(makerandkeybyte())
99-
}
100-
func makerandkeybyte() []byte {
101-
result := make([]byte, 10)
102-
for i := 0; i < 10; i++ {
103-
switch rand.Intn(4) {
104-
case 0:
105-
result[i] = byte(rand.Intn(10) + 48)
106-
case 1:
107-
result[i] = byte(rand.Intn(26) + 97)
108-
case 2:
109-
result[i] = byte(rand.Intn(26) + 65)
110-
case 3:
111-
switch rand.Intn(4) {
112-
case 0:
113-
result[i] = '_'
114-
case 1:
115-
result[i] = ':'
116-
case 2:
117-
result[i] = '-'
118-
case 3:
119-
result[i] = '|'
120-
}
121-
}
28+
if BTS(bnil) != sempty {
29+
panic("empty byte to str failed")
12230
}
123-
return result
12431
}

util/common/hash.go

+6-70
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package common
22

3-
func BkdrhashString(data string, total uint64) uint64 {
3+
func Bkdrhash(data []byte, total uint64) uint64 {
44
if total == 1 {
55
return 0
66
}
@@ -11,7 +11,7 @@ func BkdrhashString(data string, total uint64) uint64 {
1111
}
1212
return hash % total
1313
}
14-
func DjbhashString(data string, total uint64) uint64 {
14+
func Djbhash(data []byte, total uint64) uint64 {
1515
if total == 1 {
1616
return 0
1717
}
@@ -21,7 +21,7 @@ func DjbhashString(data string, total uint64) uint64 {
2121
}
2222
return hash % total
2323
}
24-
func FnvhashString(data string, total uint64) uint64 {
24+
func Fnvhash(data []byte, total uint64) uint64 {
2525
if total == 1 {
2626
return 0
2727
}
@@ -32,7 +32,7 @@ func FnvhashString(data string, total uint64) uint64 {
3232
}
3333
return hash % total
3434
}
35-
func DekhashString(data string, total uint64) uint64 {
35+
func Dekhash(data []byte, total uint64) uint64 {
3636
if total == 1 {
3737
return 0
3838
}
@@ -42,7 +42,7 @@ func DekhashString(data string, total uint64) uint64 {
4242
}
4343
return hash % total
4444
}
45-
func RshashString(data string, total uint64) uint64 {
45+
func Rshash(data []byte, total uint64) uint64 {
4646
if total == 1 {
4747
return 0
4848
}
@@ -54,71 +54,7 @@ func RshashString(data string, total uint64) uint64 {
5454
}
5555
return hash % total
5656
}
57-
func SdbmhashString(data string, total uint64) uint64 {
58-
if total == 1 {
59-
return 0
60-
}
61-
hash := uint64(0)
62-
for _, v := range data {
63-
hash = uint64(v) + (hash << 6) + (hash << 16) - hash
64-
}
65-
return hash % total
66-
}
67-
func BkdrhashByte(data []byte, total uint64) uint64 {
68-
if total == 1 {
69-
return 0
70-
}
71-
seed := uint64(131)
72-
hash := uint64(0)
73-
for _, v := range data {
74-
hash = hash*seed + uint64(v)
75-
}
76-
return hash % total
77-
}
78-
func DjbhashByte(data []byte, total uint64) uint64 {
79-
if total == 1 {
80-
return 0
81-
}
82-
hash := uint64(5381)
83-
for _, v := range data {
84-
hash = ((hash << 5) + hash) + uint64(v)
85-
}
86-
return hash % total
87-
}
88-
func FnvhashByte(data []byte, total uint64) uint64 {
89-
if total == 1 {
90-
return 0
91-
}
92-
hash := uint64(2166136261)
93-
for _, v := range data {
94-
hash *= uint64(16777619)
95-
hash ^= uint64(v)
96-
}
97-
return hash % total
98-
}
99-
func DekhashByte(data []byte, total uint64) uint64 {
100-
if total == 1 {
101-
return 0
102-
}
103-
hash := uint64(len(data))
104-
for _, v := range data {
105-
hash = ((hash << 5) ^ (hash >> 27)) ^ uint64(v)
106-
}
107-
return hash % total
108-
}
109-
func RshashByte(data []byte, total uint64) uint64 {
110-
if total == 1 {
111-
return 0
112-
}
113-
seed := uint64(63689)
114-
hash := uint64(0)
115-
for _, v := range data {
116-
hash = hash*seed + uint64(v)
117-
seed *= uint64(378551)
118-
}
119-
return hash % total
120-
}
121-
func SdbmhashByte(data []byte, total uint64) uint64 {
57+
func Sdbmhash(data []byte, total uint64) uint64 {
12258
if total == 1 {
12359
return 0
12460
}

0 commit comments

Comments
 (0)