Skip to main content

sim_lib_journal/
projection.rs

1use crate::{JournalEntry, Verification};
2use sim_kernel::ContentId;
3
4/// Detached row in a read-only projection.
5#[derive(Clone, Debug, PartialEq, Eq)]
6pub enum ProjectionRow {
7    Head { sequence: u64, entry: ContentId },
8    Entry(JournalEntry),
9    Object(ContentId),
10}
11
12/// Read-only flat Table-shaped snapshot. It exposes no mutation method and is
13/// never consulted by journal authority.
14#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct TableProjection {
16    rows: Vec<ProjectionRow>,
17}
18
19impl TableProjection {
20    pub(crate) fn from_verification(value: Verification) -> Self {
21        Self { rows: rows(value) }
22    }
23    pub fn rows(&self) -> &[ProjectionRow] {
24        &self.rows
25    }
26}
27
28/// Read-only Dir-shaped snapshot grouped by journal/object namespaces.
29#[derive(Clone, Debug, PartialEq, Eq)]
30pub struct DirProjection {
31    journal: Vec<ProjectionRow>,
32    objects: Vec<ProjectionRow>,
33}
34
35impl DirProjection {
36    pub(crate) fn from_verification(value: Verification) -> Self {
37        let all = rows(value);
38        let (objects, journal) = all
39            .into_iter()
40            .partition(|r| matches!(r, ProjectionRow::Object(_)));
41        Self { journal, objects }
42    }
43    pub fn journal(&self) -> &[ProjectionRow] {
44        &self.journal
45    }
46    pub fn objects(&self) -> &[ProjectionRow] {
47        &self.objects
48    }
49}
50
51fn rows(value: Verification) -> Vec<ProjectionRow> {
52    let mut rows = Vec::new();
53    if let Some(h) = value.head {
54        rows.push(ProjectionRow::Head {
55            sequence: h.sequence,
56            entry: h.entry,
57        });
58    }
59    rows.extend(value.entries.into_iter().map(ProjectionRow::Entry));
60    rows.extend(value.object_ids.into_iter().map(ProjectionRow::Object));
61    rows
62}