|
|
|
|
@ -7,7 +7,12 @@ use std::{future::Future, pin::Pin, sync::Arc};
@@ -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 {
@@ -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 {
@@ -124,6 +130,7 @@ pub struct WriteCache {
|
|
|
|
|
add_cache: BTreeMap<String, BTreeMap<Vec<u8>, Vec<u8>>>, |
|
|
|
|
remove_cache: BTreeMap<String, BTreeSet<Vec<u8>>>, |
|
|
|
|
changes_count: i32, |
|
|
|
|
last_flush: Instant, |
|
|
|
|
db: persy::Persy, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -228,6 +235,7 @@ impl WriteCache {
@@ -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 {
@@ -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")] |
|
|
|
|
|