-
Notifications
You must be signed in to change notification settings - Fork 482
feat(inverted_index): add index reader #2803
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 7 commits into
GreptimeTeam:develop
from
zhongzc:zhongzc/inverted-index-format
Nov 27, 2023
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0451d41
feat(inverted_index): add reader
zhongzc ac59189
fix: toml format
zhongzc ea5f46d
chore: add prefix relative_ to the offset parameter
zhongzc 9eae701
docs: add doc comment
zhongzc 935931d
chore: update proto
zhongzc 9ca5bc4
fix: outdated docs
zhongzc a280cf1
Merge remote-tracking branch 'origin/develop' into zhongzc/inverted-i…
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [package] | ||
| name = "index" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license.workspace = true | ||
|
|
||
| [dependencies] | ||
| async-trait.workspace = true | ||
| common-base.workspace = true | ||
| common-error.workspace = true | ||
| common-macro.workspace = true | ||
| fst.workspace = true | ||
| futures.workspace = true | ||
| greptime-proto.workspace = true | ||
| prost.workspace = true | ||
| snafu.workspace = true | ||
|
|
||
| [dev-dependencies] | ||
| tokio-util.workspace = true | ||
| tokio.workspace = true |
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,16 @@ | ||
| // 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. | ||
|
|
||
| pub mod error; | ||
| pub mod format; |
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,99 @@ | ||
| // 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 std::any::Any; | ||
| use std::io::Error as IoError; | ||
|
|
||
| use common_error::ext::ErrorExt; | ||
| use common_error::status_code::StatusCode; | ||
| use common_macro::stack_trace_debug; | ||
| use snafu::{Location, Snafu}; | ||
|
|
||
| #[derive(Snafu)] | ||
| #[snafu(visibility(pub))] | ||
| #[stack_trace_debug] | ||
| pub enum Error { | ||
| #[snafu(display("Failed to seek"))] | ||
| Seek { | ||
| #[snafu(source)] | ||
| error: IoError, | ||
| location: Location, | ||
| }, | ||
|
|
||
| #[snafu(display("Failed to read"))] | ||
| Read { | ||
| #[snafu(source)] | ||
| error: IoError, | ||
| location: Location, | ||
| }, | ||
|
|
||
| #[snafu(display( | ||
| "Unexpected inverted index blob size, min: {min_blob_size}, actual: {actual_blob_size}" | ||
| ))] | ||
| UnexpectedBlobSize { | ||
| min_blob_size: u64, | ||
| actual_blob_size: u64, | ||
| location: Location, | ||
| }, | ||
|
|
||
| #[snafu(display("Unexpected inverted index footer payload size, max: {max_payload_size}, actual: {actual_payload_size}"))] | ||
| UnexpectedFooterPayloadSize { | ||
| max_payload_size: u64, | ||
| actual_payload_size: u64, | ||
| location: Location, | ||
| }, | ||
|
|
||
| #[snafu(display("Unexpected inverted index offset size, offset: {offset}, size: {size}, blob_size: {blob_size}, payload_size: {payload_size}"))] | ||
| UnexpectedOffsetSize { | ||
| offset: u64, | ||
| size: u64, | ||
| blob_size: u64, | ||
| payload_size: u64, | ||
| }, | ||
|
|
||
| #[snafu(display("Failed to decode fst"))] | ||
| DecodeFst { | ||
| #[snafu(source)] | ||
| error: fst::Error, | ||
| location: Location, | ||
| }, | ||
|
|
||
| #[snafu(display("Failed to decode protobuf"))] | ||
| DecodeProto { | ||
| #[snafu(source)] | ||
| error: prost::DecodeError, | ||
| location: Location, | ||
| }, | ||
| } | ||
|
|
||
| impl ErrorExt for Error { | ||
| fn status_code(&self) -> StatusCode { | ||
| use Error::*; | ||
| match self { | ||
| Seek { .. } | ||
| | Read { .. } | ||
| | UnexpectedFooterPayloadSize { .. } | ||
| | UnexpectedOffsetSize { .. } | ||
| | UnexpectedBlobSize { .. } | ||
| | DecodeProto { .. } | ||
| | DecodeFst { .. } => StatusCode::Unexpected, | ||
| } | ||
| } | ||
|
|
||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
| } | ||
|
|
||
| pub type Result<T> = std::result::Result<T, Error>; |
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,55 @@ | ||
| // 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. | ||
|
|
||
| //! # SST Files with Inverted Index Format Specification | ||
| //! | ||
| //! ## File Structure | ||
| //! | ||
| //! Each SST file includes a series of inverted indices followed by a footer. | ||
| //! | ||
| //! `inverted_index₀ inverted_index₁ ... inverted_indexₙ footer` | ||
| //! | ||
| //! - Each `inverted_indexᵢ` represents an index entry corresponding to tag values and their locations within the file. | ||
| //! - `footer`: Contains metadata about the inverted indices, encoded as a protobuf message. | ||
| //! | ||
| //! ## Inverted Index Internals | ||
| //! | ||
| //! An inverted index comprises a collection of bitmaps, a null bitmap, and a finite state transducer (FST) indicating tag values' positions: | ||
| //! | ||
| //! `bitmap₀ bitmap₁ bitmap₂ ... bitmapₙ null_bitmap fst` | ||
| //! | ||
| //! - `bitmapᵢ`: Bitset indicating the presence of tag values within a row group. | ||
| //! - `null_bitmap`: Bitset tracking the presence of null values within the tag column. | ||
| //! - `fst`: Finite State Transducer providing an ordered map of bytes, representing the tag values. | ||
| //! | ||
| //! ## Footer Details | ||
| //! | ||
| //! The footer encapsulates the metadata for inversion mappings: | ||
| //! | ||
| //! `footer_payload footer_payload_size` | ||
| //! | ||
| //! - `footer_payload`: Protobuf-encoded `InvertedIndexFooter` information describing the metadata of each inverted index. | ||
| //! - `footer_payload_size`: Size in bytes of the `footer_payload`, displayed as a `u32` integer. | ||
| //! - The footer aids in the interpretation of the inverted indices, providing necessary offset and count information. | ||
| //! | ||
| //! ## Reference | ||
| //! | ||
| //! More detailed information regarding the encoding of the inverted indices can be found in the [RFC]. | ||
| //! | ||
| //! [RFC]: https://github.com/GreptimeTeam/greptimedb/blob/develop/docs/rfcs/2023-11-03-inverted-index.md | ||
|
|
||
| pub mod reader; | ||
|
|
||
| const FOOTER_PAYLOAD_SIZE_SIZE: u64 = 4; | ||
| const MIN_BLOB_SIZE: u64 = FOOTER_PAYLOAD_SIZE_SIZE; | ||
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,43 @@ | ||
| // 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. | ||
|
|
||
| mod blob; | ||
| mod footer; | ||
|
|
||
| use async_trait::async_trait; | ||
| use common_base::BitVec; | ||
| use fst::Map; | ||
| use greptime_proto::v1::index::{InvertedIndexMeta, InvertedIndexMetas}; | ||
|
|
||
| use crate::inverted_index::error::Result; | ||
|
|
||
| pub type FstMap = Map<Vec<u8>>; | ||
|
|
||
| /// InvertedIndexReader defines an asynchronous reader of inverted index data | ||
| #[async_trait] | ||
| pub trait InvertedIndexReader { | ||
| /// Retrieve metadata of all inverted indices stored within the blob. | ||
| async fn metadata(&mut self) -> Result<InvertedIndexMetas>; | ||
|
|
||
| /// Retrieve the finite state transducer (FST) map for a given inverted index metadata entry. | ||
| async fn fst(&mut self, meta: &InvertedIndexMeta) -> Result<FstMap>; | ||
|
|
||
| /// Retrieve the bitmap for a given inverted index metadata entry at the specified offset and size. | ||
| async fn bitmap( | ||
| &mut self, | ||
| meta: &InvertedIndexMeta, | ||
| relative_offset: u32, | ||
| size: u32, | ||
| ) -> Result<BitVec>; | ||
| } |
Oops, something went wrong.
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.