Skip to content

Commit e241d16

Browse files
diaohancaiimbajin
andauthored
feat(core): support output filter (#303)
## Main Changes 1. computation results output supports custom write. 2. `SingleSourceShortestPathOutput` implements `org.apache.hugegraph.computer.core.output.ComputerOutput#filter` --------- Co-authored-by: imbajin <jin@apache.org>
1 parent 547d191 commit e241d16

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,8 @@ private boolean isAllTargetsReached(Vertex vertex) {
278278
}
279279
return false;
280280
}
281+
282+
public IdSet getTargetIdSet() {
283+
return this.targetIdSet;
284+
}
281285
}

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPathOutput.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import java.util.HashMap;
2121
import java.util.Map;
2222

23+
import org.apache.hugegraph.computer.core.config.Config;
2324
import org.apache.hugegraph.computer.core.graph.vertex.Vertex;
2425
import org.apache.hugegraph.computer.core.output.hg.HugeGraphOutput;
26+
import org.apache.hugegraph.computer.core.worker.Computation;
2527
import org.apache.hugegraph.util.JsonUtil;
2628

2729
public class SingleSourceShortestPathOutput extends HugeGraphOutput<String> {
@@ -45,4 +47,10 @@ protected String value(Vertex vertex) {
4547
map.put("total_weight", value.totalWeight());
4648
return JsonUtil.toJson(map);
4749
}
50+
51+
@Override
52+
public boolean filter(Config config, Computation computation, Vertex vertex) {
53+
SingleSourceShortestPath sssp = (SingleSourceShortestPath) computation;
54+
return sssp.getTargetIdSet() == null || sssp.getTargetIdSet().contains(vertex.id());
55+
}
4856
}

computer-api/src/main/java/org/apache/hugegraph/computer/core/output/ComputerOutput.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apache.hugegraph.computer.core.config.Config;
2121
import org.apache.hugegraph.computer.core.graph.vertex.Vertex;
22+
import org.apache.hugegraph.computer.core.worker.Computation;
2223

2324
/**
2425
* Computer output is used to output computer results. There is an output object
@@ -37,6 +38,14 @@ public interface ComputerOutput {
3738
*/
3839
void write(Vertex vertex);
3940

41+
/**
42+
* Write filter.
43+
* True to commit the computation result, otherwise not to commit.
44+
*/
45+
default boolean filter(Config config, Computation computation, Vertex vertex) {
46+
return true;
47+
}
48+
4049
/**
4150
* Merge output files of multiple partitions if applicable.
4251
*/

computer-core/src/main/java/org/apache/hugegraph/computer/core/compute/FileGraphPartition.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ protected PartitionStat output() {
255255
Edges edges = this.edgesInput.edges(this.vertexInput.idPointer());
256256
vertex.edges(edges);
257257

258-
output.write(vertex);
258+
if (output.filter(this.context.config(), this.computation, vertex)) {
259+
output.write(vertex);
260+
}
259261
}
260262

261263
try {

0 commit comments

Comments
 (0)