use crate::pool::DEFAULT_POOL_SIZE;
use std::time::Duration;
pub(crate) const DEFAULT_RESET_THRESHOLD: usize = 100;
pub(crate) const DEFAULT_GC_INTERVAL: Duration = Duration::from_millis(500);
pub(crate) const DEFAULT_GC_FULL_SCAN_FREQUENCY: u64 = 20;
pub(crate) const DEFAULT_CLEANUP_INTERVAL: Duration = Duration::from_secs(1);
pub(crate) const DEFAULT_RESYNC_INTERVAL: Duration = Duration::from_secs(5);
#[derive(Debug, Clone)]
pub struct DatabaseOptions {
pub pool_size: usize,
pub enable_gc: bool,
pub gc_interval: Duration,
pub gc_full_scan_frequency: u64,
pub enable_cleanup: bool,
pub cleanup_interval: Duration,
pub reset_threshold: usize,
pub resync_interval: Duration,
}
impl Default for DatabaseOptions {
fn default() -> Self {
Self {
pool_size: DEFAULT_POOL_SIZE,
enable_gc: true,
gc_interval: DEFAULT_GC_INTERVAL,
gc_full_scan_frequency: DEFAULT_GC_FULL_SCAN_FREQUENCY,
enable_cleanup: true,
cleanup_interval: DEFAULT_CLEANUP_INTERVAL,
reset_threshold: DEFAULT_RESET_THRESHOLD,
resync_interval: DEFAULT_RESYNC_INTERVAL,
}
}
}
impl DatabaseOptions {
pub fn new() -> Self {
Self::default()
}
pub fn with_pool_size(mut self, pool_size: usize) -> Self {
self.pool_size = pool_size;
self
}
pub fn with_enable_gc(mut self, enable: bool) -> Self {
self.enable_gc = enable;
self
}
pub fn with_gc_interval(mut self, interval: Duration) -> Self {
self.gc_interval = interval;
self
}
pub fn with_gc_full_scan_frequency(mut self, frequency: u64) -> Self {
self.gc_full_scan_frequency = frequency.max(1);
self
}
pub fn with_enable_cleanup(mut self, enable: bool) -> Self {
self.enable_cleanup = enable;
self
}
pub fn with_cleanup_interval(mut self, interval: Duration) -> Self {
self.cleanup_interval = interval;
self
}
pub fn with_reset_threshold(mut self, threshold: usize) -> Self {
self.reset_threshold = threshold;
self
}
pub fn with_resync_interval(mut self, interval: Duration) -> Self {
self.resync_interval = interval;
self
}
pub fn with_all_workers_disabled(mut self) -> Self {
self.enable_gc = false;
self.enable_cleanup = false;
self
}
pub fn with_high_performance(mut self) -> Self {
self.pool_size *= 2;
self.gc_interval = Duration::from_secs(1);
self.gc_full_scan_frequency = 30;
self.cleanup_interval = Duration::from_millis(100);
self.reset_threshold *= 2;
self.resync_interval = Duration::from_secs(2);
self
}
pub fn with_low_resource(mut self) -> Self {
self.pool_size /= 2;
self.gc_interval = Duration::from_secs(5);
self.gc_full_scan_frequency = 12;
self.cleanup_interval = Duration::from_millis(500);
self.reset_threshold /= 2;
self.resync_interval = Duration::from_secs(10);
self
}
pub fn with_reduced_memory(mut self) -> Self {
self.pool_size /= 2;
self.gc_interval = Duration::from_millis(100);
self.gc_full_scan_frequency = 5;
self.cleanup_interval = Duration::from_millis(100);
self.reset_threshold /= 2;
self
}
}