Skip to content

Commit

Permalink
refactor: implement Display for TableRouteKey (#1879)
Browse files Browse the repository at this point in the history
  • Loading branch information
WenyXu authored Jul 5, 2023
1 parent 4b580f4 commit 8a00424
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
14 changes: 9 additions & 5 deletions src/common/meta/src/key/table_route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt::Display;

use api::v1::meta::TableName;

use crate::key::to_removed_key;
Expand Down Expand Up @@ -42,12 +44,14 @@ impl<'a> TableRouteKey<'a> {
)
}

pub fn key(&self) -> String {
format!("{}-{}", self.prefix(), self.table_id)
pub fn removed_key(&self) -> String {
to_removed_key(&self.to_string())
}
}

pub fn removed_key(&self) -> String {
to_removed_key(&self.key())
impl<'a> Display for TableRouteKey<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}-{}", self.prefix(), self.table_id)
}
}

Expand All @@ -69,7 +73,7 @@ mod tests {
let prefix = key.prefix();
assert_eq!("__meta_table_route-greptime-public-demo", prefix);

let key_string = key.key();
let key_string = key.to_string();
assert_eq!("__meta_table_route-greptime-public-demo-123", key_string);

let removed = key.removed_key();
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,15 @@ impl DistTable {
schema_name,
table_name: old_table_name,
}
.key();
.to_string();

let new_key = TableRouteKey {
table_id: table_id.into(),
catalog_name,
schema_name,
table_name: new_table_name,
}
.key();
.to_string();

self.catalog_manager
.backend()
Expand Down
4 changes: 2 additions & 2 deletions src/meta-srv/src/procedure/region_failover/update_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl UpdateRegionMetadata {
let table_route = value
.table_route
.with_context(|| CorruptedTableRouteSnafu {
key: key.key(),
key: key.to_string(),
reason: "'table_route' is empty",
})?;
let mut table_route = TableRoute::try_from_raw(&value.peers, table_route)
Expand Down Expand Up @@ -177,7 +177,7 @@ fn pretty_log_table_route_change(
info!(
"Updating region routes in table route value (key = '{}') to [{}]. \
Failed region {} was on Datanode {}.",
key.key(),
key.to_string(),
region_routes.join(", "),
failed_region.region_number,
failed_region.datanode_id,
Expand Down
2 changes: 1 addition & 1 deletion src/meta-srv/src/service/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ async fn handle_create(
table_info.ident.table_id = id as u32;

let table_route_key = TableRouteKey::with_table_name(id, &table_name)
.key()
.to_string()
.into_bytes();

let table = Table {
Expand Down
14 changes: 9 additions & 5 deletions src/meta-srv/src/table_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ pub(crate) async fn get_table_route_value(
key: &TableRouteKey<'_>,
) -> Result<TableRouteValue> {
let kv = kv_store
.get(key.key().into_bytes())
.get(key.to_string().into_bytes())
.await?
.with_context(|| TableRouteNotFoundSnafu { key: key.key() })?;
.with_context(|| TableRouteNotFoundSnafu {
key: key.to_string(),
})?;
kv.value
.as_slice()
.try_into()
Expand All @@ -115,7 +117,7 @@ pub(crate) async fn put_table_route_value(
) -> Result<()> {
let req = PutRequest {
header: None,
key: key.key().into_bytes(),
key: key.to_string().into_bytes(),
value: value.into(),
prev_kv: false,
};
Expand All @@ -127,11 +129,13 @@ pub(crate) async fn remove_table_route_value(
kv_store: &KvStoreRef,
key: &TableRouteKey<'_>,
) -> Result<(Vec<u8>, TableRouteValue)> {
let from_key = key.key().into_bytes();
let from_key = key.to_string().into_bytes();
let to_key = key.removed_key().into_bytes();
let v = move_value(kv_store, from_key, to_key)
.await?
.context(TableRouteNotFoundSnafu { key: key.key() })?;
.context(TableRouteNotFoundSnafu {
key: key.to_string(),
})?;
let trv: TableRouteValue = v.1.as_slice().try_into().context(DecodeTableRouteSnafu)?;

Ok((v.0, trv))
Expand Down

0 comments on commit 8a00424

Please sign in to comment.