mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-30 14:34:22 +01:00
add helper WalSession RAII-wrapper
This commit is contained in:
40
packages/turso-sync/src/wal_session.rs
Normal file
40
packages/turso-sync/src/wal_session.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use crate::Result;
|
||||
|
||||
pub struct WalSession<'a> {
|
||||
conn: &'a turso::Connection,
|
||||
in_txn: bool,
|
||||
}
|
||||
|
||||
impl<'a> WalSession<'a> {
|
||||
pub fn new(conn: &'a turso::Connection) -> Self {
|
||||
Self {
|
||||
conn,
|
||||
in_txn: false,
|
||||
}
|
||||
}
|
||||
pub fn begin(&mut self) -> Result<()> {
|
||||
assert!(!self.in_txn);
|
||||
self.conn.wal_insert_begin()?;
|
||||
self.in_txn = true;
|
||||
Ok(())
|
||||
}
|
||||
pub fn end(&mut self) -> Result<()> {
|
||||
assert!(self.in_txn);
|
||||
self.conn.wal_insert_end()?;
|
||||
self.in_txn = false;
|
||||
Ok(())
|
||||
}
|
||||
pub fn in_txn(&self) -> bool {
|
||||
self.in_txn
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for WalSession<'a> {
|
||||
fn drop(&mut self) {
|
||||
if self.in_txn {
|
||||
let _ = self
|
||||
.end()
|
||||
.inspect_err(|e| tracing::error!("failed to close WAL session: {}", e));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user