catalog/system_schema/
utils.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::Weak;
16
17use common_meta::key::TableMetadataManagerRef;
18use snafu::OptionExt;
19
20use crate::error::{GetInformationExtensionSnafu, Result, UpgradeWeakCatalogManagerRefSnafu};
21use crate::information_schema::InformationExtensionRef;
22use crate::kvbackend::KvBackendCatalogManager;
23use crate::CatalogManager;
24
25pub mod tables;
26
27/// Try to get the `[InformationExtension]` from `[CatalogManager]` weak reference.
28pub fn information_extension(
29    catalog_manager: &Weak<dyn CatalogManager>,
30) -> Result<InformationExtensionRef> {
31    let catalog_manager = catalog_manager
32        .upgrade()
33        .context(UpgradeWeakCatalogManagerRefSnafu)?;
34
35    let information_extension = catalog_manager
36        .as_any()
37        .downcast_ref::<KvBackendCatalogManager>()
38        .map(|manager| manager.information_extension())
39        .context(GetInformationExtensionSnafu)?;
40
41    Ok(information_extension)
42}
43
44/// Try to get the `[TableMetadataManagerRef]` from `[CatalogManager]` weak reference.
45pub fn table_meta_manager(
46    catalog_manager: &Weak<dyn CatalogManager>,
47) -> Result<Option<TableMetadataManagerRef>> {
48    let catalog_manager = catalog_manager
49        .upgrade()
50        .context(UpgradeWeakCatalogManagerRefSnafu)?;
51
52    Ok(catalog_manager
53        .as_any()
54        .downcast_ref::<KvBackendCatalogManager>()
55        .map(|manager| manager.table_metadata_manager_ref().clone()))
56}