operator/req_convert/insert/
row_to_region.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 ahash::{HashMap, HashSet};
16use api::v1::RowInsertRequests;
17use api::v1::region::InsertRequests as RegionInsertRequests;
18use partition::manager::PartitionRuleManager;
19use snafu::OptionExt;
20use table::metadata::{TableId, TableInfoRef};
21
22use crate::error::{Result, TableNotFoundSnafu};
23use crate::insert::InstantAndNormalInsertRequests;
24use crate::req_convert::common::partitioner::Partitioner;
25
26pub struct RowToRegion<'a> {
27    tables_info: HashMap<String, TableInfoRef>,
28    instant_table_ids: HashSet<TableId>,
29    partition_manager: &'a PartitionRuleManager,
30}
31
32impl<'a> RowToRegion<'a> {
33    pub fn new(
34        tables_info: HashMap<String, TableInfoRef>,
35        instant_table_ids: HashSet<TableId>,
36        partition_manager: &'a PartitionRuleManager,
37    ) -> Self {
38        Self {
39            tables_info,
40            instant_table_ids,
41            partition_manager,
42        }
43    }
44
45    pub async fn convert(
46        &self,
47        requests: RowInsertRequests,
48    ) -> Result<InstantAndNormalInsertRequests> {
49        let mut region_request = Vec::with_capacity(requests.inserts.len());
50        let mut instant_request = Vec::with_capacity(requests.inserts.len());
51        for request in requests.inserts {
52            let Some(rows) = request.rows else { continue };
53
54            let table_info = self.get_table_info(&request.table_name)?;
55            let table_id = table_info.table_id();
56
57            let requests = Partitioner::new(self.partition_manager)
58                .partition_insert_requests(table_info, rows)
59                .await?;
60
61            if self.instant_table_ids.contains(&table_id) {
62                instant_request.extend(requests);
63            } else {
64                region_request.extend(requests);
65            }
66        }
67
68        Ok(InstantAndNormalInsertRequests {
69            normal_requests: RegionInsertRequests {
70                requests: region_request,
71            },
72            instant_requests: RegionInsertRequests {
73                requests: instant_request,
74            },
75        })
76    }
77
78    fn get_table_info(&self, table_name: &str) -> Result<&TableInfoRef> {
79        self.tables_info
80            .get(table_name)
81            .context(TableNotFoundSnafu { table_name })
82    }
83}