@ -1,6 +1,7 @@
use crate ::sparql ::algebra ::DatasetSpec ;
use crate ::sparql ::EvaluationError ;
use crate ::store ::numeric_encoder ::{
EncodedQuad , EncodedTerm , StrContainer , StrId , StrLookup , WithStoreError ,
EncodedQuad , EncodedTerm , ReadEncoder , StrContainer , StrId , StrLookup , WithStoreError ,
} ;
use crate ::store ::ReadableEncodedStore ;
use lasso ::{ Rodeo , Spur } ;
@ -11,15 +12,39 @@ pub(crate) struct DatasetView<S: ReadableEncodedStore> {
store : S ,
extra : RefCell < Rodeo > ,
default_graph_as_union : bool ,
dataset : Option < EncodedDatasetSpec < S ::StrId > > ,
}
impl < S : ReadableEncodedStore > DatasetView < S > {
pub fn new ( store : S , default_graph_as_union : bool ) -> Self {
Self {
pub fn new (
store : S ,
default_graph_as_union : bool ,
dataset : & DatasetSpec ,
) -> Result < Self , EvaluationError > {
let dataset = if dataset . is_empty ( ) {
None
} else {
Some ( EncodedDatasetSpec {
default : dataset
. default
. iter ( )
. flat_map ( | g | store . get_encoded_named_node ( g . as_ref ( ) ) . transpose ( ) )
. collect ::< Result < Vec < _ > , _ > > ( )
. map_err ( | e | e . into ( ) ) ? ,
named : dataset
. named
. iter ( )
. flat_map ( | g | store . get_encoded_named_node ( g . as_ref ( ) ) . transpose ( ) )
. collect ::< Result < Vec < _ > , _ > > ( )
. map_err ( | e | e . into ( ) ) ? ,
} )
} ;
Ok ( Self {
store ,
extra : RefCell ::new ( Rodeo ::default ( ) ) ,
default_graph_as_union ,
}
dataset ,
} )
}
}
@ -66,7 +91,56 @@ impl<S: ReadableEncodedStore> ReadableEncodedStore for DatasetView<S> {
if let Some ( ( subject , predicate , object , graph_name ) ) =
try_map_quad_pattern ( subject , predicate , object , graph_name )
{
if graph_name = = None {
if let Some ( dataset ) = & self . dataset {
if let Some ( graph_name ) = graph_name {
if graph_name = = EncodedTerm ::DefaultGraph {
let iters = dataset
. default
. iter ( )
. map ( | graph_name | {
self . store . encoded_quads_for_pattern (
subject ,
predicate ,
object ,
Some ( * graph_name ) ,
)
} )
. collect ::< Vec < _ > > ( ) ;
Box ::new ( map_iter ( iters . into_iter ( ) . flatten ( ) ) . map ( | quad | {
let quad = quad ? ;
Ok ( EncodedQuad ::new (
quad . subject ,
quad . predicate ,
quad . object ,
EncodedTerm ::DefaultGraph ,
) )
} ) )
} else if dataset . named . contains ( & graph_name ) {
Box ::new ( map_iter ( self . store . encoded_quads_for_pattern (
subject ,
predicate ,
object ,
Some ( graph_name ) ,
) ) )
} else {
Box ::new ( empty ( ) )
}
} else {
let iters = dataset
. named
. iter ( )
. map ( | graph_name | {
self . store . encoded_quads_for_pattern (
subject ,
predicate ,
object ,
Some ( * graph_name ) ,
)
} )
. collect ::< Vec < _ > > ( ) ;
Box ::new ( map_iter ( iters . into_iter ( ) . flatten ( ) ) )
}
} else if graph_name = = None {
Box ::new (
map_iter (
self . store
@ -173,3 +247,8 @@ pub enum DatasetStrId<I: StrId> {
}
impl < I : StrId > StrId for DatasetStrId < I > { }
struct EncodedDatasetSpec < I : StrId > {
default : Vec < EncodedTerm < I > > ,
named : Vec < EncodedTerm < I > > ,
}