-
Notifications
You must be signed in to change notification settings - Fork 15
/
bloomfilter.go
52 lines (42 loc) · 1.7 KB
/
bloomfilter.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
// Package bloomfilter contains common data and interfaces needed to implement bloomfilters.
//
// It is based on the theory explained in: http://llimllib.github.io/bloomfilter-tutorial/
// In the repo, there are created the following types of bloomfilter: derived from bitset, sliding bloomfilters
// and rpc bloomfilter implementation.
package bloomfilter
import "math"
// Bloomfilter interface implemented in the different packages
type Bloomfilter interface {
Add([]byte)
Check([]byte) bool
Union(interface{}) (float64, error)
}
// Config for bloomfilter defining the parameters:
// P - desired false positive probability, N - number of elements to be stored in the filter and
// HashName - the name of the particular hashfunction
type Config struct {
N uint `json:"n"`
P float64 `json:"p"`
HashName string `json:"hash_name"`
}
// EmptyConfig configuration used for first empty `previous` bloomfilter in the sliding three bloomfilters
var EmptyConfig = Config{
N: 2,
P: .5,
}
// M function computes the length of the bit array of the bloomfilter as function of n and p
func M(n uint, p float64) uint {
return uint(math.Ceil(-(float64(n) * math.Log(p)) / math.Log(math.Pow(2.0, math.Log(2.0)))))
}
// K function computes the number of hashfunctions of the bloomfilter as function of n and p
func K(m, n uint) uint {
return uint(math.Ceil(math.Log(2.0) * float64(m) / float64(n)))
}
// EmptySet type is a synonym of int
type EmptySet int
// Check implementation for EmptySet
func (EmptySet) Check(_ []byte) bool { return false }
// Add implementation for EmptySet
func (EmptySet) Add(_ []byte) {}
// Union implementation for EmptySet
func (EmptySet) Union(interface{}) (float64, error) { return -1, nil }