Skip to content

Commit

Permalink
expand the result builder interface
Browse files Browse the repository at this point in the history
so that we can inject as parameter the info needed to render results
  • Loading branch information
lassewesth committed Mar 19, 2024
1 parent e1f9df4 commit 0f97c2a
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ RESULT_TO_CALLER processAlgorithm(
String humanReadableAlgorithmName,
Supplier<MemoryEstimation> estimationFactory,
AlgorithmComputation<RESULT_FROM_ALGORITHM> algorithmComputation,
Optional<MutateOrWriteStep<RESULT_FROM_ALGORITHM>> mutateOrWriteStep,
ResultBuilder<RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> resultBuilder
Optional<MutateOrWriteStep<CONFIGURATION, RESULT_FROM_ALGORITHM>> mutateOrWriteStep,
ResultBuilder<CONFIGURATION, RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> resultBuilder
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public <CONFIGURATION extends AlgoBaseConfig & RelationshipWeightConfig, RESULT_
String humanReadableAlgorithmName,
Supplier<MemoryEstimation> estimationFactory,
AlgorithmComputation<RESULT_FROM_ALGORITHM> algorithmComputation,
Optional<MutateOrWriteStep<RESULT_FROM_ALGORITHM>> mutateOrWriteStep,
ResultBuilder<RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> resultBuilder
Optional<MutateOrWriteStep<CONFIGURATION, RESULT_FROM_ALGORITHM>> mutateOrWriteStep,
ResultBuilder<CONFIGURATION, RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> resultBuilder
) {
var timingsBuilder = new AlgorithmProcessingTimingsBuilder();

Expand All @@ -85,18 +85,36 @@ public <CONFIGURATION extends AlgoBaseConfig & RelationshipWeightConfig, RESULT_
var graph = graphWithGraphStore.getLeft();
var graphStore = graphWithGraphStore.getRight();

if (graph.isEmpty()) return resultBuilder.build(graph, graphStore, Optional.empty(), timingsBuilder.build());
if (graph.isEmpty()) return resultBuilder.build(
graph,
graphStore,
configuration,
Optional.empty(),
timingsBuilder.build()
);

memoryGuard.assertAlgorithmCanRun(humanReadableAlgorithmName, configuration, graph, estimationFactory);

// do the actual computation
var result = computeWithTiming(timingsBuilder, humanReadableAlgorithmName, algorithmComputation, resultBuilder, graph);
var result = computeWithTiming(
timingsBuilder,
humanReadableAlgorithmName,
algorithmComputation,
resultBuilder,
graph
);

// do any side effects
mutateOrWriteWithTiming(mutateOrWriteStep, timingsBuilder, graph, graphStore, result, resultBuilder);

// inject dependencies to render results
return resultBuilder.build(graph, graphStore, Optional.ofNullable(result), timingsBuilder.build());
return resultBuilder.build(
graph,
graphStore,
configuration,
Optional.ofNullable(result),
timingsBuilder.build()
);
}

/**
Expand All @@ -116,7 +134,7 @@ <CONFIGURATION extends AlgoBaseConfig & RelationshipWeightConfig, RESULT_TO_CALL
AlgorithmProcessingTimingsBuilder timingsBuilder,
GraphName graphName,
CONFIGURATION configuration,
ResultBuilder<RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> resultBuilder
ResultBuilder<CONFIGURATION, RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> resultBuilder
) {
try (ProgressTimer ignored = ProgressTimer.start(timingsBuilder::withPreProcessingMillis)) {
// tee up the graph we want to work on
Expand All @@ -134,11 +152,11 @@ <CONFIGURATION extends AlgoBaseConfig & RelationshipWeightConfig, RESULT_TO_CALL
}
}

<RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> RESULT_FROM_ALGORITHM computeWithTiming(
<CONFIGURATION, RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> RESULT_FROM_ALGORITHM computeWithTiming(
AlgorithmProcessingTimingsBuilder timingsBuilder,
String humanReadableAlgorithmName,
AlgorithmComputation<RESULT_FROM_ALGORITHM> algorithmComputation,
ResultBuilder<RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> resultBuilder,
ResultBuilder<CONFIGURATION, RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> resultBuilder,
Graph graph
) {
try (ProgressTimer ignored = ProgressTimer.start(timingsBuilder::withComputeMillis)) {
Expand All @@ -164,13 +182,13 @@ private <RESULT_FROM_ALGORITHM> RESULT_FROM_ALGORITHM computeWithMetric(
}
}

<RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> void mutateOrWriteWithTiming(
Optional<MutateOrWriteStep<RESULT_FROM_ALGORITHM>> mutateOrWriteStep,
<CONFIGURATION, RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> void mutateOrWriteWithTiming(
Optional<MutateOrWriteStep<CONFIGURATION, RESULT_FROM_ALGORITHM>> mutateOrWriteStep,
AlgorithmProcessingTimingsBuilder timingsBuilder,
Graph graph,
GraphStore graphStore,
RESULT_FROM_ALGORITHM result,
ResultBuilder<RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> resultBuilder
ResultBuilder<CONFIGURATION, RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> resultBuilder
) {
mutateOrWriteStep.ifPresent(step -> {
try (ProgressTimer ignored = ProgressTimer.start(timingsBuilder::withPostProcessingMillis)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import org.neo4j.gds.api.Graph;
import org.neo4j.gds.api.GraphStore;

public interface MutateOrWriteStep<RESULT_FROM_ALGORITHM> {
public interface MutateOrWriteStep<CONFIGURATION, RESULT_FROM_ALGORITHM> {
<RESULT_TO_CALLER> void execute(
Graph graph,
GraphStore graphStore,
RESULT_FROM_ALGORITHM result,
ResultBuilder<RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> resultBuilder
ResultBuilder<CONFIGURATION, RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> resultBuilder
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public PathFindingAlgorithmsMutateModeBusinessFacade(
public <RESULT> RESULT singlePairShortestPathAStarMutate(
GraphName graphName,
ShortestPathAStarMutateConfig configuration,
ResultBuilder<PathFindingResult, RESULT> resultBuilder
ResultBuilder<ShortestPathAStarMutateConfig, PathFindingResult, RESULT> resultBuilder
) {
var mutateStep = new ShortestPathMutateStep(configuration);
var mutateStep = new ShortestPathMutateStep<ShortestPathAStarMutateConfig>(configuration);

return algorithmProcessingTemplate.processAlgorithm(
graphName,
Expand All @@ -72,9 +72,9 @@ public <RESULT> RESULT singlePairShortestPathAStarMutate(
public <RESULT> RESULT singlePairShortestPathDijkstraMutate(
GraphName graphName,
ShortestPathDijkstraMutateConfig configuration,
ResultBuilder<PathFindingResult, RESULT> resultBuilder
ResultBuilder<ShortestPathDijkstraMutateConfig, PathFindingResult, RESULT> resultBuilder
) {
var mutateStep = new ShortestPathMutateStep(configuration);
var mutateStep = new ShortestPathMutateStep<ShortestPathDijkstraMutateConfig>(configuration);

return algorithmProcessingTemplate.processAlgorithm(
graphName,
Expand All @@ -90,9 +90,9 @@ public <RESULT> RESULT singlePairShortestPathDijkstraMutate(
public <RESULT> RESULT singlePairShortestPathYensMutate(
GraphName graphName,
ShortestPathYensMutateConfig configuration,
ResultBuilder<PathFindingResult, RESULT> resultBuilder
ResultBuilder<ShortestPathYensMutateConfig, PathFindingResult, RESULT> resultBuilder
) {
var mutateStep = new ShortestPathMutateStep(configuration);
var mutateStep = new ShortestPathMutateStep<ShortestPathYensMutateConfig>(configuration);

return algorithmProcessingTemplate.processAlgorithm(
graphName,
Expand All @@ -108,9 +108,9 @@ public <RESULT> RESULT singlePairShortestPathYensMutate(
public <RESULT> RESULT singleSourceShortestPathDijkstraMutate(
GraphName graphName,
AllShortestPathsDijkstraMutateConfig configuration,
ResultBuilder<PathFindingResult, RESULT> resultBuilder
ResultBuilder<AllShortestPathsDijkstraMutateConfig, PathFindingResult, RESULT> resultBuilder
) {
var mutateStep = new ShortestPathMutateStep(configuration);
var mutateStep = new ShortestPathMutateStep<AllShortestPathsDijkstraMutateConfig>(configuration);

return algorithmProcessingTemplate.processAlgorithm(
graphName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public PathFindingAlgorithmsStreamModeBusinessFacade(
public <RESULT> RESULT singlePairShortestPathAStarStream(
GraphName graphName,
ShortestPathAStarStreamConfig configuration,
ResultBuilder<PathFindingResult, RESULT> resultBuilder
ResultBuilder<ShortestPathAStarStreamConfig, PathFindingResult, RESULT> resultBuilder
) {
return algorithmProcessingTemplate.processAlgorithm(
graphName,
Expand All @@ -74,7 +74,7 @@ public <RESULT> RESULT singlePairShortestPathAStarStream(
public <RESULT> RESULT singlePairShortestPathDijkstraStream(
GraphName graphName,
ShortestPathDijkstraStreamConfig configuration,
ResultBuilder<PathFindingResult, RESULT> resultBuilder
ResultBuilder<ShortestPathDijkstraStreamConfig, PathFindingResult, RESULT> resultBuilder
) {
return algorithmProcessingTemplate.processAlgorithm(
graphName,
Expand All @@ -90,7 +90,7 @@ public <RESULT> RESULT singlePairShortestPathDijkstraStream(
public <RESULT> RESULT singlePairShortestPathYensStream(
GraphName graphName,
ShortestPathYensStreamConfig configuration,
ResultBuilder<PathFindingResult, RESULT> resultBuilder
ResultBuilder<ShortestPathYensStreamConfig, PathFindingResult, RESULT> resultBuilder
) {
return algorithmProcessingTemplate.processAlgorithm(
graphName,
Expand All @@ -106,7 +106,7 @@ public <RESULT> RESULT singlePairShortestPathYensStream(
public <RESULT> RESULT singleSourceShortestPathDijkstraStream(
GraphName graphName,
AllShortestPathsDijkstraStreamConfig configuration,
ResultBuilder<PathFindingResult, RESULT> resultBuilder
ResultBuilder<AllShortestPathsDijkstraStreamConfig, PathFindingResult, RESULT> resultBuilder
) {
return algorithmProcessingTemplate.processAlgorithm(
graphName,
Expand All @@ -122,7 +122,7 @@ public <RESULT> RESULT singleSourceShortestPathDijkstraStream(
public <RESULT> RESULT steinerTreeStream(
GraphName graphName,
SteinerTreeStreamConfig configuration,
ResultBuilder<SteinerTreeResult, RESULT> resultBuilder
ResultBuilder<SteinerTreeStreamConfig, SteinerTreeResult, RESULT> resultBuilder
) {
return algorithmProcessingTemplate.processAlgorithm(
graphName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public PathFindingAlgorithmsWriteModeBusinessFacade(
public <RESULT> RESULT singlePairShortestPathAStarWrite(
GraphName graphName,
ShortestPathAStarWriteConfig configuration,
ResultBuilder<PathFindingResult, RESULT> resultBuilder
ResultBuilder<ShortestPathAStarWriteConfig, PathFindingResult, RESULT> resultBuilder
) {
return runAlgorithmAndWrite(
graphName,
Expand All @@ -93,7 +93,7 @@ public <RESULT> RESULT singlePairShortestPathAStarWrite(
public <RESULT> RESULT singlePairShortestPathDijkstraWrite(
GraphName graphName,
ShortestPathDijkstraWriteConfig configuration,
ResultBuilder<PathFindingResult, RESULT> resultBuilder
ResultBuilder<ShortestPathDijkstraWriteConfig, PathFindingResult, RESULT> resultBuilder
) {
return runAlgorithmAndWrite(
graphName,
Expand All @@ -108,7 +108,7 @@ public <RESULT> RESULT singlePairShortestPathDijkstraWrite(
public <RESULT> RESULT singlePairShortestPathYensWrite(
GraphName graphName,
ShortestPathYensWriteConfig configuration,
ResultBuilder<PathFindingResult, RESULT> resultBuilder
ResultBuilder<ShortestPathYensWriteConfig, PathFindingResult, RESULT> resultBuilder
) {
return runAlgorithmAndWrite(
graphName,
Expand All @@ -123,7 +123,7 @@ public <RESULT> RESULT singlePairShortestPathYensWrite(
public <RESULT> RESULT singleSourceShortestPathDijkstraWrite(
GraphName graphName,
AllShortestPathsDijkstraWriteConfig configuration,
ResultBuilder<PathFindingResult, RESULT> resultBuilder
ResultBuilder<AllShortestPathsDijkstraWriteConfig, PathFindingResult, RESULT> resultBuilder
) {
return runAlgorithmAndWrite(
graphName,
Expand All @@ -141,9 +141,9 @@ private <CONFIGURATION extends AlgoBaseConfig & RelationshipWeightConfig & Write
String label,
Supplier<MemoryEstimation> memoryEstimation,
AlgorithmComputation<PathFindingResult> algorithm,
ResultBuilder<PathFindingResult, RESULT> resultBuilder
ResultBuilder<CONFIGURATION, PathFindingResult, RESULT> resultBuilder
) {
MutateOrWriteStep<PathFindingResult> writeStep = new ShortestPathWriteStep<>(
MutateOrWriteStep<CONFIGURATION, PathFindingResult> writeStep = new ShortestPathWriteStep<>(
log,
relationshipStreamExporterBuilder,
taskRegistryFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* This class is generic in the union type sense, it has fields and accessors for lots of stuff,
* where any given usage probably won't need all of them.
*/
public abstract class ResultBuilder<RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> {
public abstract class ResultBuilder<CONFIGURATION, RESULT_FROM_ALGORITHM, RESULT_TO_CALLER> {
// a marker
private static final int NOT_AVAILABLE = -1;

Expand All @@ -59,11 +59,13 @@ public void withRelationshipsWritten(long relationshipsWritten) {
* You implement this and use as much or as little of the gathered data as is appropriate.
* Plus your own injected dependencies of course.
*
* @param result empty when graph was empty
* @param configuration
* @param result empty when graph was empty
*/
public abstract RESULT_TO_CALLER build(
Graph graph,
GraphStore graphStore,
CONFIGURATION configuration,
Optional<RESULT_FROM_ALGORITHM> result,
AlgorithmProcessingTimings timings
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import static org.neo4j.gds.paths.dijkstra.config.ShortestPathDijkstraWriteConfig.TOTAL_COST_KEY;

class ShortestPathMutateStep implements MutateOrWriteStep<PathFindingResult> {
class ShortestPathMutateStep<CONFIGURATION> implements MutateOrWriteStep<CONFIGURATION, PathFindingResult> {
private final MutateRelationshipConfig configuration;

ShortestPathMutateStep(MutateRelationshipConfig configuration) {
Expand All @@ -42,7 +42,7 @@ public <RESULT_TO_CALLER> void execute(
Graph graph,
GraphStore graphStore,
PathFindingResult result,
ResultBuilder<PathFindingResult, RESULT_TO_CALLER> resultBuilder
ResultBuilder<CONFIGURATION, PathFindingResult, RESULT_TO_CALLER> resultBuilder
) {
var mutateRelationshipType = RelationshipType.of(configuration.mutateRelationshipType());

Expand All @@ -56,13 +56,11 @@ public <RESULT_TO_CALLER> void execute(

SingleTypeRelationships relationships;

result.forEachPath(pathResult -> {
relationshipsBuilder.addFromInternal(
pathResult.sourceNode(),
pathResult.targetNode(),
pathResult.totalCost()
);
});
result.forEachPath(pathResult -> relationshipsBuilder.addFromInternal(
pathResult.sourceNode(),
pathResult.targetNode(),
pathResult.totalCost()
));

relationships = relationshipsBuilder.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
/**
* This is relationship writes as needed by path finding algorithms (for now).
*/
class ShortestPathWriteStep<CONFIGURATION extends WriteRelationshipConfig & WritePathOptionsConfig> implements MutateOrWriteStep<PathFindingResult> {
class ShortestPathWriteStep<CONFIGURATION extends WriteRelationshipConfig & WritePathOptionsConfig> implements MutateOrWriteStep<CONFIGURATION, PathFindingResult> {
private final Log log;

private final RelationshipStreamExporterBuilder exporterBuilder;
Expand Down Expand Up @@ -79,7 +79,7 @@ public <RESULT_TO_CALLER> void execute(
Graph graph,
GraphStore graphStore,
PathFindingResult result,
ResultBuilder<PathFindingResult, RESULT_TO_CALLER> resultBuilder
ResultBuilder<CONFIGURATION, PathFindingResult, RESULT_TO_CALLER> resultBuilder
) {
var writeNodeIds = configuration.writeNodeIds();
var writeCosts = configuration.writeCosts();
Expand Down
Loading

0 comments on commit 0f97c2a

Please sign in to comment.