Implements SPARQL DESCRIBE

pull/10/head
Tpt 6 years ago
parent 9ae5f4fcb7
commit e752277a1b
  1. 47
      lib/src/sparql/eval.rs
  2. 16
      lib/src/sparql/mod.rs

@ -74,6 +74,14 @@ impl<S: EncodedQuadsStore> SimpleEvaluator<S> {
}))) })))
} }
pub fn evaluate_describe_plan<'a>(&'a self, plan: &'a PlanNode) -> Result<QueryResult<'a>> {
Ok(QueryResult::Graph(Box::new(DescribeIterator {
store: self.store.clone(),
iter: self.eval_plan(plan, vec![]),
quads_iters: Vec::default(),
})))
}
fn eval_plan<'a>(&self, node: &'a PlanNode, from: EncodedTuple) -> EncodedTuplesIterator<'a> { fn eval_plan<'a>(&self, node: &'a PlanNode, from: EncodedTuple) -> EncodedTuplesIterator<'a> {
match node { match node {
PlanNode::Init => Box::new(once(Ok(from))), PlanNode::Init => Box::new(once(Ok(from))),
@ -1103,3 +1111,42 @@ fn decode_triple<S: BytesStore>(
encoder.decode_term(object)?, encoder.decode_term(object)?,
)) ))
} }
struct DescribeIterator<'a, S: EncodedQuadsStore> {
store: Arc<S>,
iter: EncodedTuplesIterator<'a>,
quads_iters: Vec<S::QuadsForSubjectIterator>,
}
impl<'a, S: EncodedQuadsStore> Iterator for DescribeIterator<'a, S> {
type Item = Result<Triple>;
fn next(&mut self) -> Option<Result<Triple>> {
while let Some(mut quads_iter) = self.quads_iters.pop() {
if let Some(quad) = quads_iter.next() {
self.quads_iters.push(quads_iter);
return Some(quad.and_then(|quad| self.store.encoder().decode_triple(&quad)));
}
}
let tuple = match self.iter.next()? {
Ok(tuple) => tuple,
Err(error) => return Some(Err(error)),
};
let mut error_to_return = None;
for subject in tuple {
if let Some(subject) = subject {
match self.store.quads_for_subject(subject) {
Ok(quads_iter) => self.quads_iters.push(quads_iter),
Err(error) => {
error_to_return = Some(error);
}
}
}
}
if let Some(error) = error_to_return {
Some(Err(error))
} else {
self.next()
}
}
}

@ -65,7 +65,14 @@ impl<S: EncodedQuadsStore> SparqlDataset for StoreDataset<S> {
evaluator: SimpleEvaluator::new(store), evaluator: SimpleEvaluator::new(store),
} }
} }
_ => unimplemented!(), Query::Describe { algebra, dataset } => {
let store = self.encoded();
let (plan, _) = PlanBuilder::build(&*store, &algebra)?;
SimplePreparedQuery::Describe {
plan,
evaluator: SimpleEvaluator::new(store),
}
}
}) })
} }
} }
@ -85,6 +92,10 @@ pub enum SimplePreparedQuery<S: EncodedQuadsStore> {
construct: Vec<TripleTemplate>, construct: Vec<TripleTemplate>,
evaluator: SimpleEvaluator<S>, evaluator: SimpleEvaluator<S>,
}, },
Describe {
plan: PlanNode,
evaluator: SimpleEvaluator<S>,
},
} }
impl<S: EncodedQuadsStore> PreparedQuery for SimplePreparedQuery<S> { impl<S: EncodedQuadsStore> PreparedQuery for SimplePreparedQuery<S> {
@ -101,6 +112,9 @@ impl<S: EncodedQuadsStore> PreparedQuery for SimplePreparedQuery<S> {
construct, construct,
evaluator, evaluator,
} => evaluator.evaluate_construct_plan(&plan, &construct), } => evaluator.evaluate_construct_plan(&plan, &construct),
SimplePreparedQuery::Describe { plan, evaluator } => {
evaluator.evaluate_describe_plan(&plan)
}
} }
} }
} }

Loading…
Cancel
Save