From e9cef7425ddb12aa50a40c1fe1107a4b58041773 Mon Sep 17 00:00:00 2001 From: imbajin Date: Wed, 12 Jan 2022 20:08:59 +0800 Subject: [PATCH] fix: useless ring detection removed valid path (#1723) * remove useless ring detection due to it will remove valid path * limit max depth to 5000 to avoid stackoverflow --- .../hugegraph/api/traversers/NeighborRankAPI.java | 2 +- .../com/baidu/hugegraph/api/traversers/PathsAPI.java | 7 ++++--- .../hugegraph/api/traversers/PersonalRankAPI.java | 2 +- .../hugegraph/traversal/algorithm/HugeTraverser.java | 2 +- .../hugegraph/traversal/algorithm/PathsTraverser.java | 11 +++-------- 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/NeighborRankAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/NeighborRankAPI.java index 6957e99e57..0a69036a66 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/NeighborRankAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/NeighborRankAPI.java @@ -68,7 +68,7 @@ public String neighborRank(@Context GraphManager manager, "The source of rank request can't be null"); E.checkArgument(request.steps != null && !request.steps.isEmpty(), "The steps of rank request can't be empty"); - E.checkArgument(request.steps.size() <= Long.parseLong(DEFAULT_MAX_DEPTH), + E.checkArgument(request.steps.size() <= DEFAULT_MAX_DEPTH, "The steps length of rank request can't exceed %s", DEFAULT_MAX_DEPTH); E.checkArgument(request.alpha > 0 && request.alpha <= 1.0, diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PathsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PathsAPI.java index 095463b7cb..9824a4b938 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PathsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PathsAPI.java @@ -21,6 +21,7 @@ import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_CAPACITY; import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_MAX_DEGREE; +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_MAX_DEPTH; import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_PATHS_LIMIT; import java.util.Collection; @@ -114,9 +115,9 @@ public String post(@Context GraphManager manager, "The targets of request can't be null"); E.checkArgumentNotNull(request.step, "The step of request can't be null"); - E.checkArgument(request.depth > 0, - "The depth of request must be > 0, but got: %s", - request.depth); + E.checkArgument(request.depth > 0 && request.depth <= DEFAULT_MAX_DEPTH, + "The depth of request must be in (0, %s], " + + "but got: %s", DEFAULT_MAX_DEPTH, request.depth); LOG.debug("Graph [{}] get paths from source vertices '{}', target " + "vertices '{}', with step '{}', max depth '{}', " + diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PersonalRankAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PersonalRankAPI.java index d94a2135fc..9328798a41 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PersonalRankAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PersonalRankAPI.java @@ -82,7 +82,7 @@ public String personalRank(@Context GraphManager manager, "The limit of rank request must be > 0 or == -1, " + "but got: %s", request.limit); E.checkArgument(request.maxDepth > 1L && - request.maxDepth <= Long.parseLong(DEFAULT_MAX_DEPTH), + request.maxDepth <= DEFAULT_MAX_DEPTH, "The max depth of rank request must be " + "in range (1, %s], but got '%s'", DEFAULT_MAX_DEPTH, request.maxDepth); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java index 70dda43d90..84dc3a2070 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java @@ -82,8 +82,8 @@ public class HugeTraverser { public static final String DEFAULT_MAX_DEGREE = "10000"; public static final String DEFAULT_SKIP_DEGREE = "100000"; public static final String DEFAULT_SAMPLE = "100"; - public static final String DEFAULT_MAX_DEPTH = "50"; public static final String DEFAULT_WEIGHT = "0"; + public static final int DEFAULT_MAX_DEPTH = 5000; protected static final int MAX_VERTICES = 10; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java index 641e3da011..8de63f9e24 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java @@ -20,7 +20,6 @@ package com.baidu.hugegraph.traversal.algorithm; import java.util.Iterator; -import java.util.Objects; import org.apache.tinkerpop.gremlin.structure.Edge; @@ -52,7 +51,9 @@ public PathSet paths(Id sourceV, Directions sourceDir, sourceDir == targetDir.opposite(), "Source direction must equal to target direction" + " or opposite to target direction"); - checkPositive(depth, "max depth"); + E.checkArgument(depth > 0 && depth <= DEFAULT_MAX_DEPTH, + "The depth must be in (0, %s], but got: %s", + DEFAULT_MAX_DEPTH, depth); checkDegree(degree); checkCapacity(capacity); checkLimit(limit); @@ -124,9 +125,6 @@ public void forward(Id targetV, Directions direction) { PathSet results = this.record.findPath(target, null, true, false); for (Path path : results) { - if (Objects.equals(target, targetV)) { - continue; - } this.paths.add(path); if (this.reachLimit()) { return; @@ -160,9 +158,6 @@ public void backward(Id sourceV, Directions direction) { PathSet results = this.record.findPath(target, null, true, false); for (Path path : results) { - if (Objects.equals(target, sourceV)) { - continue; - } this.paths.add(path); if (this.reachLimit()) { return;