use super::DocumentMapHandle;
use crate::RdtResult;
use std::sync::atomic::{AtomicBool, Ordering};
pub struct TransactionHandle {
map_handle: DocumentMapHandle,
committed: AtomicBool,
}
impl TransactionHandle {
pub(crate) fn new(map_handle: DocumentMapHandle) -> Self {
Self {
map_handle,
committed: AtomicBool::new(false),
}
}
pub fn commit(self) -> RdtResult<()> {
if self.committed.swap(true, Ordering::AcqRel) {
return Ok(());
}
self.map_handle.commit_transaction_internal()
}
pub fn is_active(&self) -> bool {
!self.committed.load(Ordering::Acquire)
}
}
impl Drop for TransactionHandle {
fn drop(&mut self) {
if !self.committed.swap(true, Ordering::AcqRel) {
if let Err(e) = self.map_handle.commit_transaction_internal() {
tracing::warn!("Failed to auto-commit transaction: {}", e);
}
}
}
}