Skip to content
This repository was archived by the owner on Feb 1, 2019. It is now read-only.

Commit 098fd24

Browse files
alexvaninim-kulikov
authored andcommitted
Support for int32 slices (#4)
1 parent e5d4b47 commit 098fd24

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

hrw.go

+6
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ func SortSliceByValue(slice interface{}, hash uint64) {
8989
binary.BigEndian.PutUint64(key, uint64(slice[i]))
9090
rule = append(rule, weight(Hash(key), hash))
9191
}
92+
case []int32:
93+
var key = make([]byte, 16)
94+
for i := 0; i < length; i++ {
95+
binary.BigEndian.PutUint32(key, uint32(slice[i]))
96+
rule = append(rule, weight(Hash(key), hash))
97+
}
9298
case []string:
9399
for i := 0; i < length; i++ {
94100
rule = append(rule, weight(hash,

hrw_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,47 @@ func TestUniformDistribution(t *testing.T) {
329329

330330
})
331331

332+
t.Run("sortByInt32Value", func(t *testing.T) {
333+
var (
334+
i uint64
335+
a, b [size]int32
336+
counts = make(map[int32]int, size)
337+
key = make([]byte, 16)
338+
)
339+
340+
for i = 0; i < size; i++ {
341+
a[i] = int32(i)
342+
}
343+
344+
for i = 0; i < keys; i++ {
345+
copy(b[:], a[:])
346+
binary.BigEndian.PutUint64(key, i)
347+
hash := Hash(key)
348+
SortSliceByValue(b[:], hash)
349+
counts[b[0]]++
350+
}
351+
352+
var chi2 float64
353+
mean := float64(keys) / float64(size)
354+
delta := mean * percent
355+
for node, count := range counts {
356+
d := mean - float64(count)
357+
chi2 += math.Pow(float64(count)-mean, 2) / mean
358+
if d > delta || (0-d) > delta {
359+
t.Errorf(
360+
"Node %d received %d keys, expected %.0f (+/- %.2f)",
361+
node, count, mean, delta,
362+
)
363+
}
364+
}
365+
if chi2 > chiTable[size-1] {
366+
t.Errorf(
367+
"Chi2 condition for .9 is not met (expected %.2f <= %.2f)",
368+
chi2, chiTable[size-1])
369+
}
370+
371+
})
372+
332373
t.Run("hash collision", func(t *testing.T) {
333374
var (
334375
i uint64

0 commit comments

Comments
 (0)