Skip to content

Commit a2010a7

Browse files
committed
feat(cognitarium): add select mapping to msg on describe
1 parent ef789f8 commit a2010a7

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

contracts/okp4-cognitarium/src/contract.rs

+54-31
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ pub mod query {
143143
SelectQuery, SelectResponse, SimpleWhereCondition, StoreResponse, TriplePattern, Value,
144144
VarOrNamedNode, VarOrNode, VarOrNodeOrLiteral, WhereCondition,
145145
};
146-
use crate::querier::{PlanBuilder, QueryEngine};
146+
use crate::querier::{PlanBuilder, QueryEngine, SelectResults};
147147
use crate::rdf::{self, Atom, PrefixMap, TripleWriter};
148-
use crate::state::{HasCachedNamespaces, NamespaceResolver};
148+
use crate::state::{HasCachedNamespaces, Namespace, NamespaceResolver};
149149

150150
pub fn store(deps: Deps<'_>) -> StdResult<StoreResponse> {
151151
STORE.load(deps.storage).map(Into::into)
@@ -170,27 +170,9 @@ pub mod query {
170170
PlanBuilder::new(deps.storage, &prefix_map, None).with_limit(count as usize);
171171
let plan = plan_builder.build_plan(&query.r#where)?;
172172

173-
let res = QueryEngine::new(deps.storage).select(plan, query.select)?;
174-
let mut ns_resolver = plan_builder.cached_namespaces().into();
175-
176-
Ok(SelectResponse {
177-
head: Head { vars: res.head },
178-
results: res
179-
.solutions
180-
.map(|res| {
181-
res.and_then(|(name, var)| -> StdResult<(String, Value)> {
182-
Ok((
183-
name,
184-
var.as_value(&mut |ns_key| {
185-
let res = ns_resolver.resolve_from_key(deps.storage, ns_key);
186-
res.and_then(NamespaceResolver::none_as_error_middleware)
187-
.map(|ns| ns.value)
188-
})?,
189-
))
190-
})
191-
})
192-
.collect()?,
193-
})
173+
QueryEngine::new(deps.storage)
174+
.select(plan, query.select)
175+
.and_then(|res| util::map_select_solutions(deps, res, plan_builder.cached_namespaces()))
194176
}
195177

196178
pub fn describe(
@@ -256,11 +238,11 @@ pub mod query {
256238
.with_limit(store.limits.max_query_limit as usize);
257239
let plan = plan_builder.build_plan(&r#where)?;
258240

259-
let response = QueryEngine::new(deps.storage).select(
260-
plan,
261-
select,
262-
plan_builder.cached_namespaces().into(),
263-
)?;
241+
let response = QueryEngine::new(deps.storage)
242+
.select(plan, select)
243+
.and_then(|res| {
244+
util::map_select_solutions(deps, res, plan_builder.cached_namespaces())
245+
})?;
264246

265247
let mut vars = response.head.vars;
266248
let mut bindings = response.results.bindings;
@@ -353,10 +335,15 @@ pub mod query {
353335
}
354336

355337
pub mod util {
356-
use crate::msg::{Results, SelectItem, SimpleWhereCondition, TriplePattern, WhereCondition};
338+
use super::*;
339+
use crate::msg::{
340+
Head, Results, SelectItem, SelectResponse, SimpleWhereCondition, TriplePattern, Value,
341+
WhereCondition,
342+
};
343+
use crate::querier::SelectResults;
357344
use crate::rdf::Atom;
358-
use cosmwasm_std::StdResult;
359-
use std::collections::{HashMap, HashSet};
345+
use crate::state::{Namespace, NamespaceResolver};
346+
use std::collections::{BTreeMap, HashMap, HashSet};
360347

361348
pub fn as_select_veriables(patterns: &[TriplePattern]) -> Vec<SelectItem> {
362349
let variables = patterns
@@ -398,6 +385,42 @@ pub mod util {
398385
};
399386
Ok(atoms)
400387
}
388+
389+
pub fn map_select_solutions(
390+
deps: Deps<'_>,
391+
res: SelectResults<'_>,
392+
ns_cache: Vec<Namespace>,
393+
) -> StdResult<SelectResponse> {
394+
let mut ns_resolver = ns_cache.into();
395+
396+
Ok(SelectResponse {
397+
head: Head { vars: res.head },
398+
results: Results {
399+
bindings: res
400+
.solutions
401+
.map(|res| {
402+
res.and_then(|vars| -> StdResult<BTreeMap<String, Value>> {
403+
vars.iter()
404+
.map(|(name, var)| {
405+
Ok((
406+
name,
407+
var.as_value(&mut |ns_key| {
408+
let res =
409+
ns_resolver.resolve_from_key(deps.storage, ns_key);
410+
res.and_then(
411+
NamespaceResolver::none_as_error_middleware,
412+
)
413+
.map(|ns| ns.value)
414+
})?,
415+
))
416+
})
417+
.collect()
418+
})
419+
})
420+
.collect()?,
421+
},
422+
})
423+
}
401424
}
402425

403426
#[cfg(test)]

0 commit comments

Comments
 (0)