mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 21:07:58 +08:00
28 lines
829 B
Rust
28 lines
829 B
Rust
pub(crate) trait KvStorePartition {
|
|
fn partition_number(key: PartitionKey<'_>, num_partitions: u8) -> u32 {
|
|
crc32fast::hash(key.to_string().as_bytes()) % u32::from(num_partitions)
|
|
}
|
|
|
|
fn shard_key(key: PartitionKey<'_>, num_partitions: u8) -> String {
|
|
format!("shard_{}", Self::partition_number(key, num_partitions))
|
|
}
|
|
}
|
|
|
|
pub(crate) enum PartitionKey<'a> {
|
|
MerchantIdPaymentId {
|
|
merchant_id: &'a str,
|
|
payment_id: &'a str,
|
|
},
|
|
}
|
|
|
|
impl<'a> std::fmt::Display for PartitionKey<'a> {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match *self {
|
|
PartitionKey::MerchantIdPaymentId {
|
|
merchant_id,
|
|
payment_id,
|
|
} => f.write_str(&format!("mid_{merchant_id}_pid_{payment_id}")),
|
|
}
|
|
}
|
|
}
|