Skip to content

Commit

Permalink
Fixed path expander 'limit' interaction with 'minLevel' (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
InverseFalcon authored and jexp committed Apr 12, 2017
1 parent 4808c3b commit e57ac60
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/main/java/apoc/path/PathExplorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public static Traverser traverse(TraversalDescription traversalDescription, Iter
if (maxLevel != -1) td = td.evaluator(Evaluators.toDepth((int) maxLevel));

if (labelFilter != null && !labelFilter.trim().isEmpty()) {
td = td.evaluator(new LabelEvaluator(labelFilter, filterStartNode, limit));
td = td.evaluator(new LabelEvaluator(labelFilter, filterStartNode, limit, (int) minLevel));
}

td = td.uniqueness(uniqueness); // this is how Cypher works !! Uniqueness.RELATIONSHIP_PATH
Expand All @@ -185,11 +185,13 @@ public static class LabelEvaluator implements Evaluator {
private boolean endNodesOnly;
private boolean filterStartNode;
private long limit = -1;
private long minLevel = -1;
private long resultCount = 0;

public LabelEvaluator(String labelFilter, boolean filterStartNode, long limit) {
public LabelEvaluator(String labelFilter, boolean filterStartNode, long limit, int minLevel) {
this.filterStartNode = filterStartNode;
this.limit = limit;
this.minLevel = minLevel;
Map<Character, Set<String>> labelMap = new HashMap<>(4);

if (labelFilter != null && !labelFilter.isEmpty()) {
Expand Down Expand Up @@ -231,8 +233,9 @@ public LabelEvaluator(String labelFilter, boolean filterStartNode, long limit) {

@Override
public Evaluation evaluate(Path path) {
int depth = path.length();
// if start node shouldn't be filtered
if (path.length() == 0 && !filterStartNode) {
if (depth == 0 && !filterStartNode) {
return whitelistAllowedEvaluation;
}

Expand All @@ -243,8 +246,8 @@ public Evaluation evaluate(Path path) {

Node check = path.endNode();
Evaluation result = labelExists(check, blacklistLabels) ? EXCLUDE_AND_PRUNE :
labelExists(check, terminationLabels) ? filterEndNode(check, true) :
labelExists(check, endNodeLabels) ? filterEndNode(check, false) :
labelExists(check, terminationLabels) ? filterEndNode(check, depth, true) :
labelExists(check, endNodeLabels) ? filterEndNode(check, depth, false) :
whitelistAllowed(check) ? whitelistAllowedEvaluation : EXCLUDE_AND_PRUNE;

return result;
Expand All @@ -267,8 +270,10 @@ private boolean whitelistAllowed(Node node) {
return whitelistLabels.isEmpty() || labelExists(node, whitelistLabels);
}

private Evaluation filterEndNode(Node node, boolean isTerminationFilter) {
resultCount++;
private Evaluation filterEndNode(Node node, int depth, boolean isTerminationFilter) {
if (depth >= minLevel) {
resultCount++;
}
return isTerminationFilter || !whitelistAllowed(node) ? INCLUDE_AND_PRUNE : INCLUDE_AND_CONTINUE;
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/apoc/path/ExpandPathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,20 @@ public void testEndNodeListBeforeWhitelist() {
assertEquals("Gene Hackman", path.endNode().getProperty("name"));
});
}

@Test
public void testLimitPlaysNiceWithMinLevel() {
db.execute("MATCH (c:Person) WHERE c.name in ['Clint Eastwood', 'Gene Hackman'] SET c:Western");

TestUtil.testResult(db,
"MATCH (k:Person {name:'Keanu Reeves'}) " +
"CALL apoc.path.expandConfig(k, {relationshipFilter:'ACTED_IN|PRODUCED|DIRECTED', labelFilter:'>Western', uniqueness: 'NODE_GLOBAL', limit:1, minLevel:3}) yield path " +
"return path",
result -> {
List<Map<String, Object>> maps = Iterators.asList(result);
assertEquals(1, maps.size());
Path path = (Path) maps.get(0).get("path");
assertEquals("Clint Eastwood", path.endNode().getProperty("name"));
});
}
}

0 comments on commit e57ac60

Please sign in to comment.