|
| 1 | +package org.rocksdb; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | + |
| 5 | +public class RibbonFilter extends Filter { |
| 6 | + |
| 7 | + private static final double DEFAULT_BITS_PER_KEY = 10.0; |
| 8 | + private static final int DEFAULT_BLOOM_BEFORE_LEVEL = -1; |
| 9 | + |
| 10 | + private final double bitsPerKey; |
| 11 | + private final int bloomBeforeLevel; |
| 12 | + |
| 13 | + /** |
| 14 | + * Creates a new Ribbon filter with default bits per key (10.0). |
| 15 | + */ |
| 16 | + public RibbonFilter() { |
| 17 | + this(DEFAULT_BITS_PER_KEY); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Creates a new Ribbon filter with the specified bits per key. |
| 22 | + * Higher bits per key = lower false positive rate but more memory usage. |
| 23 | + * |
| 24 | + * @param bitsPerKey the number of bits per key (typical: 8-12) |
| 25 | + */ |
| 26 | + public RibbonFilter(final double bitsPerKey) { |
| 27 | + this(createNewRibbonFilter(bitsPerKey, DEFAULT_BLOOM_BEFORE_LEVEL), bitsPerKey, DEFAULT_BLOOM_BEFORE_LEVEL); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Internal constructor for FilterPolicyType |
| 32 | + * |
| 33 | + * @param nativeHandle handle to existing ribbon filter at RocksDB C++ side |
| 34 | + * @param bitsPerKey number of bits to use - recorded for comparison |
| 35 | + */ |
| 36 | + RibbonFilter(final long nativeHandle, final double bitsPerKey) { |
| 37 | + this(nativeHandle, bitsPerKey, DEFAULT_BLOOM_BEFORE_LEVEL); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates a new Ribbon filter with bits per key and bloom_before_level. |
| 42 | + * This allows using Bloom filters for hot levels and Ribbon for cold levels. |
| 43 | + * |
| 44 | + * @param bitsPerKey the number of bits per key (typical: 8-12) |
| 45 | + * @param bloomBeforeLevel use Bloom filters for levels below this, Ribbon for levels at or above |
| 46 | + * (-1 means always use Ribbon, 0 means use Bloom for L0, 1 means Bloom for L0-L1, etc.) |
| 47 | + */ |
| 48 | + public RibbonFilter(final double bitsPerKey, final int bloomBeforeLevel) { |
| 49 | + this(createNewRibbonFilter(bitsPerKey, bloomBeforeLevel), bitsPerKey, bloomBeforeLevel); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Internal constructor |
| 54 | + * |
| 55 | + * @param nativeHandle handle to existing ribbon filter at RocksDB C++ side |
| 56 | + * @param bitsPerKey number of bits to use - recorded for comparison |
| 57 | + * @param bloomBeforeLevel bloom before level setting - recorded for comparison |
| 58 | + */ |
| 59 | + private RibbonFilter(final long nativeHandle, final double bitsPerKey, final int bloomBeforeLevel) { |
| 60 | + super(nativeHandle); |
| 61 | + this.bitsPerKey = bitsPerKey; |
| 62 | + this.bloomBeforeLevel = bloomBeforeLevel; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean equals(Object o) { |
| 67 | + if (this == o) |
| 68 | + return true; |
| 69 | + if (o == null || getClass() != o.getClass()) |
| 70 | + return false; |
| 71 | + RibbonFilter that = (RibbonFilter) o; |
| 72 | + return Double.compare(that.bitsPerKey, bitsPerKey) == 0 && |
| 73 | + bloomBeforeLevel == that.bloomBeforeLevel; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public int hashCode() { |
| 78 | + return Objects.hash(bitsPerKey, bloomBeforeLevel); |
| 79 | + } |
| 80 | + |
| 81 | + private static native long createNewRibbonFilter(final double bitsPerKey, final int bloomBeforeLevel); |
| 82 | +} |
0 commit comments