use std::fs;
use super::snapshot::Snapshot;
use crate::conversation::Error;
use crate::conversation::ids::parse_conversation_id;
use crate::conversation::rows::CONVERSATIONS;
use crate::operator::transfer::units::{self, Measured};
pub(crate) const NO_TURNS: &str = "no_turns";
pub(crate) const UNFIT: &str = "unfit";
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Omission {
pub kind: &'static str,
pub name: String,
pub reason: &'static str,
pub projections: usize,
pub redactions: usize,
}
#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct Inventory {
pub units: Vec<Measured>,
pub omitted: Vec<Omission>,
}
impl Inventory {
pub(crate) fn totals(&self) -> (usize, usize, u64) {
let scopes = self.units.iter().filter(|u| u.kind == "scope").count();
let bytes = self.units.iter().map(|unit| unit.bytes).sum();
(scopes, self.units.len() - scopes, bytes)
}
}
pub(crate) type Bodies<'b> = dyn FnMut(usize, usize, &[u8]) -> Result<(), Error> + 'b;
pub(crate) fn inventory(snapshot: &Snapshot, bodies: &mut Bodies<'_>) -> Result<Inventory, Error> {
let store = snapshot.store()?;
let mut made = Inventory::default();
for scope in store.scopes()? {
let (unit, body) = units::scope(&store, &scope)?;
bodies(made.units.len(), 0, &body)?;
made.units.push(unit);
}
let listed = store.conversations()?;
for id in &listed {
if store.last_turn_number(id)? == 0 {
made.omitted.push(Omission {
kind: "conversation",
name: id.clone(),
reason: NO_TURNS,
projections: store.read_projections(id, 0, None)?.len(),
redactions: units::acts(&store, id)?,
});
continue;
}
let index = made.units.len();
let mut part = 0;
let measured = units::conversation(&store, id, &mut |body| {
part += 1;
bodies(index, part - 1, body)
});
match measured {
Ok(unit) => made.units.push(unit),
Err(Error::Value(_)) => made.omitted.push(Omission {
kind: "conversation",
name: id.clone(),
reason: UNFIT,
projections: 0,
redactions: 0,
}),
Err(error) => return Err(error),
}
}
let directory = snapshot.root().join(CONVERSATIONS);
let mut trails = Vec::new();
if directory.is_dir() {
for entry in fs::read_dir(&directory)? {
let Ok(name) = entry?.file_name().into_string() else {
continue;
};
if parse_conversation_id(&name).is_err() || listed.contains(&name) {
continue;
}
let redactions = units::acts(&store, &name)?;
if redactions > 0 {
trails.push(Omission {
kind: "conversation",
name,
reason: NO_TURNS,
projections: 0,
redactions,
});
}
}
}
made.omitted.extend(trails);
made.omitted.sort_by(|a, b| a.name.cmp(&b.name));
Ok(made)
}