Skip to main content

sim_lib_journal/
backend.rs

1// SPDX-License-Identifier: MPL-2.0
2// This Source Code Form is subject to the terms of the Mozilla Public
3// License, v. 2.0. If a copy of the MPL was not distributed with this
4// file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
6use crate::{
7    CommitAction, GuardedAdmission, JournalEntry, JournalError, JournalHead, JournalObject, Lease,
8    StoredDatumRef,
9};
10use sim_kernel::{ContentId, Datum};
11use std::collections::BTreeMap;
12use std::sync::Arc;
13
14/// A consistent backend snapshot used for verification and replay.
15#[derive(Clone, Debug, Default)]
16pub struct StoredState {
17    pub objects: BTreeMap<ContentId, Vec<u8>>,
18    /// Verified semantic values keyed by their kernel Datum identity.
19    pub datums: BTreeMap<ContentId, Datum>,
20    pub entries: BTreeMap<u64, JournalEntry>,
21    pub head: Option<JournalHead>,
22}
23
24/// One atomic admission request. Implementations publish immutable objects and
25/// compare the head/fence in the same linearized operation.
26///
27/// Every field is `pub(crate)`: the only place inside this crate that
28/// constructs one is [`Journal::verified_admission`](crate::Journal), which
29/// runs `verify::verify_state`/`verify::verify_batch` first. A backend
30/// receives an `Admission` only as a trait-method parameter and reads it
31/// through the accessors below; it cannot build one from scratch. This closes
32/// the direct-bypass path where a caller outside this crate could once
33/// construct an unverified `Admission` (any fence, any entries, no gapless or
34/// predecessor check) and hand it straight to [`JournalBackend::admit`] or
35/// [`JournalBackend::admit_then`], skipping every journal-law check that
36/// `Journal` exists to enforce.
37pub struct Admission {
38    pub(crate) fence: u64,
39    pub(crate) expected: Option<JournalHead>,
40    pub(crate) objects: Vec<JournalObject>,
41    pub(crate) entries: Vec<JournalEntry>,
42}
43
44impl Admission {
45    /// Returns the fence this admission was verified against.
46    pub const fn fence(&self) -> u64 {
47        self.fence
48    }
49
50    /// Returns the head this admission was verified to extend, if any.
51    pub const fn expected(&self) -> Option<&JournalHead> {
52        self.expected.as_ref()
53    }
54
55    /// Returns the immutable objects this admission publishes.
56    pub fn objects(&self) -> &[JournalObject] {
57        &self.objects
58    }
59
60    /// Returns the ordered entry batch this admission publishes.
61    pub fn entries(&self) -> &[JournalEntry] {
62        &self.entries
63    }
64}
65
66/// Object-safe storage seam. A Table backend implements `admit` with canonical
67/// `table/cas`; it must refuse writes unless CAS and durability are provable.
68pub trait JournalBackend: Send + Sync {
69    fn acquire_lease(&self) -> Result<Lease, JournalError>;
70    fn read_state(&self) -> Result<StoredState, JournalError>;
71    fn admit(&self, admission: Admission) -> Result<JournalHead, JournalError>;
72    /// Admits a new batch and invokes a short continuation before releasing
73    /// exclusion against every head/fence mutation. Exact redelivery does not
74    /// invoke the continuation. Unsupported backends refuse before committing.
75    fn admit_then(
76        &self,
77        _admission: Admission,
78        _action: &mut dyn CommitAction,
79    ) -> Result<GuardedAdmission, JournalError> {
80        Err(JournalError::WriteRefused("guarded admission unsupported"))
81    }
82    /// Durably publishes one immutable semantic object without making it a
83    /// journal retention root.
84    fn put_datum(&self, object: JournalObject) -> Result<StoredDatumRef, JournalError>;
85    /// Resolves one semantic object by meaning, returning an owned Datum.
86    fn get_datum(&self, meaning: &ContentId) -> Result<Datum, JournalError>;
87    /// Rebuilds and verifies the derived semantic-to-storage correspondence.
88    fn rebuild_datum_index(&self) -> Result<Vec<StoredDatumRef>, JournalError>;
89}
90
91impl<T: JournalBackend + ?Sized> JournalBackend for Arc<T> {
92    fn acquire_lease(&self) -> Result<Lease, JournalError> {
93        (**self).acquire_lease()
94    }
95    fn read_state(&self) -> Result<StoredState, JournalError> {
96        (**self).read_state()
97    }
98    fn admit(&self, admission: Admission) -> Result<JournalHead, JournalError> {
99        (**self).admit(admission)
100    }
101    fn admit_then(
102        &self,
103        admission: Admission,
104        action: &mut dyn CommitAction,
105    ) -> Result<GuardedAdmission, JournalError> {
106        (**self).admit_then(admission, action)
107    }
108    fn put_datum(&self, object: JournalObject) -> Result<StoredDatumRef, JournalError> {
109        (**self).put_datum(object)
110    }
111    fn get_datum(&self, meaning: &ContentId) -> Result<Datum, JournalError> {
112        (**self).get_datum(meaning)
113    }
114    fn rebuild_datum_index(&self) -> Result<Vec<StoredDatumRef>, JournalError> {
115        (**self).rebuild_datum_index()
116    }
117}