Skip to content

Commit 7db3f7c

Browse files
committed
remove panic
1 parent be82e09 commit 7db3f7c

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

bloom.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,35 +161,35 @@ func NewBloom(opts *BloomOptions) *BloomFilter {
161161
}
162162

163163
// Add adds the key to the bloom filter
164-
func (bf *BloomFilter) Add(key []byte) {
164+
func (bf *BloomFilter) Add(key []byte) error {
165165
bf.lock.Lock()
166166
defer bf.lock.Unlock()
167167

168168
indices := bf.candidates(string(key))
169169

170170
if bf.count >= bf.capacity {
171-
log.Panicf("BloomFilter has reached full capacity %d", bf.capacity)
171+
return fmt.Errorf("BloomFilter has reached full capacity %d", bf.capacity)
172172
}
173173

174174
for i := 0; i < len(indices); i++ {
175175
idx, mask := bf.getBitIndexN(indices[i])
176176

177177
if int(idx) >= len(bf.mem) {
178-
panic("Error finding key: Index out of bounds")
178+
return fmt.Errorf("Error finding key: Index out of bounds")
179179
}
180180

181181
// set the bit at mask position of the byte at idx
182182
// e.g. if idx = 2 and mask = 01000000, set the bit at 2nd position of byte 2
183183
bf.mem[idx] |= mask
184184
}
185185
bf.count++
186-
186+
return nil
187187
}
188188

189189
// Put adds the key to the bloom filter, and also stores it in the persistent store
190190
func (bf *BloomFilter) Put(key, val []byte) error {
191191
if !bf.hasStore() {
192-
fmt.Errorf("BloomFilter does not have a store, use Add() to add keys")
192+
return fmt.Errorf("BloomFilter does not have a store, use Add() to add keys")
193193
}
194194

195195
bf.Add(key)
@@ -204,7 +204,7 @@ func (bf *BloomFilter) Contains(key []byte) bool {
204204
idx, mask := bf.getBitIndexN(indices[i])
205205

206206
if int(idx) >= len(bf.mem) {
207-
panic("Error finding key: Index out of bounds")
207+
return false
208208
}
209209
bit := bf.mem[idx]
210210

scalable_bloom.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func (sbf *ScalableBloomFilter) add(key []byte) {
9191
for i := 0; i < len(indices); i++ {
9292
idx, mask := bf.getBitIndexN(indices[i])
9393
if int(idx) >= bf.bit_width {
94+
// this should not happen
9495
panic("Error adding key: Index out of bounds")
9596
}
9697
bf.mem[bf.pageOffset+int(idx)] |= mask
@@ -124,7 +125,7 @@ func (sbf *ScalableBloomFilter) contains(bf *BloomFilter, key []byte) bool {
124125
idx, mask := bf.getBitIndexN(indices[i])
125126

126127
if int(idx) >= bf.bit_width {
127-
panic("Error finding key: Index out of bounds")
128+
return false
128129
}
129130
if bit := topFilter.mem[bf.pageOffset+int(idx)]; bit&mask == 0 {
130131
return false

0 commit comments

Comments
 (0)