pub struct StorageTransactionLog { /* private fields */ }Expand description
ACID-like transaction log with commit, rollback, and replay support.
Committed transactions are stored in a bounded VecDeque. When the deque
exceeds max_committed, the oldest committed entry is evicted to bound memory
usage. Active transactions live in a separate HashMap so lookup is O(1).
All timestamps are caller-supplied logical clocks (e.g. milliseconds since
epoch), so the struct itself is Send + Sync and has no hidden I/O.
Implementations§
Source§impl StorageTransactionLog
impl StorageTransactionLog
Sourcepub fn new(max_committed: usize) -> Self
pub fn new(max_committed: usize) -> Self
Create a new transaction log.
max_committed controls how many committed Transaction records are
retained for replay. A value of 0 means no committed records are
retained (replay will always return an empty list).
Sourcepub fn begin(&mut self, now: u64) -> TransactionId
pub fn begin(&mut self, now: u64) -> TransactionId
Begin a new transaction and return its TransactionId.
now is the caller-supplied logical timestamp for started_at.
Sourcepub fn append(
&mut self,
tx_id: TransactionId,
op: TxOperation,
) -> Result<(), TxError>
pub fn append( &mut self, tx_id: TransactionId, op: TxOperation, ) -> Result<(), TxError>
Append an operation to an active transaction.
§Errors
TxError::TransactionNotFound– no transaction withtx_idexistsTxError::TransactionNotActive– the transaction is no longer active
Sourcepub fn commit(&mut self, tx_id: TransactionId, now: u64) -> Result<(), TxError>
pub fn commit(&mut self, tx_id: TransactionId, now: u64) -> Result<(), TxError>
Commit a transaction, moving it to the committed deque.
If the deque has reached max_committed, the oldest entry is evicted
before the new one is pushed.
§Errors
TxError::TransactionNotFound– no active transaction withtx_idTxError::TransactionAlreadyEnded– the transaction is not active
Sourcepub fn rollback(
&mut self,
tx_id: TransactionId,
now: u64,
) -> Result<Vec<TxOperation>, TxError>
pub fn rollback( &mut self, tx_id: TransactionId, now: u64, ) -> Result<Vec<TxOperation>, TxError>
Roll back a transaction, returning its operations in reverse order.
The caller is expected to use the returned operations to undo changes
(e.g. restore prev_value for Put/Delete entries).
§Errors
TxError::TransactionNotFound– no active transaction withtx_idTxError::TransactionAlreadyEnded– the transaction is not active
Sourcepub fn abort(&mut self, tx_id: TransactionId, now: u64) -> Result<(), TxError>
pub fn abort(&mut self, tx_id: TransactionId, now: u64) -> Result<(), TxError>
Abort a transaction without providing rollback data to the caller.
Unlike rollback, this method discards the operations
and does not return them. Use this when the transaction state is
already inconsistent or when rollback undo is handled elsewhere.
§Errors
TxError::TransactionNotFound– no active transaction withtx_idTxError::TransactionAlreadyEnded– the transaction is not active
Sourcepub fn replay_committed(&self, since_id: TransactionId) -> Vec<&Transaction>
pub fn replay_committed(&self, since_id: TransactionId) -> Vec<&Transaction>
Returns all committed transactions whose id is strictly greater than
since_id, in ascending id order.
This is the primary replay API: supply TransactionId(0) to replay from
the beginning of the retained window.
Sourcepub fn get_transaction(&self, tx_id: TransactionId) -> Option<&Transaction>
pub fn get_transaction(&self, tx_id: TransactionId) -> Option<&Transaction>
Look up a transaction by id.
Searches the active map first, then the committed deque. Returns None
if the transaction has been evicted or was never created.
Sourcepub fn active_transactions(&self) -> Vec<&Transaction>
pub fn active_transactions(&self) -> Vec<&Transaction>
Returns references to all currently active (in-flight) transactions, sorted by id for deterministic ordering.
Sourcepub fn active_count(&self) -> usize
pub fn active_count(&self) -> usize
Number of currently active transactions.
Sourcepub fn committed_count_total(&self) -> u64
pub fn committed_count_total(&self) -> u64
Cumulative count of all committed transactions since this log was created.
Sourcepub fn rolled_back_count_total(&self) -> u64
pub fn rolled_back_count_total(&self) -> u64
Cumulative count of all rolled-back transactions since this log was created.
Sourcepub fn stats(&self) -> TxStats
pub fn stats(&self) -> TxStats
Returns a statistics snapshot.
total_operations counts the sum of operations across all committed
transactions currently held in the deque (not across all historical
transactions).
Sourcepub fn committed_retained(&self) -> usize
pub fn committed_retained(&self) -> usize
Returns the number of committed transactions currently in the deque.
Sourcepub fn max_committed(&self) -> usize
pub fn max_committed(&self) -> usize
Returns the configured maximum number of retained committed transactions.
Sourcepub fn next_transaction_id(&self) -> TransactionId
pub fn next_transaction_id(&self) -> TransactionId
Returns the id that will be assigned to the next transaction.
Sourcepub fn drain_committed(&mut self) -> Vec<Transaction>
pub fn drain_committed(&mut self) -> Vec<Transaction>
Drain all committed transactions from the deque and return them.
This can be used to persist committed transactions to durable storage.
Auto Trait Implementations§
impl Freeze for StorageTransactionLog
impl RefUnwindSafe for StorageTransactionLog
impl Send for StorageTransactionLog
impl Sync for StorageTransactionLog
impl Unpin for StorageTransactionLog
impl UnsafeUnpin for StorageTransactionLog
impl UnwindSafe for StorageTransactionLog
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more