diff --git a/contracts/okp4-cognitarium/src/contract.rs b/contracts/okp4-cognitarium/src/contract.rs index 0ac156bb..1cf22059 100644 --- a/contracts/okp4-cognitarium/src/contract.rs +++ b/contracts/okp4-cognitarium/src/contract.rs @@ -51,9 +51,9 @@ pub mod execute { use super::*; use crate::msg::{DataFormat, Prefix, SelectItem, TriplePattern, WhereClause}; use crate::querier::{PlanBuilder, QueryEngine}; - use crate::rdf::{Atom, TripleReader}; + use crate::rdf::TripleReader; use crate::storer::StoreEngine; - use std::collections::HashSet; + use std::collections::{BTreeMap, HashSet}; use std::io::BufReader; pub fn verify_owner(deps: &DepsMut, info: &MessageInfo) -> Result<(), ContractError> { @@ -103,12 +103,16 @@ pub mod execute { .collect(); let response = QueryEngine::new(deps.storage).select(plan, variables)?; - let atoms: Vec = response - .results - .bindings - .iter() - .flat_map(|row| delete.iter().map(|pattern| pattern.resolve(row, &prefixes))) - .collect::, _>>()?; + let atoms: Vec = if response.results.bindings.is_empty() { + vec![] + } else { + response + .results + .bindings + .iter() + .flat_map(|row| delete.iter().map(|pattern| pattern.resolve(row, &prefixes))) + .collect::, _>>()? + }; let mut storer = StoreEngine::new(deps.storage)?; let count = storer.delete_all(&atoms)?; diff --git a/contracts/okp4-cognitarium/src/storer/engine.rs b/contracts/okp4-cognitarium/src/storer/engine.rs index 5c7a0736..64cde7f8 100644 --- a/contracts/okp4-cognitarium/src/storer/engine.rs +++ b/contracts/okp4-cognitarium/src/storer/engine.rs @@ -115,6 +115,8 @@ impl<'a> StoreEngine<'a> { .map_err(ContractError::Std) } + /// Flushes the store to the storage. + /// Returns the number of triples added or removed (absolute value). pub fn finish(&mut self) -> Result { STORE.save(self.storage, &self.store)?; NAMESPACE_KEY_INCREMENT.save(self.storage, &self.ns_key_inc_offset)?; @@ -122,7 +124,11 @@ impl<'a> StoreEngine<'a> { namespaces().save(self.storage, entry.0.to_string(), entry.1)?; } - Ok(self.store.stat.triple_count - self.initial_triple_count) + if self.store.stat.triple_count > self.initial_triple_count { + Ok(self.store.stat.triple_count - self.initial_triple_count) + } else { + Ok(self.initial_triple_count - self.store.stat.triple_count) + } } fn resolve_namespace_key(&mut self, ns_str: String) -> StdResult {