Bloom filters
README.md

bloom #

Bloom filters for OCaml.

If your set lives on disk or across the network, every membership test costs you a read or a round trip. Put a Bloom filter in front of it. It is a few kilobytes of bits, and it tells you when a key is missing, so you skip the lookup. When it says yes you still have to check, because a yes can be wrong.

You tell Bloom.v how many elements you expect (n) and how often a wrong yes is acceptable (p). It then allocates m = n ln(1/p) / (ln 2)² bits and sets k = ⌈(ln 2) m / n⌉ of them per element; for a given n those two numbers give the fewest wrong answers. A thousand elements at one per cent works out to m = 1,000 × 4.605 / 0.480 ≈ 9,586 bits, so about 1.2 KB, with k = 7.

Textbook filters hash every element k times with k different functions. I hash it once. The bit array is cut into k partitions of pairwise coprime lengths, each close to m / k, and hash h sets bit h mod lᵢ in partition i. By the Chinese remainder theorem, two hashes smaller than the product of the lengths land on the same k bits only if they are equal, so one hash is enough.

Installation #

Install with opam:

$ opam install bloom

If opam cannot find the package, it has probably not reached the public opam-repository yet. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install bloom

Usage #

Generic interface #

(* Create a Bloom filter expecting 1000 elements with 1% error rate *)
let bf = Bloom.v ~error_rate:0.01 1000

(* Add elements *)
let () = Bloom.add bf "hello"
let () = Bloom.add bf "world"

(* Query membership *)
let _ = Bloom.mem bf "hello"  (* true *)
let _ = Bloom.mem bf "other"  (* probably false *)

(* Estimate the number of elements *)
let _ = Bloom.size_estimate bf

You can ask a filter roughly how full it is. Bloom.size_estimate counts the X bits that are set and returns −(m / k) ln(1 − X / m), the number of insertions that would set X bits on average. It walks the whole array to do so, so don't call it in a tight loop on a big filter.

Functorial interface #

By default elements go through Hashtbl.hash. If you need a different hash, give it to Bloom.Make. Make sure it never returns a negative number: the bit index comes straight from it and nothing checks the bounds.

module My_bloom = Bloom.Make (struct
  type t = string
  let hash = Hashtbl.hash
end)

let my_bf = My_bloom.v ~error_rate:0.01 1000
let () = My_bloom.add my_bf "hello"
let _ = My_bloom.mem my_bf "hello"  (* true *)

Set operations #

You can merge two filters if you built them with the same parameters. If you didn't, you get Invalid_argument. After a Bloom.union you have exactly the filter you would have had by adding both sets to one filter. Bloom.inter is looser. It answers yes more often than a filter built from the intersection would, though never more often than one of its two inputs.

let bf1 = Bloom.v ~error_rate:0.01 1000
let bf2 = Bloom.v ~error_rate:0.01 1000
let () = Bloom.add bf1 "a"
let () = Bloom.add bf2 "b"
let combined = Bloom.union bf1 bf2
let _ = Bloom.mem combined "a"  (* true *)
let _ = Bloom.mem combined "b"  (* true *)

Serialisation #

To store a filter or send it somewhere, use Bloom.to_bytes. It writes the parameters as big-endian 64-bit words, then the bits. Bloom.of_bytes reads them back.

let bytes = Bloom.to_bytes bf
let bf' = Bloom.of_bytes bytes  (* ('a t, [`Msg of string]) result *)

Tests #

Run dune runtest. The tests that measure the false positive rate and size_estimate use random elements from a fixed seed, so you get the same numbers on every run.

Licence #

MIT. See LICENSE.md.