-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloomer.go
70 lines (62 loc) · 1.26 KB
/
bloomer.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
package bloomer
import (
"hash"
"math"
"crypto/sha1"
"encoding/binary"
"github.com/emef/bitfield"
)
type Bloomer struct {
field bitfield.BitField
size int
k int
sha hash.Hash
}
func New(size, k int) *Bloomer {
return &Bloomer{
bitfield.New(size),
size,
k,
sha1.New()}
}
func NewSuggested(n int, p float64) *Bloomer {
m := -(float64(n) * math.Log(p)) / math.Pow(math.Log(2), 2)
k := (m / float64(n)) * math.Log(2)
return New(int(m), int(math.Ceil(k)))
}
func (b Bloomer) Add(value []byte) {
keys := b.getHashKeys(value)
for _, key := range keys {
b.field.Set(key)
}
}
func (b Bloomer) Test(value []byte) bool {
keys := b.getHashKeys(value)
for _, key := range keys {
if !b.field.Test(key) {
return false
}
}
return true
}
func (b Bloomer) TestAndSet(value []byte) bool {
found := true
keys := b.getHashKeys(value)
for _, key := range keys {
if !b.field.Test(key) {
found = false
b.Set(key)
}
}
return found
}
func (b Bloomer) getHashKeys(value []byte) []uint32 {
keys := make([]uint32, b.k)
hashBytes := b.sha.Sum(value)
hash0:= binary.BigEndian.Uint64(hashBytes[:8])
hash1 := binary.BigEndian.Uint64(hashBytes[8:16])
for i := 0; i < b.k; i++ {
keys[i] = uint32((hash0 + uint64(i) * hash1) % uint64(b.size))
}
return keys
}