index/inverted_index/format/writer.rs
1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15mod blob;
16mod single;
17
18use std::num::NonZeroUsize;
19
20use async_trait::async_trait;
21use futures::Stream;
22
23use crate::bitmap::{Bitmap, BitmapType};
24use crate::inverted_index::error::Result;
25pub use crate::inverted_index::format::writer::blob::InvertedIndexBlobWriter;
26use crate::Bytes;
27
28pub type ValueStream = Box<dyn Stream<Item = Result<(Bytes, Bitmap)>> + Send + Unpin>;
29
30/// Trait for writing inverted index data to underlying storage.
31#[mockall::automock]
32#[async_trait]
33pub trait InvertedIndexWriter: Send {
34 /// Adds entries to an index.
35 ///
36 /// * `name` is the index identifier.
37 /// * `null_bitmap` marks positions of null entries.
38 /// * `values` is a stream of values and their locations, yielded lexicographically.
39 /// Errors occur if the values are out of order.
40 /// * `bitmap_type` is the type of bitmap to encode.
41 async fn add_index(
42 &mut self,
43 name: String,
44 null_bitmap: Bitmap,
45 values: ValueStream,
46 bitmap_type: BitmapType,
47 ) -> Result<()>;
48
49 /// Finalizes the index writing process, ensuring all data is written.
50 /// `total_row_count` and `segment_row_count` is used to fill in the metadata.
51 async fn finish(&mut self, total_row_count: u64, segment_row_count: NonZeroUsize)
52 -> Result<()>;
53}