Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chg: [join] fix overflowing filters on join operation #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions bloom.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,13 @@ func (s *BloomFilter) Join(s2 *BloomFilter) error {
return fmt.Errorf("filters have different dimensions (M = %d vs. %d))",
s.M, s2.M)
}
if (s.N + s2.N) > s.n {
return fmt.Errorf("addition of member counts would overflow")
}
for i = 0; i < s.M; i++ {
s.v[i] |= s2.v[i]
}
if s.N+s2.N < s.N {
return fmt.Errorf("addition of member counts would overflow")
}
s.N += s2.N

return nil
}

Expand Down
22 changes: 16 additions & 6 deletions bloom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func GenerateDisjointExampleFilter(capacity uint64, p float64, samples uint64, o
return filter, testValues
}

//This tests the checking of values against a given filter
// This tests the checking of values against a given filter
func TestChecking(t *testing.T) {
capacity := uint64(100000)
p := float64(0.001)
Expand All @@ -255,7 +255,7 @@ func TestChecking(t *testing.T) {
}
}

//This tests the checking of values against a given filter after resetting it
// This tests the checking of values against a given filter after resetting it
func TestReset(t *testing.T) {
capacity := uint64(100000)
p := float64(0.001)
Expand All @@ -271,8 +271,8 @@ func TestReset(t *testing.T) {
}
}

//This tests the checking of values against a given filter
//see https://en.wikipedia.org/wiki/Bloom_filter#Probability_of_false_positives
// This tests the checking of values against a given filter
// see https://en.wikipedia.org/wiki/Bloom_filter#Probability_of_false_positives
func TestFalsePositives(t *testing.T) {
capacity := uint64(10000)
p := float64(0.001)
Expand Down Expand Up @@ -371,6 +371,7 @@ func TestAccessors(t *testing.T) {
func TestJoiningRegular(t *testing.T) {
a, aval := GenerateExampleFilter(100000, 0.0001, 10000)
b, bval := GenerateDisjointExampleFilter(100000, 0.0001, 20000, a)
c, _ := GenerateDisjointExampleFilter(100000, 0.0001, 85000, b)
for _, v := range bval {
if a.Check(v) {
t.Errorf("value not missing in joined filter: %s", string(v))
Expand Down Expand Up @@ -399,9 +400,18 @@ func TestJoiningRegular(t *testing.T) {
t.Errorf("value not found in joined filter: %s", string(v))
}
}
expected := "addition of member counts would overflow"
actual := b.Join(&c)
if actual == nil {
t.Errorf("Expected error %v not triggered", expected)
} else {
if actual.Error() != expected {
t.Errorf("Error actual = %v, and Expected = %v.", actual, expected)
}
}
}

//This benchmarks the checking of values against a given filter
// This benchmarks the checking of values against a given filter
func BenchmarkChecking(b *testing.B) {
capacity := uint64(1e9)
p := float64(0.001)
Expand All @@ -418,7 +428,7 @@ func BenchmarkChecking(b *testing.B) {
}
}

//This benchmarks the checking without using a fixed fingerprint variable (instead a temporary variable is created each time)
// This benchmarks the checking without using a fixed fingerprint variable (instead a temporary variable is created each time)
func BenchmarkSimpleChecking(b *testing.B) {
capacity := uint64(1e9)
p := float64(0.001)
Expand Down