Skip to content

Commit 7952684

Browse files
committed
extract newCapacity
1 parent 4985e69 commit 7952684

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

bloom.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func NewBloom(err_rate float64, capacity int, database Store) *BloomFilter {
4949
if err_rate <= 0 || err_rate >= 1 {
5050
panic("Error rate must be between 0 and 1")
5151
}
52-
if capacity < 0 {
52+
if capacity <= 0 {
5353
panic("Capacity must be greater than 0")
5454
}
5555

@@ -62,13 +62,12 @@ func NewBloom(err_rate float64, capacity int, database Store) *BloomFilter {
6262
ln22 := math.Pow(math.Ln2, 2)
6363

6464
// M
65-
bit_width := int(math.Ceil((float64(capacity) * math.Abs(math.Log(err_rate))) /
66-
ln22))
65+
bit_width := int((float64(capacity) * math.Abs(math.Log(err_rate)) / ln22))
66+
6767
//m
6868
bits_per_slice := bit_width / numHashFn
6969

7070
seeds := make([]int64, numHashFn)
71-
7271
for i := 0; i < len(seeds); i++ {
7372
seeds[i] = int64((i + 1) << 16)
7473
}

cmd/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/binary"
5+
"fmt"
56

67
"github.com/dsa0x/gobloomgo"
78
)
@@ -17,17 +18,18 @@ func main() {
1718
// opts := badger.DefaultOptions("/tmp/bloom.db")
1819
// db := gobloomgo.NewBadger(opts)
1920

21+
num := 18232
2022
// bf := gobloomgo.NewBloom(0.1, 50000, nil)
21-
bf := gobloomgo.NewScalableBloom(0.000001, 10, nil)
23+
bf := gobloomgo.NewScalableBloom(0.001, num, nil)
2224

23-
for i := 0; i < 100000; i++ {
25+
for i := 0; i < num; i++ {
2426
var by [4]byte
2527
binary.LittleEndian.PutUint32(by[:], uint32(i))
2628
bf.Add(by[:], []byte("bar"))
2729
}
2830
bf.Add([]byte("foo"), []byte("var"))
2931

30-
// fmt.Printf("Count: %d, Capacity: %d, ExpCap: %.f\n", bf.Count(), bf.Capacity(), bf.ExpCapacity())
32+
fmt.Printf("Count: %d, Capacity: %d,\n", bf.Count(), bf.Capacity())
3133
// fmt.Println(bf.Capacity(), bf.ExpCapacity(), bf.Count(), bf.Prob())
3234

3335
}

scalable_bloom.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,15 @@ func (sbf *ScalableBloomFilter) Top() *BloomFilter {
9797
// grow increases the capacity of the bloom filter by adding a new filter
9898
func (sbf *ScalableBloomFilter) grow() {
9999
err_rate := sbf.err_rate * math.Pow(sbf.ratio, float64(len(sbf.filters)))
100+
newCapacity := sbf.getNewCap()
101+
newFilter := NewBloom(err_rate, newCapacity, sbf.db)
102+
sbf.filters = append(sbf.filters, newFilter)
103+
}
104+
105+
func (sbf *ScalableBloomFilter) getNewCap() int {
100106
i := float64(len(sbf.filters)) - 1.0
101107
newCapacity := float64(sbf.m0) * float64(math.Pow(float64(sbf.growth_rate), i)) * math.Ln2
102-
newFilter := NewBloom(err_rate, int(newCapacity), sbf.db)
103-
sbf.filters = append(sbf.filters, newFilter)
108+
return int(newCapacity)
104109
}
105110

106111
// Size returns the total capacity of the scalable bloom filter

0 commit comments

Comments
 (0)