✌️ Fixed in SortSliceByValue issue when weight calculated twice (#4)
- fixes for SortSliceByValue - fixes for tests
This commit is contained in:
parent
9a6fcdb1f8
commit
d32f396204
2 changed files with 33 additions and 34 deletions
25
hrw.go
25
hrw.go
|
@ -87,64 +87,63 @@ func SortSliceByValue(slice interface{}, hash uint64) {
|
||||||
var key = make([]byte, 16)
|
var key = make([]byte, 16)
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, Hash(key))
|
||||||
}
|
}
|
||||||
case []uint:
|
case []uint:
|
||||||
var key = make([]byte, 16)
|
var key = make([]byte, 16)
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, Hash(key))
|
||||||
}
|
}
|
||||||
case []int8:
|
case []int8:
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
key := byte(slice[i])
|
key := byte(slice[i])
|
||||||
rule = append(rule, weight(Hash([]byte{key}), hash))
|
rule = append(rule, Hash([]byte{key}))
|
||||||
}
|
}
|
||||||
case []uint8:
|
case []uint8:
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
key := slice[i]
|
key := slice[i]
|
||||||
rule = append(rule, weight(Hash([]byte{key}), hash))
|
rule = append(rule, Hash([]byte{key}))
|
||||||
}
|
}
|
||||||
case []int16:
|
case []int16:
|
||||||
var key = make([]byte, 8)
|
var key = make([]byte, 8)
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint16(key, uint16(slice[i]))
|
binary.BigEndian.PutUint16(key, uint16(slice[i]))
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, Hash(key))
|
||||||
}
|
}
|
||||||
case []uint16:
|
case []uint16:
|
||||||
var key = make([]byte, 8)
|
var key = make([]byte, 8)
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint16(key, slice[i])
|
binary.BigEndian.PutUint16(key, slice[i])
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, Hash(key))
|
||||||
}
|
}
|
||||||
case []int32:
|
case []int32:
|
||||||
var key = make([]byte, 16)
|
var key = make([]byte, 16)
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint32(key, uint32(slice[i]))
|
binary.BigEndian.PutUint32(key, uint32(slice[i]))
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, Hash(key))
|
||||||
}
|
}
|
||||||
case []uint32:
|
case []uint32:
|
||||||
var key = make([]byte, 16)
|
var key = make([]byte, 16)
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint32(key, slice[i])
|
binary.BigEndian.PutUint32(key, slice[i])
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, Hash(key))
|
||||||
}
|
}
|
||||||
case []int64:
|
case []int64:
|
||||||
var key = make([]byte, 32)
|
var key = make([]byte, 32)
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, Hash(key))
|
||||||
}
|
}
|
||||||
case []uint64:
|
case []uint64:
|
||||||
var key = make([]byte, 32)
|
var key = make([]byte, 32)
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint64(key, slice[i])
|
binary.BigEndian.PutUint64(key, slice[i])
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, Hash(key))
|
||||||
}
|
}
|
||||||
case []string:
|
case []string:
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
rule = append(rule, weight(hash,
|
rule = append(rule, Hash([]byte(slice[i])))
|
||||||
Hash([]byte(slice[i]))))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -154,7 +153,7 @@ func SortSliceByValue(slice interface{}, hash uint64) {
|
||||||
|
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
h := val.Index(i).Interface().(Hasher)
|
h := val.Index(i).Interface().(Hasher)
|
||||||
rule = append(rule, weight(hash, h.Hash()))
|
rule = append(rule, h.Hash())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
42
hrw_test.go
42
hrw_test.go
|
@ -50,12 +50,12 @@ func Example() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// trying GET four.example.com/examples/object-key
|
|
||||||
// trying GET three.example.com/examples/object-key
|
// trying GET three.example.com/examples/object-key
|
||||||
// trying GET one.example.com/examples/object-key
|
|
||||||
// trying GET two.example.com/examples/object-key
|
// trying GET two.example.com/examples/object-key
|
||||||
// trying GET six.example.com/examples/object-key
|
|
||||||
// trying GET five.example.com/examples/object-key
|
// trying GET five.example.com/examples/object-key
|
||||||
|
// trying GET six.example.com/examples/object-key
|
||||||
|
// trying GET one.example.com/examples/object-key
|
||||||
|
// trying GET four.example.com/examples/object-key
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h hashString) Hash() uint64 {
|
func (h hashString) Hash() uint64 {
|
||||||
|
@ -74,7 +74,7 @@ func TestSortSliceByIndex(t *testing.T) {
|
||||||
|
|
||||||
func TestSortSliceByValue(t *testing.T) {
|
func TestSortSliceByValue(t *testing.T) {
|
||||||
actual := []string{"a", "b", "c", "d", "e", "f"}
|
actual := []string{"a", "b", "c", "d", "e", "f"}
|
||||||
expect := []string{"d", "b", "a", "f", "c", "e"}
|
expect := []string{"d", "f", "c", "b", "a", "e"}
|
||||||
hash := Hash(testKey)
|
hash := Hash(testKey)
|
||||||
SortSliceByValue(actual, hash)
|
SortSliceByValue(actual, hash)
|
||||||
if !reflect.DeepEqual(actual, expect) {
|
if !reflect.DeepEqual(actual, expect) {
|
||||||
|
@ -144,7 +144,7 @@ func TestSortSliceByValueFail(t *testing.T) {
|
||||||
|
|
||||||
func TestSortSliceByValueHasher(t *testing.T) {
|
func TestSortSliceByValueHasher(t *testing.T) {
|
||||||
actual := []hashString{"a", "b", "c", "d", "e", "f"}
|
actual := []hashString{"a", "b", "c", "d", "e", "f"}
|
||||||
expect := []hashString{"d", "b", "a", "f", "c", "e"}
|
expect := []hashString{"d", "f", "c", "b", "a", "e"}
|
||||||
hash := Hash(testKey)
|
hash := Hash(testKey)
|
||||||
SortSliceByValue(actual, hash)
|
SortSliceByValue(actual, hash)
|
||||||
if !reflect.DeepEqual(actual, expect) {
|
if !reflect.DeepEqual(actual, expect) {
|
||||||
|
@ -156,42 +156,42 @@ func TestSortSliceByValueIntSlice(t *testing.T) {
|
||||||
cases := []slices{
|
cases := []slices{
|
||||||
{
|
{
|
||||||
actual: []int{0, 1, 2, 3, 4, 5},
|
actual: []int{0, 1, 2, 3, 4, 5},
|
||||||
expect: []int{2, 3, 1, 4, 0, 5},
|
expect: []int{2, 0, 5, 3, 1, 4},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
actual: []uint{0, 1, 2, 3, 4, 5},
|
actual: []uint{0, 1, 2, 3, 4, 5},
|
||||||
expect: []uint{2, 3, 1, 4, 0, 5},
|
expect: []uint{2, 0, 5, 3, 1, 4},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
actual: []int8{0, 1, 2, 3, 4, 5},
|
actual: []int8{0, 1, 2, 3, 4, 5},
|
||||||
expect: []int8{2, 0, 5, 1, 4, 3},
|
expect: []int8{5, 2, 1, 4, 0, 3},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
actual: []uint8{0, 1, 2, 3, 4, 5},
|
actual: []uint8{0, 1, 2, 3, 4, 5},
|
||||||
expect: []uint8{2, 0, 5, 1, 4, 3},
|
expect: []uint8{5, 2, 1, 4, 0, 3},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
actual: []int16{0, 1, 2, 3, 4, 5},
|
actual: []int16{0, 1, 2, 3, 4, 5},
|
||||||
expect: []int16{5, 4, 0, 3, 2, 1},
|
expect: []int16{1, 0, 3, 2, 4, 5},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
actual: []uint16{0, 1, 2, 3, 4, 5},
|
actual: []uint16{0, 1, 2, 3, 4, 5},
|
||||||
expect: []uint16{5, 4, 0, 3, 2, 1},
|
expect: []uint16{1, 0, 3, 2, 4, 5},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
actual: []int32{0, 1, 2, 3, 4, 5},
|
actual: []int32{0, 1, 2, 3, 4, 5},
|
||||||
expect: []int32{1, 3, 5, 4, 2, 0},
|
expect: []int32{5, 1, 2, 0, 3, 4},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
actual: []uint32{0, 1, 2, 3, 4, 5},
|
actual: []uint32{0, 1, 2, 3, 4, 5},
|
||||||
expect: []uint32{1, 3, 5, 4, 2, 0},
|
expect: []uint32{5, 1, 2, 0, 3, 4},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -201,12 +201,12 @@ func TestSortSliceByValueIntSlice(t *testing.T) {
|
||||||
|
|
||||||
{
|
{
|
||||||
actual: []int64{0, 1, 2, 3, 4, 5},
|
actual: []int64{0, 1, 2, 3, 4, 5},
|
||||||
expect: []int64{1, 5, 3, 4, 2, 0},
|
expect: []int64{5, 3, 0, 1, 4, 2},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
actual: []uint64{0, 1, 2, 3, 4, 5},
|
actual: []uint64{0, 1, 2, 3, 4, 5},
|
||||||
expect: []uint64{1, 5, 3, 4, 2, 0},
|
expect: []uint64{5, 3, 0, 1, 4, 2},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
hash := Hash(testKey)
|
hash := Hash(testKey)
|
||||||
|
@ -253,7 +253,7 @@ func TestUniformDistribution(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i = 0; i < keys; i++ {
|
for i = 0; i < keys; i++ {
|
||||||
binary.BigEndian.PutUint64(key, i)
|
binary.BigEndian.PutUint64(key, i+size)
|
||||||
hash := Hash(key)
|
hash := Hash(key)
|
||||||
counts[SortByWeight(nodes[:], hash)[0]]++
|
counts[SortByWeight(nodes[:], hash)[0]]++
|
||||||
}
|
}
|
||||||
|
@ -293,7 +293,7 @@ func TestUniformDistribution(t *testing.T) {
|
||||||
for i = 0; i < keys; i++ {
|
for i = 0; i < keys; i++ {
|
||||||
copy(b[:], a[:])
|
copy(b[:], a[:])
|
||||||
|
|
||||||
binary.BigEndian.PutUint64(key, i)
|
binary.BigEndian.PutUint64(key, i+size)
|
||||||
hash := Hash(key)
|
hash := Hash(key)
|
||||||
SortSliceByIndex(b[:], hash)
|
SortSliceByIndex(b[:], hash)
|
||||||
counts[b[0]]++
|
counts[b[0]]++
|
||||||
|
@ -333,7 +333,7 @@ func TestUniformDistribution(t *testing.T) {
|
||||||
|
|
||||||
for i = 0; i < keys; i++ {
|
for i = 0; i < keys; i++ {
|
||||||
copy(b[:], a[:])
|
copy(b[:], a[:])
|
||||||
binary.BigEndian.PutUint64(key, i)
|
binary.BigEndian.PutUint64(key, i+size)
|
||||||
hash := Hash(key)
|
hash := Hash(key)
|
||||||
SortSliceByValue(b[:], hash)
|
SortSliceByValue(b[:], hash)
|
||||||
counts[b[0]]++
|
counts[b[0]]++
|
||||||
|
@ -373,7 +373,7 @@ func TestUniformDistribution(t *testing.T) {
|
||||||
|
|
||||||
for i = 0; i < keys; i++ {
|
for i = 0; i < keys; i++ {
|
||||||
copy(b[:], a[:])
|
copy(b[:], a[:])
|
||||||
binary.BigEndian.PutUint64(key, i)
|
binary.BigEndian.PutUint64(key, i+size)
|
||||||
hash := Hash(key)
|
hash := Hash(key)
|
||||||
SortSliceByValue(b[:], hash)
|
SortSliceByValue(b[:], hash)
|
||||||
counts[b[0]]++
|
counts[b[0]]++
|
||||||
|
@ -413,7 +413,7 @@ func TestUniformDistribution(t *testing.T) {
|
||||||
|
|
||||||
for i = 0; i < keys; i++ {
|
for i = 0; i < keys; i++ {
|
||||||
copy(b[:], a[:])
|
copy(b[:], a[:])
|
||||||
binary.BigEndian.PutUint64(key, i)
|
binary.BigEndian.PutUint64(key, i+size)
|
||||||
hash := Hash(key)
|
hash := Hash(key)
|
||||||
SortSliceByValue(b[:], hash)
|
SortSliceByValue(b[:], hash)
|
||||||
counts[b[0]]++
|
counts[b[0]]++
|
||||||
|
@ -447,7 +447,7 @@ func TestUniformDistribution(t *testing.T) {
|
||||||
)
|
)
|
||||||
|
|
||||||
for i = 0; i < keys; i++ {
|
for i = 0; i < keys; i++ {
|
||||||
binary.BigEndian.PutUint64(key, i)
|
binary.BigEndian.PutUint64(key, i+size)
|
||||||
hash := Hash(key)
|
hash := Hash(key)
|
||||||
counts[hash]++
|
counts[hash]++
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue