use std::sync::OnceLock;
use triblespace_core::blob::encodings::simplearchive::SimpleArchive;
use triblespace_core::blob::encodings::succinctarchive::{
SuccinctArchiveBlob, WaveletMatrixFreezeBackend,
};
use triblespace_core::blob::Blob;
use triblespace_core::collection::{
CollectionAttachment, CollectionData, CollectionOperationError, MapMapping,
};
use triblespace_core::repo::StoreRead;
use triblespace_core::trible::Fragment;
pub struct BackendSuccinctMapping<B> {
backend: OnceLock<B>,
}
impl<B> BackendSuccinctMapping<B> {
pub fn new(backend: B) -> Self {
Self {
backend: OnceLock::from(backend),
}
}
}
impl<B> MapMapping for BackendSuccinctMapping<B>
where
B: WaveletMatrixFreezeBackend + Default,
B::Error: std::fmt::Display,
{
type Target = SuccinctArchiveBlob;
fn fragment(&self) -> Fragment {
<SuccinctArchiveBlob as CollectionAttachment>::fragment(&())
}
fn bind(parent: &Fragment, attached: &Fragment) -> Result<Self, CollectionOperationError> {
<SuccinctArchiveBlob as CollectionAttachment>::bind(parent, attached)?;
Ok(Self {
backend: OnceLock::new(),
})
}
fn map<R>(
&self,
node: &Blob<SimpleArchive>,
siblings: &[CollectionData],
reader: &R,
) -> Result<Blob<SuccinctArchiveBlob>, CollectionOperationError>
where
R: StoreRead,
{
match SuccinctArchiveBlob::build_from_simple_archive_with_backend(
node,
self.backend.get_or_init(B::default),
) {
Ok(output) => Ok(output),
Err(error) => {
tracing::warn!(%error, "Succinct backend build failed; retrying canonical CPU build");
<SuccinctArchiveBlob as CollectionAttachment>::map(&(), node, siblings, reader)
}
}
}
}
#[cfg(feature = "cuda")]
pub type CudaSuccinctMapping = BackendSuccinctMapping<crate::CudaWaveletFreeze>;