-
Notifications
You must be signed in to change notification settings - Fork 3
/
bloomring.go
72 lines (66 loc) · 1.34 KB
/
bloomring.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
package ss_bloomring
import (
"github.com/riobard/go-bloom"
"hash/fnv"
"sync"
)
// simply use Double FNV here as our Bloom Filter hash
func doubleFNV(b []byte) (uint64, uint64) {
hx := fnv.New64()
hx.Write(b)
x := hx.Sum64()
hy := fnv.New64a()
hy.Write(b)
y := hy.Sum64()
return x, y
}
type BloomRing struct {
slotCapacity int
slotPosition int
slotCount int
entryCounter int
slots []bloom.Filter
mutex sync.RWMutex
}
func NewBloomRing(slot, capacity int, falsePositiveRate float64) *BloomRing {
// Calculate entries for each slot
r := &BloomRing{
slotCapacity: capacity / slot,
slotCount: slot,
slots: make([]bloom.Filter, slot),
}
for i := 0; i < slot; i++ {
r.slots[i] = bloom.New(r.slotCapacity, falsePositiveRate, doubleFNV)
}
return r
}
func (r *BloomRing) Add(b []byte) {
if r == nil {
return
}
r.mutex.Lock()
defer r.mutex.Unlock()
slot := r.slots[r.slotPosition]
if r.entryCounter > r.slotCapacity {
// Move to next slot and reset
r.slotPosition = (r.slotPosition + 1) % r.slotCount
slot = r.slots[r.slotPosition]
slot.Reset()
r.entryCounter = 0
}
r.entryCounter++
slot.Add(b)
}
func (r *BloomRing) Test(b []byte) bool {
if r == nil {
return false
}
r.mutex.RLock()
defer r.mutex.RUnlock()
for _, s := range r.slots {
if s.Test(b) {
return true
}
}
return false
}