use crate::error::Result;
use crate::store::hybrid::HybridSearcher;
use crate::store::metadata::MetadataStore;
use crate::types::TenantId;
fn current_time_ms() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as i64)
.unwrap_or(0)
}
#[derive(Debug, Clone, Default)]
pub struct PromotionResult {
pub promoted_count: usize,
}
#[derive(Debug, Clone)]
pub struct HistoryPromotion {
pub age_threshold_ms: i64,
}
impl HistoryPromotion {
pub fn new(age_threshold_ms: i64) -> Self {
Self { age_threshold_ms }
}
pub fn run(
&self,
metadata: &impl MetadataStore,
hybrid: Option<&HybridSearcher>,
tenant_id: &TenantId,
) -> Result<PromotionResult> {
let now = current_time_ms();
let cutoff = now.saturating_sub(self.age_threshold_ms);
let ids = metadata.list_stale_superseded(tenant_id, cutoff)?;
if ids.is_empty() {
return Ok(PromotionResult { promoted_count: 0 });
}
let mut promoted = 0usize;
for id in &ids {
if metadata.promote_to_history_if_stale(tenant_id, id, cutoff, now)? {
promoted += 1;
}
}
if promoted > 0 {
if let Some(h) = hybrid {
h.bump_tenant_memory_version(tenant_id);
}
}
Ok(PromotionResult {
promoted_count: promoted,
})
}
}
#[cfg(test)]
mod tests {
#[test]
fn promotion_is_default_constructable_with_threshold() {
let p = super::HistoryPromotion::new(30 * 86_400_000);
assert_eq!(p.age_threshold_ms, 30 * 86_400_000);
}
}