Skip to content

Commit

Permalink
refactor: Use CrudMgr to impl FileFormatMgr (#14999)
Browse files Browse the repository at this point in the history
- Fix: #14934
  • Loading branch information
drmingdrmer authored Mar 19, 2024
1 parent 33090ea commit 59d8fd6
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 233 deletions.
1 change: 1 addition & 0 deletions src/meta/app/src/principal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod role_ident;
mod role_info;
mod user_auth;
mod user_defined_file_format;
pub mod user_defined_file_format_ident;
mod user_defined_function;
mod user_grant;
mod user_identity;
Expand Down
83 changes: 83 additions & 0 deletions src/meta/app/src/principal/user_defined_file_format_ident.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2021 Datafuse Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::tenant_key::TIdent;

/// Defines the meta-service key for user defined file format.
pub type UserDefinedFileFormatIdent = TIdent<Resource>;

pub use kvapi_impl::Resource;

mod kvapi_impl {

use databend_common_exception::ErrorCode;
use databend_common_meta_kvapi::kvapi;

use crate::principal::UserDefinedFileFormat;
use crate::tenant_key::TenantResource;
use crate::tenant_key_errors::ExistError;
use crate::tenant_key_errors::UnknownError;

pub struct Resource;
impl TenantResource for Resource {
const PREFIX: &'static str = "__fd_file_formats";
type ValueType = UserDefinedFileFormat;
}

impl kvapi::Value for UserDefinedFileFormat {
fn dependency_keys(&self) -> impl IntoIterator<Item = String> {
[]
}
}

impl kvapi::ValueWithName for UserDefinedFileFormat {
fn name(&self) -> &str {
&self.name
}
}

impl From<ExistError<Resource>> for ErrorCode {
fn from(err: ExistError<Resource>) -> Self {
ErrorCode::FileFormatAlreadyExists(err.to_string())
}
}

impl From<UnknownError<Resource>> for ErrorCode {
fn from(err: UnknownError<Resource>) -> Self {
ErrorCode::UnknownFileFormat(err.to_string())
}
}
}

#[cfg(test)]
mod tests {
use databend_common_meta_kvapi::kvapi::Key;

use crate::principal::user_defined_file_format_ident::UserDefinedFileFormatIdent;
use crate::tenant::Tenant;

#[test]
fn test_file_format_ident() {
let tenant = Tenant::new("test".to_string());
let ident = UserDefinedFileFormatIdent::new(tenant, "test1");

let key = ident.to_string_key();
assert_eq!(key, "__fd_file_formats/test/test1");

assert_eq!(
ident,
UserDefinedFileFormatIdent::from_str_key(&key).unwrap()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

mod file_format_api;
mod file_format_mgr;
use databend_common_meta_api::crud::CrudMgr;
use databend_common_meta_app::principal::user_defined_file_format_ident;

pub use file_format_api::FileFormatApi;
pub use file_format_mgr::FileFormatMgr;
pub type FileFormatMgr = CrudMgr<user_defined_file_format_ident::Resource>;
41 changes: 0 additions & 41 deletions src/query/management/src/file_format/file_format_api.rs

This file was deleted.

158 changes: 0 additions & 158 deletions src/query/management/src/file_format/file_format_mgr.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/query/management/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub use cluster::ClusterApi;
pub use cluster::ClusterMgr;
pub use connection::ConnectionApi;
pub use connection::ConnectionMgr;
pub use file_format::FileFormatApi;
pub use file_format::FileFormatMgr;
pub use network_policy::NetworkPolicyMgr;
pub use password_policy::PasswordPolicyMgr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ impl Interpreter for CreateFileFormatInterpreter {

let tenant = self.ctx.get_tenant();
let _create_file_format = user_mgr
.add_file_format(
tenant.as_str(),
user_defined_file_format,
&plan.create_option,
)
.add_file_format(&tenant, user_defined_file_format, &plan.create_option)
.await?;

Ok(PipelineBuildResult::create())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Interpreter for DropFileFormatInterpreter {
let user_mgr = UserApiProvider::instance();

user_mgr
.drop_file_format(tenant.as_str(), &plan.name, plan.if_exists)
.drop_file_format(&tenant, &plan.name, plan.if_exists)
.await?;

Ok(PipelineBuildResult::create())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Interpreter for ShowFileFormatsInterpreter {

let user_mgr = UserApiProvider::instance();
let tenant = self.ctx.get_tenant();
let mut formats = user_mgr.get_file_formats(tenant.as_str()).await?;
let mut formats = user_mgr.get_file_formats(&tenant).await?;

formats.sort_by(|a, b| a.name.cmp(&b.name));

Expand Down
2 changes: 1 addition & 1 deletion src/query/service/src/sessions/query_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ impl TableContext for QueryContext {
let user_mgr = UserApiProvider::instance();
let tenant = self.get_tenant();
Ok(user_mgr
.get_file_format(tenant.as_str(), name)
.get_file_format(&tenant, name)
.await?
.file_format_params)
}
Expand Down
Loading

0 comments on commit 59d8fd6

Please sign in to comment.