-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.go
57 lines (51 loc) · 1.04 KB
/
filter.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
package godata
// BloomFilter
import (
mh "github.com/spaolacci/murmur3"
"sync"
)
type BloomFilter struct {
db *BitMap
k int
}
//todo
type CuckooFilter struct {
}
const (
MAX_DB = 8388609
)
// max is the max of bitmap number k is the number of HashShower fuction.
func NewBloomFilter(k int) *BloomFilter {
return &BloomFilter{
db: NewBitMap(MAX_DB),
k: k,
}
}
// add data
func (b *BloomFilter) Add(value []byte) {
mu := new(sync.Mutex)
mu.Lock()
defer mu.Unlock()
for i := 0;i < b.k;i++ {
valBit := HashShower(value,uint32(i))
b.db.Add(valBit)
}
}
// is exit.
func (b *BloomFilter) IsExit(value []byte) bool {
for i := 0;i < b.k;i++ {
valBit := HashShower(value,uint32(i))
if !b.db.IsExit(valBit) {
return false
}
}
return true
}
// this HashShower fuction is fast but not safety.
func HashShower(data []byte, seed uint32) uint64 {
HashShower64 := mh.New64WithSeed(seed)
HashShower64.Write(data)
// in order to use less mem,so it should a % n;
//a %b == a & b -1 (b must 2^n)
return HashShower64.Sum64() & (MAX_DB-2)
}