index/inverted_index/create.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
15pub mod sort;
16pub mod sort_create;
17
18use async_trait::async_trait;
19
20use crate::BytesRef;
21use crate::bitmap::BitmapType;
22use crate::inverted_index::error::Result;
23use crate::inverted_index::format::writer::InvertedIndexWriter;
24
25/// `InvertedIndexCreator` provides functionality to construct an inverted index
26#[async_trait]
27pub trait InvertedIndexCreator: Send {
28 /// Adds a value to the named index. A `None` value represents an absence of data (null)
29 ///
30 /// It should be equivalent to calling `push_with_name_n` with `n = 1`
31 #[must_use = "a true result requires calling `spill` before pushing more"]
32 fn push_with_name(&mut self, index_name: &str, value: Option<BytesRef<'_>>) -> bool {
33 self.push_with_name_n(index_name, value, 1)
34 }
35
36 /// Buffers `n` identical values for the named index. `None` values represent absence of
37 /// data (null).
38 ///
39 /// Returns true when buffered data exceeds the memory limit; the caller must then call
40 /// [`InvertedIndexCreator::spill`] before pushing more. Pushing is synchronous so the
41 /// per-row path does not allocate a future.
42 #[must_use = "a true result requires calling `spill` before pushing more"]
43 fn push_with_name_n(&mut self, index_name: &str, value: Option<BytesRef<'_>>, n: usize)
44 -> bool;
45
46 /// Moves the buffers that asked for it to external storage.
47 async fn spill(&mut self) -> Result<()>;
48
49 /// Finalizes the index creation process, ensuring all data is properly indexed and stored
50 /// in the provided writer
51 async fn finish(
52 &mut self,
53 writer: &mut dyn InvertedIndexWriter,
54 bitmap_type: BitmapType,
55 ) -> Result<()>;
56}