Skip to content

Commit 34af1b6

Browse files
authored
refactor: define CatalogIdToNameIdent with TIdent (#15432)
1 parent 4bbb5d5 commit 34af1b6

File tree

5 files changed

+188
-56
lines changed

5 files changed

+188
-56
lines changed

src/meta/api/src/schema_api_impl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use databend_common_meta_app::id_generator::IdGenerator;
6565
use databend_common_meta_app::schema::database_name_ident::DatabaseNameIdent;
6666
use databend_common_meta_app::schema::database_name_ident::DatabaseNameIdentRaw;
6767
use databend_common_meta_app::schema::CatalogIdIdent;
68-
use databend_common_meta_app::schema::CatalogIdToName;
68+
use databend_common_meta_app::schema::CatalogIdToNameIdent;
6969
use databend_common_meta_app::schema::CatalogInfo;
7070
use databend_common_meta_app::schema::CatalogMeta;
7171
use databend_common_meta_app::schema::CatalogNameIdent;
@@ -3670,7 +3670,7 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
36703670
// (catalog_id) -> (tenant, catalog_name)
36713671
let catalog_id = fetch_id(self, IdGenerator::catalog_id()).await?;
36723672
let id_key = CatalogIdIdent::new(name_key.tenant(), catalog_id);
3673-
let id_to_name_key = CatalogIdToName { catalog_id };
3673+
let id_to_name_key = CatalogIdToNameIdent::new(name_key.tenant(), catalog_id);
36743674

36753675
debug!(catalog_id = catalog_id, name_key :? =(name_key); "new catalog id");
36763676

@@ -3762,7 +3762,7 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
37623762
// (catalog_id) -> catalog_meta
37633763
// (catalog_id) -> (tenant, catalog_name)
37643764
let id_key = CatalogIdIdent::new(name_key.tenant(), catalog_id);
3765-
let id_to_name_key = CatalogIdToName { catalog_id };
3765+
let id_to_name_key = CatalogIdToNameIdent::new(name_key.tenant(), catalog_id);
37663766

37673767
debug!(
37683768
catalog_id = catalog_id,

src/meta/app/src/schema/catalog.rs

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,6 @@ pub struct CatalogMeta {
165165
pub created_on: DateTime<Utc>,
166166
}
167167

168-
#[derive(Clone, Debug, Default, Eq, PartialEq)]
169-
pub struct CatalogIdToName {
170-
pub catalog_id: u64,
171-
}
172-
173-
impl Display for CatalogIdToName {
174-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
175-
write!(f, "{}", self.catalog_id)
176-
}
177-
}
178-
179168
#[derive(Clone, Debug, PartialEq, Eq)]
180169
pub struct CreateCatalogReq {
181170
pub if_not_exists: bool,
@@ -260,43 +249,3 @@ impl ListCatalogReq {
260249
ListCatalogReq { tenant }
261250
}
262251
}
263-
264-
mod kvapi_key_impl {
265-
use databend_common_meta_kvapi::kvapi;
266-
use databend_common_meta_kvapi::kvapi::KeyBuilder;
267-
use databend_common_meta_kvapi::kvapi::KeyError;
268-
use databend_common_meta_kvapi::kvapi::KeyParser;
269-
270-
use super::CatalogIdToName;
271-
use crate::schema::CatalogNameIdent;
272-
273-
impl kvapi::KeyCodec for CatalogIdToName {
274-
fn encode_key(&self, b: KeyBuilder) -> KeyBuilder {
275-
b.push_u64(self.catalog_id)
276-
}
277-
278-
fn decode_key(parser: &mut KeyParser) -> Result<Self, KeyError> {
279-
let catalog_id = parser.next_u64()?;
280-
281-
Ok(Self { catalog_id })
282-
}
283-
}
284-
285-
/// "__fd_catalog_id_to_name/<catalog_id> -> CatalogNameIdent"
286-
impl kvapi::Key for CatalogIdToName {
287-
const PREFIX: &'static str = "__fd_catalog_id_to_name";
288-
289-
type ValueType = CatalogNameIdent;
290-
291-
fn parent(&self) -> Option<String> {
292-
None
293-
// Some(CatalogIdIdent::new(self.catalog_id).to_string_key())
294-
}
295-
}
296-
297-
impl kvapi::Value for CatalogNameIdent {
298-
fn dependency_keys(&self) -> impl IntoIterator<Item = String> {
299-
[]
300-
}
301-
}
302-
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 crate::tenant_key::ident::TIdent;
16+
use crate::tenant_key::raw::TIdentRaw;
17+
18+
pub type CatalogIdIdent = TIdent<Resource, u64>;
19+
pub type CatalogIdIdentRaw = TIdentRaw<Resource, u64>;
20+
21+
pub use kvapi_impl::Resource;
22+
23+
impl CatalogIdIdent {
24+
pub fn catalog_id(&self) -> u64 {
25+
*self.name()
26+
}
27+
}
28+
29+
impl CatalogIdIdentRaw {
30+
pub fn catalog_id(&self) -> u64 {
31+
*self.name()
32+
}
33+
}
34+
35+
mod kvapi_impl {
36+
37+
use databend_common_meta_kvapi::kvapi;
38+
39+
use crate::schema::CatalogMeta;
40+
use crate::tenant_key::resource::TenantResource;
41+
42+
pub struct Resource;
43+
impl TenantResource for Resource {
44+
const PREFIX: &'static str = "__fd_catalog_by_id";
45+
const TYPE: &'static str = "CatalogIdIdent";
46+
const HAS_TENANT: bool = false;
47+
type ValueType = CatalogMeta;
48+
}
49+
50+
impl kvapi::Value for CatalogMeta {
51+
fn dependency_keys(&self) -> impl IntoIterator<Item = String> {
52+
[]
53+
}
54+
}
55+
56+
// // Use these error types to replace usage of ErrorCode if possible.
57+
// impl From<ExistError<Resource>> for ErrorCode {
58+
// impl From<UnknownError<Resource>> for ErrorCode {
59+
}
60+
61+
#[cfg(test)]
62+
mod tests {
63+
use databend_common_meta_kvapi::kvapi::Key;
64+
65+
use super::CatalogIdIdent;
66+
use crate::tenant::Tenant;
67+
68+
#[test]
69+
fn test_background_job_id_ident() {
70+
let tenant = Tenant::new_literal("dummy");
71+
let ident = CatalogIdIdent::new(tenant, 3);
72+
73+
let key = ident.to_string_key();
74+
assert_eq!(key, "__fd_catalog_by_id/3");
75+
76+
assert_eq!(ident, CatalogIdIdent::from_str_key(&key).unwrap());
77+
}
78+
79+
#[test]
80+
fn test_background_job_id_ident_with_key_space() {
81+
// TODO(xp): implement this test
82+
// let tenant = Tenant::new_literal("test");
83+
// let ident = CatalogIdIdent::new(tenant, 3);
84+
//
85+
// let key = ident.to_string_key();
86+
// assert_eq!(key, "__fd_catalog_by_id/3");
87+
//
88+
// assert_eq!(ident, CatalogIdIdent::from_str_key(&key).unwrap());
89+
}
90+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 crate::tenant_key::ident::TIdent;
16+
use crate::tenant_key::raw::TIdentRaw;
17+
18+
pub type CatalogIdToNameIdent = TIdent<Resource, u64>;
19+
pub type CatalogIdToNameIdentRaw = TIdentRaw<Resource, u64>;
20+
21+
pub use kvapi_impl::Resource;
22+
23+
impl CatalogIdToNameIdent {
24+
pub fn catalog_id(&self) -> u64 {
25+
*self.name()
26+
}
27+
}
28+
29+
impl CatalogIdToNameIdentRaw {
30+
pub fn catalog_id(&self) -> u64 {
31+
*self.name()
32+
}
33+
}
34+
35+
mod kvapi_impl {
36+
37+
use databend_common_meta_kvapi::kvapi;
38+
39+
use crate::schema::CatalogNameIdent;
40+
use crate::tenant_key::resource::TenantResource;
41+
42+
// TODO(TIdent): parent should return Some(CatalogIdIdent::new(self.catalog_id).to_string_key())
43+
pub struct Resource;
44+
impl TenantResource for Resource {
45+
const PREFIX: &'static str = "__fd_catalog_id_to_name";
46+
const TYPE: &'static str = "CatalogIdToNameIdent";
47+
const HAS_TENANT: bool = false;
48+
type ValueType = CatalogNameIdent;
49+
}
50+
51+
impl kvapi::Value for CatalogNameIdent {
52+
fn dependency_keys(&self) -> impl IntoIterator<Item = String> {
53+
[]
54+
}
55+
}
56+
57+
// // Use these error types to replace usage of ErrorCode if possible.
58+
// impl From<ExistError<Resource>> for ErrorCode {
59+
// impl From<UnknownError<Resource>> for ErrorCode {
60+
}
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use databend_common_meta_kvapi::kvapi::Key;
65+
66+
use super::CatalogIdToNameIdent;
67+
use crate::tenant::Tenant;
68+
69+
#[test]
70+
fn test_background_job_id_ident() {
71+
let tenant = Tenant::new_literal("dummy");
72+
let ident = CatalogIdToNameIdent::new(tenant, 3);
73+
74+
let key = ident.to_string_key();
75+
assert_eq!(key, "__fd_catalog_id_to_name/3");
76+
77+
assert_eq!(ident, CatalogIdToNameIdent::from_str_key(&key).unwrap());
78+
}
79+
80+
#[test]
81+
fn test_background_job_id_ident_with_key_space() {
82+
// TODO(xp): implement this test
83+
// let tenant = Tenant::new_literal("test");
84+
// let ident = CatalogIdToNameIdent::new(tenant, 3);
85+
//
86+
// let key = ident.to_string_key();
87+
// assert_eq!(key, "__fd_catalog_id_to_name/3");
88+
//
89+
// assert_eq!(ident, CatalogIdToNameIdent::from_str_key(&key).unwrap());
90+
}
91+
}

src/meta/app/src/schema/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
//! Schema types
1616
1717
pub mod catalog;
18-
pub mod catalog_id;
18+
pub mod catalog_id_ident;
19+
pub mod catalog_id_to_name_ident;
1920
pub mod catalog_name_ident;
2021
pub mod database_id_history_ident;
2122
pub mod database_name_ident;
@@ -34,7 +35,8 @@ mod table;
3435
mod virtual_column;
3536

3637
pub use catalog::*;
37-
pub use catalog_id::CatalogIdIdent;
38+
pub use catalog_id_ident::CatalogIdIdent;
39+
pub use catalog_id_to_name_ident::CatalogIdToNameIdent;
3840
pub use catalog_name_ident::CatalogNameIdent;
3941
pub use create_option::CreateOption;
4042
pub use database::CreateDatabaseReply;

0 commit comments

Comments
 (0)