use crate::error::Result;
use crate::types::TxnMode;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OwnedLeaseOutcome {
Exhausted,
Closed,
Cancelled,
Failed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OwnedReadSessionStatus {
Open,
LeaseActive,
Exhausted,
Closed,
Cancelled,
Failed,
}
impl OwnedReadSessionStatus {
pub const fn is_terminal(self) -> bool {
matches!(
self,
Self::Exhausted | Self::Closed | Self::Cancelled | Self::Failed
)
}
}
impl From<OwnedLeaseOutcome> for OwnedReadSessionStatus {
fn from(outcome: OwnedLeaseOutcome) -> Self {
match outcome {
OwnedLeaseOutcome::Exhausted => Self::Exhausted,
OwnedLeaseOutcome::Closed => Self::Closed,
OwnedLeaseOutcome::Cancelled => Self::Cancelled,
OwnedLeaseOutcome::Failed => Self::Failed,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OwnedTransactionSessionStatus {
Open,
LeaseActive,
Committable,
MustAbort,
Committed,
RolledBack,
Closed,
}
impl OwnedTransactionSessionStatus {
pub const fn can_acquire_lease(self) -> bool {
matches!(self, Self::Open | Self::Committable)
}
pub const fn can_commit(self) -> bool {
matches!(self, Self::Open | Self::Committable)
}
pub const fn can_rollback(self) -> bool {
matches!(self, Self::Open | Self::Committable | Self::MustAbort)
}
}
impl From<OwnedLeaseOutcome> for OwnedTransactionSessionStatus {
fn from(outcome: OwnedLeaseOutcome) -> Self {
match outcome {
OwnedLeaseOutcome::Exhausted => Self::Committable,
OwnedLeaseOutcome::Closed
| OwnedLeaseOutcome::Cancelled
| OwnedLeaseOutcome::Failed => Self::MustAbort,
}
}
}
pub trait TxnManager<'a, T> {
fn begin(&'a self, mode: TxnMode) -> Result<T>;
fn commit(&'a self, txn: T) -> Result<()>;
fn rollback(&'a self, txn: T) -> Result<()>;
}