-
Notifications
You must be signed in to change notification settings - Fork 483
feat(inverted_index.search): add fst values mapper #2862
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
zhongzc
merged 12 commits into
GreptimeTeam:develop
from
zhongzc:zhongzc/index-fst-values-mapper
Dec 4, 2023
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3bf69bb
feat(inverted_index.search): add fst applier
zhongzc 8fdd24f
fix: typos
zhongzc 738ebe2
feat(inverted_index.search): add fst values mapper
zhongzc cfbdf6b
chore: remove meta check
zhongzc 1cc4b2c
fix: fmt & clippy
zhongzc 5bc2f9e
refactor: one expect for test
zhongzc 83fb712
Merge remote-tracking branch 'origin/develop' into zhongzc/index-fst-…
zhongzc 40534ab
chore: match error in test
zhongzc eef919f
fix: fmt
zhongzc 08fd331
refactor: add helper function to construct fst value
zhongzc 69cf378
refactor: bytemuck to extract offset and size
zhongzc 65e47b7
fix: toml format
zhongzc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,4 +13,5 @@ | |
| // limitations under the License. | ||
|
|
||
| pub mod fst_apply; | ||
| pub mod fst_values_mapper; | ||
| pub mod predicate; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/index/src/inverted_index/search/fst_values_mapper.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright 2023 Greptime Team | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use common_base::BitVec; | ||
| use greptime_proto::v1::index::InvertedIndexMeta; | ||
|
|
||
| use crate::inverted_index::error::Result; | ||
| use crate::inverted_index::format::reader::InvertedIndexReader; | ||
|
|
||
| /// `FstValuesMapper` maps FST-encoded u64 values to their corresponding bitmaps | ||
| /// within an inverted index. The higher 32 bits of each u64 value represent the | ||
| /// bitmap offset and the lower 32 bits represent its size. This mapper uses these | ||
| /// combined offset-size pairs to fetch and union multiple bitmaps into a single `BitVec`. | ||
| pub struct FstValuesMapper<'a> { | ||
| /// `reader` retrieves bitmap data using offsets and sizes from FST values. | ||
| reader: &'a mut dyn InvertedIndexReader, | ||
|
|
||
| /// `metadata` provides context for interpreting the index structures. | ||
| metadata: &'a InvertedIndexMeta, | ||
| } | ||
|
|
||
| impl<'a> FstValuesMapper<'a> { | ||
| pub fn new( | ||
| reader: &'a mut dyn InvertedIndexReader, | ||
| metadata: &'a InvertedIndexMeta, | ||
| ) -> FstValuesMapper<'a> { | ||
| FstValuesMapper { reader, metadata } | ||
| } | ||
|
|
||
| /// Maps an array of FST values to a `BitVec` by retrieving and combining bitmaps. | ||
| pub async fn map_values(&mut self, values: &[u64]) -> Result<BitVec> { | ||
| let mut bitmap = BitVec::new(); | ||
|
|
||
| for value in values { | ||
| // relative_offset (higher 32 bits), size (lower 32 bits) | ||
| let relative_offset = (value >> 32) as u32; | ||
| let size = *value as u32; | ||
|
|
||
| let bm = self | ||
| .reader | ||
| .bitmap(self.metadata, relative_offset, size) | ||
| .await?; | ||
|
|
||
| // Ensure the longest BitVec is the left operand to prevent truncation during OR. | ||
| if bm.len() > bitmap.len() { | ||
| bitmap = bm | bitmap | ||
| } else { | ||
| bitmap |= bm | ||
| } | ||
| } | ||
|
waynexia marked this conversation as resolved.
|
||
|
|
||
| Ok(bitmap) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use common_base::bit_vec::prelude::*; | ||
|
|
||
| use super::*; | ||
| use crate::inverted_index::format::reader::MockInvertedIndexReader; | ||
|
|
||
| fn value(offset: u32, size: u32) -> u64 { | ||
| ((offset as u64) << 32) | (size as u64) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_map_values() { | ||
| let mut mock_reader = MockInvertedIndexReader::new(); | ||
| mock_reader | ||
| .expect_bitmap() | ||
| .returning(|_, offset, size| match (offset, size) { | ||
| (1, 1) => Ok(bitvec![u8, Lsb0; 1, 0, 1, 0, 1, 0, 1]), | ||
| (2, 1) => Ok(bitvec![u8, Lsb0; 0, 1, 0, 1, 0, 1, 0, 1]), | ||
| _ => unreachable!(), | ||
| }); | ||
|
|
||
| let meta = InvertedIndexMeta::default(); | ||
| let mut values_mapper = FstValuesMapper::new(&mut mock_reader, &meta); | ||
|
|
||
| let result = values_mapper.map_values(&[]).await.unwrap(); | ||
| assert_eq!(result.count_ones(), 0); | ||
|
|
||
| let result = values_mapper.map_values(&[value(1, 1)]).await.unwrap(); | ||
| assert_eq!(result, bitvec![u8, Lsb0; 1, 0, 1, 0, 1, 0, 1]); | ||
|
|
||
| let result = values_mapper.map_values(&[value(2, 1)]).await.unwrap(); | ||
| assert_eq!(result, bitvec![u8, Lsb0; 0, 1, 0, 1, 0, 1, 0, 1]); | ||
|
|
||
| let result = values_mapper | ||
| .map_values(&[value(1, 1), value(2, 1)]) | ||
| .await | ||
| .unwrap(); | ||
| assert_eq!(result, bitvec![u8, Lsb0; 1, 1, 1, 1, 1, 1, 1, 1]); | ||
|
|
||
| let result = values_mapper | ||
| .map_values(&[value(2, 1), value(1, 1)]) | ||
| .await | ||
| .unwrap(); | ||
| assert_eq!(result, bitvec![u8, Lsb0; 1, 1, 1, 1, 1, 1, 1, 1]); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.