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