Skip to main content

fasm_storage/
commit.rs

1//! The [`Commit`] trait: finalizing one storage session.
2
3use core::error::Error;
4use core::future::Future;
5
6use crate::error::RetryableStorageError;
7use crate::maybe_send::{MaybeSend, MaybeSync};
8
9/// Finalizes the writes performed through a mutable storage handle.
10///
11/// `Commit` is deliberately **separate from [`KvStore`]**. A read-only handle
12/// and a write handle can both be `KvStore`s; only the write handle is a
13/// *session* that has to be closed out. Keeping the two apart lets a caller
14/// accept `KV: KvStore` where it only reads, and `KV: KvStore + Commit` where
15/// it owns the session, and have the compiler enforce the difference.
16///
17/// # The atomicity rule
18///
19/// **One state-transition invocation = one storage session = one atomic
20/// commit.** Everything a transition writes lands together or not at all.
21/// Dropping a session before invoking `commit` is the rollback. Once `commit`
22/// has been invoked, however, a non-retryable error may leave the caller unable
23/// to tell whether the atomic commit happened.
24///
25/// `commit` **consumes `self`**, which is the whole point. A session cannot be
26/// committed twice, cannot be written to after commit, and cannot be
27/// accidentally shared across two transitions — the type system says so rather
28/// than the documentation.
29///
30/// # In-memory backends
31///
32/// The in-memory `fasm-storage-btreemap` backend provides the same buffering
33/// and rollback as the durable backends: `commit` applies the buffered writes
34/// to the in-process base map, and dropping the transaction rolls them back.
35/// It provides no crash atomicity — the base map is memory, so a crash loses
36/// it — and it belongs in tests and simulations only.
37///
38/// [`KvStore`]: crate::KvStore
39pub trait Commit {
40    /// Error type produced by the commit itself.
41    ///
42    /// An error for which [`RetryableStorageError::is_retryable`] is `true`
43    /// guarantees that this commit attempt did not commit and is safe to replay
44    /// from a fresh session. An error for which it is `false` makes no such
45    /// guarantee: the outcome may be unknown, as with a lost acknowledgement
46    /// after a durable commit. Callers must reconcile persisted state before
47    /// replaying such an attempt.
48    ///
49    /// FoundationDB's `retryable_not_committed` distinction is the precedent:
50    /// backends should map the equivalent guarantee onto `is_retryable`.
51    type Error: Error + RetryableStorageError + MaybeSend + MaybeSync + 'static;
52
53    /// Finalize every write performed through this handle, atomically.
54    ///
55    /// On `Ok(())` all writes are durable and visible. On `Err(err)`, replay is
56    /// safe only when [`RetryableStorageError::is_retryable`] returns `true`,
57    /// which guarantees that the attempt did not commit. A `false` result may
58    /// mean the outcome is unknown; reconcile persisted state before replaying.
59    ///
60    /// On native targets [`MaybeSend`] has `Send` as a supertrait, so trait
61    /// elaboration still gives callers a `Send` future that can cross task
62    /// boundaries. In a browser it does not require `Send`, allowing a commit
63    /// future to retain thread-local Web API handles.
64    fn commit(self) -> impl Future<Output = Result<(), Self::Error>> + MaybeSend;
65}