use crate::{Db, DbResult};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use std::thread::JoinHandle;
use std::time::Duration;
#[derive(Default, Clone)]
pub struct CancelSig(Arc<AtomicBool>);
impl CancelSig {
pub fn cancel(&self) {
self.0.store(true, Ordering::Relaxed);
}
pub fn is_canceled(&self) -> bool {
self.0.load(Ordering::Relaxed)
}
}
pub trait BackgroundCompacter {
fn begin_compacter(&self, freq: Duration, cancel: CancelSig) -> JoinHandle<DbResult<usize>>;
}
impl<D> BackgroundCompacter for Arc<Mutex<D>>
where
D: Db + Send + Sync + 'static,
{
fn begin_compacter(&self, freq: Duration, cancel: CancelSig) -> JoinHandle<DbResult<usize>> {
let db = self.clone();
thread::spawn(move || {
let mut count = 0;
loop {
thread::sleep(freq);
if cancel.is_canceled() {
return Ok(count);
}
db.lock()?.compact_log()?;
count += 1;
}
})
}
}