Skip to content

Commit

Permalink
fix(cognitarium): fix incorrect variable resolution on delete
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Aug 30, 2023
1 parent dc0ed63 commit 76abfcd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
20 changes: 12 additions & 8 deletions contracts/okp4-cognitarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -103,12 +103,16 @@ pub mod execute {
.collect();

let response = QueryEngine::new(deps.storage).select(plan, variables)?;
let atoms: Vec<Atom> = response
.results
.bindings
.iter()
.flat_map(|row| delete.iter().map(|pattern| pattern.resolve(row, &prefixes)))
.collect::<Result<Vec<_>, _>>()?;
let atoms: Vec<Atom> = if response.results.bindings.is_empty() {
vec![]
} else {
response
.results
.bindings
.iter()
.flat_map(|row| delete.iter().map(|pattern| pattern.resolve(row, &prefixes)))
.collect::<Result<Vec<_>, _>>()?
};

let mut storer = StoreEngine::new(deps.storage)?;
let count = storer.delete_all(&atoms)?;
Expand Down
8 changes: 7 additions & 1 deletion contracts/okp4-cognitarium/src/storer/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,20 @@ 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<Uint128, ContractError> {
STORE.save(self.storage, &self.store)?;
NAMESPACE_KEY_INCREMENT.save(self.storage, &self.ns_key_inc_offset)?;
for entry in &self.ns_cache {
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<u128> {
Expand Down

0 comments on commit 76abfcd

Please sign in to comment.