@@ -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
190190func (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
0 commit comments