-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
stable.go
39 lines (33 loc) · 831 Bytes
/
stable.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
package bloom
import (
"database/sql/driver"
"errors"
boom "github.com/tylertreat/BoomFilters"
)
type StableBloomFilter struct {
boom.StableBloomFilter
}
const defaultCapacity = 100_000_000
const defaultD = 2
const defaultFpRate = 0.001
func NewDefaultStableBloomFilter() *StableBloomFilter {
return &StableBloomFilter{*boom.NewStableBloomFilter(defaultCapacity, defaultD, defaultFpRate)}
}
func (s *StableBloomFilter) Scan(value interface{}) error {
bytes, ok := value.([]byte)
if !ok {
return errors.New("invalid type for StableBloomFilter")
}
bf := boom.NewStableBloomFilter(0, 0, 0)
if err := bf.GobDecode(bytes); err != nil {
return err
}
s.StableBloomFilter = *bf
return nil
}
func (s StableBloomFilter) Value() (driver.Value, error) {
if s.Cells() == 0 {
return nil, nil
}
return s.GobEncode()
}