use oxgraph_snapshot::{Section, Snapshot};
use crate::{
error::{BcsrSection, BcsrSnapshotError},
internal::{
validation::BcsrValidation,
view::{BcsrHypergraph, BcsrSections},
},
word::{BcsrSnapshotIndex, BcsrSnapshotWord},
};
type SnapshotSections<'view, VertexIndex, RelationIndex, IncidenceIndex> = BcsrSections<
'view,
<IncidenceIndex as BcsrSnapshotIndex>::LittleEndianWord,
<VertexIndex as BcsrSnapshotIndex>::LittleEndianWord,
<RelationIndex as BcsrSnapshotIndex>::LittleEndianWord,
>;
impl<'view, VertexIndex, RelationIndex, IncidenceIndex>
BcsrHypergraph<
'view,
VertexIndex,
RelationIndex,
IncidenceIndex,
<IncidenceIndex as BcsrSnapshotIndex>::LittleEndianWord,
<VertexIndex as BcsrSnapshotIndex>::LittleEndianWord,
<RelationIndex as BcsrSnapshotIndex>::LittleEndianWord,
>
where
VertexIndex: BcsrSnapshotIndex,
RelationIndex: BcsrSnapshotIndex,
IncidenceIndex: BcsrSnapshotIndex,
{
pub fn from_snapshot(snapshot: &Snapshot<'view>) -> Result<Self, BcsrSnapshotError> {
Self::from_snapshot_with(snapshot, BcsrValidation::Layout)
}
pub fn from_snapshot_with(
snapshot: &Snapshot<'view>,
level: BcsrValidation,
) -> Result<Self, BcsrSnapshotError> {
let sections = load_sections::<VertexIndex, RelationIndex, IncidenceIndex>(snapshot)?;
Ok(Self::open_with(sections, level)?)
}
}
fn load_sections<'view, VertexIndex, RelationIndex, IncidenceIndex>(
snapshot: &Snapshot<'view>,
) -> Result<SnapshotSections<'view, VertexIndex, RelationIndex, IncidenceIndex>, BcsrSnapshotError>
where
VertexIndex: BcsrSnapshotIndex,
RelationIndex: BcsrSnapshotIndex,
IncidenceIndex: BcsrSnapshotIndex,
{
let head_offsets = load_section(
snapshot,
BcsrSection::HeadOffsets,
IncidenceIndex::HEAD_OFFSETS_KIND,
)?;
let head_participants = load_section(
snapshot,
BcsrSection::HeadParticipants,
VertexIndex::HEAD_PARTICIPANTS_KIND,
)?;
let tail_offsets = load_section(
snapshot,
BcsrSection::TailOffsets,
IncidenceIndex::TAIL_OFFSETS_KIND,
)?;
let tail_participants = load_section(
snapshot,
BcsrSection::TailParticipants,
VertexIndex::TAIL_PARTICIPANTS_KIND,
)?;
let vertex_outgoing_offsets = load_section(
snapshot,
BcsrSection::VertexOutgoingOffsets,
IncidenceIndex::VERTEX_OUTGOING_OFFSETS_KIND,
)?;
let vertex_outgoing_hyperedges = load_section(
snapshot,
BcsrSection::VertexOutgoingHyperedges,
RelationIndex::VERTEX_OUTGOING_HYPEREDGES_KIND,
)?;
let vertex_incoming_offsets = load_section(
snapshot,
BcsrSection::VertexIncomingOffsets,
IncidenceIndex::VERTEX_INCOMING_OFFSETS_KIND,
)?;
let vertex_incoming_hyperedges = load_section(
snapshot,
BcsrSection::VertexIncomingHyperedges,
RelationIndex::VERTEX_INCOMING_HYPEREDGES_KIND,
)?;
Ok(BcsrSections {
head_offsets,
head_participants,
tail_offsets,
tail_participants,
vertex_outgoing_offsets,
vertex_outgoing_hyperedges,
vertex_incoming_offsets,
vertex_incoming_hyperedges,
})
}
fn load_section<'view, Word>(
snapshot: &Snapshot<'view>,
section: BcsrSection,
kind: u32,
) -> Result<&'view [Word], BcsrSnapshotError>
where
Word: BcsrSnapshotWord,
{
let payload = lookup_section(snapshot, section, kind)?;
payload
.try_as_slice()
.map_err(|error| BcsrSnapshotError::SectionView { section, error })
}
fn lookup_section<'view>(
snapshot: &Snapshot<'view>,
section: BcsrSection,
kind: u32,
) -> Result<Section<'view>, BcsrSnapshotError> {
snapshot
.section(kind)
.ok_or(BcsrSnapshotError::MissingSection { section, kind })
}