use std::collections::HashMap;
use std::path::PathBuf;
use crate::plan::{DeliverableStatus, LockInfo, PlanGraph};
use chrono::{DateTime, Utc};
use crate::task::CriticalPathResult;
pub(crate) struct PlanState {
pub(crate) graph: PlanGraph,
pub(crate) statuses: HashMap<String, DeliverableStatus>,
pub(crate) locks: HashMap<String, LockInfo>,
pub(crate) file_to_deliverable: HashMap<PathBuf, String>,
pub(crate) cached_result: CriticalPathResult,
}
impl PlanState {
pub(crate) fn new(
graph: PlanGraph,
statuses: HashMap<String, DeliverableStatus>,
cached_result: CriticalPathResult,
) -> Self {
Self {
graph,
statuses,
locks: HashMap::new(),
file_to_deliverable: HashMap::new(),
cached_result,
}
}
pub(crate) fn reap_expired(&mut self, now: DateTime<Utc>) -> Vec<LockInfo> {
let expired_ids: Vec<String> = self
.locks
.iter()
.filter(|(_, info)| info.expires_at < now)
.map(|(id, _)| id.clone())
.collect();
let mut reaped = Vec::with_capacity(expired_ids.len());
for id in expired_ids {
if let Some(info) = self.locks.remove(&id) {
if let Some(deliverable) = self.graph.deliverables.iter().find(|d| d.id == id) {
for f in &deliverable.owned_files {
self.file_to_deliverable.remove(f);
}
}
self.statuses.insert(id, DeliverableStatus::Ready);
reaped.push(info);
}
}
reaped
}
}