Skip to content

Commit

Permalink
fix cluster slots algo slightly, thanks to @nickynick for the fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Picciano committed Aug 26, 2017
1 parent 5410c82 commit aa3d816
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
8 changes: 5 additions & 3 deletions cluster_crc16.go
@@ -1,6 +1,8 @@
package radix

import "bytes"
import (
"bytes"
)

var tab = [256]uint16{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
Expand Down Expand Up @@ -54,8 +56,8 @@ func CRC16(buf []byte) uint16 {
// taking into account key hash tags
func ClusterSlot(key []byte) uint16 {
if start := bytes.Index(key, []byte("{")); start >= 0 {
if end := bytes.Index(key[start+2:], []byte("}")); end >= 0 {
key = key[start+1 : start+2+end]
if end := bytes.Index(key[start+1:], []byte("}")); end > 0 {
key = key[start+1 : start+1+end]
}
}
return CRC16(key) % numSlots
Expand Down
20 changes: 16 additions & 4 deletions cluster_crc16_test.go
Expand Up @@ -15,9 +15,21 @@ func TestClusterSlot(t *T) {
k := []byte(randStr())
crcSlot := ClusterSlot(k)

// ClusterSlot without handling curly braces
rawClusterSlot := func(s string) uint16 {
return CRC16([]byte(s)) % numSlots
}

clusterSlotf := func(s string, args ...interface{}) uint16 {
return ClusterSlot([]byte(fmt.Sprintf(s, args...)))
}

kk := randStr()
assert.Equal(t, crcSlot, ClusterSlot([]byte(fmt.Sprintf("{%s}%s", k, kk))))
assert.Equal(t, crcSlot, ClusterSlot([]byte(fmt.Sprintf("{%s}}%s", k, kk))))
assert.Equal(t, crcSlot, ClusterSlot([]byte(fmt.Sprintf("%s{%s}", kk, k))))
assert.Equal(t, crcSlot, ClusterSlot([]byte(fmt.Sprintf("%s{%s}}%s", kk, k, kk))))
assert.Equal(t, crcSlot, clusterSlotf("{%s}%s", k, kk))
assert.Equal(t, crcSlot, clusterSlotf("{%s}}%s", k, kk))
assert.Equal(t, crcSlot, clusterSlotf("%s{%s}", kk, k))
assert.Equal(t, crcSlot, clusterSlotf("%s{%s}}%s", kk, k, kk))
assert.Equal(t, rawClusterSlot(string(k)+"{"), clusterSlotf("%s{", k))
// if the braces are empty it should match the whole string
assert.Equal(t, rawClusterSlot("foo{}{bar}"), ClusterSlot([]byte(`foo{}{bar}`)))
}

0 comments on commit aa3d816

Please sign in to comment.