@VisibleForTesting static <T> BloomFilter<T> create( Funnel<? super T> funnel, long expectedInsertions, double fpp, Strategy strategy){ /** 省略参数检查*/ /* * TODO(user): Put a warning in the javadoc about tiny fpp values, since the resulting size * is proportional to -log(p), but there is not much of a point after all, e.g. * optimalM(1000, 0.0000000000000001) = 76680 which is less than 10kb. Who cares! */ //最优 number of bit set long numBits = optimalNumOfBits(expectedInsertions, fpp); //最优 hash函数个数 int numHashFunctions = optimalNumOfHashFunctions(expectedInsertions, numBits); try { // 这里用 用LockFreeBitArray,因为java.util.bitset 最大个数只能使用int最大值来体现,显然,int 已经不满足google了 returnnew BloomFilter<T>(new LockFreeBitArray(numBits), numHashFunctions, funnel, strategy); } catch (IllegalArgumentException e) { thrownew IllegalArgumentException("Could not create BloomFilter of " + numBits + " bits", e); } }
//# 创建 bloomFilter privateBloomFilter( LockFreeBitArray bits, int numHashFunctions, Funnel<? super T> funnel, Strategy strategy){ checkArgument(numHashFunctions > 0, "numHashFunctions (%s) must be > 0", numHashFunctions); checkArgument( numHashFunctions <= 255, "numHashFunctions (%s) must be <= 255", numHashFunctions); this.bits = checkNotNull(bits); this.numHashFunctions = numHashFunctions; this.funnel = checkNotNull(funnel); this.strategy = checkNotNull(strategy); }
@Override public <T> booleanmightContain( T object, Funnel<? super T> funnel, int numHashFunctions, LockFreeBitArray bits){ long bitSize = bits.bitSize(); byte[] bytes = Hashing.murmur3_128().hashObject(object, funnel).getBytesInternal(); long hash1 = lowerEight(bytes); long hash2 = upperEight(bytes);
long combinedHash = hash1; for (int i = 0; i < numHashFunctions; i++) { // Make the combined hash positive and indexable if (!bits.get((combinedHash & Long.MAX_VALUE) % bitSize)) { returnfalse; } combinedHash += hash2; } returntrue; }