Skip to main content

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::CatalogManager;
21use crate::error::{GetInformationExtensionSnafu, Result, UpgradeWeakCatalogManagerRefSnafu};
22use crate::information_schema::InformationExtensionRef;
23use crate::kvbackend::KvBackendCatalogManager;
24use crate::system_schema::semantic_graph::EntityGraphProviderRef;
25
26pub mod tables;
27
28/// Try to get the entity-graph provider from a `[CatalogManager]` weak reference.
29/// Returns `None` when the manager is not the kv-backed one or the provider has
30/// not been injected yet (the computed graph tables then stream empty).
31pub fn entity_graph_provider(
32    catalog_manager: &Weak<dyn CatalogManager>,
33) -> Result<Option<EntityGraphProviderRef>> {
34    let catalog_manager = catalog_manager
35        .upgrade()
36        .context(UpgradeWeakCatalogManagerRefSnafu)?;
37
38    Ok(catalog_manager
39        .as_any()
40        .downcast_ref::<KvBackendCatalogManager>()
41        .and_then(|manager| manager.entity_graph_provider()))
42}
43
44/// Try to get the `[InformationExtension]` from `[CatalogManager]` weak reference.
45pub fn information_extension(
46    catalog_manager: &Weak<dyn CatalogManager>,
47) -> Result<InformationExtensionRef> {
48    let catalog_manager = catalog_manager
49        .upgrade()
50        .context(UpgradeWeakCatalogManagerRefSnafu)?;
51
52    let information_extension = catalog_manager
53        .as_any()
54        .downcast_ref::<KvBackendCatalogManager>()
55        .map(|manager| manager.information_extension())
56        .context(GetInformationExtensionSnafu)?;
57
58    Ok(information_extension)
59}
60
61/// Try to get the `[TableMetadataManagerRef]` from `[CatalogManager]` weak reference.
62pub fn table_meta_manager(
63    catalog_manager: &Weak<dyn CatalogManager>,
64) -> Result<Option<TableMetadataManagerRef>> {
65    let catalog_manager = catalog_manager
66        .upgrade()
67        .context(UpgradeWeakCatalogManagerRefSnafu)?;
68
69    Ok(catalog_manager
70        .as_any()
71        .downcast_ref::<KvBackendCatalogManager>()
72        .map(|manager| manager.table_metadata_manager_ref().clone()))
73}