-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloom_filter.go
327 lines (281 loc) · 8.52 KB
/
bloom_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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package sdhash
import (
"bufio"
"bytes"
"encoding/base64"
"errors"
"fmt"
"github.com/pierrec/lz4"
"io"
"io/ioutil"
bits2 "math/bits"
"os"
"strconv"
"strings"
)
var bitMasks32 = []uint32{
0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF,
0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF,
0x01FFFF, 0x03FFFF, 0x07FFFF, 0x0FFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF,
0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF,
}
// BloomFilter represent a bloom filter and it is used to calculate similarity digests.
type BloomFilter interface {
// ElemCount returns the number of elements in the BloomFilter.
ElemCount() uint64
// MaxElem returns the maximum number of elements that can be present in the BloomFilter.
MaxElem() uint64
// BitsPerElem returns the number of bits for each elements of the BloomFilter.
BitsPerElem() float64
// WriteToFile serialize the current BloomFilter to a file specified by filename.
WriteToFile(filename string) error
// String returns the serialized representation of the BloomFilter.
String() string
insertSha1(sha1 []uint32) bool
querySha1(sha1 []uint32) bool
}
type bloomFilter struct {
buffer []uint8 // Beginning of the bloom filter
hamming uint32 // weight of this bf
bitMask uint64 // bit mask
maxElem uint64 // max number of elements
hashCount uint16 // number of hash functions used (k)
bfElemCount uint64 // actual number of elements inserted
compSize uint64 // size of compressed bf to be read
name string // name associated with bloom filter
}
// NewBloomFilter returns a new BloomFilter with the default initial values.
func NewBloomFilter() BloomFilter {
if bf, err := newBloomFilter(64*mB, 5, 0); err != nil {
panic(err)
} else {
return bf
}
}
// NewBloomFilterFromIndexFile read a BloomFilter serialized into a file.
func NewBloomFilterFromIndexFile(indexFileName string) (BloomFilter, error) {
buffer, err := ioutil.ReadFile(indexFileName)
if err != nil {
return nil, err
}
bf := &bloomFilter{}
var bfSize uint64
if bfSize, err = bf.deserialize(bytes.NewReader(buffer)); err != nil {
return nil, err
}
bfComp := buffer[uint64(len(buffer))-bf.compSize:]
bf.buffer = make([]uint8, bfSize)
if n, err := lz4.UncompressBlock(bfComp, bf.buffer); err != nil || uint64(n) != bfSize {
return nil, err
}
bf.computeHamming()
return bf, err
}
// NewBloomFilterFromString create a new BloomFilter from a serialized string.
func NewBloomFilterFromString(filter string) (BloomFilter, error) {
var err error
r := bufio.NewReader(strings.NewReader(filter))
bf := &bloomFilter{}
var bfSize uint64
if bfSize, err = bf.deserialize(r); err != nil {
return nil, err
}
var rawBf string
if rawBf, err = r.ReadString('\n'); err != nil {
return nil, errors.New("failed to read raw bf")
}
var decodedBf []byte
if decodedBf, err = base64.StdEncoding.DecodeString(rawBf); err != nil {
return nil, errors.New("failed to decode raw bf")
}
bf.buffer = make([]uint8, bfSize)
if n, err := lz4.UncompressBlock(decodedBf, bf.buffer); err != nil || uint64(n) != bfSize {
return nil, err
}
bf.computeHamming()
return bf, err
}
func newBloomFilter(size uint64, hashCount uint16, maxElem uint64) (*bloomFilter, error) {
bf := &bloomFilter{
hashCount: hashCount,
maxElem: maxElem,
}
// Make sure size is a power of 2 and at least 64
if size >= 64 && (size&(size-1)) == 0 {
var logSize uint16
for tmp := size; tmp > 0; tmp, logSize = tmp>>1, logSize+1 {
}
bf.bitMask = uint64(bitMasks32[logSize+1])
} else {
return nil, errors.New("invalid size")
}
bf.buffer = make([]uint8, size)
return bf, nil
}
func newBloomFilterFromExistingData(data []uint8, bfElemCount int) *bloomFilter {
var logSize uint16
for tmp := len(data); tmp > 0; tmp, logSize = tmp>>1, logSize+1 {
}
bf := &bloomFilter{
bitMask: uint64(bitMasks32[logSize+1]),
hashCount: 5,
bfElemCount: uint64(bfElemCount),
buffer: make([]uint8, len(data)),
}
copy(bf.buffer, data)
bf.computeHamming()
return bf
}
func (bf *bloomFilter) ElemCount() uint64 {
return bf.bfElemCount
}
func (bf *bloomFilter) MaxElem() uint64 {
return bf.maxElem
}
func (bf *bloomFilter) BitsPerElem() float64 {
return float64(len(bf.buffer)<<3) / float64(bf.bfElemCount)
}
func (bf *bloomFilter) WriteToFile(filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()
var header string
var buf []byte
if header, buf, err = bf.serialize(); err != nil {
return err
}
if _, err = f.WriteString(header); err != nil {
return err
}
if n, err := f.Write(buf); err != nil {
return err
} else if uint64(n) != bf.compSize {
return errors.New("failed to compress bloom filter")
}
return nil
}
func (bf *bloomFilter) String() string {
var header string
var buf []byte
var err error
if header, buf, err = bf.serialize(); err != nil {
return err.Error()
}
return header + base64.StdEncoding.EncodeToString(buf) + "\n"
}
func (bf *bloomFilter) fold(times uint32) {
bfSize := len(bf.buffer)
for i := uint32(0); i < times; i++ {
for j := 0; j < bfSize/2; j++ {
bf.buffer[j] |= bf.buffer[j+(bfSize/2)]
}
bfSize >>= 2
if bfSize == 32 {
break
}
}
var logSize uint16
for tmp := bfSize; tmp > 0; tmp, logSize = tmp>>1, logSize+1 {
}
bf.bitMask = uint64(bitMasks32[logSize+1])
bf.buffer = bf.buffer[:bfSize]
}
func (bf *bloomFilter) computeHamming() {
bf.hamming = 0
for j := 0; j < len(bf.buffer); j++ {
bf.hamming += uint32(bits2.OnesCount8(bf.buffer[j]))
}
}
func (bf *bloomFilter) insertSha1(sha1 []uint32) bool {
return bf.queryAndSet(sha1, true)
}
func (bf *bloomFilter) querySha1(sha1 []uint32) bool {
return bf.queryAndSet(sha1, false)
}
func (bf *bloomFilter) queryAndSet(sha1 []uint32, modeSet bool) bool {
var pos, k uint32
var bitCount uint16
for i := uint16(0); i < bf.hashCount; i++ {
pos = sha1[i] & uint32(bf.bitMask)
k = pos >> 3
if (bf.buffer[k] & bits[pos&0x7]) != 0 { // Bit is set
bitCount++
} else {
if modeSet {
bf.buffer[k] |= bits[pos&0x7]
} else {
return false
}
}
}
if modeSet {
if bitCount < bf.hashCount {
bf.bfElemCount++
return true
}
return false
}
return bitCount == bf.hashCount
}
func (bf *bloomFilter) serialize() (header string, buf []byte, err error) {
buf = make([]uint8, 160*mB)
var n int
if n, err = lz4.CompressBlock(bf.buffer, buf, nil); err != nil {
return "", nil, err
}
bf.compSize = uint64(n)
header = fmt.Sprintf("sdbf-idx:%v:%v:%v:%v:%v:%s\n", len(bf.buffer), bf.bfElemCount, bf.hashCount,
bf.bitMask, bf.compSize, bf.name)
return header, buf[:bf.compSize], err
}
func (bf *bloomFilter) deserialize(rd io.Reader) (bfSize uint64, err error) {
r := bufio.NewReader(rd)
var bfSizeStr, bfElemCountStr, hashCountStr, bitMaskStr, compSizeStr string
var bfElemCount, hashCount, bitMask, compSize uint64
if _, err := r.ReadBytes(':'); err != nil { // discard headerbit
return 0, errors.New("failed to read headerbit")
}
if bfSizeStr, err = r.ReadString(':'); err != nil {
return 0, errors.New("failed to read bfSize")
}
if bfSize, err = strconv.ParseUint(bfSizeStr[:len(bfSizeStr)-1], 10, 64); err != nil {
return 0, errors.New("failed to parse bfSize")
}
if bfElemCountStr, err = r.ReadString(':'); err != nil {
return 0, errors.New("failed to read bfElemCount")
}
if bfElemCount, err = strconv.ParseUint(bfElemCountStr[:len(bfElemCountStr)-1], 10, 64); err != nil {
return 0, errors.New("failed to parse bfElemCount")
}
if hashCountStr, err = r.ReadString(':'); err != nil {
return 0, errors.New("failed to read hashCount")
}
if hashCount, err = strconv.ParseUint(hashCountStr[:len(hashCountStr)-1], 10, 16); err != nil {
return 0, errors.New("failed to parse hashCount")
}
if bitMaskStr, err = r.ReadString(':'); err != nil {
return 0, errors.New("failed to read bitMask")
}
if bitMask, err = strconv.ParseUint(bitMaskStr[:len(bitMaskStr)-1], 10, 64); err != nil {
return 0, errors.New("failed to parse bitMask")
}
if compSizeStr, err = r.ReadString(':'); err != nil {
return 0, errors.New("failed to read compSize")
}
if compSize, err = strconv.ParseUint(compSizeStr[:len(compSizeStr)-1], 10, 64); err != nil {
return 0, errors.New("failed to parse compSize")
}
if bf.name, err = r.ReadString('\n'); err != nil {
return 0, errors.New("failed to read name")
}
bf.bfElemCount = bfElemCount
bf.hashCount = uint16(hashCount)
bf.bitMask = bitMask
bf.compSize = compSize
bf.name = bf.name[:len(bf.name)-1] // remove ending newline
return bfSize, nil
}