Skip to content

Commit e014eeb

Browse files
authored
refactor: Add TIdent for all meta-service key that share the same form of <PREFIX>/<TENANT>/<NAME> (#14940)
The only difference between these keys is the `PREFIX` and `ValueType`. Thus we can just extract these two information into a trait `TenantResource` and `TIdent<R: TenantResource>` automatically implements `kvapi::Key` for these keys.
1 parent 3c98535 commit e014eeb

File tree

7 files changed

+190
-171
lines changed

7 files changed

+190
-171
lines changed

src/meta/app/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub mod schema;
3030
pub mod share;
3131
pub mod storage;
3232
pub mod tenant;
33+
pub mod tenant_key;
3334

3435
mod key_with_tenant;
3536
pub use key_with_tenant::KeyWithTenant;

src/meta/app/src/principal/network_policy_ident.rs

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,56 +12,21 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::tenant::Tenant;
15+
use crate::tenant_key::TIdent;
1616

17-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18-
pub struct NetworkPolicyIdent {
19-
tenant: Tenant,
20-
name: String,
21-
}
22-
23-
impl NetworkPolicyIdent {
24-
pub fn new(tenant: Tenant, name: impl ToString) -> Self {
25-
Self {
26-
tenant,
27-
name: name.to_string(),
28-
}
29-
}
30-
}
17+
/// Defines the meta-service key for network policy.
18+
pub type NetworkPolicyIdent = TIdent<kvapi_impl::Resource>;
3119

32-
mod kvapi_key_impl {
20+
mod kvapi_impl {
3321
use databend_common_meta_kvapi::kvapi;
34-
use databend_common_meta_kvapi::kvapi::KeyError;
3522

36-
use crate::principal::network_policy_ident::NetworkPolicyIdent;
3723
use crate::principal::NetworkPolicy;
38-
use crate::tenant::Tenant;
39-
use crate::KeyWithTenant;
24+
use crate::tenant_key::TenantResource;
4025

41-
impl kvapi::Key for NetworkPolicyIdent {
26+
pub struct Resource;
27+
impl TenantResource for Resource {
4228
const PREFIX: &'static str = "__fd_network_policies";
4329
type ValueType = NetworkPolicy;
44-
45-
fn parent(&self) -> Option<String> {
46-
Some(self.tenant.to_string_key())
47-
}
48-
49-
fn encode_key(&self, b: kvapi::KeyBuilder) -> kvapi::KeyBuilder {
50-
b.push_str(self.tenant_name()).push_str(&self.name)
51-
}
52-
53-
fn decode_key(p: &mut kvapi::KeyParser) -> Result<Self, KeyError> {
54-
let tenant = p.next_nonempty()?;
55-
let name = p.next_str()?;
56-
57-
Ok(NetworkPolicyIdent::new(Tenant::new_nonempty(tenant), name))
58-
}
59-
}
60-
61-
impl KeyWithTenant for NetworkPolicyIdent {
62-
fn tenant(&self) -> &Tenant {
63-
&self.tenant
64-
}
6530
}
6631

6732
impl kvapi::Value for NetworkPolicy {

src/meta/app/src/principal/password_policy_ident.rs

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,56 +12,22 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::tenant::Tenant;
15+
use crate::tenant_key::TIdent;
1616

17-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18-
pub struct PasswordPolicyIdent {
19-
tenant: Tenant,
20-
name: String,
21-
}
22-
23-
impl PasswordPolicyIdent {
24-
pub fn new(tenant: Tenant, name: impl ToString) -> Self {
25-
Self {
26-
tenant,
27-
name: name.to_string(),
28-
}
29-
}
30-
}
17+
/// Defines the meta-service key for password policy.
18+
pub type PasswordPolicyIdent = TIdent<kvapi_impl::Resource>;
3119

32-
mod kvapi_key_impl {
20+
mod kvapi_impl {
3321
use databend_common_meta_kvapi::kvapi;
34-
use databend_common_meta_kvapi::kvapi::KeyError;
3522

36-
use crate::principal::password_policy_ident::PasswordPolicyIdent;
3723
use crate::principal::PasswordPolicy;
38-
use crate::tenant::Tenant;
39-
use crate::KeyWithTenant;
24+
use crate::tenant_key::TenantResource;
4025

41-
impl kvapi::Key for PasswordPolicyIdent {
26+
pub struct Resource;
27+
28+
impl TenantResource for Resource {
4229
const PREFIX: &'static str = "__fd_password_policies";
4330
type ValueType = PasswordPolicy;
44-
45-
fn parent(&self) -> Option<String> {
46-
Some(self.tenant.to_string_key())
47-
}
48-
49-
fn encode_key(&self, b: kvapi::KeyBuilder) -> kvapi::KeyBuilder {
50-
b.push_str(self.tenant_name()).push_str(&self.name)
51-
}
52-
53-
fn decode_key(p: &mut kvapi::KeyParser) -> Result<Self, KeyError> {
54-
let tenant = p.next_nonempty()?;
55-
let name = p.next_str()?;
56-
57-
Ok(PasswordPolicyIdent::new(Tenant::new_nonempty(tenant), name))
58-
}
59-
}
60-
61-
impl KeyWithTenant for PasswordPolicyIdent {
62-
fn tenant(&self) -> &Tenant {
63-
&self.tenant
64-
}
6531
}
6632

6733
impl kvapi::Value for PasswordPolicy {

src/meta/app/src/principal/user_setting_ident.rs

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,57 +12,21 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::tenant::Tenant;
15+
use crate::tenant_key::TIdent;
1616

1717
/// Define the meta-service key for a user setting.
18-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19-
pub struct SettingIdent {
20-
pub tenant: Tenant,
21-
pub name: String,
22-
}
23-
24-
impl SettingIdent {
25-
pub fn new(tenant: Tenant, name: impl ToString) -> Self {
26-
Self {
27-
tenant,
28-
name: name.to_string(),
29-
}
30-
}
31-
}
18+
pub type SettingIdent = TIdent<kvapi_impl::Resource>;
3219

33-
mod kvapi_key_impl {
20+
mod kvapi_impl {
3421
use databend_common_meta_kvapi::kvapi;
35-
use databend_common_meta_kvapi::kvapi::KeyError;
3622

37-
use crate::principal::user_setting_ident::SettingIdent;
3823
use crate::principal::UserSetting;
39-
use crate::tenant::Tenant;
40-
use crate::KeyWithTenant;
24+
use crate::tenant_key::TenantResource;
4125

42-
impl kvapi::Key for SettingIdent {
26+
pub struct Resource;
27+
impl TenantResource for Resource {
4328
const PREFIX: &'static str = "__fd_settings";
4429
type ValueType = UserSetting;
45-
46-
fn parent(&self) -> Option<String> {
47-
Some(self.tenant.to_string_key())
48-
}
49-
50-
fn encode_key(&self, b: kvapi::KeyBuilder) -> kvapi::KeyBuilder {
51-
b.push_str(self.tenant_name()).push_str(&self.name)
52-
}
53-
54-
fn decode_key(p: &mut kvapi::KeyParser) -> Result<Self, KeyError> {
55-
let tenant = p.next_nonempty()?;
56-
let name = p.next_str()?;
57-
58-
Ok(SettingIdent::new(Tenant::new_nonempty(tenant), name))
59-
}
60-
}
61-
62-
impl KeyWithTenant for SettingIdent {
63-
fn tenant(&self) -> &Tenant {
64-
&self.tenant
65-
}
6630
}
6731

6832
impl kvapi::Value for UserSetting {
@@ -83,8 +47,6 @@ mod tests {
8347
fn test_setting_ident() {
8448
let tenant = Tenant::new("tenant1");
8549
let ident = SettingIdent::new(tenant.clone(), "test");
86-
assert_eq!(tenant, ident.tenant);
87-
assert_eq!("test", ident.name);
8850
assert_eq!("__fd_settings/tenant1/test", ident.to_string_key());
8951

9052
let got = SettingIdent::from_str_key(&ident.to_string_key()).unwrap();

src/meta/app/src/principal/user_stage_file_ident.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ mod kvapi_key_impl {
5252

5353
fn encode_key(&self, b: KeyBuilder) -> KeyBuilder {
5454
b.push_str(self.stage.tenant_name())
55-
.push_str(&self.stage.name)
55+
.push_str(self.stage.name())
5656
.push_str(&self.path)
5757
}
5858

src/meta/app/src/principal/user_stage_ident.rs

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,57 +12,21 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::tenant::Tenant;
15+
use crate::tenant_key::TIdent;
1616

1717
/// Define the meta-service key for a stage.
18-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19-
pub struct StageIdent {
20-
pub tenant: Tenant,
21-
pub name: String,
22-
}
23-
24-
impl StageIdent {
25-
pub fn new(tenant: Tenant, name: impl ToString) -> Self {
26-
Self {
27-
tenant,
28-
name: name.to_string(),
29-
}
30-
}
31-
}
18+
pub type StageIdent = TIdent<kvapi_impl::Resource>;
3219

33-
mod kvapi_key_impl {
20+
mod kvapi_impl {
3421
use databend_common_meta_kvapi::kvapi;
35-
use databend_common_meta_kvapi::kvapi::KeyError;
3622

37-
use crate::principal::user_stage_ident::StageIdent;
3823
use crate::principal::StageInfo;
39-
use crate::tenant::Tenant;
40-
use crate::KeyWithTenant;
24+
use crate::tenant_key::TenantResource;
4125

42-
impl kvapi::Key for StageIdent {
26+
pub struct Resource;
27+
impl TenantResource for Resource {
4328
const PREFIX: &'static str = "__fd_stages";
4429
type ValueType = StageInfo;
45-
46-
fn parent(&self) -> Option<String> {
47-
Some(self.tenant.to_string_key())
48-
}
49-
50-
fn encode_key(&self, b: kvapi::KeyBuilder) -> kvapi::KeyBuilder {
51-
b.push_str(self.tenant_name()).push_str(&self.name)
52-
}
53-
54-
fn decode_key(p: &mut kvapi::KeyParser) -> Result<Self, KeyError> {
55-
let tenant = p.next_nonempty()?;
56-
let name = p.next_str()?;
57-
58-
Ok(StageIdent::new(Tenant::new_nonempty(tenant), name))
59-
}
60-
}
61-
62-
impl KeyWithTenant for StageIdent {
63-
fn tenant(&self) -> &Tenant {
64-
&self.tenant
65-
}
6630
}
6731

6832
impl kvapi::Value for StageInfo {

0 commit comments

Comments
 (0)