Skip to content

Commit a96258f

Browse files
committed
feat(cognitarium): handle limit & skip at plan build
1 parent 23536ae commit a96258f

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

contracts/okp4-cognitarium/src/querier/plan_builder.rs

+37-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub struct PlanBuilder<'a> {
1212
storage: &'a dyn Storage,
1313
prefixes: HashMap<String, String>,
1414
variables: Vec<String>,
15+
limit: Option<usize>,
16+
skip: Option<usize>,
1517
}
1618

1719
impl<'a> PlanBuilder<'a> {
@@ -20,9 +22,21 @@ impl<'a> PlanBuilder<'a> {
2022
storage,
2123
prefixes: Self::make_prefixes(prefixes),
2224
variables: Vec::new(),
25+
skip: None,
26+
limit: None,
2327
}
2428
}
2529

30+
pub fn with_limit(&mut self, limit: usize) -> &Self {
31+
self.limit = Some(limit);
32+
self
33+
}
34+
35+
pub fn with_skip(&mut self, skip: usize) -> &Self {
36+
self.skip = Some(skip);
37+
self
38+
}
39+
2640
pub fn build_plan(&mut self, where_clause: WhereClause) -> StdResult<QueryPlan> {
2741
let bgp: Vec<QueryNode> = where_clause
2842
.iter()
@@ -33,9 +47,31 @@ impl<'a> PlanBuilder<'a> {
3347
.collect::<StdResult<Vec<QueryNode>>>()?;
3448

3549
self.build_from_bgp(bgp)
50+
.map(|mut node| {
51+
if let Some(skip) = self.skip {
52+
node = QueryNode::Skip {
53+
child: Box::new(node),
54+
first: skip,
55+
}
56+
}
57+
node
58+
})
59+
.map(|mut node| {
60+
if let Some(limit) = self.skip {
61+
node = QueryNode::Limit {
62+
child: Box::new(node),
63+
first: limit,
64+
}
65+
}
66+
node
67+
})
68+
.map(|node| QueryPlan {
69+
entrypoint: Box::new(node),
70+
variables: self.variables.clone(),
71+
})
3672
}
3773

38-
fn build_from_bgp(&self, bgp: Vec<QueryNode>) -> StdResult<QueryPlan> {
74+
fn build_from_bgp(&self, bgp: Vec<QueryNode>) -> StdResult<QueryNode> {
3975
bgp.into_iter()
4076
.reduce(|left: QueryNode, right: QueryNode| -> QueryNode {
4177
if left
@@ -56,10 +92,6 @@ impl<'a> PlanBuilder<'a> {
5692
})
5793
.map(Ok)
5894
.unwrap_or(Err(StdError::generic_err("Empty basic graph pattern")))
59-
.map(|query_node| QueryPlan {
60-
entrypoint: Box::new(query_node),
61-
variables: self.variables.clone(),
62-
})
6395
}
6496

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

0 commit comments

Comments
 (0)