use rho_protocol::{CompactionId, CompactionStatus, CompactionTrigger};
use rho_sdk::CompactionOutcome;
use super::LiveEvents;
use crate::app::session_events::WireEvent;
pub(crate) enum CompactionEnd<'a> {
Completed(&'a CompactionOutcome),
Cancelled,
Failed(String),
}
impl LiveEvents {
pub(crate) fn compaction_started(&mut self, trigger: CompactionTrigger) -> Vec<WireEvent> {
let id = fresh_compaction_id();
self.compaction = Some(id.clone());
self.messages.interrupt();
vec![WireEvent::Compaction {
id,
status: CompactionStatus::Started {
trigger: Some(trigger),
},
}]
}
pub(crate) fn compaction_ended(&mut self, end: CompactionEnd<'_>) -> Vec<WireEvent> {
let (id, status, usage) = match end {
CompactionEnd::Completed(outcome) => (
Some(self.compaction.take().unwrap_or_else(fresh_compaction_id)),
CompactionStatus::Completed {
counts: crate::compaction::outcome_counts(outcome),
},
Some(outcome.current_tokens()),
),
CompactionEnd::Cancelled => (self.compaction.take(), CompactionStatus::Cancelled, None),
CompactionEnd::Failed(error) => (
self.compaction.take(),
CompactionStatus::Failed { error },
None,
),
};
let Some(id) = id else {
return Vec::new();
};
self.messages.interrupt();
std::iter::once(WireEvent::Compaction { id, status })
.chain(usage.map(|used| self.fill(used)))
.collect()
}
}
fn fresh_compaction_id() -> CompactionId {
CompactionId::new(format!("compaction-{}", uuid::Uuid::new_v4()))
}