common_meta/ddl/utils/
table_id.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 snafu::OptionExt;
16use store_api::storage::TableId;
17use table::table_reference::TableReference;
18
19use crate::error::{Result, TableNotFoundSnafu};
20use crate::key::table_name::{TableNameKey, TableNameManager};
21
22/// Get all the table ids from the table names.
23///
24/// Returns an error if any table does not exist.
25pub(crate) async fn get_all_table_ids_by_names<'a>(
26    table_name_manager: &TableNameManager,
27    table_names: &[TableReference<'a>],
28) -> Result<Vec<TableId>> {
29    let table_name_keys = table_names
30        .iter()
31        .map(TableNameKey::from)
32        .collect::<Vec<_>>();
33    let table_name_values = table_name_manager.batch_get(table_name_keys).await?;
34    let mut table_ids = Vec::with_capacity(table_name_values.len());
35    for (value, table_name) in table_name_values.into_iter().zip(table_names) {
36        let value = value
37            .with_context(|| TableNotFoundSnafu {
38                table_name: table_name.to_string(),
39            })?
40            .table_id();
41
42        table_ids.push(value);
43    }
44
45    Ok(table_ids)
46}