use oxgraph_layout_util::SnapshotWidth;
use oxgraph_snapshot::{PlanError, SnapshotWriter};
use crate::{
EncodedPropertySnapshot, IdFamily, IdentityModeRecord, PropertyIndex, PropertyLayer,
PropertySnapshotMetaWord, SNAPSHOT_PROPERTY_VERSION,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct LayerLengthError {
pub id_family: IdFamily,
pub required: usize,
pub actual: usize,
}
impl core::fmt::Display for LayerLengthError {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
formatter,
"{:?} property layer holds {} rows but the topology requires {}",
self.id_family, self.actual, self.required
)
}
}
impl core::error::Error for LayerLengthError {}
pub fn validate_layer_lengths<Id, I>(
layers: &[PropertyLayer<Id, I>],
id_family: IdFamily,
required: usize,
) -> Result<(), LayerLengthError>
where
I: PropertyIndex,
{
for layer in layers {
if layer.len() < required {
return Err(LayerLengthError {
id_family,
required,
actual: layer.len(),
});
}
}
Ok(())
}
pub fn append_identity_and_property_sections<W, MapW>(
writer: &mut SnapshotWriter,
identity_modes: &[IdentityModeRecord<W>],
identity_map_kind: u32,
identity_map: &[MapW],
encoded: &EncodedPropertySnapshot,
) -> Result<(), PlanError>
where
W: PropertySnapshotMetaWord,
MapW: SnapshotWidth,
{
writer.section_bytes(
W::PROPERTY_DESCRIPTORS_KIND,
SNAPSHOT_PROPERTY_VERSION,
0,
&encoded.descriptors,
)?;
writer.section_bytes(
W::PROPERTY_DATA_KIND,
SNAPSHOT_PROPERTY_VERSION,
0,
&encoded.data,
)?;
writer.section_little_endian(
W::IDENTITY_MODES_KIND,
SNAPSHOT_PROPERTY_VERSION,
identity_modes,
)?;
writer.section_widths(identity_map_kind, SNAPSHOT_PROPERTY_VERSION, identity_map)?;
Ok(())
}