Skip to main content

tuwunel_database/
cork.rs

1use std::sync::Arc;
2
3use crate::{Database, Engine};
4
5#[clippy::has_significant_drop]
6pub struct Cork {
7	engine: Arc<Engine>,
8	flush: bool,
9	sync: bool,
10}
11
12impl Database {
13	#[inline]
14	#[must_use]
15	pub fn cork(&self) -> Cork { Cork::new(&self.engine, false, false) }
16
17	#[inline]
18	#[must_use]
19	pub fn cork_and_flush(&self) -> Cork { Cork::new(&self.engine, true, false) }
20
21	#[inline]
22	#[must_use]
23	pub fn cork_and_sync(&self) -> Cork { Cork::new(&self.engine, true, true) }
24}
25
26impl Cork {
27	#[inline]
28	pub(super) fn new(engine: &Arc<Engine>, flush: bool, sync: bool) -> Self {
29		engine.cork();
30		Self { engine: engine.clone(), flush, sync }
31	}
32}
33
34impl Drop for Cork {
35	fn drop(&mut self) {
36		self.engine.uncork();
37		if self.flush {
38			self.engine.flush().ok();
39		}
40		if self.sync {
41			self.engine.sync().ok();
42		}
43	}
44}