Skip to content

Commit e655d19

Browse files
committed
test Get panic when no store, update readme
1 parent 7952684 commit e655d19

File tree

3 files changed

+21
-44
lines changed

3 files changed

+21
-44
lines changed

bloom.go

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -83,49 +83,6 @@ func NewBloom(err_rate float64, capacity int, database Store) *BloomFilter {
8383
}
8484
}
8585

86-
func NewBloomWithK(err_rate float64, capacity int, database Store, k int) *BloomFilter {
87-
if err_rate <= 0 || err_rate >= 1 {
88-
panic("Error rate must be between 0 and 1")
89-
}
90-
if capacity < 0 {
91-
panic("Capacity must be greater than 0")
92-
}
93-
94-
// P
95-
err_rate /= 100.0
96-
97-
// number of hash functions (k)
98-
numHashFn := int(math.Ceil(math.Log2(1.0 / err_rate)))
99-
if k > 0 {
100-
numHashFn = int(k)
101-
}
102-
103-
//ln22 = ln2^2
104-
ln22 := math.Pow(math.Ln2, 2)
105-
106-
// M
107-
bit_width := int(math.Ceil((float64(capacity) * math.Abs(math.Log(err_rate))) /
108-
ln22))
109-
//m
110-
bits_per_slice := bit_width / numHashFn
111-
112-
seeds := make([]int64, numHashFn)
113-
114-
for i := 0; i < len(seeds); i++ {
115-
seeds[i] = int64((i + 1) << 16)
116-
}
117-
118-
return &BloomFilter{
119-
err_rate: err_rate,
120-
capacity: capacity,
121-
bit_width: bit_width,
122-
bit_array: make([]bool, bit_width),
123-
m: bits_per_slice,
124-
seeds: seeds,
125-
db: database,
126-
}
127-
}
128-
12986
func NewBloomFromFile(path string) {
13087

13188
}
@@ -159,7 +116,7 @@ func (bf *BloomFilter) Find(key []byte) bool {
159116
// Get Gets the key from the underlying persistent store
160117
func (bf *BloomFilter) Get(key []byte) []byte {
161118
if !bf.hasStore() {
162-
return nil
119+
log.Panicf("BloomFilter has no persistent store. Use Find() instead")
163120
}
164121

165122
if !bf.Find(key) {

bloom_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ func TestBloomFilter_Add(t *testing.T) {
8080
bf.Add([]byte("test"), []byte("bar"))
8181
t.Errorf("Expected function to panic when number of entries exceed the capacity")
8282
})
83+
84+
t.Run("get should panic when there is no persistent store", func(t *testing.T) {
85+
defer func() {
86+
if r := recover(); r == nil {
87+
fmt.Println("recovered from panic")
88+
return
89+
}
90+
}()
91+
92+
count := 100
93+
bf := NewBloom(0.1, count, nil)
94+
bf.Add([]byte("foo"), []byte("bar"))
95+
val := bf.Get([]byte("foo"))
96+
t.Errorf("Expected function to panic when there is no persistent store, got %s", val)
97+
})
8398
}
8499
func TestBloomFilter_AddToDB(t *testing.T) {
85100
store, cleanupFunc := DBSetupTest(t)

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ import (
7878
)
7979

8080
func main() {
81+
82+
bf := gobloomgo.NewScalableBloom(0.01, 100, nil)
83+
bf.Find([]byte("foo"))
84+
85+
// with a persistent store
8186
opts := badger.DefaultOptions("/tmp/bloom.db")
8287
db := gobloomgo.NewBadger(opts)
8388
bf := gobloomgo.NewScalableBloom(0.9, 100, db)

0 commit comments

Comments
 (0)