Skip to content

Commit 240069d

Browse files
committed
Add RibbonFilter Java API
1 parent a9906f0 commit 240069d

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2+
// This source code is licensed under both the GPLv2 (found in the
3+
// COPYING file in the root directory) and Apache 2.0 License
4+
// (found in the LICENSE.Apache file in the root directory).
5+
6+
package org.rocksdb;
7+
8+
import java.util.Objects;
9+
10+
public class RibbonFilter extends Filter {
11+
12+
private static final double DEFAULT_BITS_PER_KEY = 10.0;
13+
private static final int DEFAULT_BLOOM_BEFORE_LEVEL = -1;
14+
15+
private final double bitsPerKey;
16+
private final int bloomBeforeLevel;
17+
18+
/**
19+
* Creates a new Ribbon filter with default bits per key (10.0).
20+
*/
21+
public RibbonFilter() {
22+
this(DEFAULT_BITS_PER_KEY);
23+
}
24+
25+
/**
26+
* Creates a new Ribbon filter with the specified bits per key.
27+
*
28+
* @param bitsPerKey the number of bits per key (typical: 8-12)
29+
*/
30+
public RibbonFilter(final double bitsPerKey) {
31+
this(createNewRibbonFilter(bitsPerKey, DEFAULT_BLOOM_BEFORE_LEVEL), bitsPerKey, DEFAULT_BLOOM_BEFORE_LEVEL);
32+
}
33+
34+
/**
35+
* Internal constructor for FilterPolicyType
36+
*
37+
* @param nativeHandle handle to existing ribbon filter at RocksDB C++ side
38+
* @param bitsPerKey number of bits to use - recorded for comparison
39+
*/
40+
RibbonFilter(final long nativeHandle, final double bitsPerKey) {
41+
this(nativeHandle, bitsPerKey, DEFAULT_BLOOM_BEFORE_LEVEL);
42+
}
43+
44+
/**
45+
* Creates a new Ribbon filter with bits per key and bloom_before_level.
46+
*
47+
* @param bitsPerKey the number of bits per key (typical: 8-12)
48+
* @param bloomBeforeLevel use Bloom filters for levels below this, Ribbon for levels at or above
49+
* (-1 means always use Ribbon, 0 means use Bloom for L0,
50+
* 1 means Bloom for L0-L1, etc.)
51+
*/
52+
public RibbonFilter(final double bitsPerKey, final int bloomBeforeLevel) {
53+
this(createNewRibbonFilter(bitsPerKey, bloomBeforeLevel), bitsPerKey, bloomBeforeLevel);
54+
}
55+
56+
/**
57+
* Internal constructor
58+
*
59+
* @param nativeHandle handle to existing ribbon filter at RocksDB C++ side
60+
* @param bitsPerKey number of bits to use - recorded for comparison
61+
* @param bloomBeforeLevel bloom before level setting - recorded for comparison
62+
*/
63+
private RibbonFilter(final long nativeHandle, final double bitsPerKey, final int bloomBeforeLevel) {
64+
super(nativeHandle);
65+
this.bitsPerKey = bitsPerKey;
66+
this.bloomBeforeLevel = bloomBeforeLevel;
67+
}
68+
69+
@Override
70+
public boolean equals(Object o) {
71+
if (this == o)
72+
return true;
73+
if (o == null || getClass() != o.getClass())
74+
return false;
75+
RibbonFilter that = (RibbonFilter) o;
76+
return Double.compare(that.bitsPerKey, bitsPerKey) == 0 &&
77+
bloomBeforeLevel == that.bloomBeforeLevel;
78+
}
79+
80+
@Override
81+
public int hashCode() {
82+
return Objects.hash(bitsPerKey, bloomBeforeLevel);
83+
}
84+
85+
private static native long createNewRibbonFilter(final double bitsPerKey, final int bloomBeforeLevel);
86+
}

0 commit comments

Comments
 (0)