Skip to main content

alopex_core/txn/
mod.rs

1//! Transaction management traits.
2
3use crate::error::Result;
4use crate::types::TxnMode;
5
6/// Terminal input reported by a stream lease to its owning session.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum OwnedLeaseOutcome {
9    /// The result was fully consumed and leaves a transaction committable.
10    Exhausted,
11    /// The consumer ended early.  The generic core contract conservatively requires rollback.
12    Closed,
13    /// Cancellation or timeout invalidated the active operation.
14    Cancelled,
15    /// A source, conversion, or resource failure invalidated the active operation.
16    Failed,
17}
18
19/// One-way lifecycle state for an owned read-only session.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum OwnedReadSessionStatus {
22    /// The session is ready for its single stream lease.
23    Open,
24    /// One lease currently owns iterator advancement.
25    LeaseActive,
26    /// The stream ended normally and released its transaction.
27    Exhausted,
28    /// The consumer closed the stream before normal end.
29    Closed,
30    /// The consumer cancelled the stream or its deadline elapsed.
31    Cancelled,
32    /// Source cleanup failed after a stream error.
33    Failed,
34}
35
36impl OwnedReadSessionStatus {
37    /// Return whether this status has no legal later transition.
38    pub const fn is_terminal(self) -> bool {
39        matches!(
40            self,
41            Self::Exhausted | Self::Closed | Self::Cancelled | Self::Failed
42        )
43    }
44}
45
46impl From<OwnedLeaseOutcome> for OwnedReadSessionStatus {
47    fn from(outcome: OwnedLeaseOutcome) -> Self {
48        match outcome {
49            OwnedLeaseOutcome::Exhausted => Self::Exhausted,
50            OwnedLeaseOutcome::Closed => Self::Closed,
51            OwnedLeaseOutcome::Cancelled => Self::Cancelled,
52            OwnedLeaseOutcome::Failed => Self::Failed,
53        }
54    }
55}
56
57/// One-way lifecycle state for an owned transaction and its optional stream lease.
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59pub enum OwnedTransactionSessionStatus {
60    /// No stream lease is active; point operations and a final action are allowed.
61    Open,
62    /// Exactly one lease owns iterator advancement.  Commit and rollback are blocked.
63    LeaseActive,
64    /// A normally exhausted lease released the transaction for later commit or another lease.
65    Committable,
66    /// An early close, cancellation, failure, or dropped lease requires rollback.
67    MustAbort,
68    /// Commit completed exactly once.
69    Committed,
70    /// Rollback completed exactly once.
71    RolledBack,
72    /// A terminal action consumed the backend transaction but returned an error.
73    Closed,
74}
75
76impl OwnedTransactionSessionStatus {
77    /// Return whether a new lease may be started.
78    pub const fn can_acquire_lease(self) -> bool {
79        matches!(self, Self::Open | Self::Committable)
80    }
81
82    /// Return whether commit may consume the transaction.
83    pub const fn can_commit(self) -> bool {
84        matches!(self, Self::Open | Self::Committable)
85    }
86
87    /// Return whether rollback may consume the transaction.
88    pub const fn can_rollback(self) -> bool {
89        matches!(self, Self::Open | Self::Committable | Self::MustAbort)
90    }
91}
92
93impl From<OwnedLeaseOutcome> for OwnedTransactionSessionStatus {
94    fn from(outcome: OwnedLeaseOutcome) -> Self {
95        match outcome {
96            OwnedLeaseOutcome::Exhausted => Self::Committable,
97            OwnedLeaseOutcome::Closed
98            | OwnedLeaseOutcome::Cancelled
99            | OwnedLeaseOutcome::Failed => Self::MustAbort,
100        }
101    }
102}
103
104/// A manager for creating and committing transactions.
105///
106/// This trait is generic over the transaction type it manages.
107pub trait TxnManager<'a, T> {
108    /// Begins a new transaction in the specified mode.
109    fn begin(&'a self, mode: TxnMode) -> Result<T>;
110
111    /// Commits a transaction, applying its changes to the store.
112    fn commit(&'a self, txn: T) -> Result<()>;
113
114    /// Rolls back a transaction, discarding its changes.
115    fn rollback(&'a self, txn: T) -> Result<()>;
116}