public class FilterBuilder
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable
Constructor and Description |
---|
FilterBuilder()
Constructs a new builder for Bloom filters and counting Bloom filters.
|
FilterBuilder(int expectedElements,
double falsePositiveProbability)
Constructs a new Bloom Filter Builder by specifying the expected size of the filter and the tolerable false
positive probability.
|
FilterBuilder(int size,
int hashes)
Constructs a new Bloom Filter Builder using the specified size in bits and the specified number of hash
functions.
|
Modifier and Type | Method and Description |
---|---|
FilterBuilder |
addReadSlave(java.lang.String host,
int port)
Adds a read slave to speed up reading access (e.g. contains or getEstimatedCount) to normal and counting
Redis-backed Bloom filters.
|
<T> BloomFilter<T> |
buildBloomFilter()
Constructs a Bloom filter using the specified parameters and computing missing parameters if possible (e.g.
|
<T> CountingBloomFilter<T> |
buildCountingBloomFilter()
Constructs a Counting Bloom filter using the specified parameters and by computing missing parameters if possible (e.g.
|
FilterBuilder |
clone() |
FilterBuilder |
complete()
Checks if all necessary parameters were set and tries to infer optimal parameters (e.g. size and hashes from
given expectedElements and falsePositiveProbability).
|
int |
countingBits() |
FilterBuilder |
countingBits(int countingBits)
Sets the number of bits used for counting in case of a counting Bloom filter.
|
static java.nio.charset.Charset |
defaultCharset() |
int |
expectedElements() |
FilterBuilder |
expectedElements(int expectedElements)
Sets the number of expected elements.
|
double |
falsePositiveProbability() |
FilterBuilder |
falsePositiveProbability(double falsePositiveProbability)
Sets the tolerable false positive probability.
|
java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Integer>> |
getReadSlaves() |
int |
hashes() |
FilterBuilder |
hashes(int numberOfHashes)
Set the number of hash functions to be used.
|
HashProvider.HashFunction |
hashFunction() |
FilterBuilder |
hashFunction(HashProvider.HashFunction hf)
Uses a given custom hash function.
|
FilterBuilder |
hashFunction(HashProvider.HashMethod hashMethod)
Sets the method used to generate hash values.
|
HashProvider.HashMethod |
hashMethod() |
boolean |
isCompatibleTo(FilterBuilder other)
Checks wether a configuration is compatible to another configuration based on the size of the Bloom filter and
its hash functions.
|
java.lang.String |
name() |
FilterBuilder |
name(java.lang.String name)
Sets the name of the Bloom filter.
|
static int |
optimalK(long n,
long m)
Calculates the optimal hashes (number of hash function) given expectedElements (expected number of
elements in bloom filter) and size (size of bloom filter in bits).
|
static int |
optimalM(long n,
double p)
Calculates the optimal size size of the bloom filter in bits given expectedElements (expected
number of elements in bloom filter) and falsePositiveProbability (tolerable false positive rate).
|
static int |
optimalN(long k,
long m)
Calculates the amount of elements a Bloom filter for which the given configuration of size and hashes is
optimal.
|
static double |
optimalP(long k,
long m,
double insertedElements)
Calculates the best-case (uniform hash function) false positive probability.
|
boolean |
overwriteIfExists() |
FilterBuilder |
overwriteIfExists(boolean overwrite)
Sets whether any existing Bloom filter with same name should be overwritten in Redis.
|
boolean |
redisBacked() |
FilterBuilder |
redisBacked(boolean redisBacked)
Instructs the FilterBuilder to build a Redis-Backed Bloom filters.
|
int |
redisConnections() |
FilterBuilder |
redisConnections(int numConnections)
Sets the number of connections to use for Redis.
|
java.lang.String |
redisHost() |
FilterBuilder |
redisHost(java.lang.String host)
Sets the host of the backing Redis instance.
|
int |
redisPort() |
FilterBuilder |
redisPort(int port)
Sets the port of the backing Redis instance.
|
int |
size() |
FilterBuilder |
size(int size)
Sets the size of the filter in bits.
|
public FilterBuilder()
public FilterBuilder(int expectedElements, double falsePositiveProbability)
expectedElements
- expected elements in the filterfalsePositiveProbability
- tolerable false positive probabilitypublic FilterBuilder(int size, int hashes)
size
- bit size of the Bloom filterhashes
- number of hash functions to usepublic FilterBuilder expectedElements(int expectedElements)
expectedElements
- number of expected elements.public FilterBuilder size(int size)
size
- size of the filter in bitspublic FilterBuilder falsePositiveProbability(double falsePositiveProbability)
falsePositiveProbability
- the tolerable falsepublic FilterBuilder hashes(int numberOfHashes)
numberOfHashes
- number of hash functions used by the filter.public FilterBuilder countingBits(int countingBits)
Default: 16
countingBits
- Number of counting bits used by the counting Bloom filterpublic FilterBuilder name(java.lang.String name)
overwriteIfExists(boolean)
. Default: ""
name
- The name of the filterpublic FilterBuilder redisBacked(boolean redisBacked)
Default: false
redisBacked
- a boolean indicating whether redis should be usedpublic FilterBuilder redisHost(java.lang.String host)
Default: localhost
host
- the Redis hostpublic FilterBuilder redisPort(int port)
Default: 6379
port
- the Redis portpublic FilterBuilder redisConnections(int numConnections)
Default: 10
numConnections
- the number of connections to use for Redispublic FilterBuilder overwriteIfExists(boolean overwrite)
Default: false
overwrite
- boolean indicating whether to overwrite any existing filter with the same namepublic FilterBuilder addReadSlave(java.lang.String host, int port)
host
- host of the Redis read slaveport
- port of the Redis read slavepublic FilterBuilder hashFunction(HashProvider.HashMethod hashMethod)
HashProvider.HashMethod
. Default: MD5
For the generation of hash values the String representation of objects is used.
hashMethod
- the method used to generate hash valuespublic FilterBuilder hashFunction(HashProvider.HashFunction hf)
hf
- the custom hash functionpublic <T> BloomFilter<T> buildBloomFilter()
T
- the type of element contained in the Bloom filter.public <T> CountingBloomFilter<T> buildCountingBloomFilter()
T
- the type of element contained in the Counting Bloom filter.public FilterBuilder complete()
public FilterBuilder clone()
clone
in class java.lang.Object
public boolean redisBacked()
true
if the Bloom Filter will be Redis-backedpublic int expectedElements()
public int size()
public int hashes()
public int countingBits()
public double falsePositiveProbability()
public java.lang.String name()
public java.lang.String redisHost()
public int redisPort()
public int redisConnections()
public HashProvider.HashMethod hashMethod()
public HashProvider.HashFunction hashFunction()
public static java.nio.charset.Charset defaultCharset()
public boolean overwriteIfExists()
true
if the Bloom filter that is to be built should overwrite any existing Bloom filter with the same namepublic java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Integer>> getReadSlaves()
public boolean isCompatibleTo(FilterBuilder other)
other
- the other configurationtrue
if the configurations are compatiblepublic static int optimalM(long n, double p)
n
- Expected number of elements inserted in the bloom filterp
- Tolerable false positive ratepublic static int optimalK(long n, long m)
n
- Expected number of elements inserted in the bloom filterm
- The size of the bloom filter in bits.public static int optimalN(long k, long m)
k
- number of hashesm
- The size of the bloom filter in bits.public static double optimalP(long k, long m, double insertedElements)
k
- number of hashesm
- The size of the bloom filter in bits.insertedElements
- number of elements inserted in the filter