From 0f6df0f77311bf1d4ce5373288262af48fa7a803 Mon Sep 17 00:00:00 2001 From: liningrui Date: Wed, 17 Apr 2019 22:17:00 +0800 Subject: [PATCH] tiny improve Change-Id: Ib8c2da1f85c9e78504d704b0b3629a24b0666e42 --- .../api/traversers/NeighborRankAPI.java | 8 ++-- .../api/traversers/PersonalRankAPI.java | 15 ++++--- .../traversal/algorithm/HugeTraverser.java | 1 + .../algorithm/NeighborRankTraverser.java | 5 +-- .../algorithm/PersonalRankTraverser.java | 39 +++++++++---------- 5 files changed, 35 insertions(+), 33 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 3a1c4667d0..040bc7269f 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 @@ -21,9 +21,9 @@ import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_CAPACITY; import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_DEGREE; +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_MAX_DEPTH; import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_PATHS_LIMIT; import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.NO_LIMIT; -import static com.baidu.hugegraph.traversal.algorithm.NeighborRankTraverser.MAX_STEPS; import static com.baidu.hugegraph.traversal.algorithm.NeighborRankTraverser.MAX_TOP; import java.util.ArrayList; @@ -71,11 +71,11 @@ 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() <= MAX_STEPS, + E.checkArgument(request.steps.size() <= Long.valueOf(DEFAULT_MAX_DEPTH), "The steps length of rank request can't exceed %s", - MAX_STEPS); + DEFAULT_MAX_DEPTH); E.checkArgument(request.alpha > 0 && request.alpha <= 1.0, - "The alpha of rank request must belong (0, 1], " + + "The alpha of rank request must be in range (0, 1], " + "but got '%s'", request.alpha); LOG.debug("Graph [{}] get neighbor rank from '{}' with steps '{}', " + 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 5b62d015b6..17d6ae35e1 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 @@ -21,6 +21,7 @@ import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_DEGREE; import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_LIMIT; +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_MAX_DEPTH; import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.NO_LIMIT; import java.util.Map; @@ -66,7 +67,7 @@ public String personalRank(@Context GraphManager manager, E.checkArgument(request.label != null, "The edge label of rank request can't be null"); E.checkArgument(request.alpha > 0 && request.alpha <= 1.0, - "The alpha of rank request must belong (0, 1], " + + "The alpha of rank request must be in range (0, 1], " + "but got '%s'", request.alpha); E.checkArgument(request.degree > 0 || request.degree == NO_LIMIT, "The degree of rank request must be > 0, but got: %s", @@ -74,9 +75,11 @@ public String personalRank(@Context GraphManager manager, E.checkArgument(request.limit > 0 || request.limit == NO_LIMIT, "The limit of rank request must be > 0, but got: %s", request.limit); - E.checkArgument(request.maxDepth >= 1, - "The max depth of rank request must >= 1, but got '%s'", - request.maxDepth); + E.checkArgument(request.maxDepth > 0 && + request.maxDepth <= Long.valueOf(DEFAULT_MAX_DEPTH), + "The max depth of rank request must be " + + "in range (0, %s], but got '%s'", + DEFAULT_MAX_DEPTH, request.maxDepth); LOG.debug("Graph [{}] get personal rank from '{}' with " + "edge label '{}', alpha '{}', degree '{}', " + @@ -137,8 +140,8 @@ private static class RankRequest { @Override public String toString() { - return String.format("RankRequest{source=%s,label=%s," + - "alpha=%s,degree=%s,limit=%s, maxDepth=%s," + + return String.format("RankRequest{source=%s,label=%s,alpha=%s" + + "degree=%s,limit=%s,maxDepth=%s," + "withLabel=%s,sorted=%s}", this.source, this.label, this.alpha, this.degree, this.limit, this.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 b09638020d..279064fcaa 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 @@ -66,6 +66,7 @@ public class HugeTraverser { public static final String DEFAULT_LIMIT = "100"; public static final String DEFAULT_DEGREE = "10000"; public static final String DEFAULT_SAMPLE = "100"; + public static final String DEFAULT_MAX_DEPTH = "50"; public static final String DEFAULT_WEIGHT = "0"; // Empirical value of scan limit, with which results can be returned in 3s diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/NeighborRankTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/NeighborRankTraverser.java index 823820a5dd..0766811d49 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/NeighborRankTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/NeighborRankTraverser.java @@ -41,7 +41,6 @@ public class NeighborRankTraverser extends HugeTraverser { - public static final int MAX_STEPS = 100; public static final int MAX_TOP = 1000; public static final int DEFAULT_CAPACITY_PER_LAYER = 100000; @@ -168,8 +167,8 @@ private boolean belongToPrevLayers(List ranks, Id target, for (int i = ranks.size() - 2; i > 0; i--) { Ranks prevLayerRanks = ranks.get(i); if (prevLayerRanks.containsKey(target)) { - Set get = prevLayerNodes.computeIfAbsent(i, HashSet::new); - get.add(target); + Set nodes = prevLayerNodes.computeIfAbsent(i, HashSet::new); + nodes.add(target); return true; } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PersonalRankTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PersonalRankTraverser.java index 70f503ad5b..ab96e984af 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PersonalRankTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PersonalRankTraverser.java @@ -70,17 +70,17 @@ public Map personalRank(Id source, String label, inSeeds.add(source); } - Set firstAdjacencies = new HashSet<>(); + Set rootAdjacencies = new HashSet<>(); for (long i = 0; i < this.maxDepth; i++) { - Map incrRanks = this.calcIncrRanks(outSeeds, inSeeds, - labelId, ranks); - ranks = this.compensateRoot(source, incrRanks); + Map newRanks = this.calcNewRanks(outSeeds, inSeeds, + labelId, ranks); + ranks = this.compensateRoot(source, newRanks); if (i == 0) { - firstAdjacencies.addAll(ranks.keySet()); + rootAdjacencies.addAll(ranks.keySet()); } } // Remove directly connected neighbors - removeAll(ranks, firstAdjacencies); + removeAll(ranks, rootAdjacencies); // Remove unnecessary label if (withLabel == WithLabel.SAME_LABEL) { removeAll(ranks, dir == Directions.OUT ? inSeeds : outSeeds); @@ -90,9 +90,9 @@ public Map personalRank(Id source, String label, return ranks; } - private Map calcIncrRanks(Set outSeeds, Set inSeeds, - Id label, Map ranks) { - Map incrRanks = new HashMap<>(); + private Map calcNewRanks(Set outSeeds, Set inSeeds, + Id label, Map ranks) { + Map newRanks = new HashMap<>(); BiFunction, Directions, Set> neighborIncrRanks; neighborIncrRanks = (seeds, dir) -> { Set tmpSeeds = new HashSet<>(); @@ -106,18 +106,17 @@ private Map calcIncrRanks(Set outSeeds, Set inSeeds, long degree = neighbors.size(); if (degree == 0L) { - incrRanks.put(seed, oldRank); + newRanks.put(seed, oldRank); continue; } - double spreadRank = oldRank * alpha / degree; + double incrRank = oldRank * alpha / degree; // Collect all neighbors increment for (Id neighbor : neighbors) { tmpSeeds.add(neighbor); // Assign an initial value when firstly update neighbor rank - double incrRank = incrRanks.getOrDefault(neighbor, 0.0); - incrRank += spreadRank; - incrRanks.put(neighbor, incrRank); + double rank = newRanks.getOrDefault(neighbor, 0.0); + newRanks.put(neighbor, rank + incrRank); } } return tmpSeeds; @@ -128,14 +127,14 @@ private Map calcIncrRanks(Set outSeeds, Set inSeeds, outSeeds.addAll(tmpOutSeeds); inSeeds.addAll(tmpInSeeds); - return incrRanks; + return newRanks; } - private Map compensateRoot(Id root, Map incrRanks) { - double oldRank = incrRanks.getOrDefault(root, 0.0); - oldRank += (1 - this.alpha); - incrRanks.put(root, oldRank); - return incrRanks; + private Map compensateRoot(Id root, Map newRanks) { + double rank = newRanks.getOrDefault(root, 0.0); + rank += (1 - this.alpha); + newRanks.put(root, rank); + return newRanks; } private Directions getStartDirection(Id source, String label) {