Skip to content

Commit 55165c7

Browse files
committed
Add RibbonFilter Java bindings
1 parent ac94531 commit 55165c7

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

java/src/main/java/org/rocksdb/FilterPolicyType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public enum FilterPolicyType {
2828
public Filter createFilter(final long handle, final double param) {
2929
if (this == kBloomFilterPolicy) {
3030
return new BloomFilter(handle, param);
31+
} else if (this == kRibbonFilterPolicy) {
32+
return new RibbonFilter(handle, param);
3133
}
3234
return null;
3335
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)