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 SweepResult {
pub expired_count: usize,
}
#[derive(Debug, Clone, Default)]
pub struct ExpirySweep;
impl ExpirySweep {
pub fn new() -> Self {
Self
}
pub fn run(
&self,
metadata: &impl MetadataStore,
hybrid: Option<&HybridSearcher>,
tenant_id: &TenantId,
) -> Result<SweepResult> {
let now = current_time_ms();
let ids = metadata.list_expired_before(tenant_id, now)?;
if ids.is_empty() {
return Ok(SweepResult { expired_count: 0 });
}
let mut promoted = 0usize;
for id in &ids {
if metadata.mark_expired_if_final(tenant_id, id, now)? {
promoted += 1;
}
}
if promoted > 0 {
if let Some(h) = hybrid {
h.bump_tenant_memory_version(tenant_id);
}
}
Ok(SweepResult {
expired_count: promoted,
})
}
}
#[cfg(test)]
mod tests {
#[test]
fn sweep_struct_is_default_constructable() {
let _ = super::ExpirySweep::new();
let _ = super::ExpirySweep;
let r = super::SweepResult::default();
assert_eq!(r.expired_count, 0);
}
}