001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.hash;
016
017import static com.google.common.base.Preconditions.checkArgument;
018import static com.google.common.base.Preconditions.checkNotNull;
019
020import com.google.common.annotations.Beta;
021import com.google.common.annotations.VisibleForTesting;
022import com.google.common.base.Objects;
023import com.google.common.base.Predicate;
024import com.google.common.hash.BloomFilterStrategies.LockFreeBitArray;
025import com.google.common.math.DoubleMath;
026import com.google.common.math.LongMath;
027import com.google.common.primitives.SignedBytes;
028import com.google.common.primitives.UnsignedBytes;
029import com.google.errorprone.annotations.CanIgnoreReturnValue;
030import java.io.DataInputStream;
031import java.io.DataOutputStream;
032import java.io.IOException;
033import java.io.InputStream;
034import java.io.OutputStream;
035import java.io.Serializable;
036import java.math.RoundingMode;
037import javax.annotation.CheckForNull;
038import org.checkerframework.checker.nullness.qual.Nullable;
039
040/**
041 * A Bloom filter for instances of {@code T}. A Bloom filter offers an approximate containment test
042 * with one-sided error: if it claims that an element is contained in it, this might be in error,
043 * but if it claims that an element is <i>not</i> contained in it, then this is definitely true.
044 *
045 * <p>If you are unfamiliar with Bloom filters, this nice <a
046 * href="http://llimllib.github.io/bloomfilter-tutorial/">tutorial</a> may help you understand how
047 * they work.
048 *
049 * <p>The false positive probability ({@code FPP}) of a Bloom filter is defined as the probability
050 * that {@linkplain #mightContain(Object)} will erroneously return {@code true} for an object that
051 * has not actually been put in the {@code BloomFilter}.
052 *
053 * <p>Bloom filters are serializable. They also support a more compact serial representation via the
054 * {@link #writeTo} and {@link #readFrom} methods. Both serialized forms will continue to be
055 * supported by future versions of this library. However, serial forms generated by newer versions
056 * of the code may not be readable by older versions of the code (e.g., a serialized Bloom filter
057 * generated today may <i>not</i> be readable by a binary that was compiled 6 months ago).
058 *
059 * <p>As of Guava 23.0, this class is thread-safe and lock-free. It internally uses atomics and
060 * compare-and-swap to ensure correctness when multiple threads are used to access it.
061 *
062 * @param <T> the type of instances that the {@code BloomFilter} accepts
063 * @author Dimitris Andreou
064 * @author Kevin Bourrillion
065 * @since 11.0 (thread-safe since 23.0)
066 */
067@Beta
068@ElementTypesAreNonnullByDefault
069public final class BloomFilter<T extends @Nullable Object> implements Predicate<T>, Serializable {
070  /**
071   * A strategy to translate T instances, to {@code numHashFunctions} bit indexes.
072   *
073   * <p>Implementations should be collections of pure functions (i.e. stateless).
074   */
075  interface Strategy extends java.io.Serializable {
076
077    /**
078     * Sets {@code numHashFunctions} bits of the given bit array, by hashing a user element.
079     *
080     * <p>Returns whether any bits changed as a result of this operation.
081     */
082    <T extends @Nullable Object> boolean put(
083        @ParametricNullness T object,
084        Funnel<? super T> funnel,
085        int numHashFunctions,
086        LockFreeBitArray bits);
087
088    /**
089     * Queries {@code numHashFunctions} bits of the given bit array, by hashing a user element;
090     * returns {@code true} if and only if all selected bits are set.
091     */
092    <T extends @Nullable Object> boolean mightContain(
093        @ParametricNullness T object,
094        Funnel<? super T> funnel,
095        int numHashFunctions,
096        LockFreeBitArray bits);
097
098    /**
099     * Identifier used to encode this strategy, when marshalled as part of a BloomFilter. Only
100     * values in the [-128, 127] range are valid for the compact serial form. Non-negative values
101     * are reserved for enums defined in BloomFilterStrategies; negative values are reserved for any
102     * custom, stateful strategy we may define (e.g. any kind of strategy that would depend on user
103     * input).
104     */
105    int ordinal();
106  }
107
108  /** The bit set of the BloomFilter (not necessarily power of 2!) */
109  private final LockFreeBitArray bits;
110
111  /** Number of hashes per element */
112  private final int numHashFunctions;
113
114  /** The funnel to translate Ts to bytes */
115  private final Funnel<? super T> funnel;
116
117  /** The strategy we employ to map an element T to {@code numHashFunctions} bit indexes. */
118  private final Strategy strategy;
119
120  /** Creates a BloomFilter. */
121  private BloomFilter(
122      LockFreeBitArray bits, int numHashFunctions, Funnel<? super T> funnel, Strategy strategy) {
123    checkArgument(numHashFunctions > 0, "numHashFunctions (%s) must be > 0", numHashFunctions);
124    checkArgument(
125        numHashFunctions <= 255, "numHashFunctions (%s) must be <= 255", numHashFunctions);
126    this.bits = checkNotNull(bits);
127    this.numHashFunctions = numHashFunctions;
128    this.funnel = checkNotNull(funnel);
129    this.strategy = checkNotNull(strategy);
130  }
131
132  /**
133   * Creates a new {@code BloomFilter} that's a copy of this instance. The new instance is equal to
134   * this instance but shares no mutable state.
135   *
136   * @since 12.0
137   */
138  public BloomFilter<T> copy() {
139    return new BloomFilter<T>(bits.copy(), numHashFunctions, funnel, strategy);
140  }
141
142  /**
143   * Returns {@code true} if the element <i>might</i> have been put in this Bloom filter, {@code
144   * false} if this is <i>definitely</i> not the case.
145   */
146  public boolean mightContain(@ParametricNullness T object) {
147    return strategy.mightContain(object, funnel, numHashFunctions, bits);
148  }
149
150  /**
151   * @deprecated Provided only to satisfy the {@link Predicate} interface; use {@link #mightContain}
152   *     instead.
153   */
154  @Deprecated
155  @Override
156  public boolean apply(@ParametricNullness T input) {
157    return mightContain(input);
158  }
159
160  /**
161   * Puts an element into this {@code BloomFilter}. Ensures that subsequent invocations of {@link
162   * #mightContain(Object)} with the same element will always return {@code true}.
163   *
164   * @return true if the Bloom filter's bits changed as a result of this operation. If the bits
165   *     changed, this is <i>definitely</i> the first time {@code object} has been added to the
166   *     filter. If the bits haven't changed, this <i>might</i> be the first time {@code object} has
167   *     been added to the filter. Note that {@code put(t)} always returns the <i>opposite</i>
168   *     result to what {@code mightContain(t)} would have returned at the time it is called.
169   * @since 12.0 (present in 11.0 with {@code void} return type})
170   */
171  @CanIgnoreReturnValue
172  public boolean put(@ParametricNullness T object) {
173    return strategy.put(object, funnel, numHashFunctions, bits);
174  }
175
176  /**
177   * Returns the probability that {@linkplain #mightContain(Object)} will erroneously return {@code
178   * true} for an object that has not actually been put in the {@code BloomFilter}.
179   *
180   * <p>Ideally, this number should be close to the {@code fpp} parameter passed in {@linkplain
181   * #create(Funnel, int, double)}, or smaller. If it is significantly higher, it is usually the
182   * case that too many elements (more than expected) have been put in the {@code BloomFilter},
183   * degenerating it.
184   *
185   * @since 14.0 (since 11.0 as expectedFalsePositiveProbability())
186   */
187  public double expectedFpp() {
188    return Math.pow((double) bits.bitCount() / bitSize(), numHashFunctions);
189  }
190
191  /**
192   * Returns an estimate for the total number of distinct elements that have been added to this
193   * Bloom filter. This approximation is reasonably accurate if it does not exceed the value of
194   * {@code expectedInsertions} that was used when constructing the filter.
195   *
196   * @since 22.0
197   */
198  public long approximateElementCount() {
199    long bitSize = bits.bitSize();
200    long bitCount = bits.bitCount();
201
202    /**
203     * Each insertion is expected to reduce the # of clear bits by a factor of
204     * `numHashFunctions/bitSize`. So, after n insertions, expected bitCount is `bitSize * (1 - (1 -
205     * numHashFunctions/bitSize)^n)`. Solving that for n, and approximating `ln x` as `x - 1` when x
206     * is close to 1 (why?), gives the following formula.
207     */
208    double fractionOfBitsSet = (double) bitCount / bitSize;
209    return DoubleMath.roundToLong(
210        -Math.log1p(-fractionOfBitsSet) * bitSize / numHashFunctions, RoundingMode.HALF_UP);
211  }
212
213  /** Returns the number of bits in the underlying bit array. */
214  @VisibleForTesting
215  long bitSize() {
216    return bits.bitSize();
217  }
218
219  /**
220   * Determines whether a given Bloom filter is compatible with this Bloom filter. For two Bloom
221   * filters to be compatible, they must:
222   *
223   * <ul>
224   *   <li>not be the same instance
225   *   <li>have the same number of hash functions
226   *   <li>have the same bit size
227   *   <li>have the same strategy
228   *   <li>have equal funnels
229   * </ul>
230   *
231   * @param that The Bloom filter to check for compatibility.
232   * @since 15.0
233   */
234  public boolean isCompatible(BloomFilter<T> that) {
235    checkNotNull(that);
236    return this != that
237        && this.numHashFunctions == that.numHashFunctions
238        && this.bitSize() == that.bitSize()
239        && this.strategy.equals(that.strategy)
240        && this.funnel.equals(that.funnel);
241  }
242
243  /**
244   * Combines this Bloom filter with another Bloom filter by performing a bitwise OR of the
245   * underlying data. The mutations happen to <b>this</b> instance. Callers must ensure the Bloom
246   * filters are appropriately sized to avoid saturating them.
247   *
248   * @param that The Bloom filter to combine this Bloom filter with. It is not mutated.
249   * @throws IllegalArgumentException if {@code isCompatible(that) == false}
250   * @since 15.0
251   */
252  public void putAll(BloomFilter<T> that) {
253    checkNotNull(that);
254    checkArgument(this != that, "Cannot combine a BloomFilter with itself.");
255    checkArgument(
256        this.numHashFunctions == that.numHashFunctions,
257        "BloomFilters must have the same number of hash functions (%s != %s)",
258        this.numHashFunctions,
259        that.numHashFunctions);
260    checkArgument(
261        this.bitSize() == that.bitSize(),
262        "BloomFilters must have the same size underlying bit arrays (%s != %s)",
263        this.bitSize(),
264        that.bitSize());
265    checkArgument(
266        this.strategy.equals(that.strategy),
267        "BloomFilters must have equal strategies (%s != %s)",
268        this.strategy,
269        that.strategy);
270    checkArgument(
271        this.funnel.equals(that.funnel),
272        "BloomFilters must have equal funnels (%s != %s)",
273        this.funnel,
274        that.funnel);
275    this.bits.putAll(that.bits);
276  }
277
278  @Override
279  public boolean equals(@CheckForNull Object object) {
280    if (object == this) {
281      return true;
282    }
283    if (object instanceof BloomFilter) {
284      BloomFilter<?> that = (BloomFilter<?>) object;
285      return this.numHashFunctions == that.numHashFunctions
286          && this.funnel.equals(that.funnel)
287          && this.bits.equals(that.bits)
288          && this.strategy.equals(that.strategy);
289    }
290    return false;
291  }
292
293  @Override
294  public int hashCode() {
295    return Objects.hashCode(numHashFunctions, funnel, strategy, bits);
296  }
297
298  /**
299   * Creates a {@link BloomFilter} with the expected number of insertions and expected false
300   * positive probability.
301   *
302   * <p>Note that overflowing a {@code BloomFilter} with significantly more elements than specified,
303   * will result in its saturation, and a sharp deterioration of its false positive probability.
304   *
305   * <p>The constructed {@code BloomFilter} will be serializable if the provided {@code Funnel<T>}
306   * is.
307   *
308   * <p>It is recommended that the funnel be implemented as a Java enum. This has the benefit of
309   * ensuring proper serialization and deserialization, which is important since {@link #equals}
310   * also relies on object identity of funnels.
311   *
312   * @param funnel the funnel of T's that the constructed {@code BloomFilter} will use
313   * @param expectedInsertions the number of expected insertions to the constructed {@code
314   *     BloomFilter}; must be positive
315   * @param fpp the desired false positive probability (must be positive and less than 1.0)
316   * @return a {@code BloomFilter}
317   */
318  public static <T extends @Nullable Object> BloomFilter<T> create(
319      Funnel<? super T> funnel, int expectedInsertions, double fpp) {
320    return create(funnel, (long) expectedInsertions, fpp);
321  }
322
323  /**
324   * Creates a {@link BloomFilter} with the expected number of insertions and expected false
325   * positive probability.
326   *
327   * <p>Note that overflowing a {@code BloomFilter} with significantly more elements than specified,
328   * will result in its saturation, and a sharp deterioration of its false positive probability.
329   *
330   * <p>The constructed {@code BloomFilter} will be serializable if the provided {@code Funnel<T>}
331   * is.
332   *
333   * <p>It is recommended that the funnel be implemented as a Java enum. This has the benefit of
334   * ensuring proper serialization and deserialization, which is important since {@link #equals}
335   * also relies on object identity of funnels.
336   *
337   * @param funnel the funnel of T's that the constructed {@code BloomFilter} will use
338   * @param expectedInsertions the number of expected insertions to the constructed {@code
339   *     BloomFilter}; must be positive
340   * @param fpp the desired false positive probability (must be positive and less than 1.0)
341   * @return a {@code BloomFilter}
342   * @since 19.0
343   */
344  public static <T extends @Nullable Object> BloomFilter<T> create(
345      Funnel<? super T> funnel, long expectedInsertions, double fpp) {
346    return create(funnel, expectedInsertions, fpp, BloomFilterStrategies.MURMUR128_MITZ_64);
347  }
348
349  @VisibleForTesting
350  static <T extends @Nullable Object> BloomFilter<T> create(
351      Funnel<? super T> funnel, long expectedInsertions, double fpp, Strategy strategy) {
352    checkNotNull(funnel);
353    checkArgument(
354        expectedInsertions >= 0, "Expected insertions (%s) must be >= 0", expectedInsertions);
355    checkArgument(fpp > 0.0, "False positive probability (%s) must be > 0.0", fpp);
356    checkArgument(fpp < 1.0, "False positive probability (%s) must be < 1.0", fpp);
357    checkNotNull(strategy);
358
359    if (expectedInsertions == 0) {
360      expectedInsertions = 1;
361    }
362    /*
363     * TODO(user): Put a warning in the javadoc about tiny fpp values, since the resulting size
364     * is proportional to -log(p), but there is not much of a point after all, e.g.
365     * optimalM(1000, 0.0000000000000001) = 76680 which is less than 10kb. Who cares!
366     */
367    long numBits = optimalNumOfBits(expectedInsertions, fpp);
368    int numHashFunctions = optimalNumOfHashFunctions(expectedInsertions, numBits);
369    try {
370      return new BloomFilter<T>(new LockFreeBitArray(numBits), numHashFunctions, funnel, strategy);
371    } catch (IllegalArgumentException e) {
372      throw new IllegalArgumentException("Could not create BloomFilter of " + numBits + " bits", e);
373    }
374  }
375
376  /**
377   * Creates a {@link BloomFilter} with the expected number of insertions and a default expected
378   * false positive probability of 3%.
379   *
380   * <p>Note that overflowing a {@code BloomFilter} with significantly more elements than specified,
381   * will result in its saturation, and a sharp deterioration of its false positive probability.
382   *
383   * <p>The constructed {@code BloomFilter} will be serializable if the provided {@code Funnel<T>}
384   * is.
385   *
386   * <p>It is recommended that the funnel be implemented as a Java enum. This has the benefit of
387   * ensuring proper serialization and deserialization, which is important since {@link #equals}
388   * also relies on object identity of funnels.
389   *
390   * @param funnel the funnel of T's that the constructed {@code BloomFilter} will use
391   * @param expectedInsertions the number of expected insertions to the constructed {@code
392   *     BloomFilter}; must be positive
393   * @return a {@code BloomFilter}
394   */
395  public static <T extends @Nullable Object> BloomFilter<T> create(
396      Funnel<? super T> funnel, int expectedInsertions) {
397    return create(funnel, (long) expectedInsertions);
398  }
399
400  /**
401   * Creates a {@link BloomFilter} with the expected number of insertions and a default expected
402   * false positive probability of 3%.
403   *
404   * <p>Note that overflowing a {@code BloomFilter} with significantly more elements than specified,
405   * will result in its saturation, and a sharp deterioration of its false positive probability.
406   *
407   * <p>The constructed {@code BloomFilter} will be serializable if the provided {@code Funnel<T>}
408   * is.
409   *
410   * <p>It is recommended that the funnel be implemented as a Java enum. This has the benefit of
411   * ensuring proper serialization and deserialization, which is important since {@link #equals}
412   * also relies on object identity of funnels.
413   *
414   * @param funnel the funnel of T's that the constructed {@code BloomFilter} will use
415   * @param expectedInsertions the number of expected insertions to the constructed {@code
416   *     BloomFilter}; must be positive
417   * @return a {@code BloomFilter}
418   * @since 19.0
419   */
420  public static <T extends @Nullable Object> BloomFilter<T> create(
421      Funnel<? super T> funnel, long expectedInsertions) {
422    return create(funnel, expectedInsertions, 0.03); // FYI, for 3%, we always get 5 hash functions
423  }
424
425  // Cheat sheet:
426  //
427  // m: total bits
428  // n: expected insertions
429  // b: m/n, bits per insertion
430  // p: expected false positive probability
431  //
432  // 1) Optimal k = b * ln2
433  // 2) p = (1 - e ^ (-kn/m))^k
434  // 3) For optimal k: p = 2 ^ (-k) ~= 0.6185^b
435  // 4) For optimal k: m = -nlnp / ((ln2) ^ 2)
436
437  /**
438   * Computes the optimal k (number of hashes per element inserted in Bloom filter), given the
439   * expected insertions and total number of bits in the Bloom filter.
440   *
441   * <p>See http://en.wikipedia.org/wiki/File:Bloom_filter_fp_probability.svg for the formula.
442   *
443   * @param n expected insertions (must be positive)
444   * @param m total number of bits in Bloom filter (must be positive)
445   */
446  @VisibleForTesting
447  static int optimalNumOfHashFunctions(long n, long m) {
448    // (m / n) * log(2), but avoid truncation due to division!
449    return Math.max(1, (int) Math.round((double) m / n * Math.log(2)));
450  }
451
452  /**
453   * Computes m (total bits of Bloom filter) which is expected to achieve, for the specified
454   * expected insertions, the required false positive probability.
455   *
456   * <p>See http://en.wikipedia.org/wiki/Bloom_filter#Probability_of_false_positives for the
457   * formula.
458   *
459   * @param n expected insertions (must be positive)
460   * @param p false positive rate (must be 0 < p < 1)
461   */
462  @VisibleForTesting
463  static long optimalNumOfBits(long n, double p) {
464    if (p == 0) {
465      p = Double.MIN_VALUE;
466    }
467    return (long) (-n * Math.log(p) / (Math.log(2) * Math.log(2)));
468  }
469
470  private Object writeReplace() {
471    return new SerialForm<T>(this);
472  }
473
474  private static class SerialForm<T extends @Nullable Object> implements Serializable {
475    final long[] data;
476    final int numHashFunctions;
477    final Funnel<? super T> funnel;
478    final Strategy strategy;
479
480    SerialForm(BloomFilter<T> bf) {
481      this.data = LockFreeBitArray.toPlainArray(bf.bits.data);
482      this.numHashFunctions = bf.numHashFunctions;
483      this.funnel = bf.funnel;
484      this.strategy = bf.strategy;
485    }
486
487    Object readResolve() {
488      return new BloomFilter<T>(new LockFreeBitArray(data), numHashFunctions, funnel, strategy);
489    }
490
491    private static final long serialVersionUID = 1;
492  }
493
494  /**
495   * Writes this {@code BloomFilter} to an output stream, with a custom format (not Java
496   * serialization). This has been measured to save at least 400 bytes compared to regular
497   * serialization.
498   *
499   * <p>Use {@linkplain #readFrom(InputStream, Funnel)} to reconstruct the written BloomFilter.
500   */
501  public void writeTo(OutputStream out) throws IOException {
502    // Serial form:
503    // 1 signed byte for the strategy
504    // 1 unsigned byte for the number of hash functions
505    // 1 big endian int, the number of longs in our bitset
506    // N big endian longs of our bitset
507    DataOutputStream dout = new DataOutputStream(out);
508    dout.writeByte(SignedBytes.checkedCast(strategy.ordinal()));
509    dout.writeByte(UnsignedBytes.checkedCast(numHashFunctions)); // note: checked at the c'tor
510    dout.writeInt(bits.data.length());
511    for (int i = 0; i < bits.data.length(); i++) {
512      dout.writeLong(bits.data.get(i));
513    }
514  }
515
516  /**
517   * Reads a byte stream, which was written by {@linkplain #writeTo(OutputStream)}, into a {@code
518   * BloomFilter}.
519   *
520   * <p>The {@code Funnel} to be used is not encoded in the stream, so it must be provided here.
521   * <b>Warning:</b> the funnel provided <b>must</b> behave identically to the one used to populate
522   * the original Bloom filter!
523   *
524   * @throws IOException if the InputStream throws an {@code IOException}, or if its data does not
525   *     appear to be a BloomFilter serialized using the {@linkplain #writeTo(OutputStream)} method.
526   */
527  public static <T extends @Nullable Object> BloomFilter<T> readFrom(
528      InputStream in, Funnel<? super T> funnel) throws IOException {
529    checkNotNull(in, "InputStream");
530    checkNotNull(funnel, "Funnel");
531    int strategyOrdinal = -1;
532    int numHashFunctions = -1;
533    int dataLength = -1;
534    try {
535      DataInputStream din = new DataInputStream(in);
536      // currently this assumes there is no negative ordinal; will have to be updated if we
537      // add non-stateless strategies (for which we've reserved negative ordinals; see
538      // Strategy.ordinal()).
539      strategyOrdinal = din.readByte();
540      numHashFunctions = UnsignedBytes.toInt(din.readByte());
541      dataLength = din.readInt();
542
543      Strategy strategy = BloomFilterStrategies.values()[strategyOrdinal];
544
545      LockFreeBitArray dataArray = new LockFreeBitArray(LongMath.checkedMultiply(dataLength, 64L));
546      for (int i = 0; i < dataLength; i++) {
547        dataArray.putData(i, din.readLong());
548      }
549
550      return new BloomFilter<T>(dataArray, numHashFunctions, funnel, strategy);
551    } catch (RuntimeException e) {
552      String message =
553          "Unable to deserialize BloomFilter from InputStream."
554              + " strategyOrdinal: "
555              + strategyOrdinal
556              + " numHashFunctions: "
557              + numHashFunctions
558              + " dataLength: "
559              + dataLength;
560      throw new IOException(message, e);
561    }
562  }
563}