-
Notifications
You must be signed in to change notification settings - Fork 0
/
bf.go
161 lines (144 loc) · 3.31 KB
/
bf.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package bloomfilter
import (
"fmt"
gh "github.com/dgryski/go-farm"
"io/ioutil"
"math"
"os"
"strconv"
"strings"
"sync/atomic"
)
const FILE_BF_BITEMAP = ".bitmap"
const FILE_BF_META = ".meta"
type BF struct {
bm *Bitmap
BitSize uint64
HashSize int
MaxSize uint64
Total uint64
}
func NewBloomFilter(max uint32, fpp float64) *BF {
return &BF{
bm: NewBitmap(bitSize(max, fpp)),
HashSize: hashNum(max, fpp),
BitSize: bitSize(max, fpp),
MaxSize: uint64(max),
Total: 0,
}
}
func hash(str string) uint64 {
return gh.Hash64([]byte(str))
}
func bitSize(max uint32, fpp float64) uint64 {
return uint64(-(float64(max)*math.Log(fpp))/(math.Log(2)*math.Log(2))) + 1
}
func hashNum(max uint32, fpp float64) int {
return int(float64(bitSize(max, fpp)/uint64(max)) * math.Log(2))
}
func (b *BF) Put(str string) bool {
r := true
h1 := hash(str)
h2 := hash(str + str)
for i := 0; i < b.HashSize; i++ {
c := h1 + uint64(i)*h2
r = b.bm.Set(c%b.bm.maxSize) && r
}
if !r {
atomic.AddUint64(&b.Total, 1)
}
return r
}
func (b *BF) Contains(str string) bool {
r := true
h1 := hash(str)
h2 := hash(str + str)
for i := 0; i < b.HashSize; i++ {
c := h1 + uint64(i)*h2
r = r && b.bm.Get(c%b.bm.maxSize)
if !r {
return r
}
}
return r
}
func LoadBF(path string, name string) (*BF, error) {
metaPath := path + "/" + name + FILE_BF_META
meta, err := ioutil.ReadFile(metaPath)
if err != nil {
return nil, err
}
bf, err := parseMeta(string(meta))
if err != nil {
return nil, err
}
bitPath := path + "/" + name + FILE_BF_BITEMAP
bitFile, err := os.OpenFile(bitPath, os.O_RDONLY|os.O_SYNC, 0666)
if err != nil {
return nil, err
}
defer bitFile.Close()
bitFileInfo, err := bitFile.Stat()
if err != nil {
return nil, err
}
bm, err := LoadBitMap(bitFile, bitFileInfo.Size())
if err != nil {
return nil, err
}
bf.bm = bm
return bf, nil
}
func parseMeta(meta string) (*BF, error) {
ss := strings.Split(meta, ":")
if len(ss) != 4 {
return nil, fmt.Errorf("meta loss field")
}
bf := &BF{}
var err error
bf.MaxSize, err = strconv.ParseUint(ss[0], 10, 0)
if err != nil {
return nil, fmt.Errorf("MaxSize %w", err)
}
bf.HashSize, err = strconv.Atoi(ss[1])
if err != nil {
return nil, fmt.Errorf("HashSize %w", err)
}
bf.BitSize, err = strconv.ParseUint(ss[2], 10, 0)
if err != nil {
return nil, fmt.Errorf("BitSize %w", err)
}
bf.Total, err = strconv.ParseUint(ss[3], 10, 0)
if err != nil {
return nil, fmt.Errorf("Total %w", err)
}
return bf, nil
}
func (b *BF) meta() string {
return fmt.Sprintf("%d:%d:%d:%d", b.MaxSize, b.HashSize, b.BitSize, b.Total)
}
func (b *BF) Dump(path string, name string) error {
bitPath := path + "/" + name + FILE_BF_BITEMAP
metaPath := path + "/" + name + FILE_BF_META
metaPathTemp := metaPath + ".temp"
bitPathTemp := bitPath + ".temp"
bitFile, err := os.OpenFile(bitPathTemp, os.O_RDWR|os.O_CREATE|os.O_SYNC|os.O_TRUNC, 0666)
if err != nil {
return err
}
defer bitFile.Close()
metaFile, err := os.OpenFile(metaPathTemp, os.O_RDWR|os.O_CREATE|os.O_SYNC|os.O_TRUNC, 0666)
if err != nil {
return err
}
defer metaFile.Close()
b.bm.Dump(bitFile)
metaFile.Write([]byte(b.meta()))
if err = os.Rename(metaPathTemp, metaPath); err != nil {
return err
}
if err = os.Rename(bitPathTemp, bitPath); err != nil {
return err
}
return nil
}