Skip to content

Commit 83a09cf

Browse files
committed
extract check and fetch_id from loop
1 parent 0e212e1 commit 83a09cf

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

src/meta/api/src/data_mask_api_impl.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,21 @@ impl<KV: kvapi::KVApi<Error = MetaError>> DatamaskApi for KV {
6161

6262
let name_ident = &req.name;
6363

64+
let row_access_name_ident = RowAccessPolicyNameIdent::new(
65+
name_ident.tenant().clone(),
66+
name_ident.data_mask_name().to_string(),
67+
);
68+
if self.get_pb(&row_access_name_ident).await?.is_some() {
69+
return Err(AppError::DatamaskAlreadyExists(
70+
name_ident.exist_error("name conflicts with an existing row access policy"),
71+
)
72+
.into());
73+
}
74+
75+
let masking_policy_id = fetch_id(self, IdGenerator::data_mask_id()).await?;
76+
6477
let mut trials = txn_backoff(None, func_name!());
65-
let id = loop {
78+
loop {
6679
trials.next().unwrap()?.await;
6780

6881
let mut txn = TxnRequest::default();
@@ -93,25 +106,12 @@ impl<KV: kvapi::KVApi<Error = MetaError>> DatamaskApi for KV {
93106
};
94107
}
95108

96-
let row_access_name_ident = RowAccessPolicyNameIdent::new(
97-
name_ident.tenant().clone(),
98-
name_ident.data_mask_name().to_string(),
99-
);
100-
if self.get_pb(&row_access_name_ident).await?.is_some() {
101-
return Err(AppError::DatamaskAlreadyExists(
102-
name_ident.exist_error("name conflicts with an existing row access policy"),
103-
)
104-
.into());
105-
}
106-
107109
// Create data mask by inserting these record:
108110
// name -> id
109111
// id -> policy
110112
// data mask name -> data mask table id list
111113

112-
let id = fetch_id(self, IdGenerator::data_mask_id()).await?;
113-
114-
let id = DataMaskId::new(id);
114+
let id = DataMaskId::new(masking_policy_id);
115115
let id_ident = DataMaskIdIdent::new_generic(name_ident.tenant(), id);
116116
let id_list_key = MaskPolicyTableIdListIdent::new_from(name_ident.clone());
117117

@@ -144,12 +144,12 @@ impl<KV: kvapi::KVApi<Error = MetaError>> DatamaskApi for KV {
144144
);
145145

146146
if succ {
147-
break id;
147+
break;
148148
}
149149
}
150150
};
151151

152-
Ok(CreateDatamaskReply { id: *id })
152+
Ok(CreateDatamaskReply { id: masking_policy_id })
153153
}
154154

155155
async fn drop_data_mask(

src/meta/api/src/row_access_policy_api_impl.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,20 @@ impl<KV: kvapi::KVApi<Error = MetaError>> RowAccessPolicyApi for KV {
6161

6262
let name_ident = &req.name;
6363

64-
let id = fetch_id(self, IdGenerator::row_access_id()).await?;
64+
let mask_name_ident = DataMaskNameIdent::new(
65+
name_ident.tenant().clone(),
66+
name_ident.row_access_name().to_string(),
67+
);
68+
if self.get_pb(&mask_name_ident).await?.is_some() {
69+
return Ok(Err(
70+
name_ident.exist_error("name conflicts with an existing masking policy")
71+
));
72+
}
73+
74+
let row_access_id = fetch_id(self, IdGenerator::row_access_id()).await?;
75+
let policy_id = RowAccessPolicyId::new(row_access_id);
6576
let mut trials = txn_backoff(None, func_name!());
66-
let id = loop {
77+
loop {
6778
trials.next().unwrap()?.await;
6879

6980
let mut txn = TxnRequest::default();
@@ -87,22 +98,11 @@ impl<KV: kvapi::KVApi<Error = MetaError>> RowAccessPolicyApi for KV {
8798
}
8899
}
89100

90-
let mask_name_ident = DataMaskNameIdent::new(
91-
name_ident.tenant().clone(),
92-
name_ident.row_access_name().to_string(),
93-
);
94-
if self.get_pb(&mask_name_ident).await?.is_some() {
95-
return Ok(Err(
96-
name_ident.exist_error("name conflicts with an existing masking policy")
97-
));
98-
}
99-
100101
// Create row policy by inserting these record:
101102
// name -> id
102103
// id -> policy
103104

104-
let id = RowAccessPolicyId::new(id);
105-
let id_ident = RowAccessPolicyIdIdent::new_generic(name_ident.tenant(), id);
105+
let id_ident = RowAccessPolicyIdIdent::new_generic(name_ident.tenant(), policy_id);
106106

107107
debug!(
108108
id :? =(&id_ident),
@@ -115,8 +115,8 @@ impl<KV: kvapi::KVApi<Error = MetaError>> RowAccessPolicyApi for KV {
115115
txn.condition.push(txn_cond_eq_seq(name_ident, curr_seq));
116116
txn.condition.push(txn_cond_eq_seq(&mask_name_ident, 0));
117117
txn.if_then.extend(vec![
118-
txn_op_put_pb(name_ident, &id, None)?, // name -> policy_id
119-
txn_op_put_pb(&id_ident, &meta, None)?, // id -> meta
118+
txn_op_put_pb(name_ident, &policy_id, None)?, // name -> policy_id
119+
txn_op_put_pb(&id_ident, &meta, None)?, // id -> meta
120120
]);
121121

122122
let (succ, _responses) = send_txn(self, txn).await?;
@@ -129,12 +129,12 @@ impl<KV: kvapi::KVApi<Error = MetaError>> RowAccessPolicyApi for KV {
129129
);
130130

131131
if succ {
132-
break id;
132+
break;
133133
}
134134
}
135135
};
136136

137-
Ok(Ok(CreateRowAccessPolicyReply { id: *id }))
137+
Ok(Ok(CreateRowAccessPolicyReply { id: row_access_id }))
138138
}
139139

140140
async fn drop_row_access_policy(

0 commit comments

Comments
 (0)