Skip to content

Commit

Permalink
✌️ Fixed in SortSliceByValue issue when weight calculated twice (#4)
Browse files Browse the repository at this point in the history
- fixes for SortSliceByValue
- fixes for tests
  • Loading branch information
im-kulikov authored May 24, 2019
1 parent 9a6fcdb commit d32f396
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
25 changes: 12 additions & 13 deletions hrw.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,64 +87,63 @@ func SortSliceByValue(slice interface{}, hash uint64) {
var key = make([]byte, 16)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint64(key, uint64(slice[i]))
rule = append(rule, weight(Hash(key), hash))
rule = append(rule, Hash(key))
}
case []uint:
var key = make([]byte, 16)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint64(key, uint64(slice[i]))
rule = append(rule, weight(Hash(key), hash))
rule = append(rule, Hash(key))
}
case []int8:
for i := 0; i < length; i++ {
key := byte(slice[i])
rule = append(rule, weight(Hash([]byte{key}), hash))
rule = append(rule, Hash([]byte{key}))
}
case []uint8:
for i := 0; i < length; i++ {
key := slice[i]
rule = append(rule, weight(Hash([]byte{key}), hash))
rule = append(rule, Hash([]byte{key}))
}
case []int16:
var key = make([]byte, 8)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint16(key, uint16(slice[i]))
rule = append(rule, weight(Hash(key), hash))
rule = append(rule, Hash(key))
}
case []uint16:
var key = make([]byte, 8)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint16(key, slice[i])
rule = append(rule, weight(Hash(key), hash))
rule = append(rule, Hash(key))
}
case []int32:
var key = make([]byte, 16)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint32(key, uint32(slice[i]))
rule = append(rule, weight(Hash(key), hash))
rule = append(rule, Hash(key))
}
case []uint32:
var key = make([]byte, 16)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint32(key, slice[i])
rule = append(rule, weight(Hash(key), hash))
rule = append(rule, Hash(key))
}
case []int64:
var key = make([]byte, 32)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint64(key, uint64(slice[i]))
rule = append(rule, weight(Hash(key), hash))
rule = append(rule, Hash(key))
}
case []uint64:
var key = make([]byte, 32)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint64(key, slice[i])
rule = append(rule, weight(Hash(key), hash))
rule = append(rule, Hash(key))
}
case []string:
for i := 0; i < length; i++ {
rule = append(rule, weight(hash,
Hash([]byte(slice[i]))))
rule = append(rule, Hash([]byte(slice[i])))
}

default:
Expand All @@ -154,7 +153,7 @@ func SortSliceByValue(slice interface{}, hash uint64) {

for i := 0; i < length; i++ {
h := val.Index(i).Interface().(Hasher)
rule = append(rule, weight(hash, h.Hash()))
rule = append(rule, h.Hash())
}
}

Expand Down
42 changes: 21 additions & 21 deletions hrw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func Example() {
}

// Output:
// trying GET four.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 six.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 {
Expand All @@ -74,7 +74,7 @@ func TestSortSliceByIndex(t *testing.T) {

func TestSortSliceByValue(t *testing.T) {
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)
SortSliceByValue(actual, hash)
if !reflect.DeepEqual(actual, expect) {
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestSortSliceByValueFail(t *testing.T) {

func TestSortSliceByValueHasher(t *testing.T) {
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)
SortSliceByValue(actual, hash)
if !reflect.DeepEqual(actual, expect) {
Expand All @@ -156,42 +156,42 @@ func TestSortSliceByValueIntSlice(t *testing.T) {
cases := []slices{
{
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},
expect: []uint{2, 3, 1, 4, 0, 5},
expect: []uint{2, 0, 5, 3, 1, 4},
},

{
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},
expect: []uint8{2, 0, 5, 1, 4, 3},
expect: []uint8{5, 2, 1, 4, 0, 3},
},

{
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},
expect: []uint16{5, 4, 0, 3, 2, 1},
expect: []uint16{1, 0, 3, 2, 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},
expect: []uint32{1, 3, 5, 4, 2, 0},
expect: []uint32{5, 1, 2, 0, 3, 4},
},

{
Expand All @@ -201,12 +201,12 @@ func TestSortSliceByValueIntSlice(t *testing.T) {

{
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},
expect: []uint64{1, 5, 3, 4, 2, 0},
expect: []uint64{5, 3, 0, 1, 4, 2},
},
}
hash := Hash(testKey)
Expand Down Expand Up @@ -253,7 +253,7 @@ func TestUniformDistribution(t *testing.T) {
}

for i = 0; i < keys; i++ {
binary.BigEndian.PutUint64(key, i)
binary.BigEndian.PutUint64(key, i+size)
hash := Hash(key)
counts[SortByWeight(nodes[:], hash)[0]]++
}
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestUniformDistribution(t *testing.T) {
for i = 0; i < keys; i++ {
copy(b[:], a[:])

binary.BigEndian.PutUint64(key, i)
binary.BigEndian.PutUint64(key, i+size)
hash := Hash(key)
SortSliceByIndex(b[:], hash)
counts[b[0]]++
Expand Down Expand Up @@ -333,7 +333,7 @@ func TestUniformDistribution(t *testing.T) {

for i = 0; i < keys; i++ {
copy(b[:], a[:])
binary.BigEndian.PutUint64(key, i)
binary.BigEndian.PutUint64(key, i+size)
hash := Hash(key)
SortSliceByValue(b[:], hash)
counts[b[0]]++
Expand Down Expand Up @@ -373,7 +373,7 @@ func TestUniformDistribution(t *testing.T) {

for i = 0; i < keys; i++ {
copy(b[:], a[:])
binary.BigEndian.PutUint64(key, i)
binary.BigEndian.PutUint64(key, i+size)
hash := Hash(key)
SortSliceByValue(b[:], hash)
counts[b[0]]++
Expand Down Expand Up @@ -413,7 +413,7 @@ func TestUniformDistribution(t *testing.T) {

for i = 0; i < keys; i++ {
copy(b[:], a[:])
binary.BigEndian.PutUint64(key, i)
binary.BigEndian.PutUint64(key, i+size)
hash := Hash(key)
SortSliceByValue(b[:], hash)
counts[b[0]]++
Expand Down Expand Up @@ -447,7 +447,7 @@ func TestUniformDistribution(t *testing.T) {
)

for i = 0; i < keys; i++ {
binary.BigEndian.PutUint64(key, i)
binary.BigEndian.PutUint64(key, i+size)
hash := Hash(key)
counts[hash]++
}
Expand Down

0 comments on commit d32f396

Please sign in to comment.