Skip to content

Commit

Permalink
feat(cognitarium): allow delete without where clause
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Feb 28, 2024
1 parent b574e59 commit e22d9b9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 63 deletions.
71 changes: 49 additions & 22 deletions contracts/okp4-cognitarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub mod execute {
use crate::msg::{
DataFormat, Prefix, SimpleWhereCondition, TripleDeleteTemplate, WhereClause, WhereCondition,
};
use crate::querier::{PlanBuilder, QueryEngine};
use crate::querier::{PlanBuilder, QueryEngine, ResolvedVariables};
use crate::rdf::PrefixMap;
use crate::state::{HasCachedNamespaces, Triple};
use crate::storer::StoreEngine;
Expand Down Expand Up @@ -120,9 +120,29 @@ pub mod execute {
let mut plan_builder = PlanBuilder::new(deps.storage, &prefix_map, None);
let plan = plan_builder.build_plan(&r#where)?;

let triples = QueryEngine::new(deps.storage)
.construct_triples(plan, &prefix_map, delete, plan_builder.cached_namespaces())?
.collect::<StdResult<Vec<Triple>>>()?;
let query_engine = QueryEngine::new(deps.storage);
let delete_templates = query_engine.make_triple_templates(
&plan,
&prefix_map,
delete,
plan_builder.cached_namespaces(),
)?;

let triples = if r#where.is_empty() {
let empty_vars = ResolvedVariables::with_capacity(0);
delete_templates
.into_iter()
.filter_map(|tpl| match tpl.resolve(&empty_vars) {
Ok(Some(v)) => Some(Ok(v)),
Ok(None) => None,
Err(e) => Some(Err(e)),
})
.collect::<StdResult<Vec<Triple>>>()?
} else {
query_engine
.construct_triples(plan, delete_templates)?
.collect::<StdResult<Vec<Triple>>>()?
};

let mut store = StoreEngine::new(deps.storage)?;
let count = store.delete_all(&triples)?;
Expand Down Expand Up @@ -990,6 +1010,31 @@ mod tests {
17,
Uint128::from(0u128),
),
(
DeleteData {
prefixes: vec![
Prefix {
prefix: "core".to_string(),
namespace: "https://ontology.okp4.space/core/".to_string(),
},
Prefix {
prefix: "thesaurus".to_string(),
namespace: "https://ontology.okp4.space/thesaurus/topic/".to_string(),
},
],
delete: vec![msg::TripleDeleteTemplate {
subject: VarOrNamedNode::NamedNode(Full(id.to_string())),
predicate: VarOrNamedNode::NamedNode(Prefixed("core:hasTopic".to_string())),
object: VarOrNamedNodeOrLiteral::NamedNode(Prefixed(
"thesaurus:Test".to_string(),
)),
}],
r#where: vec![],
},
1,
0,
Uint128::from(6921u128),
),
];

for case in cases {
Expand Down Expand Up @@ -1100,24 +1145,6 @@ mod tests {
},
expected: StdError::generic_err("Selected variable not found in query").into(),
},
TC {
command: DeleteData {
prefixes: vec![],
delete: vec![msg::TripleDeleteTemplate {
subject: VarOrNamedNode::NamedNode(Full(
"https://ontology.okp4.space/thesaurus/topic/Test".to_string(),
)),
predicate: VarOrNamedNode::NamedNode(Full(
"https://ontology.okp4.space/core/hasTopic".to_string(),
)),
object: VarOrNamedNodeOrLiteral::NamedNode(Full(
"https://ontology.okp4.space/thesaurus/topic/Test".to_string(),
)),
}],
r#where: vec![],
},
expected: StdError::generic_err("Empty basic graph pattern").into(),
},
];

for case in cases {
Expand Down
26 changes: 13 additions & 13 deletions contracts/okp4-cognitarium/src/querier/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,28 @@ impl<'a> QueryEngine<'a> {
pub fn construct_triples(
&'a self,
plan: QueryPlan,
templates: Vec<TripleTemplate>,
) -> StdResult<ResolvedTripleIterator<'_>> {
ResolvedTripleIterator::try_new(self.eval_plan(plan), templates)
}

pub fn make_triple_templates(
&'a self,
plan: &QueryPlan,
prefixes: &HashMap<String, String>,
templates: Either<
Vec<(VarOrNode, VarOrNamedNode, VarOrNodeOrLiteral)>,
Vec<(VarOrNamedNode, VarOrNamedNode, VarOrNamedNodeOrLiteral)>,
>,
ns_cache: Vec<Namespace>,
) -> StdResult<ResolvedTripleIterator<'_>> {
) -> StdResult<Vec<TripleTemplate>> {
let mut ns_resolver: NamespaceResolver = ns_cache.into();

let templates = match templates {
match templates {
Left(tpl) => tpl
.into_iter()
.map(|t| {
TripleTemplate::try_new(
self.storage,
&mut ns_resolver,
&plan,
prefixes,
Left(t),
)
TripleTemplate::try_new(self.storage, &mut ns_resolver, plan, prefixes, Left(t))
})
.collect::<StdResult<Vec<TripleTemplate>>>(),
Right(tpl) => tpl
Expand All @@ -106,15 +108,13 @@ impl<'a> QueryEngine<'a> {
TripleTemplate::try_new(
self.storage,
&mut ns_resolver,
&plan,
plan,
prefixes,
Right(t),
)
})
.collect::<StdResult<Vec<TripleTemplate>>>(),
}?;

ResolvedTripleIterator::try_new(self.eval_plan(plan), templates)
}
}

pub fn eval_plan(&'a self, plan: QueryPlan) -> ResolvedVariablesIterator<'_> {
Expand Down
1 change: 1 addition & 0 deletions contracts/okp4-cognitarium/src/querier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ mod variable;

pub use engine::*;
pub use plan_builder::*;
pub use variable::ResolvedVariables;
50 changes: 22 additions & 28 deletions contracts/okp4-cognitarium/src/querier/plan_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,27 @@ impl<'a> PlanBuilder<'a> {
})
.collect::<StdResult<Vec<QueryNode>>>()?;

Self::build_from_bgp(bgp)
.map(|mut node| {
if let Some(skip) = self.skip {
node = QueryNode::Skip {
child: Box::new(node),
first: skip,
}
}
node
})
.map(|mut node| {
if let Some(limit) = self.limit {
node = QueryNode::Limit {
child: Box::new(node),
first: limit,
}
}
node
})
.map(|node| QueryPlan {
entrypoint: node,
variables: self.variables.clone(),
})
let mut node = Self::build_from_bgp(bgp);

if let Some(skip) = self.skip {
node = QueryNode::Skip {
child: Box::new(node),
first: skip,
}
}
if let Some(limit) = self.limit {
node = QueryNode::Limit {
child: Box::new(node),
first: limit,
}
}
Ok(QueryPlan {
entrypoint: node,
variables: self.variables.clone(),
})
}

fn build_from_bgp(bgp: Vec<QueryNode>) -> StdResult<QueryNode> {
fn build_from_bgp(bgp: Vec<QueryNode>) -> QueryNode {
bgp.into_iter()
.reduce(|left: QueryNode, right: QueryNode| -> QueryNode {
if left
Expand All @@ -97,10 +92,9 @@ impl<'a> PlanBuilder<'a> {
right: Box::new(right),
}
})
.map_or_else(
|| Err(StdError::generic_err("Empty basic graph pattern")),
Ok,
)
.unwrap_or(QueryNode::Noop {
bound_variables: vec![],
})
}

fn build_triple_pattern(&mut self, pattern: &TriplePattern) -> StdResult<QueryNode> {
Expand Down

0 comments on commit e22d9b9

Please sign in to comment.