use {
crate::{
ContentHash,
Ident,
database::{
ContentAddressedStorage,
Partitions,
},
},
std::sync::Arc,
};
pub(crate) struct RecordMetadata<P: Partitions> {
pub(crate) partition_key: Ident,
pub(crate) sort_key: P::SortKey,
pub(crate) content_hash: ContentHash,
}
impl<P: Partitions> Clone for RecordMetadata<P> {
fn clone(&self) -> Self {
Self {
partition_key: self.partition_key,
sort_key: self.sort_key.clone(),
content_hash: self.content_hash,
}
}
}
impl<P: Partitions> std::fmt::Debug for RecordMetadata<P> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RecordMetadata")
.field("partition_key", &self.partition_key)
.field("content_hash", &self.content_hash)
.finish_non_exhaustive()
}
}
#[derive(Debug)]
pub struct QueryResults<P: Partitions> {
pub(crate) records: Vec<RecordMetadata<P>>,
pub(crate) global_store: Arc<ContentAddressedStorage<P>>,
}
impl<P: Partitions> QueryResults<P> {
pub(crate) fn new(records: Vec<RecordMetadata<P>>, global_store: Arc<ContentAddressedStorage<P>>) -> Self {
Self {
records,
global_store,
}
}
pub(crate) fn get(&self, record: &RecordMetadata<P>) -> Option<P::RecordRef<'_>> {
self
.global_store
.get_any(record.partition_key, record.content_hash)
}
pub(crate) fn records(&self) -> &[RecordMetadata<P>] {
&self.records
}
pub fn iter(&self) -> impl Iterator<Item = P::RecordRef<'_>> {
self.records.iter().filter_map(|meta| {
self.global_store.get_any(meta.partition_key, meta.content_hash)
})
}
pub(crate) fn iter_with_metadata(&self) -> impl Iterator<Item = (&RecordMetadata<P>, Option<P::RecordRef<'_>>)> {
self.records.iter().map(|meta| {
let record = self.global_store.get_any(meta.partition_key, meta.content_hash);
(meta, record)
})
}
pub fn is_empty(&self) -> bool {
self.records.is_empty()
}
pub fn len(&self) -> usize {
self.records.len()
}
}
impl<P: Partitions> Clone for QueryResults<P> {
fn clone(&self) -> Self {
Self {
records: self.records.clone(),
global_store: Arc::clone(&self.global_store),
}
}
}