Skip to main content

sim_lib_journal/
backend.rs

1use crate::{JournalEntry, JournalError, JournalHead, JournalObject, Lease};
2use sim_kernel::ContentId;
3use std::collections::BTreeMap;
4use std::sync::Arc;
5
6/// A consistent backend snapshot used for verification and replay.
7#[derive(Clone, Debug, Default)]
8pub struct StoredState {
9    pub objects: BTreeMap<ContentId, Vec<u8>>,
10    pub entries: BTreeMap<u64, JournalEntry>,
11    pub head: Option<JournalHead>,
12}
13
14/// One atomic admission request. Implementations publish immutable objects and
15/// compare the head/fence in the same linearized operation.
16pub struct Admission {
17    pub fence: u64,
18    pub expected: Option<JournalHead>,
19    pub objects: Vec<JournalObject>,
20    pub entries: Vec<JournalEntry>,
21}
22
23/// Object-safe storage seam. A Table backend implements `admit` with canonical
24/// `table/cas`; it must refuse writes unless CAS and durability are provable.
25pub trait JournalBackend: Send + Sync {
26    fn acquire_lease(&self) -> Result<Lease, JournalError>;
27    fn read_state(&self) -> Result<StoredState, JournalError>;
28    fn admit(&self, admission: Admission) -> Result<JournalHead, JournalError>;
29}
30
31impl<T: JournalBackend + ?Sized> JournalBackend for Arc<T> {
32    fn acquire_lease(&self) -> Result<Lease, JournalError> {
33        (**self).acquire_lease()
34    }
35    fn read_state(&self) -> Result<StoredState, JournalError> {
36        (**self).read_state()
37    }
38    fn admit(&self, admission: Admission) -> Result<JournalHead, JournalError> {
39        (**self).admit(admission)
40    }
41}