#[derive(Debug, Clone)]
pub struct TransactionState {
pub tx_prev_outgoing_cluster_offset: u64,
pub tx_prev_incoming_cluster_offset: u64,
pub tx_prev_free_space_offset: u64,
pub tx_id: u64,
}
impl TransactionState {
pub fn new() -> Self {
Self {
tx_prev_outgoing_cluster_offset: 0,
tx_prev_incoming_cluster_offset: 0,
tx_prev_free_space_offset: 0,
tx_id: 0,
}
}
pub fn begin_tx(&mut self, current_tx_id: u64) {
self.tx_id = current_tx_id;
}
pub fn save_checkpoint(
&mut self,
outgoing_offset: u64,
incoming_offset: u64,
free_space_offset: u64,
) {
self.tx_prev_outgoing_cluster_offset = outgoing_offset;
self.tx_prev_incoming_cluster_offset = incoming_offset;
self.tx_prev_free_space_offset = free_space_offset;
}
pub fn is_in_progress(&self) -> bool {
self.tx_id > 0
}
pub fn rollback(&mut self) -> (u64, u64, u64) {
(
self.tx_prev_outgoing_cluster_offset,
self.tx_prev_incoming_cluster_offset,
self.tx_prev_free_space_offset,
)
}
pub fn commit(&mut self) {
self.tx_id = 0;
self.tx_prev_outgoing_cluster_offset = 0;
self.tx_prev_incoming_cluster_offset = 0;
self.tx_prev_free_space_offset = 0;
}
pub fn current_transaction_id(&self) -> u64 {
self.tx_id
}
pub fn is_active(&self) -> bool {
self.is_in_progress()
}
}
impl Default for TransactionState {
fn default() -> Self {
Self::new()
}
}