common_meta/key/
schema_metadata_manager.rs1use std::sync::Arc;
18
19use snafu::OptionExt;
20use store_api::storage::TableId;
21
22use crate::cache::{SchemaCacheRef, TableSchemaCacheRef};
23use crate::error::TableInfoNotFoundSnafu;
24use crate::{error, SchemaOptions};
25
26pub type SchemaMetadataManagerRef = Arc<SchemaMetadataManager>;
27
28pub struct SchemaMetadataManager {
29 table_id_schema_cache: TableSchemaCacheRef,
30 schema_cache: SchemaCacheRef,
31}
32
33impl SchemaMetadataManager {
34 pub fn new(table_id_schema_cache: TableSchemaCacheRef, schema_cache: SchemaCacheRef) -> Self {
36 Self {
37 table_id_schema_cache,
38 schema_cache,
39 }
40 }
41
42 pub async fn get_schema_options_by_table_id(
44 &self,
45 table_id: TableId,
46 ) -> error::Result<Option<Arc<SchemaOptions>>> {
47 let schema_name = self
48 .table_id_schema_cache
49 .get(table_id)
50 .await?
51 .with_context(|| TableInfoNotFoundSnafu {
52 table: format!("table id: {}", table_id),
53 })?;
54
55 self.schema_cache.get_by_ref(&schema_name).await
56 }
57
58 #[cfg(any(test, feature = "testing"))]
59 pub async fn register_region_table_info(
60 &self,
61 table_id: TableId,
62 table_name: &str,
63 schema_name: &str,
64 catalog_name: &str,
65 schema_value: Option<crate::key::schema_name::SchemaNameValue>,
66 kv_backend: crate::kv_backend::KvBackendRef,
67 ) {
68 use table::metadata::{RawTableInfo, TableType};
69 let value = crate::key::table_info::TableInfoValue::new(RawTableInfo {
70 ident: Default::default(),
71 name: table_name.to_string(),
72 desc: None,
73 catalog_name: catalog_name.to_string(),
74 schema_name: schema_name.to_string(),
75 meta: Default::default(),
76 table_type: TableType::Base,
77 });
78 let table_info_manager = crate::key::table_info::TableInfoManager::new(kv_backend.clone());
79 let (txn, _) = table_info_manager
80 .build_create_txn(table_id, &value)
81 .unwrap();
82 let resp = kv_backend.txn(txn).await.unwrap();
83 assert!(resp.succeeded, "Failed to create table metadata");
84 let key = crate::key::schema_name::SchemaNameKey {
85 catalog: catalog_name,
86 schema: schema_name,
87 };
88
89 crate::key::schema_name::SchemaManager::new(kv_backend.clone())
90 .create(key, schema_value, false)
91 .await
92 .expect("Failed to create schema metadata");
93 common_telemetry::info!(
94 "Register table: {}, id: {}, schema: {}, catalog: {}",
95 table_name,
96 table_id,
97 schema_name,
98 catalog_name
99 );
100 }
101}