use crate::episode::Episode;
use super::policy::EvictionPolicy;
pub mod eviction;
pub mod scoring;
pub use eviction::evict_if_needed;
pub use scoring::{calculate_recency_score, calculate_relevance_score, extract_quality_score};
#[derive(Debug, Clone)]
pub struct CapacityManager {
max_episodes: usize,
eviction_policy: EvictionPolicy,
}
impl CapacityManager {
#[must_use]
pub fn new(max_episodes: usize, policy: EvictionPolicy) -> Self {
Self {
max_episodes,
eviction_policy: policy,
}
}
#[must_use]
pub fn can_store(&self, current_count: usize) -> bool {
current_count < self.max_episodes
}
#[must_use]
pub fn evict_if_needed(&self, episodes: &[Episode]) -> Vec<uuid::Uuid> {
eviction::evict_if_needed(episodes, self.max_episodes, self.eviction_policy)
}
#[must_use]
pub fn relevance_score(&self, episode: &Episode) -> f32 {
scoring::calculate_relevance_score(episode)
}
#[must_use]
pub fn extract_quality_score(&self, episode: &Episode) -> f32 {
scoring::extract_quality_score(episode)
}
#[must_use]
pub fn calculate_recency_score(&self, episode: &Episode) -> f32 {
scoring::calculate_recency_score(episode)
}
#[must_use]
pub fn max_episodes(&self) -> usize {
self.max_episodes
}
#[must_use]
pub fn eviction_policy(&self) -> EvictionPolicy {
self.eviction_policy
}
}
impl Default for CapacityManager {
fn default() -> Self {
Self::new(1000, EvictionPolicy::default())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_capacity_manager_creation() {
let manager = CapacityManager::new(100, EvictionPolicy::LRU);
assert_eq!(manager.max_episodes(), 100);
assert_eq!(manager.eviction_policy(), EvictionPolicy::LRU);
}
#[test]
fn test_default_capacity_manager() {
let manager = CapacityManager::default();
assert_eq!(manager.max_episodes(), 1000);
assert_eq!(manager.eviction_policy(), EvictionPolicy::RelevanceWeighted);
}
#[test]
fn test_can_store_under_capacity() {
let manager = CapacityManager::new(100, EvictionPolicy::LRU);
assert!(manager.can_store(0));
assert!(manager.can_store(50));
assert!(manager.can_store(99));
}
#[test]
fn test_can_store_at_capacity() {
let manager = CapacityManager::new(100, EvictionPolicy::LRU);
assert!(!manager.can_store(100));
}
#[test]
fn test_can_store_over_capacity() {
let manager = CapacityManager::new(100, EvictionPolicy::LRU);
assert!(!manager.can_store(101));
assert!(!manager.can_store(200));
}
}