From 99053b1b0570123830e7e6b1879121e1c7009f93 Mon Sep 17 00:00:00 2001 From: imbajin Date: Tue, 14 Dec 2021 18:40:27 +0800 Subject: [PATCH 1/2] refact: params improve for personal rank api --- .../api/traversers/CrosspointsAPI.java | 3 ++- .../api/traversers/PersonalRankAPI.java | 26 +++++++++++++------ .../traversal/algorithm/HugeTraverser.java | 6 ++++- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CrosspointsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CrosspointsAPI.java index 30ca26ac52..8b8f43301e 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CrosspointsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CrosspointsAPI.java @@ -83,7 +83,8 @@ public String get(@Context GraphManager manager, PathsTraverser traverser = new PathsTraverser(g); HugeTraverser.PathSet paths = traverser.paths(sourceId, dir, targetId, dir, edgeLabel, depth, - maxDegree, capacity, limit); + maxDegree, capacity, + limit); return manager.serializer(g).writePaths("crosspoints", paths, true); } } 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 5ec50e382a..285608cb80 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 @@ -54,6 +54,10 @@ public class PersonalRankAPI extends API { private static final Logger LOG = Log.logger(RestServer.class); + private static final double DEFAULT_DIFF = 0.0001; + private static final double DEFAULT_SAMPLE = 0.85; + private static final int DEFAULT_DEPTH = 5; + @POST @Timed @Produces(APPLICATION_JSON_WITH_CHARSET) @@ -68,16 +72,19 @@ public String personalRank(@Context GraphManager manager, E.checkArgument(request.alpha > 0 && request.alpha <= 1.0, "The alpha of rank request must be in range (0, 1], " + "but got '%s'", request.alpha); + E.checkArgument(request.maxDiff > 0 && request.maxDiff <= 1.0, + "The max diff of rank request must be in range " + + "(0, 1], but got '%s'", request.maxDiff); E.checkArgument(request.maxDegree > 0L || request.maxDegree == NO_LIMIT, "The max degree of rank request must be > 0 " + "or == -1, but got: %s", request.maxDegree); E.checkArgument(request.limit > 0L || request.limit == NO_LIMIT, "The limit of rank request must be > 0 or == -1, " + "but got: %s", request.limit); - E.checkArgument(request.maxDepth > 0L && + E.checkArgument(request.maxDepth > 1L && request.maxDepth <= Long.parseLong(DEFAULT_MAX_DEPTH), "The max depth of rank request must be " + - "in range (0, %s], but got '%s'", + "in range (1, %s], but got '%s'", DEFAULT_MAX_DEPTH, request.maxDepth); LOG.debug("Graph [{}] get personal rank from '{}' with " + @@ -105,13 +112,16 @@ private static class RankRequest { @JsonProperty("label") private String label; @JsonProperty("alpha") - private double alpha; + private double alpha = DEFAULT_SAMPLE; + // TODO: used for future enhancement + @JsonProperty("max_diff") + private double maxDiff = DEFAULT_DIFF; @JsonProperty("max_degree") private long maxDegree = Long.parseLong(DEFAULT_MAX_DEGREE); @JsonProperty("limit") private long limit = Long.parseLong(DEFAULT_LIMIT); @JsonProperty("max_depth") - private int maxDepth; + private int maxDepth = DEFAULT_DEPTH; @JsonProperty("with_label") private PersonalRankTraverser.WithLabel withLabel = PersonalRankTraverser.WithLabel.BOTH_LABEL; @@ -121,11 +131,11 @@ private static class RankRequest { @Override public String toString() { return String.format("RankRequest{source=%s,label=%s,alpha=%s," + - "maxDegree=%s,limit=%s,maxDepth=%s," + - "withLabel=%s,sorted=%s}", + "maxDiff=%s,maxDegree=%s,limit=%s," + + "maxDepth=%s,withLabel=%s,sorted=%s}", this.source, this.label, this.alpha, - this.maxDegree, this.limit, this.maxDepth, - this.withLabel, this.sorted); + this.maxDiff, this.maxDegree, this.limit, + this.maxDepth, this.withLabel, this.sorted); } } } 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 a69f11d59b..4614bcfb30 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 @@ -33,6 +33,7 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.tinkerpop.gremlin.structure.Edge; +import org.slf4j.Logger; import com.baidu.hugegraph.HugeException; import com.baidu.hugegraph.HugeGraph; @@ -60,6 +61,7 @@ import com.baidu.hugegraph.util.CollectionUtil; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.InsertionOrderUtil; +import com.baidu.hugegraph.util.Log; import com.baidu.hugegraph.util.collection.CollectionFactory; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -67,6 +69,8 @@ public class HugeTraverser { + public static final Logger LOG = Log.logger(HugeTraverser.class); + private HugeGraph graph; private static CollectionFactory collectionFactory; @@ -618,7 +622,7 @@ public int hashCode() { */ @Override public boolean equals(Object other) { - if (other == null || !(other instanceof Path)) { + if (!(other instanceof Path)) { return false; } return this.vertices.equals(((Path) other).vertices); From 62acaaa3a036687cc64f5bd3d9b7de9dba72ceca Mon Sep 17 00:00:00 2001 From: imbajin Date: Wed, 15 Dec 2021 15:19:20 +0800 Subject: [PATCH 2/2] tiny improve --- .../com/baidu/hugegraph/api/traversers/PersonalRankAPI.java | 4 ++-- .../baidu/hugegraph/traversal/algorithm/HugeTraverser.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 285608cb80..d94a2135fc 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 @@ -55,7 +55,7 @@ public class PersonalRankAPI extends API { private static final Logger LOG = Log.logger(RestServer.class); private static final double DEFAULT_DIFF = 0.0001; - private static final double DEFAULT_SAMPLE = 0.85; + private static final double DEFAULT_ALPHA = 0.85; private static final int DEFAULT_DEPTH = 5; @POST @@ -112,7 +112,7 @@ private static class RankRequest { @JsonProperty("label") private String label; @JsonProperty("alpha") - private double alpha = DEFAULT_SAMPLE; + private double alpha = DEFAULT_ALPHA; // TODO: used for future enhancement @JsonProperty("max_diff") private double maxDiff = DEFAULT_DIFF; 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 4614bcfb30..adc0699431 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 @@ -69,7 +69,7 @@ public class HugeTraverser { - public static final Logger LOG = Log.logger(HugeTraverser.class); + protected static final Logger LOG = Log.logger(HugeTraverser.class); private HugeGraph graph;