mito2/sst/index/indexer/
update.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
15use common_telemetry::warn;
16
17use crate::read::Batch;
18use crate::sst::index::Indexer;
19
20impl Indexer {
21    pub(crate) async fn do_update(&mut self, batch: &mut Batch) {
22        if batch.is_empty() {
23            return;
24        }
25
26        if !self.do_update_inverted_index(batch).await {
27            self.do_abort().await;
28        }
29        if !self.do_update_fulltext_index(batch).await {
30            self.do_abort().await;
31        }
32        if !self.do_update_bloom_filter(batch).await {
33            self.do_abort().await;
34        }
35    }
36
37    /// Returns false if the update failed.
38    async fn do_update_inverted_index(&mut self, batch: &mut Batch) -> bool {
39        let Some(creator) = self.inverted_indexer.as_mut() else {
40            return true;
41        };
42
43        let Err(err) = creator.update(batch).await else {
44            return true;
45        };
46
47        if cfg!(any(test, feature = "test")) {
48            panic!(
49                "Failed to update inverted index, region_id: {}, file_id: {}, err: {:?}",
50                self.region_id, self.file_id, err
51            );
52        } else {
53            warn!(
54                err; "Failed to update inverted index, region_id: {}, file_id: {}",
55                self.region_id, self.file_id,
56            );
57        }
58
59        false
60    }
61
62    /// Returns false if the update failed.
63    async fn do_update_fulltext_index(&mut self, batch: &mut Batch) -> bool {
64        let Some(creator) = self.fulltext_indexer.as_mut() else {
65            return true;
66        };
67
68        let Err(err) = creator.update(batch).await else {
69            return true;
70        };
71
72        if cfg!(any(test, feature = "test")) {
73            panic!(
74                "Failed to update full-text index, region_id: {}, file_id: {}, err: {:?}",
75                self.region_id, self.file_id, err
76            );
77        } else {
78            warn!(
79                err; "Failed to update full-text index, region_id: {}, file_id: {}",
80                self.region_id, self.file_id,
81            );
82        }
83
84        false
85    }
86
87    /// Returns false if the update failed.
88    async fn do_update_bloom_filter(&mut self, batch: &mut Batch) -> bool {
89        let Some(creator) = self.bloom_filter_indexer.as_mut() else {
90            return true;
91        };
92
93        let Err(err) = creator.update(batch).await else {
94            return true;
95        };
96
97        if cfg!(any(test, feature = "test")) {
98            panic!(
99                "Failed to update bloom filter, region_id: {}, file_id: {}, err: {:?}",
100                self.region_id, self.file_id, err
101            );
102        } else {
103            warn!(
104                err; "Failed to update bloom filter, region_id: {}, file_id: {}",
105                self.region_id, self.file_id,
106            );
107        }
108
109        false
110    }
111}