filterguavabitarraybloom-filterbitvector

Compare Guava Bloom Filters?


Is there anyway to compare if elements of one bloom filter are present in another Bloom filter. I know you can use .equals but this doesn't consider, for example if only some of the elements are present in the second Bloom filter which is what I'm looking to do.

Somehow compare the set bits?


Solution

  • Google Guava does not have built-in support for this nor does BloomFilter<T> expose its backing bits so you cannot access those without using reflection:

    public static <T> boolean mightContainAll(BloomFilter<T> bloomFilter, BloomFilter<T> that) {
        BitSet thisBitSet = getBitSet(bloomFilter);
        BitSet thatBitSet = getBitSet(that);
        BitSet intersectionBitSet = new BitSet(thisBitSet.size());
        intersectionBitSet.or(thisBitSet);
        intersectionBitSet.and(thatBitSet);
        return intersectionBitSet.equals(thatBitSet);
    }
    
    private static <T> BitSet getBitSet(BloomFilter<T> bloomFilter) {
        try {
            Field bitsField = BloomFilter.class.getDeclaredField("bits");
            bitsField.setAccessible(true);
            Object bitArray = bitsField.get(bloomFilter);
            Field dataField = bitArray.getClass().getDeclaredField("data");
            dataField.setAccessible(true);
            return BitSet.valueOf((long[]) dataField.get(bitArray));
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }