-
-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Prefix Bloom Filter #103
base: main
Are you sure you want to change the base?
Conversation
dc3bbca
to
4d4941d
Compare
/// Checks if a key is in domain and prefix can be extracted from it. | ||
/// For example if `PrefixExtractor` suppose to extract first 4 bytes from key, | ||
/// `in_domain(&[0, 2, 3])` should return false | ||
fn in_domain(&self, key: &[u8]) -> bool; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is necessary.
If a key is [1, 2, 3] and we extract the first 4 bytes, we would just take the entire string, so:
let len = Math.min(key.len(), 4);
return key.slice(0, len);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me better explain why is this needed in more practical example:
Imagine we have a database where keys are constructed using the following template: {datatype}#{id}
and a prefix extractor that would extract {datatype}#
prefix from a key to optimize scans of all rows of datatype#
. Then, in table were populated following rows table1#a
, table1#b
, table2#a
. The keys for bloom filter would be table1#
, table2#
in this case. Then, if we use prefix scan by prefixes table1#
or table2#
everything will work correctly. But if scan is performed with table
prefix without in_domain
the full prefix will be extracted and will try to check bloom filter by table
key, which may return false negative. in_domain
in this case will help to prevent such cases and will use Bloom Filter only in case when prefix is in domain. In this example everything starting with {datatype}#
will be considered as in_domain
and use Bloom Filter, other prefixes will be not in_domain
and will just use normal prefix search ignoring Bloom Filter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.
However, for the prefix extractor to support structured prefixes, the trait needs to return an Iterator over extracted prefixed (see #97 and fjall-rs/fjall#116). At that point, the trait could just return std::iter::empty
, which would signal that the key is "not in domain".
For example we may extract prefixes, like:
eu#germany#berlin
-> extract "eu#" as prefix
-> also extract "eu#germany#"
Now prefix searches over both continent and [continent + country] can be filtered by the prefix filter.
@@ -248,8 +251,20 @@ impl Writer { | |||
// of the same key | |||
#[cfg(feature = "bloom")] | |||
if self.bloom_policy.is_active() { | |||
self.bloom_hash_buffer | |||
.push(BloomFilter::get_hash(&item.key.user_key)); | |||
let key: Option<&[u8]> = match &self.opts.prefix_extractor { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To control the bits per key independently for prefix and point reads, the prefix bloom filter needs to be built from its own buffer, and stored separately in the disk segment.
That's why there's a dedicated pointer for prefix filters in the segment metadata which is currently unused and always 0 (NULL):
lsm-tree/src/segment/file_offsets.rs
Line 24 in 77fb2a9
pub pfx_ptr: BlockOffset, |
@@ -602,7 +605,17 @@ impl Tree { | |||
// NOTE: Create key hash for hash sharing | |||
// https://fjall-rs.github.io/post/bloom-filter-hash-sharing/ | |||
#[cfg(feature = "bloom")] | |||
let key_hash = crate::bloom::BloomFilter::get_hash(key.as_ref()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the prefix bloom filter is separated, this can all be reverted because the point read path does not change at all.
This PR supposed to resolve #97
Please, let me know if I missed something.