Skip to content

Commit dec5e8a

Browse files
committed
Add ShortestPathWriteResultTransformer
1 parent b158b89 commit dec5e8a

File tree

6 files changed

+195
-40
lines changed

6 files changed

+195
-40
lines changed

procedures/pushback-procedures-facade/src/main/java/org/neo4j/gds/procedures/algorithms/pathfinding/write/BellmanFordWriteResultTransformer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
class BellmanFordWriteResultTransformer implements ResultTransformer<TimedAlgorithmResult<BellmanFordResult>, Stream<BellmanFordWriteResult>> {
3939

40-
private final BellmanFordWriteStep bellmanFordWriteStep;
40+
private final BellmanFordWriteStep writeStep;
4141
private final Graph graph;
4242
private final GraphStore graphStore;
4343
@Deprecated(forRemoval = true)
@@ -46,15 +46,15 @@ class BellmanFordWriteResultTransformer implements ResultTransformer<TimedAlgori
4646
private final Map<String, Object> configuration;
4747

4848
BellmanFordWriteResultTransformer(
49-
BellmanFordWriteStep bellmanFordWriteStep,
49+
BellmanFordWriteStep writeStep,
5050
Graph graph,
5151
GraphStore graphStore,
5252
@Deprecated(forRemoval = true)
5353
ResultStore resultStore,
5454
JobId jobId,
5555
Map<String, Object> configuration
5656
) {
57-
this.bellmanFordWriteStep = bellmanFordWriteStep;
57+
this.writeStep = writeStep;
5858
this.graph = graph;
5959
this.graphStore = graphStore;
6060
this.resultStore = resultStore;
@@ -69,7 +69,7 @@ public Stream<BellmanFordWriteResult> apply(TimedAlgorithmResult<BellmanFordResu
6969
var writeMillis = new AtomicLong();
7070
var result = algorithmResult.result();
7171
try (var ignored = ProgressTimer.start(writeMillis::set)) {
72-
relationshipsWritten = bellmanFordWriteStep.execute(
72+
relationshipsWritten = writeStep.execute(
7373
graph,
7474
graphStore,
7575
resultStore,

procedures/pushback-procedures-facade/src/main/java/org/neo4j/gds/procedures/algorithms/pathfinding/write/PushbackPathFindingWriteProcedureFacade.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public Stream<StandardWriteRelationshipsResult> deltaStepping(
131131
config.toParameters(),
132132
config.jobId(),
133133
config.logProgress(),
134-
new DeltaSteppingWriteResultTransformerBuilder(writeStep, config)
134+
new ShortestPathWriteResultTransformerBuilder(writeStep, config.jobId(), config.toMap())
135135
).join();
136136
}
137137

@@ -219,7 +219,7 @@ public Stream<StandardWriteRelationshipsResult> singlePairShortestPathAStar(
219219
config.toParameters(),
220220
config.jobId(),
221221
config.logProgress(),
222-
new ShortestPathWriteResultTransformer(writeStep)
222+
new ShortestPathWriteResultTransformerBuilder(writeStep, config.jobId(), config.toMap())
223223
).join();
224224
}
225225

@@ -248,7 +248,7 @@ public Stream<StandardWriteRelationshipsResult> singlePairShortestPathDijkstra(
248248
config.toParameters(),
249249
config.jobId(),
250250
config.logProgress(),
251-
new ShortestPathWriteResultTransformer(writeStep)
251+
new ShortestPathWriteResultTransformerBuilder(writeStep, config.jobId(), config.toMap())
252252
).join();
253253
}
254254

@@ -278,7 +278,7 @@ public Stream<StandardWriteRelationshipsResult> singlePairShortestPathYens(
278278
config.toParameters(),
279279
config.jobId(),
280280
config.logProgress(),
281-
new ShortestPathWriteResultTransformer(writeStep)
281+
new ShortestPathWriteResultTransformerBuilder(writeStep, config.jobId(), config.toMap())
282282
).join();
283283
}
284284

@@ -308,7 +308,7 @@ public Stream<StandardWriteRelationshipsResult> singleSourceShortestPathDijkstra
308308
config.toSingleSourceParameters(),
309309
config.jobId(),
310310
config.logProgress(),
311-
new ShortestPathWriteResultTransformer(writeStep)
311+
new ShortestPathWriteResultTransformerBuilder(writeStep, config.jobId(), config.toMap())
312312
).join();
313313
}
314314

procedures/pushback-procedures-facade/src/main/java/org/neo4j/gds/procedures/algorithms/pathfinding/write/ShortestPathWriteResultTransformer.java

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,73 @@
1919
*/
2020
package org.neo4j.gds.procedures.algorithms.pathfinding.write;
2121

22-
import org.neo4j.gds.core.loading.GraphResources;
22+
import org.neo4j.gds.api.Graph;
23+
import org.neo4j.gds.api.GraphStore;
24+
import org.neo4j.gds.api.ResultStore;
25+
import org.neo4j.gds.applications.algorithms.metadata.RelationshipsWritten;
26+
import org.neo4j.gds.core.utils.ProgressTimer;
27+
import org.neo4j.gds.core.utils.progress.JobId;
2328
import org.neo4j.gds.pathfinding.ShortestPathWriteStep;
2429
import org.neo4j.gds.paths.dijkstra.PathFindingResult;
2530
import org.neo4j.gds.procedures.algorithms.results.StandardWriteRelationshipsResult;
2631
import org.neo4j.gds.result.TimedAlgorithmResult;
2732
import org.neo4j.gds.results.ResultTransformer;
28-
import org.neo4j.gds.results.ResultTransformerBuilder;
2933

34+
import java.util.Map;
35+
import java.util.concurrent.atomic.AtomicLong;
3036
import java.util.stream.Stream;
3137

32-
public class ShortestPathWriteResultTransformer implements ResultTransformerBuilder<TimedAlgorithmResult<PathFindingResult>, Stream<StandardWriteRelationshipsResult>> {
33-
public ShortestPathWriteResultTransformer(ShortestPathWriteStep writeStep) {}
38+
class ShortestPathWriteResultTransformer implements ResultTransformer<TimedAlgorithmResult<PathFindingResult>, Stream<StandardWriteRelationshipsResult>> {
39+
private final ShortestPathWriteStep writeStep;
40+
private final Graph graph;
41+
private final GraphStore graphStore;
42+
@Deprecated(forRemoval = true)
43+
private final ResultStore resultStore;
44+
private final JobId jobId;
45+
private final Map<String, Object> configurationMap;
3446

35-
@Override
36-
public ResultTransformer<TimedAlgorithmResult<PathFindingResult>, Stream<StandardWriteRelationshipsResult>> build(
37-
GraphResources graphResources
47+
ShortestPathWriteResultTransformer(
48+
ShortestPathWriteStep writeStep,
49+
Graph graph,
50+
GraphStore graphStore,
51+
@Deprecated(forRemoval = true)
52+
ResultStore resultStore,
53+
JobId jobId,
54+
Map<String, Object> configurationMap
3855
) {
39-
return ar -> Stream.empty();
56+
this.writeStep = writeStep;
57+
this.graph = graph;
58+
this.graphStore = graphStore;
59+
this.resultStore = resultStore;
60+
this.jobId = jobId;
61+
this.configurationMap = configurationMap;
62+
}
63+
64+
@Override
65+
public Stream<StandardWriteRelationshipsResult> apply(TimedAlgorithmResult<PathFindingResult> algorithmResult) {
66+
67+
RelationshipsWritten relationshipsWritten;
68+
var writeMillis = new AtomicLong();
69+
var result = algorithmResult.result();
70+
try (var ignored = ProgressTimer.start(writeMillis::set)) {
71+
relationshipsWritten = writeStep.execute(
72+
graph,
73+
graphStore,
74+
resultStore,
75+
result,
76+
jobId
77+
);
78+
}
79+
80+
return Stream.of(
81+
new StandardWriteRelationshipsResult(
82+
0,
83+
algorithmResult.computeMillis(),
84+
0,
85+
writeMillis.get(),
86+
relationshipsWritten.value(),
87+
configurationMap
88+
)
89+
);
4090
}
4191
}

procedures/pushback-procedures-facade/src/main/java/org/neo4j/gds/procedures/algorithms/pathfinding/write/DeltaSteppingWriteResultTransformerBuilder.java renamed to procedures/pushback-procedures-facade/src/main/java/org/neo4j/gds/procedures/algorithms/pathfinding/write/ShortestPathWriteResultTransformerBuilder.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,43 @@
2020
package org.neo4j.gds.procedures.algorithms.pathfinding.write;
2121

2222
import org.neo4j.gds.core.loading.GraphResources;
23+
import org.neo4j.gds.core.utils.progress.JobId;
2324
import org.neo4j.gds.pathfinding.ShortestPathWriteStep;
24-
import org.neo4j.gds.paths.delta.config.AllShortestPathsDeltaWriteConfig;
2525
import org.neo4j.gds.paths.dijkstra.PathFindingResult;
2626
import org.neo4j.gds.procedures.algorithms.results.StandardWriteRelationshipsResult;
2727
import org.neo4j.gds.result.TimedAlgorithmResult;
2828
import org.neo4j.gds.results.ResultTransformer;
2929
import org.neo4j.gds.results.ResultTransformerBuilder;
3030

31+
import java.util.Map;
3132
import java.util.stream.Stream;
3233

33-
class DeltaSteppingWriteResultTransformerBuilder implements ResultTransformerBuilder<TimedAlgorithmResult<PathFindingResult>, Stream<StandardWriteRelationshipsResult>> {
34-
DeltaSteppingWriteResultTransformerBuilder(ShortestPathWriteStep writeStep, AllShortestPathsDeltaWriteConfig config) {}
34+
class ShortestPathWriteResultTransformerBuilder implements ResultTransformerBuilder<TimedAlgorithmResult<PathFindingResult>, Stream<StandardWriteRelationshipsResult>> {
35+
private final ShortestPathWriteStep writeStep;
36+
private final JobId jobId;
37+
private final Map<String, Object> configurationMap;
38+
39+
ShortestPathWriteResultTransformerBuilder(
40+
ShortestPathWriteStep writeStep,
41+
JobId jobId,
42+
Map<String, Object> configurationMap
43+
) {
44+
this.writeStep = writeStep;
45+
this.jobId = jobId;
46+
this.configurationMap = configurationMap;
47+
}
3548

3649
@Override
3750
public ResultTransformer<TimedAlgorithmResult<PathFindingResult>, Stream<StandardWriteRelationshipsResult>> build(
3851
GraphResources graphResources
3952
) {
40-
return ar -> Stream.empty();
53+
return new ShortestPathWriteResultTransformer(
54+
writeStep,
55+
graphResources.graph(),
56+
graphResources.graphStore(),
57+
graphResources.resultStore(),
58+
jobId,
59+
configurationMap
60+
);
4161
}
4262
}

procedures/pushback-procedures-facade/src/test/java/org/neo4j/gds/procedures/algorithms/pathfinding/write/BellmanFordWriteResultTransformerTest.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,15 @@
4545
class BellmanFordWriteResultTransformerTest {
4646

4747
@Mock
48-
private BellmanFordWriteStep bellmanFordWriteStep;
48+
private BellmanFordWriteStep bellmanFordWriteStepMock;
4949
@Mock
50-
private Graph graph;
50+
private Graph graphMock;
5151
@Mock
52-
private GraphStore graphStore;
52+
private GraphStore graphStoreMock;
5353
@Mock
54-
private ResultStore resultStore;
54+
private ResultStore resultStoreMock;
5555
@Mock
56-
private JobId jobId;
57-
56+
private JobId jobIdMock;
5857
@Mock
5958
private TimedAlgorithmResult<BellmanFordResult> timedResultMock;
6059

@@ -63,18 +62,13 @@ class BellmanFordWriteResultTransformerTest {
6362

6463
@BeforeEach
6564
void setUp() {
66-
bellmanFordWriteStep = mock(BellmanFordWriteStep.class);
67-
graph = mock(Graph.class);
68-
graphStore = mock(GraphStore.class);
69-
resultStore = mock(ResultStore.class);
70-
jobId = mock(JobId.class);
7165
configuration = Map.of("key", "value");
7266
transformer = new BellmanFordWriteResultTransformer(
73-
bellmanFordWriteStep,
74-
graph,
75-
graphStore,
76-
resultStore,
77-
jobId,
67+
bellmanFordWriteStepMock,
68+
graphMock,
69+
graphStoreMock,
70+
resultStoreMock,
71+
jobIdMock,
7872
configuration
7973
);
8074
}
@@ -89,7 +83,7 @@ void shouldTransformResultCorrectly() {
8983

9084
var relationshipsWritten = new RelationshipsWritten(42);
9185

92-
when(bellmanFordWriteStep.execute(graph, graphStore, resultStore, bellmanFordResult, jobId))
86+
when(bellmanFordWriteStepMock.execute(graphMock, graphStoreMock, resultStoreMock, bellmanFordResult, jobIdMock))
9387
.thenReturn(relationshipsWritten);
9488

9589
Stream<BellmanFordWriteResult> resultStream = transformer.apply(timedResultMock);
@@ -112,8 +106,7 @@ void shouldHandleNegativeCycle() {
112106

113107
var relationshipsWritten = new RelationshipsWritten(0);
114108

115-
when(bellmanFordWriteStep.execute(graph, graphStore, resultStore, bellmanFordResult, jobId))
116-
.thenReturn(relationshipsWritten);
109+
when(bellmanFordWriteStepMock.execute(graphMock, graphStoreMock, resultStoreMock, bellmanFordResult, jobIdMock)) .thenReturn(relationshipsWritten);
117110

118111
var resultStream = transformer.apply(timedResultMock);
119112
var result = resultStream.findFirst().orElseThrow();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.procedures.algorithms.pathfinding.write;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.mockito.Mock;
25+
import org.mockito.junit.jupiter.MockitoExtension;
26+
import org.neo4j.gds.api.Graph;
27+
import org.neo4j.gds.api.GraphStore;
28+
import org.neo4j.gds.api.ResultStore;
29+
import org.neo4j.gds.applications.algorithms.metadata.RelationshipsWritten;
30+
import org.neo4j.gds.core.utils.progress.JobId;
31+
import org.neo4j.gds.pathfinding.ShortestPathWriteStep;
32+
import org.neo4j.gds.paths.dijkstra.PathFindingResult;
33+
import org.neo4j.gds.procedures.algorithms.results.StandardWriteRelationshipsResult;
34+
import org.neo4j.gds.result.TimedAlgorithmResult;
35+
36+
import java.util.Map;
37+
import java.util.stream.Stream;
38+
39+
import static org.assertj.core.api.Assertions.assertThat;
40+
import static org.mockito.Mockito.mock;
41+
import static org.mockito.Mockito.when;
42+
43+
@ExtendWith(MockitoExtension.class)
44+
class ShortestPathWriteResultTransformerTest {
45+
46+
@Mock
47+
private ShortestPathWriteStep writeStepMock;
48+
@Mock
49+
private Graph graphMock;
50+
@Mock
51+
private GraphStore graphStoreMock;
52+
@Mock
53+
private ResultStore resultStoreMock;
54+
@Mock
55+
private JobId jobIdMock;
56+
@Mock
57+
private TimedAlgorithmResult<PathFindingResult> timedResultMock;
58+
59+
@Test
60+
void shouldTransformResultCorrectly() {
61+
62+
var pathFindingResult = mock(PathFindingResult.class);
63+
64+
when(timedResultMock.result()).thenReturn(pathFindingResult);
65+
when(timedResultMock.computeMillis()).thenReturn(123L);
66+
67+
var relationshipsWritten = new RelationshipsWritten(42);
68+
69+
when(writeStepMock.execute(graphMock, graphStoreMock, resultStoreMock, pathFindingResult, jobIdMock))
70+
.thenReturn(relationshipsWritten);
71+
72+
var configuration = Map.<String, Object>of("key", "value");
73+
var transformer = new ShortestPathWriteResultTransformer(
74+
writeStepMock,
75+
graphMock,
76+
graphStoreMock,
77+
resultStoreMock,
78+
jobIdMock,
79+
configuration
80+
);
81+
82+
83+
Stream<StandardWriteRelationshipsResult> resultStream = transformer.apply(timedResultMock);
84+
StandardWriteRelationshipsResult result = resultStream.findFirst().orElseThrow();
85+
86+
assertThat(result.computeMillis()).isEqualTo(123L);
87+
assertThat(result.writeMillis()).isGreaterThanOrEqualTo(0L);
88+
assertThat(result.relationshipsWritten()).isEqualTo(42L);
89+
assertThat(result.configuration()).isEqualTo(configuration);
90+
}
91+
92+
}

0 commit comments

Comments
 (0)