index/inverted_index/search/index_apply.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 predicates_apply;
16
17use async_trait::async_trait;
18pub use predicates_apply::PredicatesIndexApplier;
19
20use crate::bitmap::Bitmap;
21use crate::inverted_index::error::Result;
22use crate::inverted_index::format::reader::InvertedIndexReader;
23
24/// The output of an apply operation.
25#[derive(Clone, Debug, PartialEq)]
26pub struct ApplyOutput {
27 /// Bitmap of indices that match the predicates.
28 pub matched_segment_ids: Bitmap,
29
30 /// The total number of rows in the index.
31 pub total_row_count: usize,
32
33 /// The number of rows in each segment.
34 pub segment_row_count: usize,
35}
36
37/// A trait for processing and transforming indices obtained from an inverted index.
38///
39/// Applier instances are reusable and work with various `InvertedIndexReader` instances,
40/// avoiding repeated compilation of fixed predicates such as regex patterns.
41#[mockall::automock]
42#[async_trait]
43pub trait IndexApplier: Send + Sync {
44 /// Applies the predefined predicates to the data read by the given index reader, returning
45 /// a list of relevant indices (e.g., post IDs, group IDs, row IDs).
46 async fn apply<'a>(
47 &self,
48 context: SearchContext,
49 reader: &mut (dyn InvertedIndexReader + 'a),
50 ) -> Result<ApplyOutput>;
51
52 /// Returns the memory usage of the applier.
53 fn memory_usage(&self) -> usize;
54}
55
56/// A context for searching the inverted index.
57#[derive(Clone, Debug, Eq, PartialEq, Default)]
58pub struct SearchContext {
59 /// `index_not_found_strategy` controls the behavior of the applier when the index is not found.
60 pub index_not_found_strategy: IndexNotFoundStrategy,
61}
62
63/// Defines the behavior of an applier when the index is not found.
64#[derive(Clone, Copy, Debug, Eq, PartialEq, Default)]
65pub enum IndexNotFoundStrategy {
66 /// Return an empty list of indices.
67 #[default]
68 ReturnEmpty,
69
70 /// Ignore the index and continue.
71 Ignore,
72
73 /// Throw an error.
74 ThrowError,
75}