Skip to content

Commit

Permalink
feat(cognitarium): implement DeleteData message
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Aug 30, 2023
1 parent 6722a52 commit 758c172
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
65 changes: 53 additions & 12 deletions contracts/okp4-cognitarium/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::contract::execute::insert;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
Expand Down Expand Up @@ -38,17 +37,23 @@ pub fn execute(
) -> Result<Response, ContractError> {
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> {
Expand All @@ -69,25 +74,61 @@ 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<Prefix>,
delete: Vec<TriplePattern>,
r#where: Option<WhereClause>,
) -> Result<Response, ContractError> {
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<Atom> = 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)?;

Ok(Response::new()
.add_attribute("action", "delete")
.add_attribute("triple_count", count))
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps<'_>, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
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,
Expand Down
4 changes: 2 additions & 2 deletions contracts/okp4-cognitarium/src/querier/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 758c172

Please sign in to comment.