Skip to content

Commit

Permalink
migrate BFS stats mode
Browse files Browse the repository at this point in the history
  • Loading branch information
lassewesth committed Mar 21, 2024
1 parent e71819d commit 1299856
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
package org.neo4j.gds.applications.algorithms.pathfinding;

import org.neo4j.gds.api.GraphName;
import org.neo4j.gds.collections.ha.HugeLongArray;
import org.neo4j.gds.paths.traverse.BfsStatsConfig;
import org.neo4j.gds.steiner.SteinerTreeResult;
import org.neo4j.gds.steiner.SteinerTreeStatsConfig;

import java.util.Optional;

import static org.neo4j.gds.applications.algorithms.pathfinding.AlgorithmLabels.BFS;
import static org.neo4j.gds.applications.algorithms.pathfinding.AlgorithmLabels.STEINER;

public class PathFindingAlgorithmsStatsModeBusinessFacade {
Expand All @@ -43,6 +46,22 @@ public PathFindingAlgorithmsStatsModeBusinessFacade(
this.pathFindingAlgorithms = pathFindingAlgorithms;
}

public <RESULT> RESULT breadthFirstSearch(
GraphName graphName,
BfsStatsConfig configuration,
ResultBuilder<BfsStatsConfig, HugeLongArray, RESULT> resultBuilder
) {
return algorithmProcessingTemplate.processAlgorithm(
graphName,
configuration,
BFS,
() -> estimationFacade.breadthFirstSearchEstimation(configuration),
graph -> pathFindingAlgorithms.breadthFirstSearch(graph, configuration),
Optional.empty(),
resultBuilder
);
}

public <RESULT> RESULT steinerTree(
GraphName graphName,
SteinerTreeStatsConfig configuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
*/
package org.neo4j.gds.paths.traverse;

import org.neo4j.gds.BaseProc;
import org.neo4j.gds.executor.MemoryEstimationExecutor;
import org.neo4j.gds.executor.ProcedureExecutor;
import org.neo4j.gds.procedures.GraphDataScience;
import org.neo4j.gds.results.MemoryEstimateResult;
import org.neo4j.gds.results.StandardStatsResult;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.Procedure;
Expand All @@ -32,36 +31,28 @@
import java.util.stream.Stream;

import static org.neo4j.gds.paths.traverse.Constants.BFS_DESCRIPTION;
import static org.neo4j.gds.procedures.ProcedureConstants.MEMORY_ESTIMATION_DESCRIPTION;
import static org.neo4j.procedure.Mode.READ;

public class BfsStatsProc extends BaseProc {
public class BfsStatsProc {
@Context
public GraphDataScience facade;

@Procedure(name = "gds.bfs.stats", mode = READ)
@Description(BFS_DESCRIPTION)
public Stream<StandardStatsResult> stats(
@Name(value = "graphName") String graphName,
@Name(value = "configuration", defaultValue = "{}") Map<String, Object> configuration
) {
var statsSpec = new BfsStatsSpec();

return new ProcedureExecutor<>(
statsSpec,
executionContext()
).compute(graphName, configuration);
return facade.pathFinding().breadthFirstSearchStats(graphName, configuration);
}

@Procedure(name = "gds.bfs.stats.estimate", mode = READ)
@Description(ESTIMATE_DESCRIPTION)
@Description(MEMORY_ESTIMATION_DESCRIPTION)
public Stream<MemoryEstimateResult> estimate(
@Name(value = "graphNameOrConfiguration") Object graphNameOrConfiguration,
@Name(value = "algoConfiguration") Map<String, Object> algoConfiguration
) {
var statsSpec = new BfsStatsSpec();

return new MemoryEstimationExecutor<>(
statsSpec,
executionContext(),
transactionContext()
).computeEstimate(graphNameOrConfiguration, algoConfiguration);
return facade.pathFinding().breadthFirstSearchStatsEstimate(graphNameOrConfiguration, algoConfiguration);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gds.procedures.algorithms.pathfinding;

import org.neo4j.gds.api.Graph;
import org.neo4j.gds.api.GraphStore;
import org.neo4j.gds.applications.algorithms.pathfinding.AlgorithmProcessingTimings;
import org.neo4j.gds.applications.algorithms.pathfinding.ResultBuilder;
import org.neo4j.gds.collections.ha.HugeLongArray;
import org.neo4j.gds.paths.traverse.BfsStatsConfig;
import org.neo4j.gds.results.StandardStatsResult;

import java.util.Optional;
import java.util.stream.Stream;

class BfsStatsResultBuilder extends ResultBuilder<BfsStatsConfig, HugeLongArray, Stream<StandardStatsResult>> {
@Override
public Stream<StandardStatsResult> build(
Graph graph,
GraphStore graphStore,
BfsStatsConfig configuration,
Optional<HugeLongArray> result, // unused
AlgorithmProcessingTimings timings
) {
return Stream.of(
new StandardStatsResult(
timings.preProcessingMillis,
timings.computeMillis,
0,
configuration.toMap()
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.neo4j.gds.paths.dijkstra.config.AllShortestPathsDijkstraWriteConfig;
import org.neo4j.gds.paths.dijkstra.config.ShortestPathDijkstraStreamConfig;
import org.neo4j.gds.paths.dijkstra.config.ShortestPathDijkstraWriteConfig;
import org.neo4j.gds.paths.traverse.BfsStatsConfig;
import org.neo4j.gds.paths.traverse.BfsStreamConfig;
import org.neo4j.gds.paths.yens.config.ShortestPathYensStreamConfig;
import org.neo4j.gds.paths.yens.config.ShortestPathYensWriteConfig;
Expand All @@ -54,6 +55,7 @@
import org.neo4j.gds.procedures.algorithms.pathfinding.stubs.SingleSourceShortestPathDijkstraMutateStub;
import org.neo4j.gds.procedures.algorithms.pathfinding.stubs.SteinerTreeMutateStub;
import org.neo4j.gds.results.MemoryEstimateResult;
import org.neo4j.gds.results.StandardStatsResult;
import org.neo4j.gds.results.StandardWriteRelationshipsResult;
import org.neo4j.gds.steiner.SteinerTreeStatsConfig;
import org.neo4j.gds.steiner.SteinerTreeStreamConfig;
Expand Down Expand Up @@ -210,6 +212,34 @@ public BreadthFirstSearchMutateStub breadthFirstSearchMutateStub() {
return breadthFirstSearchMutateStub;
}

public Stream<StandardStatsResult> breadthFirstSearchStats(String graphName, Map<String, Object> configuration) {
var resultBuilder = new BfsStatsResultBuilder();

return runStatsAlgorithm(
graphName,
configuration,
BfsStatsConfig::of,
resultBuilder,
statsModeFacade::breadthFirstSearch
);
}

public Stream<MemoryEstimateResult> breadthFirstSearchStatsEstimate(
Object graphNameOrConfiguration,
Map<String, Object> algorithmConfiguration
) {
var result = runEstimation(
algorithmConfiguration,
BfsStatsConfig::of,
configuration -> estimationModeFacade.breadthFirstSearchEstimate(
configuration,
graphNameOrConfiguration
)
);

return Stream.of(result);
}

public Stream<BfsStreamResult> breadthFirstSearchStream(String graphName, Map<String, Object> configuration) {
var resultBuilder = new BfsStreamResultBuilder(nodeLookup, procedureReturnColumns.contains("path"));

Expand Down

0 comments on commit 1299856

Please sign in to comment.