Documentation ¶
Overview ¶
Package inbloom implements a portable bloom filter that can export and import data to and from implementations of the same library in different languages.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BloomFilter ¶
type BloomFilter struct {
// contains filtered or unexported fields
}
BloomFilter is our implementation of a simple dynamically sized bloom filter.
This code was adapted to Go from the libbloom C library - https://github.com/jvirkki/libbloom
Example ¶
// create a blank filter - expecting 20 members and an error rate of 1/100 f, err := NewFilter(20, 0.01) if err != nil { panic(err) } // the size of the filter fmt.Println(f.Len()) // insert some values f.Add("foo") f.Add("bar") // test for existence of keys fmt.Println(f.Contains("foo")) fmt.Println(f.Contains("wat")) fmt.Println("marshaled data:", f.MarshalBase64())
Output: 24 true false marshaled data: oU4AZAAAABQAAAAAAEIAABEAGAQAAgAgAAAwEAAJAAA=
func NewFilter ¶
func NewFilter(entries int, errorRate float64) (*BloomFilter, error)
NewFilter creates an empty bloom filter, with the given expected number of entries, and desired error rate. The number of hash functions and size of the filter are calculated from these 2 parameters
func Unmarshal ¶
func Unmarshal(data []byte) (*BloomFilter, error)
Unmarshal reads a binary dump of an inbloom filter with its header, and returns the resulting filter. Since this is a dump containing size and precisin metadata, you do not need to specify them.
If the data is corrupt or the buffer is not complete, we return an error
func UnmarshalBase64 ¶
func UnmarshalBase64(b64 string) (*BloomFilter, error)
UnmarshalBase64 is a convenience function that unmarshals a filter that has been encoded into a url parameter
func (*BloomFilter) Contains ¶
func (f *BloomFilter) Contains(key string) bool
Contains returns true if a key exists in the filter
func (*BloomFilter) Len ¶
func (f *BloomFilter) Len() int
Len returns the number of BYTES in the filter
func (*BloomFilter) Marshal ¶
func (f *BloomFilter) Marshal() []byte
Marshal dumps the filter to a byte array, with a header containing the error rate, cardinality and a checksum. This data can be passed to another inbloom filter over the network, and thus the other end can open the data without the user having to pass the filter size explicitly. See Unmarshal for reading these dumpss
func (*BloomFilter) MarshalBase64 ¶
func (f *BloomFilter) MarshalBase64() string
MarshalBase64 is a convenience method that dumps the filter's data to a base64 encoded string, ready to be passed as an GET/POST parameter