Files
continuwuity/src/database/cork.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

44 lines
739 B
Rust
Raw Normal View History

2024-04-06 01:24:08 -07:00
use std::sync::Arc;
use crate::{Database, Engine};
2024-04-06 01:24:08 -07:00
2024-05-09 15:59:08 -07:00
pub struct Cork {
2024-05-28 06:59:50 +00:00
db: Arc<Engine>,
2024-04-06 01:24:08 -07:00
flush: bool,
sync: bool,
}
impl Database {
#[inline]
#[must_use]
pub fn cork(&self) -> Cork { Cork::new(&self.db, false, false) }
#[inline]
#[must_use]
pub fn cork_and_flush(&self) -> Cork { Cork::new(&self.db, true, false) }
#[inline]
#[must_use]
pub fn cork_and_sync(&self) -> Cork { Cork::new(&self.db, true, true) }
}
2024-04-06 01:24:08 -07:00
impl Cork {
#[inline]
2024-07-01 20:54:38 +00:00
pub(super) fn new(db: &Arc<Engine>, flush: bool, sync: bool) -> Self {
2024-05-28 06:59:50 +00:00
db.cork();
Self { db: db.clone(), flush, sync }
2024-04-06 01:24:08 -07:00
}
}
impl Drop for Cork {
fn drop(&mut self) {
2024-05-28 06:59:50 +00:00
self.db.uncork();
2024-04-06 01:24:08 -07:00
if self.flush {
self.db.flush().ok();
}
if self.sync {
self.db.sync().ok();
}
}
}