Skip to content

Commit f669875

Browse files
committed
add xxhash partitioner
1 parent d6cbbde commit f669875

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

xxhashpartitioner.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package partmap
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/cespare/xxhash/v2"
7+
)
8+
9+
type XXHashPartitioner struct {
10+
partitionsNum uint
11+
mask uint64
12+
useMask bool
13+
}
14+
15+
func NewXXHashPartitioner(partitionsNum uint) (*XXHashPartitioner, error) {
16+
if partitionsNum == 0 {
17+
return nil, fmt.Errorf("partmap: %w", ErrInvalidPartitions)
18+
}
19+
20+
// power-of-two fast path
21+
useMask := (partitionsNum & (partitionsNum - 1)) == 0
22+
var mask uint64
23+
if useMask {
24+
mask = uint64(partitionsNum - 1)
25+
}
26+
27+
return &XXHashPartitioner{
28+
partitionsNum: partitionsNum,
29+
mask: mask,
30+
useMask: useMask,
31+
}, nil
32+
}
33+
34+
func (h *XXHashPartitioner) Find(key string) uint {
35+
sum := xxhash.Sum64String(key)
36+
if h.useMask {
37+
return uint(sum & h.mask)
38+
}
39+
40+
return uint(sum % uint64(h.partitionsNum))
41+
}

0 commit comments

Comments
 (0)