use crate::error::{Result, StorageError};
use std::fmt;
pub const HASH_LEN: usize = 32;
pub const PARTITION_ID_LEN: usize = 16;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Hash([u8; HASH_LEN]);
impl Hash {
pub fn of(bytes: &[u8]) -> Self {
Hash(*blake3::hash(bytes).as_bytes())
}
pub fn from_bytes(bytes: [u8; HASH_LEN]) -> Self {
Hash(bytes)
}
pub fn as_bytes(&self) -> &[u8; HASH_LEN] {
&self.0
}
pub fn to_hex(&self) -> String {
hex::encode(self.0)
}
pub fn from_hex(s: &str) -> Result<Self> {
let bytes = hex::decode(s)
.map_err(|e| StorageError::InvalidAddress(format!("bad hash hex: {e}")))?;
let arr: [u8; HASH_LEN] = bytes
.try_into()
.map_err(|_| StorageError::InvalidAddress("hash must be 32 bytes".into()))?;
Ok(Hash(arr))
}
}
impl fmt::Debug for Hash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Hash({})", self.to_hex())
}
}
impl fmt::Display for Hash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_hex())
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PartitionId([u8; PARTITION_ID_LEN]);
impl PartitionId {
pub fn derive(namespace: &str) -> Self {
let full = blake3::hash(namespace.as_bytes());
let mut out = [0u8; PARTITION_ID_LEN];
out.copy_from_slice(&full.as_bytes()[..PARTITION_ID_LEN]);
PartitionId(out)
}
pub fn for_person(account_id: &str) -> Self {
PartitionId::derive(&format!("person:{account_id}"))
}
pub fn for_household(hid: &str) -> Self {
PartitionId::derive(&format!("household:{hid}"))
}
pub fn for_service(service_id: &str) -> Self {
PartitionId::derive(&format!("service:{service_id}"))
}
pub fn from_bytes(bytes: [u8; PARTITION_ID_LEN]) -> Self {
PartitionId(bytes)
}
pub fn as_bytes(&self) -> &[u8; PARTITION_ID_LEN] {
&self.0
}
pub fn to_hex(&self) -> String {
hex::encode(self.0)
}
pub fn from_hex(s: &str) -> Result<Self> {
let bytes = hex::decode(s)
.map_err(|e| StorageError::InvalidAddress(format!("bad partition id hex: {e}")))?;
let arr: [u8; PARTITION_ID_LEN] = bytes
.try_into()
.map_err(|_| StorageError::InvalidAddress("partition id must be 16 bytes".into()))?;
Ok(PartitionId(arr))
}
pub fn shard_prefix(hex_id: &str) -> &str {
&hex_id[..2.min(hex_id.len())]
}
}
impl fmt::Debug for PartitionId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "PartitionId({})", self.to_hex())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hash_roundtrips_through_hex() {
let h = Hash::of(b"hello world");
let hex = h.to_hex();
let h2 = Hash::from_hex(&hex).unwrap();
assert_eq!(h, h2);
}
#[test]
fn partition_id_derivation_is_deterministic() {
let a = PartitionId::derive("household:acme");
let b = PartitionId::derive("household:acme");
assert_eq!(a, b);
}
#[test]
fn for_person_matches_the_prefixed_derive_convention() {
let a = PartitionId::for_person("acct-123");
let b = PartitionId::derive("person:acct-123");
assert_eq!(a, b);
}
#[test]
fn for_household_matches_the_prefixed_derive_convention() {
let a = PartitionId::for_household("hid-abc");
let b = PartitionId::derive("household:hid-abc");
assert_eq!(a, b);
}
#[test]
fn person_and_household_partitions_are_domain_separated() {
let person = PartitionId::for_person("acme");
let household = PartitionId::for_household("acme");
assert_ne!(person, household);
}
#[test]
fn for_person_is_deterministic() {
let a = PartitionId::for_person("acct-123");
let b = PartitionId::for_person("acct-123");
assert_eq!(a, b);
}
#[test]
fn for_service_matches_the_prefixed_derive_convention() {
let a = PartitionId::for_service("svc-1");
let b = PartitionId::derive("service:svc-1");
assert_eq!(a, b);
}
#[test]
fn for_service_is_deterministic() {
let a = PartitionId::for_service("svc-1");
let b = PartitionId::for_service("svc-1");
assert_eq!(a, b);
}
#[test]
fn service_partitions_are_domain_separated_from_person_and_household() {
let person = PartitionId::for_person("acme");
let household = PartitionId::for_household("acme");
let service = PartitionId::for_service("acme");
assert_ne!(service, person);
assert_ne!(service, household);
}
}