Skip to main content

sim_lib_journal/
lib.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
6//! Domain-free atomic journal behavior over content-addressed objects.
7//!
8//! A journal publishes immutable objects before making entries visible and
9//! advances its head through one fenced compare-and-swap transition. Backends
10//! implement [`JournalBackend::admit`]; callers use [`Journal`] so the gapless,
11//! closure, redelivery, and replay laws are enforced once.
12
13mod admission;
14mod backend;
15mod datum_codec;
16mod entry;
17mod head;
18mod host;
19mod lease;
20mod memory;
21mod native_codec;
22mod object;
23mod persistent;
24mod projection;
25mod replay;
26mod snapshot;
27mod verify;
28
29pub use admission::{AdmissionDisposition, CommitAction, GuardedAdmission};
30pub use backend::{Admission, JournalBackend, StoredState};
31pub use entry::JournalEntry;
32pub use head::JournalHead;
33pub use host::HostDirJournalBackend;
34pub use lease::Lease;
35pub use memory::MemoryBackend;
36pub use native_codec::{
37    BackendCapabilities, EntryLocation, EntryNamespace, Failpoint, NativeFormatId,
38    NativeStateEnvelope, VerifiedNativePrefixRef,
39};
40pub use object::JournalObject;
41pub use persistent::{
42    PersistentObjectStore, PersistentSemanticObjects, StoreError, StoredDatumRef,
43};
44pub use projection::{DirProjection, ProjectionRow, TableProjection};
45pub use replay::{Replay, replay};
46pub use snapshot::VerifiedSnapshot;
47pub use verify::{JournalError, Verification};
48
49use sim_kernel::{ContentId, Symbol};
50
51/// The single state-machine implementation shared by every backend.
52pub struct Journal<B> {
53    backend: B,
54}
55
56impl<B: JournalBackend> Journal<B> {
57    /// Wraps a backend. Durable production backends must report write safety.
58    pub fn new(backend: B) -> Self {
59        Self { backend }
60    }
61
62    /// Obtains a new fencing generation, invalidating every older lease.
63    pub fn acquire_lease(&self) -> Result<Lease, JournalError> {
64        self.backend.acquire_lease()
65    }
66
67    /// Returns the current verified head.
68    pub fn head(&self) -> Result<Option<JournalHead>, JournalError> {
69        let state = self.backend.read_state()?;
70        verify::verify_state(&state).map(|v| v.head)
71    }
72
73    /// Atomically publishes objects and an ordered entry batch under one fence.
74    ///
75    /// Exact redelivery is a no-op. Any conflicting delivery, missing/corrupt
76    /// payload, stale fence, sequence gap, or wrong predecessor is rejected.
77    pub fn publish(
78        &self,
79        lease: &Lease,
80        expected: Option<&JournalHead>,
81        objects: Vec<JournalObject>,
82        entries: Vec<JournalEntry>,
83    ) -> Result<JournalHead, JournalError> {
84        self.backend
85            .admit(self.verified_admission(lease, expected, objects, entries)?)
86    }
87
88    /// Reads and verifies the complete journal closure.
89    pub fn verify(&self) -> Result<Verification, JournalError> {
90        verify::verify_state(&self.backend.read_state()?)
91    }
92
93    /// Replays verified entries in sequence order.
94    pub fn replay(&self) -> Result<Replay, JournalError> {
95        replay(self.backend.read_state()?)
96    }
97
98    /// Reads and verifies one internally consistent semantic snapshot.
99    ///
100    /// Consumers that must reduce entries together with their payload values
101    /// use this method so both come from the same backend read. The returned
102    /// value exposes no physical storage identity or mutable backend state.
103    pub fn verified_snapshot(&self) -> Result<VerifiedSnapshot, JournalError> {
104        VerifiedSnapshot::from_state(self.backend.read_state()?)
105    }
106
107    /// Creates a detached, read-only table projection.
108    pub fn table_projection(&self) -> Result<TableProjection, JournalError> {
109        Ok(TableProjection::from_verification(self.verify()?))
110    }
111
112    /// Creates a detached, read-only directory projection.
113    pub fn dir_projection(&self) -> Result<DirProjection, JournalError> {
114        Ok(DirProjection::from_verification(self.verify()?))
115    }
116
117    /// Convenience constructor for an entry with an open kind symbol.
118    pub fn entry(
119        sequence: u64,
120        previous: Option<ContentId>,
121        kind: Symbol,
122        payloads: Vec<ContentId>,
123    ) -> JournalEntry {
124        JournalEntry::new(sequence, previous, kind, payloads)
125    }
126}
127
128#[cfg(test)]
129mod tests;