Skip to content

Commit

Permalink
[HQABUD5U] Improve coverNodes speed (#3439)
Browse files Browse the repository at this point in the history
  • Loading branch information
gem-neo4j authored Feb 1, 2023
1 parent 1e3209c commit 5a1ff56
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions core/src/main/java/apoc/algo/Cover.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.neo4j.procedure.Procedure;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -26,25 +27,16 @@ public class Cover {
@Description("apoc.algo.cover(nodes) yield rel - returns all relationships between this set of nodes")
public Stream<RelationshipResult> cover(@Name("nodes") Object nodes) {
Set<Node> nodeSet = Util.nodeStream(tx, nodes).collect(Collectors.toSet());
/*return nodeSet.parallelStream()
.flatMap(n ->
Util.inTx(db,() ->
StreamSupport.stream(n.getRelationships(Direction.OUTGOING)
.spliterator(),false)
.filter(r -> nodeSet.contains(r.getEndNode()))
.map(RelationshipResult::new)));*/
// NB prallel approach doesn't work in 3.4 (see SubgraphTest.testSubgraphAllShouldContainExpectedNodesAndRels
// so falling back to single threaded
// TODO: consider using multithreading and maybe kernel API here
return coverNodes(nodeSet).map(RelationshipResult::new);
}

// non-parallelized utility method for use by other procedures
public static Stream<Relationship> coverNodes(Collection<Node> nodes) {
Set<Node> nodeSet = new HashSet<>(nodes);
return nodes.stream()
.flatMap(n ->
StreamSupport.stream(n.getRelationships(Direction.OUTGOING)
.spliterator(),false)
.filter(r -> nodes.contains(r.getEndNode())));
.filter(r -> nodeSet.contains(r.getEndNode())));
}
}

0 comments on commit 5a1ff56

Please sign in to comment.