From 7c8b05c3bf9c21aadb29751dc98fd3b8cacef2de Mon Sep 17 00:00:00 2001 From: Tglman Date: Wed, 23 Jun 2021 21:17:51 +0100 Subject: [PATCH] add timed flush logic --- src/database/abstraction.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/database/abstraction.rs b/src/database/abstraction.rs index 9ed7dc9..2267a30 100644 --- a/src/database/abstraction.rs +++ b/src/database/abstraction.rs @@ -7,7 +7,12 @@ use std::{future::Future, pin::Pin, sync::Arc}; use std::{collections::BTreeMap, sync::RwLock}; #[cfg(feature = "persy")] -use std::{cmp::Ordering, collections::BTreeSet, iter::Peekable}; +use std::{ + cmp::Ordering, + collections::BTreeSet, + iter::Peekable, + time::{Duration, Instant}, +}; #[cfg(feature = "sled")] pub mod sled; @@ -89,6 +94,7 @@ impl DatabaseEngine for PersyEngine { add_cache: Default::default(), remove_cache: Default::default(), changes_count: Default::default(), + last_flush: Instant::now(), db: persy.clone(), })); Ok(Arc::new(PersyEngine(persy, write_cache))) @@ -124,6 +130,7 @@ pub struct WriteCache { add_cache: BTreeMap, Vec>>, remove_cache: BTreeMap>>, changes_count: i32, + last_flush: Instant, db: persy::Persy, } @@ -228,6 +235,7 @@ impl WriteCache { } self.remove_cache.clear(); tx.prepare()?.commit()?; + self.last_flush = Instant::now(); Ok(()) } @@ -318,6 +326,16 @@ impl WriteCache { } iter } + + #[allow(unused)] + pub fn flush_timed(&mut self) -> Result<()> { + if self.changes_count > 0 { + if Instant::now() - self.last_flush > Duration::from_secs(2) { + self.flush_changes()?; + } + } + Ok(()) + } } #[cfg(feature = "persy")]