Skip to content

Instantly share code, notes, and snippets.

@shenwei356
Created July 4, 2018 16:41
Show Gist options
  • Save shenwei356/659cffae84f9cb2365ba70ac3866af49 to your computer and use it in GitHub Desktop.
Save shenwei356/659cffae84f9cb2365ba70ac3866af49 to your computer and use it in GitHub Desktop.
inverse-bloom-filter
package main
import (
"compress/gzip"
"fmt"
"os"
"strconv"
boom "github.com/tylertreat/BoomFilters"
)
var gzipped = false
func main() {
// Write a filter
f := boom.NewInverseBloomFilter(100000)
for i := 0; i < 10000; i++ {
f.Add([]byte(strconv.Itoa(i)))
}
w, err := os.Create("TestInverseBloomFilter_ReadFrom.dat")
var gz *gzip.Writer
if gzipped {
gz = gzip.NewWriter(w)
}
if gzipped {
_, err = f.WriteTo(gz)
} else {
_, err = f.WriteTo(w)
}
if err != nil {
fmt.Println(err)
return
}
if gzipped {
gz.Close()
}
w.Close()
// Read the filter into a new one
f2 := boom.NewInverseBloomFilter(100000)
r, err := os.Open("TestInverseBloomFilter_ReadFrom.dat")
if err != nil {
fmt.Println(err)
return
}
var gz2 *gzip.Reader
if gzipped {
gz2, err = gzip.NewReader(r)
if err != nil {
fmt.Println(err)
return
}
}
if gzipped {
_, err = f2.ReadFrom(gz2)
} else {
_, err = f2.ReadFrom(r)
}
if err != nil {
fmt.Println(err)
return
}
if gzipped {
gz2.Close()
}
r.Close()
if f.Capacity() != f2.Capacity() {
fmt.Println("Different capacities")
return
}
for i := 0; i < 1000000; i++ {
if f.Test([]byte(strconv.Itoa(i))) != f2.Test([]byte(strconv.Itoa(i))) {
fmt.Printf("Expected both filters to Test the same for %d\n", i)
return
}
}
os.Remove("TestInverseBloomFilter_ReadFrom.dat")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment