Files
continuwuity/src/database/cork.rs
T

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

43 lines
783 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 {
2025-09-09 18:12:38 +05:00
engine: Arc<Engine>,
2024-04-06 01:24:08 -07:00
flush: bool,
sync: bool,
}
impl Database {
#[inline]
#[must_use]
2025-09-09 18:12:38 +05:00
pub fn cork(&self) -> Cork { Cork::new(&self.engine, false, false) }
#[inline]
#[must_use]
2025-09-09 18:12:38 +05:00
pub fn cork_and_flush(&self) -> Cork { Cork::new(&self.engine, true, false) }
#[inline]
#[must_use]
2025-09-09 18:12:38 +05:00
pub fn cork_and_sync(&self) -> Cork { Cork::new(&self.engine, true, true) }
}
2024-04-06 01:24:08 -07:00
impl Cork {
#[inline]
2025-09-09 18:12:38 +05:00
pub(super) fn new(engine: &Arc<Engine>, flush: bool, sync: bool) -> Self {
engine.cork();
Self { engine: engine.clone(), flush, sync }
2024-04-06 01:24:08 -07:00
}
}
impl Drop for Cork {
fn drop(&mut self) {
2025-09-09 18:12:38 +05:00
self.engine.uncork();
2024-04-06 01:24:08 -07:00
if self.flush {
2025-09-09 18:12:38 +05:00
self.engine.flush().ok();
2024-04-06 01:24:08 -07:00
}
if self.sync {
2025-09-09 18:12:38 +05:00
self.engine.sync().ok();
2024-04-06 01:24:08 -07:00
}
}
}