use super::{
BackupLayoutGuard, JournalLock, JournalLockError, PersistenceError, create_json_durable,
read_json,
};
use crate::model::{
artifacts::ArtifactChecksumRecord,
effect_graph::{EffectGraphRecord, MAX_EFFECT_GRAPH_BYTES},
};
use thiserror::Error;
pub fn create_effect_graph(
layout: &BackupLayoutGuard,
record: &EffectGraphRecord,
) -> Result<(), EffectGraphPersistenceError> {
layout.check_root()?;
let path = layout.root().join("effect-graph.json");
let _lock = JournalLock::acquire(&path)?;
check_size(record)?;
create_json_durable(&path, record)?;
Ok(())
}
pub fn read_effect_graph(
layout: &BackupLayoutGuard,
expected: &ArtifactChecksumRecord,
) -> Result<EffectGraphRecord, EffectGraphPersistenceError> {
layout.check_root()?;
let path = layout.root().join("effect-graph.json");
let _lock = JournalLock::acquire(&path)?;
let record: EffectGraphRecord = read_json(&path, MAX_EFFECT_GRAPH_BYTES)?;
check_size(&record)?;
if &record.digest() != expected {
return Err(EffectGraphPersistenceError::DigestMismatch);
}
Ok(record)
}
fn check_size(record: &EffectGraphRecord) -> Result<(), PersistenceError> {
if serde_json::to_vec_pretty(record)?.len() as u64 > MAX_EFFECT_GRAPH_BYTES {
return Err(PersistenceError::RecordTooLarge {
limit: MAX_EFFECT_GRAPH_BYTES,
});
}
Ok(())
}
#[derive(Debug, Error)]
pub enum EffectGraphPersistenceError {
#[error("effect graph digest mismatch")]
DigestMismatch,
#[error(transparent)]
Lock(#[from] JournalLockError),
#[error(transparent)]
Persistence(#[from] PersistenceError),
}
#[cfg(all(test, unix))]
mod tests;