Skip to content

Commit b6423c0

Browse files
authored
refactor(schema-api): extract CatalogApi trait from SchemaApi (#18654)
Extract catalog operations into a separate trait following single responsibility principle. This creates cleaner separation between catalog and other schema operations, continuing the modularization of the monolithic SchemaApi trait. Key changes: - Create `CatalogApi` trait in `src/meta/api/src/catalog_api.rs` with 4 catalog management methods (create, get, drop, list) - Move catalog method implementations from `schema_api.rs` to the new trait file while preserving all existing functionality - Update `SchemaApi` trait bounds to include `CatalogApi` dependency
1 parent 501a5a4 commit b6423c0

File tree

5 files changed

+151
-96
lines changed

5 files changed

+151
-96
lines changed

src/meta/api/src/catalog_api.rs

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Copyright 2021 Datafuse Labs
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+
15+
use std::sync::Arc;
16+
17+
use databend_common_meta_app::app_error::AppError;
18+
use databend_common_meta_app::schema::catalog_id_ident::CatalogId;
19+
use databend_common_meta_app::schema::catalog_name_ident::CatalogNameIdentRaw;
20+
use databend_common_meta_app::schema::CatalogIdToNameIdent;
21+
use databend_common_meta_app::schema::CatalogInfo;
22+
use databend_common_meta_app::schema::CatalogMeta;
23+
use databend_common_meta_app::schema::CatalogNameIdent;
24+
use databend_common_meta_app::schema::ListCatalogReq;
25+
use databend_common_meta_app::KeyWithTenant;
26+
use databend_common_meta_kvapi::kvapi;
27+
use databend_common_meta_kvapi::kvapi::DirName;
28+
use databend_common_meta_kvapi::kvapi::Key;
29+
use databend_common_meta_types::MetaError;
30+
use databend_common_meta_types::SeqV;
31+
use fastrace::func_name;
32+
use log::debug;
33+
34+
use crate::kv_app_error::KVAppError;
35+
use crate::kv_pb_api::KVPbApi;
36+
use crate::name_id_value_api::NameIdValueApi;
37+
use crate::serialize_struct;
38+
39+
/// CatalogApi defines APIs for catalog management.
40+
///
41+
/// This trait handles:
42+
/// - Catalog creation and deletion
43+
/// - Catalog metadata queries and listing
44+
/// - Catalog lifecycle operations
45+
#[async_trait::async_trait]
46+
pub trait CatalogApi
47+
where
48+
Self: Send + Sync,
49+
Self: kvapi::KVApi<Error = MetaError>,
50+
{
51+
#[logcall::logcall]
52+
#[fastrace::trace]
53+
async fn create_catalog(
54+
&self,
55+
name_ident: &CatalogNameIdent,
56+
meta: &CatalogMeta,
57+
) -> Result<Result<CatalogId, SeqV<CatalogId>>, KVAppError> {
58+
debug!(name_ident :? =(&name_ident), meta :? = meta; "SchemaApi: {}", func_name!());
59+
60+
let name_ident_raw = serialize_struct(&CatalogNameIdentRaw::from(name_ident))?;
61+
62+
let res = self
63+
.create_id_value(
64+
name_ident,
65+
meta,
66+
false,
67+
|id| {
68+
vec![(
69+
CatalogIdToNameIdent::new_generic(name_ident.tenant(), id).to_string_key(),
70+
name_ident_raw.clone(),
71+
)]
72+
},
73+
|_, _| Ok(vec![]),
74+
)
75+
.await?;
76+
77+
Ok(res)
78+
}
79+
80+
#[logcall::logcall]
81+
#[fastrace::trace]
82+
async fn get_catalog(
83+
&self,
84+
name_ident: &CatalogNameIdent,
85+
) -> Result<Arc<CatalogInfo>, KVAppError> {
86+
debug!(req :? =name_ident; "SchemaApi: {}", func_name!());
87+
88+
let (seq_id, seq_meta) = self
89+
.get_id_and_value(name_ident)
90+
.await?
91+
.ok_or_else(|| AppError::unknown(name_ident, func_name!()))?;
92+
93+
let catalog = CatalogInfo::new(name_ident.clone(), seq_id.data, seq_meta.data);
94+
95+
Ok(Arc::new(catalog))
96+
}
97+
98+
#[logcall::logcall]
99+
#[fastrace::trace]
100+
async fn drop_catalog(
101+
&self,
102+
name_ident: &CatalogNameIdent,
103+
) -> Result<Option<(SeqV<CatalogId>, SeqV<CatalogMeta>)>, KVAppError> {
104+
debug!(req :? =(&name_ident); "SchemaApi: {}", func_name!());
105+
106+
let removed = self
107+
.remove_id_value(name_ident, |id| {
108+
vec![CatalogIdToNameIdent::new_generic(name_ident.tenant(), id).to_string_key()]
109+
})
110+
.await?;
111+
112+
Ok(removed)
113+
}
114+
115+
#[logcall::logcall]
116+
#[fastrace::trace]
117+
async fn list_catalogs(
118+
&self,
119+
req: ListCatalogReq,
120+
) -> Result<Vec<Arc<CatalogInfo>>, KVAppError> {
121+
debug!(req :? =(&req); "SchemaApi: {}", func_name!());
122+
123+
let tenant = req.tenant;
124+
let name_key = CatalogNameIdent::new(&tenant, "dummy");
125+
let dir = DirName::new(name_key);
126+
127+
let name_id_values = self.list_id_value(&dir).await?;
128+
129+
let catalog_infos = name_id_values
130+
.map(|(name, id, seq_meta)| Arc::new(CatalogInfo::new(name, id, seq_meta.data)))
131+
.collect::<Vec<_>>();
132+
133+
Ok(catalog_infos)
134+
}
135+
}
136+
137+
#[async_trait::async_trait]
138+
impl<KV> CatalogApi for KV
139+
where
140+
KV: Send + Sync,
141+
KV: kvapi::KVApi<Error = MetaError> + ?Sized,
142+
{
143+
}

src/meta/api/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#![feature(try_blocks)]
1919

2020
extern crate databend_common_meta_types;
21+
pub mod catalog_api;
2122
mod data_mask_api;
2223
mod data_mask_api_impl;
2324
mod database_api;
@@ -45,6 +46,7 @@ mod row_access_policy_api_impl;
4546
mod sequence_api_impl;
4647
pub(crate) mod sequence_nextval_impl;
4748

49+
pub use catalog_api::CatalogApi;
4850
pub use data_mask_api::DatamaskApi;
4951
pub use database_api::DatabaseApi;
5052
pub use index_api::IndexApi;

src/meta/api/src/schema_api.rs

Lines changed: 5 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use std::collections::BTreeSet;
1818
use std::convert::Infallible;
1919
use std::fmt::Display;
2020
use std::ops::Range;
21-
use std::sync::Arc;
2221
use std::time::Duration;
2322

2423
use chrono::DateTime;
@@ -40,8 +39,6 @@ use databend_common_meta_app::principal::TenantOwnershipObjectIdent;
4039
use databend_common_meta_app::row_access_policy::row_access_policy_table_id_ident::RowAccessPolicyIdTableId;
4140
use databend_common_meta_app::row_access_policy::RowAccessPolicyTableId;
4241
use databend_common_meta_app::row_access_policy::RowAccessPolicyTableIdIdent;
43-
use databend_common_meta_app::schema::catalog_id_ident::CatalogId;
44-
use databend_common_meta_app::schema::catalog_name_ident::CatalogNameIdentRaw;
4542
use databend_common_meta_app::schema::database_name_ident::DatabaseNameIdent;
4643
use databend_common_meta_app::schema::dictionary_id_ident::DictionaryId;
4744
use databend_common_meta_app::schema::dictionary_name_ident::DictionaryNameIdent;
@@ -54,10 +51,6 @@ use databend_common_meta_app::schema::marked_deleted_index_ident::MarkedDeletedI
5451
use databend_common_meta_app::schema::marked_deleted_table_index_id::MarkedDeletedTableIndexId;
5552
use databend_common_meta_app::schema::marked_deleted_table_index_ident::MarkedDeletedTableIndexIdIdent;
5653
use databend_common_meta_app::schema::table_niv::TableNIV;
57-
use databend_common_meta_app::schema::CatalogIdToNameIdent;
58-
use databend_common_meta_app::schema::CatalogInfo;
59-
use databend_common_meta_app::schema::CatalogMeta;
60-
use databend_common_meta_app::schema::CatalogNameIdent;
6154
use databend_common_meta_app::schema::CreateDictionaryReply;
6255
use databend_common_meta_app::schema::CreateDictionaryReq;
6356
use databend_common_meta_app::schema::CreateLockRevReply;
@@ -75,7 +68,6 @@ use databend_common_meta_app::schema::ExtendLockRevReq;
7568
use databend_common_meta_app::schema::GcDroppedTableReq;
7669
use databend_common_meta_app::schema::IndexNameIdent;
7770
use databend_common_meta_app::schema::LeastVisibleTime;
78-
use databend_common_meta_app::schema::ListCatalogReq;
7971
use databend_common_meta_app::schema::ListDictionaryReq;
8072
use databend_common_meta_app::schema::ListIndexesReq;
8173
use databend_common_meta_app::schema::ListLockRevReq;
@@ -105,7 +97,6 @@ use databend_common_meta_app::schema::UpdateDictionaryReply;
10597
use databend_common_meta_app::schema::UpdateDictionaryReq;
10698
use databend_common_meta_app::tenant::Tenant;
10799
use databend_common_meta_app::tenant_key::errors::ExistError;
108-
use databend_common_meta_app::KeyWithTenant;
109100
use databend_common_meta_kvapi::kvapi;
110101
use databend_common_meta_kvapi::kvapi::DirName;
111102
use databend_common_meta_kvapi::kvapi::Key;
@@ -127,6 +118,7 @@ use log::warn;
127118
use seq_marked::SeqValue;
128119
use ConditionResult::Eq;
129120

121+
use crate::catalog_api::CatalogApi;
130122
use crate::database_api::DatabaseApi;
131123
use crate::database_util::get_db_or_err;
132124
use crate::errors::TableError;
@@ -162,6 +154,7 @@ impl<KV> SchemaApi for KV
162154
where
163155
KV: Send + Sync,
164156
KV: kvapi::KVApi<Error = MetaError> + ?Sized,
157+
Self: CatalogApi,
165158
Self: DatabaseApi,
166159
Self: IndexApi,
167160
Self: TableApi,
@@ -178,7 +171,10 @@ pub trait SchemaApi
178171
where
179172
Self: Send + Sync,
180173
Self: kvapi::KVApi<Error = MetaError>,
174+
Self: CatalogApi,
181175
Self: DatabaseApi,
176+
Self: IndexApi,
177+
Self: TableApi,
182178
{
183179
#[logcall::logcall]
184180
#[fastrace::trace]
@@ -517,91 +513,6 @@ where
517513
Ok(reply)
518514
}
519515

520-
#[logcall::logcall]
521-
#[fastrace::trace]
522-
async fn create_catalog(
523-
&self,
524-
name_ident: &CatalogNameIdent,
525-
meta: &CatalogMeta,
526-
) -> Result<Result<CatalogId, SeqV<CatalogId>>, KVAppError> {
527-
debug!(name_ident :? =(&name_ident), meta :? = meta; "SchemaApi: {}", func_name!());
528-
529-
let name_ident_raw = serialize_struct(&CatalogNameIdentRaw::from(name_ident))?;
530-
531-
let res = self
532-
.create_id_value(
533-
name_ident,
534-
meta,
535-
false,
536-
|id| {
537-
vec![(
538-
CatalogIdToNameIdent::new_generic(name_ident.tenant(), id).to_string_key(),
539-
name_ident_raw.clone(),
540-
)]
541-
},
542-
|_, _| Ok(vec![]),
543-
)
544-
.await?;
545-
546-
Ok(res)
547-
}
548-
549-
#[logcall::logcall]
550-
#[fastrace::trace]
551-
async fn get_catalog(
552-
&self,
553-
name_ident: &CatalogNameIdent,
554-
) -> Result<Arc<CatalogInfo>, KVAppError> {
555-
debug!(req :? =name_ident; "SchemaApi: {}", func_name!());
556-
557-
let (seq_id, seq_meta) = self
558-
.get_id_and_value(name_ident)
559-
.await?
560-
.ok_or_else(|| AppError::unknown(name_ident, func_name!()))?;
561-
562-
let catalog = CatalogInfo::new(name_ident.clone(), seq_id.data, seq_meta.data);
563-
564-
Ok(Arc::new(catalog))
565-
}
566-
567-
#[logcall::logcall]
568-
#[fastrace::trace]
569-
async fn drop_catalog(
570-
&self,
571-
name_ident: &CatalogNameIdent,
572-
) -> Result<Option<(SeqV<CatalogId>, SeqV<CatalogMeta>)>, KVAppError> {
573-
debug!(req :? =(&name_ident); "SchemaApi: {}", func_name!());
574-
575-
let removed = self
576-
.remove_id_value(name_ident, |id| {
577-
vec![CatalogIdToNameIdent::new_generic(name_ident.tenant(), id).to_string_key()]
578-
})
579-
.await?;
580-
581-
Ok(removed)
582-
}
583-
584-
#[logcall::logcall]
585-
#[fastrace::trace]
586-
async fn list_catalogs(
587-
&self,
588-
req: ListCatalogReq,
589-
) -> Result<Vec<Arc<CatalogInfo>>, KVAppError> {
590-
debug!(req :? =(&req); "SchemaApi: {}", func_name!());
591-
592-
let tenant = req.tenant;
593-
let name_key = CatalogNameIdent::new(&tenant, "dummy");
594-
let dir = DirName::new(name_key);
595-
596-
let name_id_values = self.list_id_value(&dir).await?;
597-
598-
let catalog_infos = name_id_values
599-
.map(|(name, id, seq_meta)| Arc::new(CatalogInfo::new(name, id, seq_meta.data)))
600-
.collect::<Vec<_>>();
601-
602-
Ok(catalog_infos)
603-
}
604-
605516
#[logcall::logcall]
606517
#[fastrace::trace]
607518
async fn set_table_lvt(

src/meta/api/src/schema_api_test_suite.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ use crate::testing::get_kv_data;
156156
use crate::testing::get_kv_u64_data;
157157
use crate::util::IdempotentKVTxnSender;
158158
use crate::DatamaskApi;
159-
use crate::IndexApi;
160159
use crate::RowAccessPolicyApi;
161160
use crate::SchemaApi;
162161
use crate::SequenceApi;

src/query/catalog/src/catalog/manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use databend_common_config::CatalogConfig;
2222
use databend_common_config::InnerConfig;
2323
use databend_common_exception::ErrorCode;
2424
use databend_common_exception::Result;
25-
use databend_common_meta_api::SchemaApi;
25+
use databend_common_meta_api::CatalogApi;
2626
use databend_common_meta_app::app_error::AppError;
2727
use databend_common_meta_app::schema::CatalogIdIdent;
2828
use databend_common_meta_app::schema::CatalogInfo;

0 commit comments

Comments
 (0)