Skip to content

[ENH] Prefetch block by prefixes #4623

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

Merged
merged 2 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions rust/blockstore/src/arrow/blockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,15 @@ impl<'me, K: ArrowReadableKey<'me> + Into<KeyWrapper>, V: ArrowReadableValue<'me
self.load_blocks(&target_block_ids).await;
}

pub(crate) async fn load_blocks_for_prefixes(&self, prefixes: impl IntoIterator<Item = &str>) {
let prefix_vec = prefixes.into_iter().collect();
let target_block_ids = self
.root
.sparse_index
.get_block_ids_for_prefixes(prefix_vec);
self.load_blocks(&target_block_ids).await;
}

pub(crate) async fn get(
&'me self,
prefix: &str,
Expand Down
42 changes: 41 additions & 1 deletion rust/blockstore/src/arrow/sparse_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,47 @@ impl SparseIndexReader {
result_uuids
}

pub(super) fn get_block_ids_range<'prefix, 'referred_data, PrefixRange>(
pub(super) fn get_block_ids_for_prefixes(&self, mut prefixes: Vec<&str>) -> Vec<Uuid> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Documentation]

The new get_block_ids_for_prefixes function could benefit from some documentation comments explaining its purpose, behavior, and usage - especially since it's a key component of the newly added functionality.

prefixes.sort();
let mut result_uuids = Vec::new();
let block_start = self.data.forward.iter();
let block_end = block_start
.clone()
.skip(1)
.map(|(delim, _)| match delim {
SparseIndexDelimiter::Start => {
unreachable!("The start delimiter should only appear in the first block")
}
SparseIndexDelimiter::Key(composite_key) => Some(composite_key.prefix.as_str()),
})
.chain([None]);
let mut prefix_iter = prefixes.into_iter().peekable();
for ((start_delim, block), end_prefix) in block_start.zip(block_end) {
if let SparseIndexDelimiter::Key(CompositeKey {
prefix: start_prefix,
key: _,
}) = start_delim
{
while let Some(&prefix) = prefix_iter.peek() {
if start_prefix.as_str() <= prefix {
break;
}
prefix_iter.next();
}
}
if let Some(&prefix) = prefix_iter.peek() {
if end_prefix.is_none() || end_prefix.is_some_and(|end_prefix| prefix <= end_prefix)
{
result_uuids.push(block.id);
}
} else {
break;
}
}
result_uuids
}

pub(super) fn get_block_ids_range<'prefix, PrefixRange>(
&self,
prefix_range: PrefixRange,
) -> Vec<Uuid>
Expand Down
12 changes: 12 additions & 0 deletions rust/blockstore/src/types/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ impl<
}
}

pub async fn load_blocks_for_prefixes<'prefix>(
&self,
prefixes: impl IntoIterator<Item = &'prefix str>,
) {
match self {
BlockfileReader::MemoryBlockfileReader(_reader) => unimplemented!(),
BlockfileReader::ArrowBlockfileReader(reader) => {
reader.load_blocks_for_prefixes(prefixes).await
}
}
}

pub async fn rank(
&'referred_data self,
prefix: &'referred_data str,
Expand Down
Loading