Skip to content

Commit

Permalink
refactor: remove serde from CatalogId and CatalogNameIdent (#15068)
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer authored Mar 22, 2024
1 parent a5913cf commit e061245
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 17 deletions.
10 changes: 6 additions & 4 deletions src/meta/api/src/schema_api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3952,8 +3952,8 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
get_catalog_or_err(self, name_key, "get_catalog").await?;

let catalog = CatalogInfo {
id: CatalogId { catalog_id },
name_ident: name_key.clone(),
id: CatalogId { catalog_id }.into(),
name_ident: name_key.clone().into(),
meta: catalog_meta,
};

Expand Down Expand Up @@ -4073,11 +4073,13 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
let catalog_info = CatalogInfo {
id: CatalogId {
catalog_id: catalog_ids[i],
},
}
.into(),
name_ident: CatalogNameIdent {
tenant: name_key.tenant.clone(),
catalog_name: tenant_catalog_names[i].catalog_name.clone(),
},
}
.into(),
meta: catalog_meta,
};
catalog_infos.push(Arc::new(catalog_info));
Expand Down
65 changes: 57 additions & 8 deletions src/meta/app/src/schema/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,59 @@ pub struct IcebergCatalogOption {
pub storage_params: Box<StorageParams>,
}

/// Same as `CatalogNameIdent`, but with `serde` support,
/// and can be used a s part of a value.
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct CatalogName {
pub tenant: String,
pub catalog_name: String,
}

impl From<CatalogNameIdent> for CatalogName {
fn from(ident: CatalogNameIdent) -> Self {
CatalogName {
tenant: ident.tenant,
catalog_name: ident.catalog_name,
}
}
}

impl Display for CatalogName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "'{}'/'{}'", self.tenant, self.catalog_name)
}
}

// serde is required by `DataSourcePlan.catalog_info`
// serde is required by `CommitSink.catalog_info`
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct CatalogInfo {
pub id: CatalogId,
pub name_ident: CatalogNameIdent,
pub id: catalog_info::CatalogId,
pub name_ident: CatalogName,
pub meta: CatalogMeta,
}

/// Private types for `CatalogInfo`.
mod catalog_info {

/// Same as [`crate::schema::CatalogId`], except with serde support, and can be used in a value,
/// while CatalogId is only used for Key.
///
/// This type is sealed in a private mod so that it is pub for use but can not be created directly.
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Default, Eq, PartialEq)]
pub struct CatalogId {
pub catalog_id: u64,
}

impl From<crate::schema::CatalogId> for CatalogId {
fn from(value: crate::schema::CatalogId) -> Self {
Self {
catalog_id: value.catalog_id,
}
}
}
}

impl CatalogInfo {
/// Get the catalog type via catalog info.
pub fn catalog_type(&self) -> CatalogType {
Expand All @@ -94,8 +140,8 @@ impl CatalogInfo {
/// Create a new default catalog info.
pub fn new_default() -> CatalogInfo {
Self {
id: CatalogId { catalog_id: 0 },
name_ident: CatalogNameIdent {
id: CatalogId { catalog_id: 0 }.into(),
name_ident: CatalogName {
// tenant for default catalog is not used.
tenant: "".to_string(),
catalog_name: "default".to_string(),
Expand All @@ -114,13 +160,16 @@ pub struct CatalogMeta {
pub created_on: DateTime<Utc>,
}

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
/// The name of a catalog,
/// which is used as a key and does not support other codec method such as serde.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CatalogNameIdent {
pub tenant: String,
pub catalog_name: String,
}

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Default, Eq, PartialEq)]
// serde is required by `CatalogInfo.id`
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct CatalogId {
pub catalog_id: u64,
}
Expand Down Expand Up @@ -148,7 +197,7 @@ impl Display for CatalogIdToName {
}
}

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CreateCatalogReq {
pub if_not_exists: bool,
pub name_ident: CatalogNameIdent,
Expand Down Expand Up @@ -198,7 +247,7 @@ impl Display for DropCatalogReq {
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct DropCatalogReply {}

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GetCatalogReq {
pub inner: CatalogNameIdent,
}
Expand Down
2 changes: 1 addition & 1 deletion src/meta/app/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

//! Schema types
mod catalog;
pub mod catalog;
mod create_option;
mod database;
mod index;
Expand Down
5 changes: 3 additions & 2 deletions src/query/catalog/src/catalog/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ impl CatalogManager {
ErrorCode::BadArguments(format!("unknown catalog type: {:?}", CatalogType::Hive))
})?;
let ctl = creator.try_create(&CatalogInfo {
id: CatalogId { catalog_id: 0 },
id: CatalogId { catalog_id: 0 }.into(),
name_ident: CatalogNameIdent {
tenant: tenant.to_string(),
catalog_name: name.clone(),
},
}
.into(),
meta: CatalogMeta {
catalog_option: CatalogOption::Hive(HiveCatalogOption {
address: hive_ctl_cfg.metastore_address.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ impl Interpreter for CreateCatalogInterpreter {
// Build and check if catalog is valid.
let ctl = catalog_manager
.build_catalog(&CatalogInfo {
id: CatalogId::default(),
id: CatalogId::default().into(),
name_ident: CatalogNameIdent {
tenant: self.plan.tenant.clone(),
catalog_name: self.plan.catalog.clone(),
},
}
.into(),
meta: CatalogMeta {
catalog_option: self.plan.meta.catalog_option.clone(),
created_on: chrono::Utc::now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use databend_storages_common_table_meta::meta::TableSnapshot;
use crate::executor::physical_plans::common::MutationKind;
use crate::executor::PhysicalPlan;

// serde is required by `PhysicalPlan`
/// The commit sink is used to commit the data to the table.
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
pub struct CommitSink {
Expand Down

0 comments on commit e061245

Please sign in to comment.