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 3, 2023
1 parent cd2cef8 commit a85a1b2
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),

Check warning on line 46 in contracts/okp4-cognitarium/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/okp4-cognitarium/src/contract.rs#L43-L46

Added lines #L43 - L46 were not covered by tests
}
}

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)?;

Check warning on line 92 in contracts/okp4-cognitarium/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/okp4-cognitarium/src/contract.rs#L92

Added line #L92 was not covered by tests

let plan =
PlanBuilder::new(deps.storage, &prefixes).build_plan(&r#where.unwrap_or_default())?;

Check warning on line 95 in contracts/okp4-cognitarium/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/okp4-cognitarium/src/contract.rs#L94-L95

Added lines #L94 - L95 were not covered by tests

let variables = delete
.iter()
.fold(HashSet::new(), |mut acc, tp| {
acc.extend(tp.variables());
acc
})
.into_iter()
.map(SelectItem::Variable)
.collect();

Check warning on line 105 in contracts/okp4-cognitarium/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/okp4-cognitarium/src/contract.rs#L97-L105

Added lines #L97 - L105 were not covered by tests

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<_>, _>>()?;

Check warning on line 113 in contracts/okp4-cognitarium/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/okp4-cognitarium/src/contract.rs#L107-L113

Added lines #L107 - L113 were not covered by tests

let mut storer = StoreEngine::new(deps.storage)?;
let count = storer.delete_all(&atoms)?;

Check warning on line 116 in contracts/okp4-cognitarium/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/okp4-cognitarium/src/contract.rs#L115-L116

Added lines #L115 - L116 were not covered by tests

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

Check warning on line 121 in contracts/okp4-cognitarium/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/okp4-cognitarium/src/contract.rs#L118-L121

Added lines #L118 - L121 were not covered by tests
}

#[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::Turtle),
)?),
QueryMsg::Describe { query, format } => {
to_binary(&query::describe(deps, query, format.unwrap_or_default())?)
}
}
}

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 a85a1b2

Please sign in to comment.