-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloom.go
61 lines (52 loc) · 1.37 KB
/
bloom.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
package db
import (
"math"
"github.com/spaolacci/murmur3"
)
// bloom is struct for bloom Filter
type bloom struct {
k uint32
size uint64
bits []byte
}
// newBloom creates a bloom filter depending on n, the number of elements that will be inserted into the bloom filter
func newBloom(n int) *bloom {
k := uint32(10) // Number of hash functions
p := 0.001 // False positive probability 0.001 = 1/1000 False Positive = 99.9% Correct
size := uint64(math.Ceil((float64(n) * math.Log(p)) / math.Log(1/math.Pow(2, math.Log(2)))))
return &bloom{
k: k,
size: size,
bits: make([]byte, size),
}
}
// recoverBloom creates a new in-memory bloom filter from a bits array
func recoverBloom(bits []byte) *bloom {
k := uint32(10)
return &bloom{
k: k,
size: uint64(len(bits)),
bits: bits,
}
}
// Insert inserts a key into the bloom filter
func (bloom *bloom) Insert(key string) {
for i := uint32(0); i < bloom.k; i++ {
hasher := murmur3.New64WithSeed(i)
hasher.Write([]byte(key))
index := hasher.Sum64() % bloom.size
bloom.bits[index] = byte(1)
}
}
// Check checks if a key exists in the bloom filter
func (bloom *bloom) Check(key string) bool {
for i := uint32(0); i < bloom.k; i++ {
hasher := murmur3.New64WithSeed(i)
hasher.Write([]byte(key))
index := hasher.Sum64() % bloom.size
if bloom.bits[index] == byte(0) {
return false
}
}
return true
}