From 758c172f6cb4c1cb88e5b73f6d37ee4c5b9d98dd Mon Sep 17 00:00:00 2001 From: ccamel Date: Thu, 3 Aug 2023 15:14:02 +0200 Subject: [PATCH] feat(cognitarium): implement DeleteData message --- contracts/okp4-cognitarium/src/contract.rs | 65 +++++++++++++++---- .../okp4-cognitarium/src/querier/engine.rs | 4 +- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/contracts/okp4-cognitarium/src/contract.rs b/contracts/okp4-cognitarium/src/contract.rs index e3787ba6..db6b0517 100644 --- a/contracts/okp4-cognitarium/src/contract.rs +++ b/contracts/okp4-cognitarium/src/contract.rs @@ -1,4 +1,3 @@ -use crate::contract::execute::insert; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ @@ -38,17 +37,23 @@ pub fn execute( ) -> Result { match msg { ExecuteMsg::InsertData { format, data } => { - insert(deps, info, format.unwrap_or(DataFormat::Turtle), data) + execute::insert(deps, info, format.unwrap_or_default(), data) } - _ => Err(StdError::generic_err("Not implemented").into()), + ExecuteMsg::DeleteData { + prefixes, + delete, + r#where, + } => execute::delete(deps, info, prefixes, delete, r#where), } } pub mod execute { use super::*; - use crate::msg::DataFormat; - use crate::rdf::TripleReader; - use crate::storer::TripleStorer; + use crate::msg::{DataFormat, Prefix, SelectItem, TriplePattern, WhereClause}; + use crate::querier::{PlanBuilder, QueryEngine}; + use crate::rdf::{Atom, TripleReader}; + use crate::storer::StoreEngine; + use std::collections::HashSet; use std::io::BufReader; pub fn verify_owner(deps: &DepsMut, info: &MessageInfo) -> Result<(), ContractError> { @@ -69,13 +74,51 @@ pub mod execute { let buf = BufReader::new(data.as_slice()); let mut reader = TripleReader::new(&format, buf); - let mut storer = TripleStorer::new(deps.storage)?; + let mut storer = StoreEngine::new(deps.storage)?; let count = storer.store_all(&mut reader)?; Ok(Response::new() .add_attribute("action", "insert") .add_attribute("triple_count", count)) } + + pub fn delete( + deps: DepsMut, + info: MessageInfo, + prefixes: Vec, + delete: Vec, + r#where: Option, + ) -> Result { + verify_owner(&deps, &info)?; + + let plan = + PlanBuilder::new(deps.storage, &prefixes).build_plan(&r#where.unwrap_or_default())?; + + let variables = delete + .iter() + .fold(HashSet::new(), |mut acc, tp| { + acc.extend(tp.variables()); + acc + }) + .into_iter() + .map(SelectItem::Variable) + .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 mut storer = StoreEngine::new(deps.storage)?; + let count = storer.delete_all(&atoms)?; + + Ok(Response::new() + .add_attribute("action", "delete") + .add_attribute("triple_count", count)) + } } #[cfg_attr(not(feature = "library"), entry_point)] @@ -83,11 +126,9 @@ pub fn query(deps: Deps<'_>, _env: Env, msg: QueryMsg) -> StdResult { match msg { QueryMsg::Store => to_binary(&query::store(deps)?), QueryMsg::Select { query } => to_binary(&query::select(deps, query)?), - QueryMsg::Describe { query, format } => to_binary(&query::describe( - deps, - query, - format.unwrap_or(DataFormat::default()), - )?), + QueryMsg::Describe { query, format } => { + to_binary(&query::describe(deps, query, format.unwrap_or_default())?) + } QueryMsg::Construct { query, format } => to_binary(&query::construct( deps, query, diff --git a/contracts/okp4-cognitarium/src/querier/engine.rs b/contracts/okp4-cognitarium/src/querier/engine.rs index 8cb5cf44..b7cdd4ed 100644 --- a/contracts/okp4-cognitarium/src/querier/engine.rs +++ b/contracts/okp4-cognitarium/src/querier/engine.rs @@ -443,7 +443,7 @@ mod test { use crate::rdf::TripleReader; use crate::state; use crate::state::{Literal, Store, StoreStat, NAMESPACE_KEY_INCREMENT, STORE}; - use crate::storer::TripleStorer; + use crate::storer::StoreEngine; use cosmwasm_std::testing::mock_dependencies; use cosmwasm_std::{Addr, Uint128}; use std::env; @@ -481,7 +481,7 @@ mod test { let data = read_test_data("sample.rdf.xml"); let buf = BufReader::new(data.as_slice()); let mut reader = TripleReader::new(&DataFormat::RDFXml, buf); - let mut storer = TripleStorer::new(storage).unwrap(); + let mut storer = StoreEngine::new(storage).unwrap(); let count = storer.store_all(&mut reader).unwrap(); assert_eq!(count, Uint128::new(40u128));