KeySelector provides the core functionality needed to map cryptographic keys to the specific bit and word positions used by Bloom filter hash functions. This is essential for implementing efficient Bloom filters where each hash function needs to examine specific bits or words from the key.
The class supports two types of selectors:
- BitSelector: Extracts individual bit positions (5-bit stride)
- WordSelector: Extracts word positions ((m-5)-bit stride)
Usage example: Create a KeySelector with m and k parameters, then call getOffsets() to extract bit and word offsets from a key.
Example: new KeySelector(1024, 5) creates a selector for m=1024, k=5.
Constraints and limitations:
- Maximum supported for 32-byte keys: m=23, k=11
- Formula constraint: ((5k + (k-1)(m-5)) / 8) + 2 ≤ keySizeInBytes
- All methods are thread-safe for concurrent access
Algorithm details:
- Bit selectors use 5-bit stride for optimal distribution
- Word selectors use (m-5)-bit stride to avoid bit selector overlap
- Both use lookup tables (UNMASK/MASK) for efficient bit operations
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface for extracting bit offsets from cryptographic keys for Bloom filter operations.classGeneric implementation of BitSelector for extracting bit offsets from cryptographic keys.classGeneric implementation of WordSelector for extracting word offsets from cryptographic keys.static interfaceInterface for extracting word offsets from cryptographic keys for Bloom filter operations.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final KeySelector.BitSelectorWhich individual bits are selected.private final intBits selected from the block.private final intBits in the full key block.private static final int[]AND with byte to zero out index-many bitsprivate static final int2 to the 15, the largest table size allowed.private static final int[]AND with byte to expose index-many bitsprivate final KeySelector.WordSelectorWhich words are selected.Constructor Summary
ConstructorsConstructorDescriptionKeySelector(int m, int k) Creates a key selector for a Bloom filter with specified parameters.Method Summary
All MethodsModifier and TypeMethodDescriptionvoidgetOffsets(byte[] key, int[] bitOffset, int[] wordOffset) Given a key, populate the word and bit offset arrays, each of which has k elements.voidgetOffsets(byte[] key, int off, int len, int[] bitOffset, int[] wordOffset) Given a key, populate the word and bit offset arrays, each of which has k elements.
Field Details
m
private final int mBits in the full key block.k
private final int kBits selected from the block.bitSel
Which individual bits are selected.wordSel
Which words are selected.UNMASK
private static final int[] UNMASKAND with byte to expose index-many bitsMASK
private static final int[] MASKAND with byte to zero out index-many bitsTWO_UP_15
private static final int TWO_UP_152 to the 15, the largest table size allowed.- See Also:
Constructor Details
KeySelector
public KeySelector(int m, int k) Creates a key selector for a Bloom filter with specified parameters.This constructor initializes both bit and word selectors with the appropriate algorithms for the given m and k values. The selectors will then be used to extract offsets from keys for Bloom filter operations.
Performance characteristics:
- Bit selection: O(k) time complexity, constant space
- Word selection: O(k) time complexity, constant space
- Both implementations use lookup tables for efficiency
- Thread-safe: no shared mutable stateMemory usage: O(1) additional space for lookup tables and selector instances.
- Parameters:
m- size of filter as a power of 2 (determines stride)k- number of 'hash functions' (determines number of offsets to extract)- Throws:
IllegalArgumentException- if parameters exceed implementation limitsImplementation constraints:
- Maximum for 32-byte keys: m=23, k=11
- Formula constraint: ((5k + (k-1)(m-5)) / 8) + 2 ≤ keySizeInBytes
- Larger values may cause ArrayIndexOutOfBoundsException
- m must be ≥ 2 for valid stride calculationsNote: The constraint formula ensures that the calculated offsets don't exceed the key boundaries. For larger m/k values, consider using larger key sizes or alternative selection algorithms.
Method Details
getOffsets
public void getOffsets(byte[] key, int[] bitOffset, int[] wordOffset) Given a key, populate the word and bit offset arrays, each of which has k elements.- Parameters:
key- cryptographic key used in populating the arraysbitOffset- Out parameter of length kwordOffset- Out parameter of length k
getOffsets
public void getOffsets(byte[] key, int off, int len, int[] bitOffset, int[] wordOffset) Given a key, populate the word and bit offset arrays, each of which has k elements.- Parameters:
key- cryptographic key used in populating the arraysoff- starting position within the key arraylen- number of bytes to process from the keybitOffset- Out parameter of length kwordOffset- Out parameter of length k