Skip to main content

sim_lib_journal/
snapshot.rs

1use std::collections::BTreeMap;
2
3use sim_kernel::{ContentId, Datum};
4
5use crate::{JournalEntry, JournalHead, StoredState, Verification, verify::verify_state};
6
7/// One internally consistent, fully verified semantic view of a journal read.
8///
9/// The snapshot contains only logical entries and the canonical Datums they
10/// retain. Physical bytes, storage locators, and backend state stay private to
11/// the journal implementation.
12#[derive(Clone, Debug, PartialEq, Eq)]
13pub struct VerifiedSnapshot {
14    head: Option<JournalHead>,
15    entries: Vec<JournalEntry>,
16    datums: BTreeMap<ContentId, Datum>,
17}
18
19impl VerifiedSnapshot {
20    pub(crate) fn from_state(state: StoredState) -> Result<Self, crate::JournalError> {
21        let Verification {
22            head,
23            entries,
24            object_ids,
25        } = verify_state(&state)?;
26        let datums = object_ids
27            .into_iter()
28            .map(|id| {
29                let datum = state
30                    .datums
31                    .get(&id)
32                    .cloned()
33                    .ok_or_else(|| crate::JournalError::MissingSemanticObject(id.clone()))?;
34                Ok((id, datum))
35            })
36            .collect::<Result<_, crate::JournalError>>()?;
37        Ok(Self {
38            head,
39            entries,
40            datums,
41        })
42    }
43
44    /// Returns the verified head from the same backend read as the entries.
45    pub const fn head(&self) -> Option<&JournalHead> {
46        self.head.as_ref()
47    }
48
49    /// Returns the complete verified entry chain in sequence order.
50    pub fn entries(&self) -> &[JournalEntry] {
51        &self.entries
52    }
53
54    /// Resolves one retained semantic object from this exact snapshot.
55    pub fn datum(&self, id: &ContentId) -> Option<&Datum> {
56        self.datums.get(id)
57    }
58
59    /// Returns every retained semantic object keyed by its canonical identity.
60    pub const fn datums(&self) -> &BTreeMap<ContentId, Datum> {
61        &self.datums
62    }
63}