Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classStores the (opaque) bloom filter offsets for reuse.static final classA pre-zeroed bit array waiting to be installed, so the memset can happen away from the lock that guards the filter.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ThreadLocal<ArrayDeque<int[]>> Per-thread pool of offset scratch arrays, so that hashing a key does not allocate its scratch space or contend on a shared queue.private intItems added to the filter.private int[]The words holding the filter bits.private final intBits in the filter array.private final intWords holding the filter bits.private final intKeys set per added item.private final KeySelectorSelector of the hash keys for an item.private final intBits in the filter.private static final intMaximum scratch arrays retained per thread.Constructor Summary
ConstructorsConstructorDescriptionCreates a filter of 2^20 bits with k defaulting to 8.BloomSHA1(int m) Creates a filter of 2^m bits, with the number of 'hash functions" k defaulting to 8.BloomSHA1(int m, int k) Creates a filter with 2^m bits and k 'hash functions', where each hash function is portion of the 160-bit SHA1 hash.Method Summary
Modifier and TypeMethodDescriptionprivate int[]acquire()Acquires offset arrays from the buffer pool.voidapplyReset(BloomSHA1.Reset reset) Install a prepared reset.final intcapacity()Returns the number of bits in the filter.voidclear()Clears the filter, synchronized.final doubleCalculates the approximate false positive rate for the current number of set members.final doublefalsePositives(int n) Calculates the approximate false positive rate for a given number of set members.getFilterKey(byte[] b, int offset, int len) Get the bloom filter offsets for reuse.voidinsert(byte[] b) Add a key to the set represented by the filter.voidinsert(byte[] b, int offset, int len) Add a key to the set represented by the filter.private final booleanisMember(byte[] b) Checks if a key is in the filter.private final booleanisMember(byte[] b, int offset, int len) Checks if a key is in the filter.final voidlocked_insert(byte[] b) Add a key to the filter without synchronization.final voidlocked_insert(byte[] b, int offset, int len) Add a key to the filter without synchronization.voidAdd the key to the filter.final booleanlocked_member(byte[] b) Checks if a key is in the filter without synchronization.final booleanlocked_member(byte[] b, int offset, int len) Checks if a key is in the filter without synchronization.booleanIs the key in the filter.final booleanmember(byte[] b) Is a key in the filter.final booleanmember(byte[] b, int offset, int len) Is a key in the filter.Allocate a zeroed bit array of this filter's size, away from whatever lock protects the filter.private voidrecycle(int[] arr) Returns a scratch array to this thread's pool, dropping it if the pool is already full so that a burst cannot retain an unbounded number.voidReleases filter key arrays back to the buffer pool.final intsize()Returns the number of keys which have been inserted.
Field Details
m
private final int mBits in the filter.k
private final int kKeys set per added item.count
private int countItems added to the filter.filter
private int[] filterThe words holding the filter bits.ks
Selector of the hash keys for an item.filterBits
private final int filterBitsBits in the filter array.filterWords
private final int filterWordsWords holding the filter bits.POOL_DEPTH
private static final int POOL_DEPTHMaximum scratch arrays retained per thread.- See Also:
bufPool
Per-thread pool of offset scratch arrays, so that hashing a key does not allocate its scratch space or contend on a shared queue. Thread confined, so no locking is needed: a FilterKey is always created and released by the same thread. Deques rather than single slots so nested use stays correct. The FilterKey itself is still allocated per getFilterKey() call, which is a small object holding the two arrays.
Constructor Details
BloomSHA1
public BloomSHA1(int m, int k) Creates a filter with 2^m bits and k 'hash functions', where each hash function is portion of the 160-bit SHA1 hash.- Parameters:
m- determines number of bits in filterk- number of hash functions See KeySelector for important restriction on max m and k
BloomSHA1
public BloomSHA1(int m) Creates a filter of 2^m bits, with the number of 'hash functions" k defaulting to 8.- Parameters:
m- determines size of filter
BloomSHA1
public BloomSHA1()Creates a filter of 2^20 bits with k defaulting to 8.
Method Details
prepareReset
Allocate a zeroed bit array of this filter's size, away from whatever lock protects the filter. Zeroing is the expensive part of a reset, so doing it here keeps a decay from stalling every concurrent insert.Pair with
applyReset(Reset), which is O(1).Tradeoff: this allocates a whole filter's worth of array per reset rather than reusing it, so it costs more total work than an in-place fill on a cache-resident filter, and leaves the collector a large short-lived array to account for. That is the right trade at the sizes where the stall would hurt: measured at m=29 an in-place fill under the lock runs about 2.6ms against about 1.4ms to allocate, and the ordering only reverses below m=28, where the whole operation is already under 0.6ms. Reusing the array on a background thread would avoid both costs, but needs a spare buffer held permanently, which is another 50% of the filter's memory.
- Returns:
- a reset token holding a fresh zeroed array
applyReset
Install a prepared reset. O(1), because the caller has already paid for the zeroed array. The caller must hold exclusive access to this filter, which for the decaying filters means holding their write lock.- Parameters:
reset- a token fromprepareReset()for this filter- Throws:
IllegalArgumentException- if the token is not for this filter
clear
public void clear()Clears the filter, synchronized.On a latency path prefer
prepareReset()plusapplyReset(Reset), which moves the memset off the lock.size
public final int size()Returns the number of keys which have been inserted. This class (BloomSHA1) does not guarantee uniqueness in any sense; if the same key is added N times, the number of set members reported will increase by N.- Returns:
- number of set members
capacity
public final int capacity()Returns the number of bits in the filter.- Returns:
- number of bits in filter
insert
public void insert(byte[] b) Add a key to the set represented by the filter. This version does not maintain 4-bit counters, it is not a counting Bloom filter.- Parameters:
b- byte array representing a key (SHA1 digest)
insert
public void insert(byte[] b, int offset, int len) Add a key to the set represented by the filter. This version does not maintain 4-bit counters, it is not a counting Bloom filter.- Parameters:
b- byte array representing a key (SHA1 digest)offset- starting offset in the byte arraylen- number of bytes to use
locked_insert
public final void locked_insert(byte[] b) Add a key to the filter without synchronization.- Parameters:
b- byte array representing a key (SHA1 digest)
locked_insert
public final void locked_insert(byte[] b, int offset, int len) Add a key to the filter without synchronization.- Parameters:
b- byte array representing a key (SHA1 digest)offset- starting offset in the byte arraylen- number of bytes to use
isMember
private final boolean isMember(byte[] b) Checks if a key is in the filter. Sets up the bit and word offset arrays.- Parameters:
b- byte array representing a key (SHA1 digest)- Returns:
- true if b is in the filter
isMember
private final boolean isMember(byte[] b, int offset, int len) Checks if a key is in the filter. Sets up the bit and word offset arrays.- Parameters:
b- byte array representing a key (SHA1 digest)offset- starting offset in the byte arraylen- number of bytes to use- Returns:
- true if b is in the filter
locked_member
public final boolean locked_member(byte[] b) Checks if a key is in the filter without synchronization.- Parameters:
b- byte array representing a key (SHA1 digest)- Returns:
- true if b is in the filter
locked_member
public final boolean locked_member(byte[] b, int offset, int len) Checks if a key is in the filter without synchronization.- Parameters:
b- byte array representing a key (SHA1 digest)offset- starting offset in the byte arraylen- number of bytes to use- Returns:
- true if b is in the filter
member
public final boolean member(byte[] b) Is a key in the filter. External interface, internally synchronized.- Parameters:
b- byte array representing a key (SHA1 digest)- Returns:
- true if b is in the filter
member
public final boolean member(byte[] b, int offset, int len) Is a key in the filter. External interface, internally synchronized.- Parameters:
b- byte array representing a key (SHA1 digest)offset- starting offset in the byte arraylen- number of bytes to use- Returns:
- true if b is in the filter
getFilterKey
Get the bloom filter offsets for reuse. Caller should call release(rv) when done with it.- Parameters:
b- byte array representing a key (SHA1 digest)offset- starting offset in the byte arraylen- number of bytes to use- Returns:
- filter key containing the offsets
locked_insert
Add the key to the filter.- Parameters:
fk- filter key containing offsets
locked_member
Is the key in the filter.- Parameters:
fk- filter key containing offsets- Returns:
- true if the key is in the filter
acquire
private int[] acquire()Acquires offset arrays from the buffer pool.- Returns:
- int array of size k containing bit offsets
recycle
private void recycle(int[] arr) Returns a scratch array to this thread's pool, dropping it if the pool is already full so that a burst cannot retain an unbounded number.- Parameters:
arr- the array to recycle
release
Releases filter key arrays back to the buffer pool.- Parameters:
fk- filter key to release
falsePositives
public final double falsePositives(int n) Calculates the approximate false positive rate for a given number of set members.- Parameters:
n- number of set members- Returns:
- approximate false positive rate
falsePositives
public final double falsePositives()Calculates the approximate false positive rate for the current number of set members.- Returns:
- approximate false positive rate