use crate::{
Result, Rudof,
api::data::staging::journal_removed,
errors::DataError,
formats::IriNormalizationMode,
utils::{check_data_is_mutable, parse_triples, rdf_data, rdf_data_mut_unjournaled},
};
use oxrdf::Triple as OxTriple;
use rudof_rdf::rdf_core::{AnyOr, BuildRDF, NeighsRDF};
pub fn remove_triples(rudof: &mut Rudof, triples: &[(&str, &str, &str)], iri_mode: IriNormalizationMode) -> Result<()> {
check_data_is_mutable(rdf_data(rudof)?)?;
let triples = parse_triples(rudof, triples, iri_mode)?;
let removed = apply_removals(rudof, triples)?;
journal_removed(rudof, removed);
Ok(())
}
pub(crate) fn apply_removals(rudof: &mut Rudof, triples: Vec<OxTriple>) -> Result<Vec<OxTriple>> {
let rdf = rdf_data_mut_unjournaled(rudof)?;
let mut removed = Vec::new();
for triple in triples {
let present = rdf
.contains(
&AnyOr::new(Some(triple.subject.clone())),
&AnyOr::new(Some(triple.predicate.clone())),
&AnyOr::new(Some(triple.object.clone())),
)
.map_err(|error| {
Box::new(DataError::FailedTripleUpdate {
operation: "remove".to_string(),
error: error.to_string(),
})
})?;
if !present {
continue;
}
rdf.remove_triple(triple.subject.clone(), triple.predicate.clone(), triple.object.clone())
.map_err(|error| {
Box::new(DataError::FailedTripleUpdate {
operation: "remove".to_string(),
error: error.to_string(),
})
})?;
removed.push(triple);
}
Ok(removed)
}