@@ -143,9 +143,9 @@ pub mod query {
143
143
SelectQuery , SelectResponse , SimpleWhereCondition , StoreResponse , TriplePattern , Value ,
144
144
VarOrNamedNode , VarOrNode , VarOrNodeOrLiteral , WhereCondition ,
145
145
} ;
146
- use crate :: querier:: { PlanBuilder , QueryEngine } ;
146
+ use crate :: querier:: { PlanBuilder , QueryEngine , SelectResults } ;
147
147
use crate :: rdf:: { self , Atom , PrefixMap , TripleWriter } ;
148
- use crate :: state:: { HasCachedNamespaces , NamespaceResolver } ;
148
+ use crate :: state:: { HasCachedNamespaces , Namespace , NamespaceResolver } ;
149
149
150
150
pub fn store ( deps : Deps < ' _ > ) -> StdResult < StoreResponse > {
151
151
STORE . load ( deps. storage ) . map ( Into :: into)
@@ -170,27 +170,9 @@ pub mod query {
170
170
PlanBuilder :: new ( deps. storage , & prefix_map, None ) . with_limit ( count as usize ) ;
171
171
let plan = plan_builder. build_plan ( & query. r#where ) ?;
172
172
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 ( ) ) )
194
176
}
195
177
196
178
pub fn describe (
@@ -256,11 +238,11 @@ pub mod query {
256
238
. with_limit ( store. limits . max_query_limit as usize ) ;
257
239
let plan = plan_builder. build_plan ( & r#where) ?;
258
240
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
+ } ) ?;
264
246
265
247
let mut vars = response. head . vars ;
266
248
let mut bindings = response. results . bindings ;
@@ -353,10 +335,15 @@ pub mod query {
353
335
}
354
336
355
337
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 ;
357
344
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 } ;
360
347
361
348
pub fn as_select_veriables ( patterns : & [ TriplePattern ] ) -> Vec < SelectItem > {
362
349
let variables = patterns
@@ -398,6 +385,42 @@ pub mod util {
398
385
} ;
399
386
Ok ( atoms)
400
387
}
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
+ }
401
424
}
402
425
403
426
#[ cfg( test) ]
0 commit comments