use {
crate::{
Ident,
database::storage::Partitions,
hash::content::ContentHasher,
},
std::hash::{Hash, Hasher},
};
pub struct RecordKey<P: Partitions> {
partition_key: crate::SpannedIdent,
sort_key: P::SortKey,
}
impl<P: Partitions> RecordKey<P> {
pub(crate) fn new(
partition_key: crate::SpannedIdent,
sort_key: P::SortKey,
) -> Self {
Self {
partition_key,
sort_key,
}
}
pub(crate) fn partition_key(&self) -> Ident {
self.partition_key.ident()
}
pub(crate) fn partition_key_spanned(&self) -> crate::SpannedIdent {
self.partition_key
}
pub fn sort_key(&self) -> &P::SortKey {
&self.sort_key
}
pub async fn get_record(
&self,
query_client: &mut impl crate::database::query::QueryReads<Storage = P>,
) -> crate::database::query_results::QueryResults<P> {
query_client
.get_record(self.partition_key, self.sort_key.clone())
.await
}
pub fn key_ident(&self) -> Ident {
let mut hasher = ContentHasher::new();
self.partition_key.hash(&mut hasher);
self.sort_key.hash(&mut hasher);
hasher.finish_ident()
}
}
impl<P: Partitions> Clone for RecordKey<P> {
fn clone(&self) -> Self {
Self {
partition_key: self.partition_key,
sort_key: self.sort_key.clone(),
}
}
}
impl<P: Partitions> PartialEq for RecordKey<P> {
fn eq(&self, other: &Self) -> bool {
self.partition_key == other.partition_key && self.sort_key == other.sort_key
}
}
impl<P: Partitions> Eq for RecordKey<P> {}
impl<P: Partitions> Hash for RecordKey<P> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.partition_key.hash(state);
self.sort_key.hash(state);
}
}
impl<P: Partitions> std::fmt::Debug for RecordKey<P> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RecordKey")
.field("partition_key", &self.partition_key)
.finish_non_exhaustive()
}
}