Skip to content

Commit 6abee1b

Browse files
authored
refactor: Define DataMaskIdIdent with TIdent (#15331)
1 parent 3ab7aaf commit 6abee1b

File tree

5 files changed

+154
-96
lines changed

5 files changed

+154
-96
lines changed

src/meta/api/src/data_mask_api_impl.rs

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use databend_common_meta_app::app_error::DatamaskAlreadyExists;
1919
use databend_common_meta_app::app_error::UnknownDatamask;
2020
use databend_common_meta_app::data_mask::CreateDatamaskReply;
2121
use databend_common_meta_app::data_mask::CreateDatamaskReq;
22+
use databend_common_meta_app::data_mask::DataMaskIdIdent;
2223
use databend_common_meta_app::data_mask::DataMaskNameIdent;
23-
use databend_common_meta_app::data_mask::DatamaskId;
2424
use databend_common_meta_app::data_mask::DatamaskMeta;
2525
use databend_common_meta_app::data_mask::DropDatamaskReply;
2626
use databend_common_meta_app::data_mask::DropDatamaskReq;
@@ -32,9 +32,11 @@ use databend_common_meta_app::id_generator::IdGenerator;
3232
use databend_common_meta_app::schema::CreateOption;
3333
use databend_common_meta_app::schema::TableId;
3434
use databend_common_meta_app::schema::TableMeta;
35+
use databend_common_meta_app::KeyWithTenant;
3536
use databend_common_meta_kvapi::kvapi;
3637
use databend_common_meta_types::ConditionResult::Eq;
3738
use databend_common_meta_types::MetaError;
39+
use databend_common_meta_types::SeqValue;
3840
use databend_common_meta_types::TxnCondition;
3941
use databend_common_meta_types::TxnOp;
4042
use databend_common_meta_types::TxnRequest;
@@ -46,10 +48,12 @@ use crate::fetch_id;
4648
use crate::get_pb_value;
4749
use crate::get_u64_value;
4850
use crate::kv_app_error::KVAppError;
51+
use crate::kv_pb_api::KVPbApi;
4952
use crate::send_txn;
5053
use crate::serialize_struct;
5154
use crate::serialize_u64;
5255
use crate::txn_backoff::txn_backoff;
56+
use crate::txn_cond_eq_seq;
5357
use crate::txn_cond_seq;
5458
use crate::txn_op_del;
5559
use crate::txn_op_put;
@@ -64,15 +68,15 @@ impl<KV: kvapi::KVApi<Error = MetaError>> DatamaskApi for KV {
6468
) -> Result<CreateDatamaskReply, KVAppError> {
6569
debug!(req :? =(&req); "DatamaskApi: {}", func_name!());
6670

67-
let name_key = &req.name;
71+
let name_ident = &req.name;
6872

6973
let mut trials = txn_backoff(None, func_name!());
7074
let id = loop {
7175
trials.next().unwrap()?.await;
7276

7377
// Get db mask by name to ensure absence
74-
let (seq, id) = get_u64_value(self, name_key).await?;
75-
debug!(seq = seq, id = id, name_key :? =(name_key); "create_data_mask");
78+
let (seq, id) = get_u64_value(self, name_ident).await?;
79+
debug!(seq = seq, id = id, name_key :? =(name_ident); "create_data_mask");
7680

7781
let mut condition = vec![];
7882
let mut if_then = vec![];
@@ -82,7 +86,7 @@ impl<KV: kvapi::KVApi<Error = MetaError>> DatamaskApi for KV {
8286
CreateOption::Create => {
8387
return Err(KVAppError::AppError(AppError::DatamaskAlreadyExists(
8488
DatamaskAlreadyExists::new(
85-
name_key.name(),
89+
name_ident.name(),
8690
format!("create data mask: {}", req.name.display()),
8791
),
8892
)));
@@ -91,7 +95,7 @@ impl<KV: kvapi::KVApi<Error = MetaError>> DatamaskApi for KV {
9195
CreateOption::CreateOrReplace => {
9296
construct_drop_mask_policy_operations(
9397
self,
94-
name_key,
98+
name_ident,
9599
false,
96100
false,
97101
func_name!(),
@@ -109,22 +113,23 @@ impl<KV: kvapi::KVApi<Error = MetaError>> DatamaskApi for KV {
109113
// data mask name -> data mask table id list
110114

111115
let id = fetch_id(self, IdGenerator::data_mask_id()).await?;
112-
let id_key = DatamaskId { id };
113-
let id_list_key = MaskPolicyTableIdListIdent::new_from(name_key.clone());
116+
117+
let id_ident = DataMaskIdIdent::new(name_ident.tenant(), id);
118+
let id_list_key = MaskPolicyTableIdListIdent::new_from(name_ident.clone());
114119

115120
debug!(
116-
id :? =(&id_key),
117-
name_key :? =(name_key);
121+
id :? =(&id_ident),
122+
name_key :? =(name_ident);
118123
"new datamask id"
119124
);
120125

121126
{
122127
let meta: DatamaskMeta = req.clone().into();
123128
let id_list = MaskpolicyTableIdList::default();
124-
condition.push(txn_cond_seq(name_key, Eq, seq));
129+
condition.push(txn_cond_seq(name_ident, Eq, seq));
125130
if_then.extend( vec![
126-
txn_op_put(name_key, serialize_u64(id)?), // name -> db_id
127-
txn_op_put(&id_key, serialize_struct(&meta)?), // id -> meta
131+
txn_op_put(name_ident, serialize_u64(id)?), // name -> db_id
132+
txn_op_put(&id_ident, serialize_struct(&meta)?), // id -> meta
128133
txn_op_put(&id_list_key, serialize_struct(&id_list)?), /* data mask name -> id_list */
129134
]);
130135

@@ -137,8 +142,8 @@ impl<KV: kvapi::KVApi<Error = MetaError>> DatamaskApi for KV {
137142
let (succ, _responses) = send_txn(self, txn_req).await?;
138143

139144
debug!(
140-
name :? =(name_key),
141-
id :? =(&id_key),
145+
name :? =(name_ident),
146+
id :? =(&id_ident),
142147
succ = succ;
143148
"create_data_mask"
144149
);
@@ -217,35 +222,28 @@ async fn get_data_mask_or_err(
217222
msg: impl Display,
218223
) -> Result<(u64, u64, u64, DatamaskMeta), KVAppError> {
219224
let (id_seq, id) = get_u64_value(kv_api, name_key).await?;
220-
data_mask_has_to_exist(id_seq, name_key, &msg)?;
225+
assert_data_mask_exist(id_seq, name_key, &msg)?;
221226

222-
let id_key = DatamaskId { id };
227+
let id_ident = DataMaskIdIdent::new(name_key.tenant(), id);
223228

224-
let (data_mask_seq, data_mask) = get_pb_value(kv_api, &id_key).await?;
225-
data_mask_has_to_exist(data_mask_seq, name_key, msg)?;
229+
let seq_v = kv_api.get_pb(&id_ident).await?;
230+
assert_data_mask_exist(seq_v.seq(), name_key, msg)?;
226231

227-
Ok((
228-
id_seq,
229-
id,
230-
data_mask_seq,
231-
// Safe unwrap(): data_mask_seq > 0 implies data_mask is not None.
232-
data_mask.unwrap(),
233-
))
232+
// Safe unwrap(): data_mask_seq > 0 implies data_mask is not None.
233+
Ok((id_seq, id, seq_v.seq(), seq_v.unwrap().data))
234234
}
235235

236-
/// Return OK if a db_id or db_meta exists by checking the seq.
237-
///
238-
/// Otherwise returns UnknownDatamask error
239-
pub fn data_mask_has_to_exist(
236+
pub fn assert_data_mask_exist(
240237
seq: u64,
241238
name_ident: &DataMaskNameIdent,
242239
msg: impl Display,
243-
) -> Result<(), KVAppError> {
240+
) -> Result<(), AppError> {
244241
if seq == 0 {
245242
debug!(seq = seq, name_ident :? =(name_ident); "data mask does not exist");
246243

247-
Err(KVAppError::AppError(AppError::UnknownDatamask(
248-
UnknownDatamask::new(name_ident.name(), format!("{}: {}", msg, name_ident.name())),
244+
Err(AppError::UnknownDatamask(UnknownDatamask::new(
245+
name_ident.name(),
246+
format!("{}: {}", msg, name_ident.data_mask_name()),
249247
)))
250248
} else {
251249
Ok(())
@@ -319,20 +317,21 @@ async fn construct_drop_mask_policy_operations(
319317
return Err(err);
320318
}
321319
};
322-
let id_key = DatamaskId { id };
323320

324-
condition.push(txn_cond_seq(&id_key, Eq, data_mask_seq));
325-
if_then.push(txn_op_del(&id_key));
321+
let id_ident = DataMaskIdIdent::new(name_key.tenant(), id);
322+
323+
condition.push(txn_cond_eq_seq(&id_ident, data_mask_seq));
324+
if_then.push(txn_op_del(&id_ident));
326325

327326
if if_delete {
328-
condition.push(txn_cond_seq(name_key, Eq, id_seq));
327+
condition.push(txn_cond_eq_seq(name_key, id_seq));
329328
if_then.push(txn_op_del(name_key));
330329
clear_table_column_mask_policy(kv_api, name_key, condition, if_then).await?;
331330
}
332331

333332
debug!(
334333
name :? =(name_key),
335-
id :? =(&DatamaskId { id }),
334+
id :? =id,
336335
ctx = ctx;
337336
"construct_drop_mask_policy_operations"
338337
);

src/meta/api/src/schema_api_test_suite.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use databend_common_expression::TableDataType;
2828
use databend_common_expression::TableField;
2929
use databend_common_expression::TableSchema;
3030
use databend_common_meta_app::data_mask::CreateDatamaskReq;
31+
use databend_common_meta_app::data_mask::DataMaskIdIdent;
3132
use databend_common_meta_app::data_mask::DataMaskNameIdent;
32-
use databend_common_meta_app::data_mask::DatamaskId;
3333
use databend_common_meta_app::data_mask::DatamaskMeta;
3434
use databend_common_meta_app::data_mask::DropDatamaskReq;
3535
use databend_common_meta_app::data_mask::MaskPolicyTableIdListIdent;
@@ -3090,7 +3090,9 @@ impl SchemaApiTestSuite {
30903090
};
30913091
mt.create_data_mask(req).await?;
30923092
let old_id: u64 = get_kv_u64_data(mt.as_kv_api(), &name).await?;
3093-
let id_key = DatamaskId { id: old_id };
3093+
3094+
let id_key = DataMaskIdIdent::new(&tenant, old_id);
3095+
30943096
let meta: DatamaskMeta = get_kv_data(mt.as_kv_api(), &id_key).await?;
30953097
assert_eq!(meta.comment, Some("before".to_string()));
30963098

@@ -3111,7 +3113,9 @@ impl SchemaApiTestSuite {
31113113

31123114
let id: u64 = get_kv_u64_data(mt.as_kv_api(), &name).await?;
31133115
assert_ne!(old_id, id);
3114-
let id_key = DatamaskId { id };
3116+
3117+
let id_key = DataMaskIdIdent::new(&tenant, id);
3118+
31153119
let meta: DatamaskMeta = get_kv_data(mt.as_kv_api(), &id_key).await?;
31163120
assert_eq!(meta.comment, Some("after".to_string()));
31173121
}
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 DataMaskIdIdent = TIdent<Resource, u64>;
19+
pub type DataMaskIdIdentRaw = TIdentRaw<Resource, u64>;
20+
21+
pub use kvapi_impl::Resource;
22+
23+
impl DataMaskIdIdent {
24+
pub fn data_mask_id(&self) -> u64 {
25+
*self.name()
26+
}
27+
}
28+
29+
impl DataMaskIdIdentRaw {
30+
pub fn data_mask_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::data_mask::DatamaskMeta;
40+
use crate::tenant_key::resource::TenantResource;
41+
42+
pub struct Resource;
43+
impl TenantResource for Resource {
44+
const PREFIX: &'static str = "__fd_datamask_by_id";
45+
const TYPE: &'static str = "DataMaskIdIdent";
46+
const HAS_TENANT: bool = false;
47+
type ValueType = DatamaskMeta;
48+
}
49+
50+
impl kvapi::Value for DatamaskMeta {
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::DataMaskIdIdent;
66+
use crate::tenant::Tenant;
67+
68+
#[test]
69+
fn test_data_mask_id_ident() {
70+
let tenant = Tenant::new_literal("dummy");
71+
let ident = DataMaskIdIdent::new(tenant, 3);
72+
73+
let key = ident.to_string_key();
74+
assert_eq!(key, "__fd_datamask_by_id/3");
75+
76+
assert_eq!(ident, DataMaskIdIdent::from_str_key(&key).unwrap());
77+
}
78+
79+
#[test]
80+
fn test_data_mask_id_ident_with_key_space() {
81+
// TODO(xp): implement this test
82+
// let tenant = Tenant::new_literal("test");
83+
// let ident = DataMaskIdIdent::new(tenant, 3);
84+
//
85+
// let key = ident.to_string_key();
86+
// assert_eq!(key, "__fd_background_job_by_id/3");
87+
//
88+
// assert_eq!(ident, DataMaskIdIdent::from_str_key(&key).unwrap());
89+
}
90+
}

src/meta/app/src/data_mask/data_mask_name_ident.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,42 @@
1313
// limitations under the License.
1414

1515
use crate::tenant_key::ident::TIdent;
16+
use crate::tenant_key::raw::TIdentRaw;
1617

1718
pub type DataMaskNameIdent = TIdent<Resource>;
19+
pub type DataMaskNameIdentRaw = TIdentRaw<Resource>;
1820

1921
pub use kvapi_impl::Resource;
2022

23+
impl DataMaskNameIdent {
24+
pub fn data_mask_name(&self) -> &str {
25+
self.name()
26+
}
27+
}
28+
29+
impl DataMaskNameIdentRaw {
30+
pub fn data_mask_name(&self) -> &str {
31+
self.name()
32+
}
33+
}
34+
2135
mod kvapi_impl {
2236

2337
use databend_common_meta_kvapi::kvapi;
2438
use databend_common_meta_kvapi::kvapi::Key;
2539

26-
use crate::data_mask::DatamaskId;
40+
use crate::data_mask::DataMaskIdIdent;
2741
use crate::tenant_key::resource::TenantResource;
2842

2943
pub struct Resource;
3044
impl TenantResource for Resource {
3145
const PREFIX: &'static str = "__fd_datamask";
3246
const TYPE: &'static str = "DataMaskNameIdent";
3347
const HAS_TENANT: bool = true;
34-
type ValueType = DatamaskId;
48+
type ValueType = DataMaskIdIdent;
3549
}
3650

37-
impl kvapi::Value for DatamaskId {
51+
impl kvapi::Value for DataMaskIdIdent {
3852
fn dependency_keys(&self) -> impl IntoIterator<Item = String> {
3953
[self.to_string_key()]
4054
}

0 commit comments

Comments
 (0)