use crate::error::Result;
use super::{CacheError, PAGE_SIZE, HUGE_PAGE_SIZE, MAX_SHARDS};
#[derive(Debug, Clone)]
pub struct PageCacheConfig {
pub capacity: usize,
pub num_shards: u32,
pub page_size: usize,
pub use_huge_pages: bool,
pub enable_prefetch: bool,
pub load_factor: f64,
pub max_conflict_length: usize,
pub enable_statistics: bool,
pub locking: LockingConfig,
pub memory: MemoryConfig,
pub performance: PerformanceConfig,
}
#[derive(Debug, Clone)]
pub struct LockingConfig {
pub use_futex: bool,
pub individual_file_locks: bool,
pub lock_timeout_ms: u64,
pub enable_lock_free: bool,
}
#[derive(Debug, Clone)]
pub struct MemoryConfig {
pub alignment: usize,
pub numa_aware: bool,
pub pre_allocate: bool,
pub use_secure_pools: bool,
pub kernel_advice: KernelAdvice,
}
#[derive(Debug, Clone)]
pub struct KernelAdvice {
pub huge_pages: bool,
pub will_need: bool,
pub sequential: bool,
pub dont_dump: bool,
}
#[derive(Debug, Clone)]
pub struct PerformanceConfig {
pub batch_size: usize,
pub prefetch_distance: usize,
pub warming_strategy: WarmingStrategy,
pub eviction: EvictionConfig,
pub maintenance: MaintenanceConfig,
}
#[derive(Debug, Clone, PartialEq)]
pub enum WarmingStrategy {
None,
OnDemand,
Background,
Aggressive,
}
#[derive(Debug, Clone)]
pub struct EvictionConfig {
pub algorithm: EvictionAlgorithm,
pub high_water_mark: f64,
pub low_water_mark: f64,
pub recency_boost: f64,
pub frequency_boost: f64,
}
#[derive(Debug, Clone, PartialEq)]
pub enum EvictionAlgorithm {
Lru,
LruAging,
Arc,
Clock,
TwoQ,
}
#[derive(Debug, Clone)]
pub struct MaintenanceConfig {
pub enable_defrag: bool,
pub defrag_threshold: f64,
pub enable_background_stats: bool,
pub maintenance_interval_ms: u64,
}
impl Default for PageCacheConfig {
fn default() -> Self {
Self::balanced()
}
}
impl PageCacheConfig {
pub fn balanced() -> Self {
Self {
capacity: 64 * 1024 * 1024, num_shards: 4,
page_size: PAGE_SIZE,
use_huge_pages: false,
enable_prefetch: true,
load_factor: 0.75,
max_conflict_length: 8,
enable_statistics: true,
locking: LockingConfig::balanced(),
memory: MemoryConfig::balanced(),
performance: PerformanceConfig::balanced(),
}
}
pub fn performance_optimized() -> Self {
Self {
capacity: 256 * 1024 * 1024, num_shards: 8,
page_size: PAGE_SIZE,
use_huge_pages: true,
enable_prefetch: true,
load_factor: 0.7, max_conflict_length: 6,
enable_statistics: false, locking: LockingConfig::performance_optimized(),
memory: MemoryConfig::performance_optimized(),
performance: PerformanceConfig::performance_optimized(),
}
}
pub fn memory_optimized() -> Self {
Self {
capacity: 32 * 1024 * 1024, num_shards: 2,
page_size: PAGE_SIZE,
use_huge_pages: false,
enable_prefetch: false,
load_factor: 0.85, max_conflict_length: 12,
enable_statistics: false,
locking: LockingConfig::memory_optimized(),
memory: MemoryConfig::memory_optimized(),
performance: PerformanceConfig::memory_optimized(),
}
}
pub fn security_optimized() -> Self {
Self {
capacity: 64 * 1024 * 1024,
num_shards: 4,
page_size: PAGE_SIZE,
use_huge_pages: false,
enable_prefetch: true,
load_factor: 0.75,
max_conflict_length: 8,
enable_statistics: true,
locking: LockingConfig::security_optimized(),
memory: MemoryConfig::security_optimized(),
performance: PerformanceConfig::security_optimized(),
}
}
pub fn with_capacity(mut self, capacity: usize) -> Self {
self.capacity = capacity;
self
}
pub fn with_shards(mut self, shards: u32) -> Self {
self.num_shards = shards;
self
}
pub fn with_huge_pages(mut self, enable: bool) -> Self {
self.use_huge_pages = enable;
self
}
pub fn with_prefetch(mut self, enable: bool) -> Self {
self.enable_prefetch = enable;
self
}
pub fn with_load_factor(mut self, factor: f64) -> Self {
self.load_factor = factor;
self
}
pub fn with_statistics(mut self, enable: bool) -> Self {
self.enable_statistics = enable;
self
}
pub fn validate(&self) -> Result<()> {
if self.capacity == 0 {
return Err(CacheError::InvalidPageSize.into());
}
if !self.page_size.is_power_of_two() || self.page_size < 1024 {
return Err(CacheError::InvalidPageSize.into());
}
if self.num_shards == 0 || self.num_shards > MAX_SHARDS as u32 {
return Err(CacheError::InvalidShardConfig.into());
}
if self.load_factor <= 0.0 || self.load_factor >= 1.0 {
return Err(CacheError::InvalidShardConfig.into());
}
if self.use_huge_pages && self.capacity < HUGE_PAGE_SIZE {
return Err(CacheError::InvalidPageSize.into());
}
Ok(())
}
pub fn calculate_page_count(&self) -> usize {
self.capacity / self.page_size
}
pub fn calculate_hash_table_size(&self) -> usize {
let pages = self.calculate_page_count();
let target_size = (pages as f64 / self.load_factor) as usize;
target_size.next_power_of_two()
}
}
impl LockingConfig {
pub fn balanced() -> Self {
Self {
use_futex: true,
individual_file_locks: false,
lock_timeout_ms: 1000,
enable_lock_free: true,
}
}
pub fn performance_optimized() -> Self {
Self {
use_futex: true,
individual_file_locks: true,
lock_timeout_ms: 100,
enable_lock_free: true,
}
}
pub fn memory_optimized() -> Self {
Self {
use_futex: false,
individual_file_locks: false,
lock_timeout_ms: 5000,
enable_lock_free: false,
}
}
pub fn security_optimized() -> Self {
Self {
use_futex: false,
individual_file_locks: false,
lock_timeout_ms: 2000,
enable_lock_free: false,
}
}
}
impl MemoryConfig {
pub fn balanced() -> Self {
Self {
alignment: PAGE_SIZE,
numa_aware: false,
pre_allocate: false,
use_secure_pools: true,
kernel_advice: KernelAdvice::balanced(),
}
}
pub fn performance_optimized() -> Self {
Self {
alignment: HUGE_PAGE_SIZE,
numa_aware: true,
pre_allocate: true,
use_secure_pools: false, kernel_advice: KernelAdvice::performance_optimized(),
}
}
pub fn memory_optimized() -> Self {
Self {
alignment: PAGE_SIZE,
numa_aware: false,
pre_allocate: false,
use_secure_pools: true,
kernel_advice: KernelAdvice::memory_optimized(),
}
}
pub fn security_optimized() -> Self {
Self {
alignment: PAGE_SIZE,
numa_aware: false,
pre_allocate: true,
use_secure_pools: true,
kernel_advice: KernelAdvice::security_optimized(),
}
}
}
impl KernelAdvice {
pub fn balanced() -> Self {
Self {
huge_pages: false,
will_need: true,
sequential: false,
dont_dump: false,
}
}
pub fn performance_optimized() -> Self {
Self {
huge_pages: true,
will_need: true,
sequential: true,
dont_dump: false,
}
}
pub fn memory_optimized() -> Self {
Self {
huge_pages: false,
will_need: false,
sequential: false,
dont_dump: true,
}
}
pub fn security_optimized() -> Self {
Self {
huge_pages: false,
will_need: true,
sequential: false,
dont_dump: true,
}
}
}
impl PerformanceConfig {
pub fn balanced() -> Self {
Self {
batch_size: 8,
prefetch_distance: 2,
warming_strategy: WarmingStrategy::OnDemand,
eviction: EvictionConfig::balanced(),
maintenance: MaintenanceConfig::balanced(),
}
}
pub fn performance_optimized() -> Self {
Self {
batch_size: 16,
prefetch_distance: 4,
warming_strategy: WarmingStrategy::Aggressive,
eviction: EvictionConfig::performance_optimized(),
maintenance: MaintenanceConfig::performance_optimized(),
}
}
pub fn memory_optimized() -> Self {
Self {
batch_size: 4,
prefetch_distance: 1,
warming_strategy: WarmingStrategy::None,
eviction: EvictionConfig::memory_optimized(),
maintenance: MaintenanceConfig::memory_optimized(),
}
}
pub fn security_optimized() -> Self {
Self {
batch_size: 4,
prefetch_distance: 1,
warming_strategy: WarmingStrategy::OnDemand,
eviction: EvictionConfig::security_optimized(),
maintenance: MaintenanceConfig::security_optimized(),
}
}
}
impl EvictionConfig {
pub fn balanced() -> Self {
Self {
algorithm: EvictionAlgorithm::Lru,
high_water_mark: 0.9,
low_water_mark: 0.7,
recency_boost: 1.2,
frequency_boost: 1.1,
}
}
pub fn performance_optimized() -> Self {
Self {
algorithm: EvictionAlgorithm::Arc,
high_water_mark: 0.85,
low_water_mark: 0.65,
recency_boost: 1.5,
frequency_boost: 1.3,
}
}
pub fn memory_optimized() -> Self {
Self {
algorithm: EvictionAlgorithm::Clock,
high_water_mark: 0.95,
low_water_mark: 0.8,
recency_boost: 1.0,
frequency_boost: 1.0,
}
}
pub fn security_optimized() -> Self {
Self {
algorithm: EvictionAlgorithm::Lru,
high_water_mark: 0.9,
low_water_mark: 0.7,
recency_boost: 1.1,
frequency_boost: 1.05,
}
}
}
impl MaintenanceConfig {
pub fn balanced() -> Self {
Self {
enable_defrag: true,
defrag_threshold: 0.3,
enable_background_stats: true,
maintenance_interval_ms: 5000,
}
}
pub fn performance_optimized() -> Self {
Self {
enable_defrag: true,
defrag_threshold: 0.2,
enable_background_stats: false,
maintenance_interval_ms: 1000,
}
}
pub fn memory_optimized() -> Self {
Self {
enable_defrag: false,
defrag_threshold: 0.5,
enable_background_stats: false,
maintenance_interval_ms: 30000,
}
}
pub fn security_optimized() -> Self {
Self {
enable_defrag: true,
defrag_threshold: 0.25,
enable_background_stats: true,
maintenance_interval_ms: 10000,
}
}
}