Skip to content

Commit aa610e8

Browse files
committed
Refactor direct path finding
Extract common logic into a function
1 parent 5d4d794 commit aa610e8

File tree

1 file changed

+60
-81
lines changed

1 file changed

+60
-81
lines changed

lib/query-planner/src/planner/walker/pathfinder.rs

Lines changed: 60 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,50 @@ pub fn find_indirect_paths(
225225
Ok(best_paths)
226226
}
227227

228+
fn try_advance_direct_path<'a>(
229+
graph: &'a Graph,
230+
path: &OperationPath,
231+
override_context: &PlannerOverrideContext,
232+
edge_ref: &EdgeReference,
233+
target: &NavigationTarget<'a>,
234+
) -> Result<Option<OperationPath>, WalkOperationError> {
235+
trace!(
236+
"Checking edge {}",
237+
graph.pretty_print_edge(edge_ref.id(), false)
238+
);
239+
240+
let can_be_satisfied = can_satisfy_edge(
241+
graph,
242+
override_context,
243+
edge_ref,
244+
path,
245+
&ExcludedFromLookup::new(),
246+
false,
247+
)?;
248+
249+
match can_be_satisfied {
250+
Some(paths) => {
251+
trace!(
252+
"Advancing path {} with edge {}",
253+
path.pretty_print(graph),
254+
graph.pretty_print_edge(edge_ref.id(), false)
255+
);
256+
257+
let next_resolution_path = path.advance(
258+
edge_ref,
259+
QueryTreeNode::from_paths(graph, &paths, None)?,
260+
target,
261+
);
262+
263+
Ok(Some(next_resolution_path))
264+
}
265+
None => {
266+
trace!("Edge not satisfied, continue look up...");
267+
Ok(None)
268+
}
269+
}
270+
}
271+
228272
#[instrument(level = "trace",skip(graph, target, override_context), fields(
229273
path = path.pretty_print(graph),
230274
current_cost = path.cost,
@@ -238,93 +282,28 @@ pub fn find_direct_paths(
238282
let mut result: Vec<OperationPath> = vec![];
239283
let path_tail_index = path.tail();
240284

241-
match target {
242-
NavigationTarget::Field(field) => {
243-
let edges_iter = graph
285+
let edges_iter: Box<dyn Iterator<Item = _>> = match target {
286+
NavigationTarget::Field(field) => Box::new(
287+
graph
244288
.edges_from(path_tail_index)
245-
.filter(|e| matches!(e.weight(), Edge::FieldMove(f) if f.name == field.name));
246-
for edge_ref in edges_iter {
247-
trace!(
248-
"checking edge {}",
249-
graph.pretty_print_edge(edge_ref.id(), false)
250-
);
251-
252-
let can_be_satisfied = can_satisfy_edge(
253-
graph,
254-
override_context,
255-
&edge_ref,
256-
path,
257-
&ExcludedFromLookup::new(),
258-
false,
259-
)?;
260-
261-
match can_be_satisfied {
262-
Some(paths) => {
263-
trace!(
264-
"Advancing path {} with edge {}",
265-
path.pretty_print(graph),
266-
graph.pretty_print_edge(edge_ref.id(), false)
267-
);
268-
269-
let next_resolution_path = path.advance(
270-
&edge_ref,
271-
QueryTreeNode::from_paths(graph, &paths, None)?,
272-
target,
273-
);
274-
275-
result.push(next_resolution_path);
276-
}
277-
None => {
278-
trace!("Edge not satisfied, continue look up...");
279-
}
280-
}
281-
}
282-
}
283-
NavigationTarget::ConcreteType(type_name, _condition) => {
284-
let edges_iter = graph
289+
.filter(move |e| matches!(e.weight(), Edge::FieldMove(f) if f.name == field.name)),
290+
),
291+
NavigationTarget::ConcreteType(type_name, _condition) => Box::new(
292+
graph
285293
.edges_from(path_tail_index)
286-
.filter(|e| match e.weight() {
294+
.filter(move |e| match e.weight() {
287295
Edge::AbstractMove(t) => t == type_name,
288296
Edge::InterfaceObjectTypeMove(t) => &t.object_type_name == type_name,
289297
_ => false,
290-
});
291-
292-
for edge_ref in edges_iter {
293-
trace!(
294-
"Checking edge {}",
295-
graph.pretty_print_edge(edge_ref.id(), false)
296-
);
297-
298-
let can_be_satisfied = can_satisfy_edge(
299-
graph,
300-
override_context,
301-
&edge_ref,
302-
path,
303-
&ExcludedFromLookup::new(),
304-
false,
305-
)?;
306-
307-
match can_be_satisfied {
308-
Some(paths) => {
309-
trace!(
310-
"Advancing path {} with edge {}",
311-
path.pretty_print(graph),
312-
graph.pretty_print_edge(edge_ref.id(), false)
313-
);
314-
315-
let next_resolution_path = path.advance(
316-
&edge_ref,
317-
QueryTreeNode::from_paths(graph, &paths, None)?,
318-
target,
319-
);
298+
}),
299+
),
300+
};
320301

321-
result.push(next_resolution_path);
322-
}
323-
None => {
324-
trace!("Edge not satisfied, continue look up...");
325-
}
326-
}
327-
}
302+
for edge_ref in edges_iter {
303+
if let Some(new_path) =
304+
try_advance_direct_path(graph, path, override_context, &edge_ref, target)?
305+
{
306+
result.push(new_path);
328307
}
329308
}
330309

0 commit comments

Comments
 (0)