Skip to main content

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 std::sync::Arc;
16
17use common_telemetry::warn;
18use datatypes::arrow::record_batch::RecordBatch;
19
20use crate::read::Batch;
21use crate::sst::index::Indexer;
22
23impl Indexer {
24    pub(crate) async fn do_update(&mut self, batch: &mut Batch) {
25        if batch.is_empty() {
26            return;
27        }
28        if let Err(err) = self.prepare_primary_key(batch) {
29            if cfg!(any(test, feature = "test")) {
30                panic!("Failed to decode primary key for indexes: {err:?}");
31            }
32            warn!(err; "Failed to decode primary key for indexes, region_id: {}, file_id: {}", self.region_id, self.file_id);
33            self.failure.get_or_insert_with(|| Arc::new(err));
34            self.do_abort().await;
35            return;
36        }
37        let mut position = 0;
38        while position < self.creators.len() {
39            if !self.creators[position].creator.requires_row_scan() {
40                position += 1;
41                continue;
42            }
43            match self.creators[position].creator.update(batch).await {
44                Ok(()) => position += 1,
45                Err(err) => {
46                    let builtin = self.creators[position].is_builtin();
47                    if builtin && cfg!(any(test, feature = "test")) {
48                        panic!("Failed to update index: {err:?}");
49                    }
50                    warn!(err; "Failed to update index, region_id: {}, file_id: {}, kinds: {:?}", self.region_id, self.file_id, self.creators[position].kinds);
51                    self.record_failure(err);
52                    if builtin {
53                        self.do_abort().await;
54                        return;
55                    }
56                    self.abort_creator(position).await;
57                }
58            }
59        }
60    }
61
62    pub(crate) async fn do_update_flat(&mut self, batch: &RecordBatch) {
63        if batch.num_rows() == 0 {
64            return;
65        }
66        let mut position = 0;
67        while position < self.creators.len() {
68            if !self.creators[position].creator.requires_row_scan() {
69                position += 1;
70                continue;
71            }
72            match self.creators[position].creator.update_flat(batch).await {
73                Ok(()) => position += 1,
74                Err(err) => {
75                    let builtin = self.creators[position].is_builtin();
76                    if builtin && cfg!(any(test, feature = "test")) {
77                        panic!("Failed to update flat index: {err:?}");
78                    }
79                    warn!(err; "Failed to update flat index, region_id: {}, file_id: {}, kinds: {:?}", self.region_id, self.file_id, self.creators[position].kinds);
80                    self.record_failure(err);
81                    if builtin {
82                        self.do_abort().await;
83                        return;
84                    }
85                    self.abort_creator(position).await;
86                }
87            }
88        }
89    }
90}