-
Notifications
You must be signed in to change notification settings - Fork 96
/
hash.go
73 lines (64 loc) · 1.69 KB
/
hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package hashring
import (
"fmt"
"hash"
)
// HashSum allows to use a builder pattern to create different HashFunc objects.
// See examples for details.
type HashSum struct {
functions []func([]byte) []byte
}
func (r *HashSum) Use(
hashKeyFunc func(bytes []byte) (HashKey, error),
) (HashFunc, error) {
// build final hash function
composed := func(bytes []byte) []byte {
for _, f := range r.functions {
bytes = f(bytes)
}
return bytes
}
// check function composition for errors
testResult := composed([]byte("test"))
_, err := hashKeyFunc(testResult)
if err != nil {
const msg = "can't use given hash.Hash with given hashKeyFunc"
return nil, fmt.Errorf("%s: %w", msg, err)
}
// build HashFunc
return func(key []byte) HashKey {
bytes := composed(key)
hashKey, err := hashKeyFunc(bytes)
if err != nil {
// panic because we already checked HashSum earlier
panic(fmt.Sprintf("hashKeyFunc failure: %v", err))
}
return hashKey
}, nil
}
// NewHash creates a new *HashSum object which can be used to create HashFunc.
// HashFunc object is thread safe if the hasher argument produces a new hash.Hash
// each time. The produced hash.Hash is allowed to be non thread-safe.
func NewHash(hasher func() hash.Hash) *HashSum {
return &HashSum{
functions: []func(key []byte) []byte{
func(key []byte) []byte {
hash := hasher()
hash.Write(key)
return hash.Sum(nil)
},
},
}
}
func (r *HashSum) FirstBytes(n int) *HashSum {
r.functions = append(r.functions, func(bytes []byte) []byte {
return bytes[:n]
})
return r
}
func (r *HashSum) LastBytes(n int) *HashSum {
r.functions = append(r.functions, func(bytes []byte) []byte {
return bytes[len(bytes)-n:]
})
return r
}