use uuid::Uuid;
use std::collections::VecDeque;
use std::sync::Mutex;
#[derive(Debug, Clone)]
pub struct TransactionLogEntry {
pub tx_id: Uuid,
pub session_id: Uuid,
pub committed_at: i64,
pub write_set: Vec<String>,
}
pub struct TransactionLog {
entries: Mutex<VecDeque<TransactionLogEntry>>,
max_entries: usize,
}
impl TransactionLog {
pub fn new(max_entries: usize) -> Self {
Self {
entries: Mutex::new(VecDeque::new()),
max_entries,
}
}
pub fn append(&self, entry: TransactionLogEntry) {
let mut log = self.entries.lock().expect("transaction log lock poisoned");
if log.len() >= self.max_entries {
log.pop_front();
}
log.push_back(entry);
}
pub fn snapshot(&self) -> Vec<TransactionLogEntry> {
self.entries
.lock()
.expect("transaction log lock poisoned")
.iter()
.cloned()
.collect()
}
pub fn len(&self) -> usize {
self.entries.lock().expect("transaction log lock poisoned").len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl Default for TransactionLog {
fn default() -> Self {
Self::new(10_000)
}
}