-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbloom_m.go
More file actions
194 lines (169 loc) · 4.58 KB
/
bloom_m.go
File metadata and controls
194 lines (169 loc) · 4.58 KB
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
// Copyright (c) 2020 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package bloom
import (
"bytes"
"encoding/binary"
"github.com/pkg/errors"
"github.com/iotexproject/go-pkgs/byteutil"
"github.com/iotexproject/go-pkgs/hash"
)
var (
ErrHashMismatch = errors.New("failed to verify hash")
ErrNumHash = errors.New("invalid number of hash functions, expect 0 < k < 256")
)
type bloomMbits struct {
buckets []uint64 // each bucket houses 64-bit
m, k, n uint64
round uint64 // each round generates 4 x 64-bit key
rem int // k = 4 * round + rem
}
func newBloomMbits(m, k uint64) (BloomFilter, error) {
if k == 0 || k >= 256 {
return nil, ErrNumHash
}
b := bloomMbits{
buckets: make([]uint64, (m+63)>>6),
m: m,
k: k,
round: k >> 2,
rem: int(k & 3),
}
return &b, nil
}
// Size of bloom filter in bits
func (b *bloomMbits) Size() uint64 {
return b.m
}
// NumHash is the number of hash functions used
func (b *bloomMbits) NumHash() uint64 {
return b.k
}
// NumElements is the number of elements in the bloom filter
func (b *bloomMbits) NumElements() uint64 {
return b.n
}
// Add key into bloom filter
func (b *bloomMbits) Add(key []byte) {
if key == nil {
return
}
var h hash.Hash256
for i := uint64(0); i < b.round; i++ {
h = hash.Hash256b(append(key, byteutil.Uint64ToBytesBigEndian(i)...))
k := h[:]
for i := 0; i < 4; i++ {
b.setBit(byteutil.BytesToUint64BigEndian(k))
k = k[8:]
}
}
if b.rem > 0 {
h = hash.Hash256b(append(key, byteutil.Uint64ToBytesBigEndian(b.round)...))
k := h[:]
for i := 0; i < b.rem; i++ {
b.setBit(byteutil.BytesToUint64BigEndian(k))
k = k[8:]
}
}
b.n++
}
// Exist checks if a key is in bloom filter
func (b *bloomMbits) Exist(key []byte) bool {
if key == nil {
return false
}
var h hash.Hash256
for i := uint64(0); i < b.round; i++ {
h = hash.Hash256b(append(key, byteutil.Uint64ToBytesBigEndian(i)...))
k := h[:]
for i := 0; i < 4; i++ {
if b.getBit(byteutil.BytesToUint64BigEndian(k)) == 0 {
return false
}
k = k[8:]
}
}
if b.rem > 0 {
h = hash.Hash256b(append(key, byteutil.Uint64ToBytesBigEndian(b.round)...))
k := h[:]
for i := 0; i < b.rem; i++ {
if b.getBit(byteutil.BytesToUint64BigEndian(k)) == 0 {
return false
}
k = k[8:]
}
}
return true
}
// Bytes returns the bytes of bloom filter (in Big Endian)
//
// m: uint64 x 1
// k: uint64 x 1
// n: uint64 x 1
// buckets: []uint64
// hash: [32]byte = Hash256b(above)
func (b *bloomMbits) Bytes() []byte {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, b.m); err != nil {
return nil
}
if err := binary.Write(buf, binary.BigEndian, b.k); err != nil {
return nil
}
if err := binary.Write(buf, binary.BigEndian, b.n); err != nil {
return nil
}
if err := binary.Write(buf, binary.BigEndian, b.buckets); err != nil {
return nil
}
// append checksum hash
h := hash.Hash256b(buf.Bytes())
if err := binary.Write(buf, binary.BigEndian, h); err != nil {
return nil
}
return buf.Bytes()
}
func (b *bloomMbits) setBit(pos uint64) {
pos %= b.m
b.buckets[pos>>6] |= 1 << (pos & 0x3f)
}
func (b *bloomMbits) getBit(pos uint64) byte {
pos %= b.m
return byte(b.buckets[pos>>6]>>(pos&0x3f)) & 1
}
// FromBytes loads data in the struct
func (b *bloomMbits) FromBytes(data []byte) error {
// last 32 bytes is hash of preceding data
dataLength := len(data) - 32
wantedHash := hash.BytesToHash256(data[dataLength:])
actualHash := hash.Hash256b(data[:dataLength])
if actualHash != wantedHash {
return errors.Wrapf(ErrHashMismatch, "wanted = %x, actual = %x", wantedHash, actualHash)
}
// read m, n, k
var m, k, n uint64
buf := bytes.NewBuffer(data[:dataLength])
if err := binary.Read(buf, binary.BigEndian, &m); err != nil {
return err
}
if err := binary.Read(buf, binary.BigEndian, &k); err != nil {
return err
}
if err := binary.Read(buf, binary.BigEndian, &n); err != nil {
return err
}
bucketsLen := (m + 63) >> 6
if bucketsLen > uint64(cap(b.buckets)) {
b.buckets = make([]uint64, bucketsLen)
} else {
b.buckets = b.buckets[:bucketsLen]
}
if err := binary.Read(buf, binary.BigEndian, b.buckets); err != nil {
return err
}
b.m, b.k, b.n, b.round, b.rem = m, k, n, k>>2, int(k&3)
return nil
}