use std::time::Duration;
pub(crate) mod builder_utils;
pub(crate) mod concurrent;
pub(crate) mod deque;
pub(crate) mod entry;
pub(crate) mod error;
pub(crate) mod frequency_sketch;
pub(crate) mod time;
pub(crate) mod timer_wheel;
#[cfg(test)]
pub(crate) mod test_utils;
use self::concurrent::constants::{
DEFAULT_EVICTION_BATCH_SIZE, DEFAULT_MAINTENANCE_TASK_TIMEOUT_MILLIS,
DEFAULT_MAX_LOG_SYNC_REPEATS,
};
#[derive(Clone, Copy, Debug, Eq)]
pub(crate) enum CacheRegion {
Window = 0,
MainProbation = 1,
MainProtected = 2,
Other = 3,
}
impl From<usize> for CacheRegion {
fn from(n: usize) -> Self {
match n {
0 => Self::Window,
1 => Self::MainProbation,
2 => Self::MainProtected,
3 => Self::Other,
_ => panic!("No such CacheRegion variant for {n}"),
}
}
}
impl CacheRegion {
pub(crate) fn name(self) -> &'static str {
match self {
Self::Window => "window",
Self::MainProbation => "main probation",
Self::MainProtected => "main protected",
Self::Other => "other",
}
}
}
impl PartialEq<Self> for CacheRegion {
fn eq(&self, other: &Self) -> bool {
core::mem::discriminant(self) == core::mem::discriminant(other)
}
}
impl PartialEq<usize> for CacheRegion {
fn eq(&self, other: &usize) -> bool {
*self as usize == *other
}
}
#[derive(Clone, Debug)]
pub(crate) struct HousekeeperConfig {
pub(crate) maintenance_task_timeout: Duration,
pub(crate) max_log_sync_repeats: u32,
pub(crate) eviction_batch_size: u32,
}
impl Default for HousekeeperConfig {
fn default() -> Self {
Self {
maintenance_task_timeout: Duration::from_millis(
DEFAULT_MAINTENANCE_TASK_TIMEOUT_MILLIS,
),
max_log_sync_repeats: DEFAULT_MAX_LOG_SYNC_REPEATS as u32,
eviction_batch_size: DEFAULT_EVICTION_BATCH_SIZE,
}
}
}
impl HousekeeperConfig {
#[cfg(test)]
pub(crate) fn new(
maintenance_task_timeout: Option<Duration>,
max_log_sync_repeats: Option<u32>,
eviction_batch_size: Option<u32>,
) -> Self {
Self {
maintenance_task_timeout: maintenance_task_timeout.unwrap_or(Duration::from_millis(
DEFAULT_MAINTENANCE_TASK_TIMEOUT_MILLIS,
)),
max_log_sync_repeats: max_log_sync_repeats
.unwrap_or(DEFAULT_MAX_LOG_SYNC_REPEATS as u32),
eviction_batch_size: eviction_batch_size.unwrap_or(DEFAULT_EVICTION_BATCH_SIZE),
}
}
}
pub(crate) fn sketch_capacity(max_capacity: u64) -> u32 {
max_capacity.try_into().unwrap_or(u32::MAX).max(128)
}
#[cfg(test)]
pub(crate) fn available_parallelism() -> usize {
use std::{num::NonZeroUsize, thread::available_parallelism};
available_parallelism().map(NonZeroUsize::get).unwrap_or(1)
}