Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add personalrank and neighborrank RESTful API #274

Merged
merged 15 commits into from
Apr 19, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,10 @@ public class PersonalRankAPI extends API {
public String personalRank(@Context GraphManager manager,
@PathParam("graph") String graph,
RankRequest request) {
LOG.debug("Graph [{}] get personal rank from '{}' with " +
"edge label '{}', alpha '{}', max depth '{}'",
graph, request.source, request.label,
request.alpha, request.maxDepth);

E.checkNotNull(request.source, "source vertex id");
E.checkNotNull(request.label, "edge label");
E.checkArgument(request.source != null,
"The source vertex id of rank request can't be null");
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], " +
"but got '%s'", request.alpha);
Expand All @@ -75,6 +72,12 @@ public String personalRank(@Context GraphManager manager,
"The max depth of rank request must >= 1, but got '%s'",
request.maxDepth);

LOG.debug("Graph [{}] get personal rank from '{}' with " +
"edge label '{}', alpha '{}', degree '{}', " +
"max depth '{}', sorted '{}'",
Linary marked this conversation as resolved.
Show resolved Hide resolved
graph, request.source, request.label, request.alpha,
request.degree, request.maxDepth, request.sorted);

Id sourceId = VertexAPI.checkAndParseVertexId(request.source);
HugeGraph g = graph(manager, graph);

Expand Down
2 changes: 1 addition & 1 deletion hugegraph-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-common</artifactId>
<version>1.5.8</version>
<version>1.6.0</version>
</dependency>

<!-- tinkerpop -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.OrderLimitMap;
import com.google.common.collect.ImmutableList;
Linary marked this conversation as resolved.
Show resolved Hide resolved
import com.google.common.collect.ImmutableMap;

public class NeighborRankTraverser extends HugeTraverser {

Expand Down Expand Up @@ -66,8 +65,8 @@ public List<Map<Id, Double>> neighborRank(Id source, List<Step> steps) {
boolean sameLayerTransfer = true;
long access = 0;
// Result
List<Map<Id, Double>> ranks = new ArrayList<>();
ranks.add(ImmutableMap.of(source, 1.0));
List<Ranks> ranks = new ArrayList<>();
Linary marked this conversation as resolved.
Show resolved Hide resolved
ranks.add(Ranks.of(source, 1.0));

root : for (Step step : steps) {
Map<Id, Double> lastLayerRanks = ranks.get(ranks.size() - 1);
Expand Down Expand Up @@ -132,7 +131,7 @@ public List<Map<Id, Double>> neighborRank(Id source, List<Step> steps) {
this.contributePrevLayers(ranks, incr, prevLayerNodesV);
}

Map<Id, Double> newLayerRanks;
Ranks newLayerRanks;
if (sameLayerTransfer) {
// First contribute to last layer, then pass to the new layer
this.contributeLastLayer(sameLayerIncrRanks, lastLayerRanks);
Expand Down Expand Up @@ -164,13 +163,12 @@ private boolean belongToSameLayer(Set<Id> sources, Id target,
}
}

private boolean belongToPrevLayers(List<Map<Id, Double>> ranks, Id target,
private boolean belongToPrevLayers(List<Ranks> ranks, Id target,
Map<Integer, Set<Id>> prevLayerNodes) {
for (int i = ranks.size() - 2; i > 0; i--) {
Map<Id, Double> prevLayerRanks = ranks.get(i);
Ranks prevLayerRanks = ranks.get(i);
if (prevLayerRanks.containsKey(target)) {
Set<Id> nodes = prevLayerNodes.computeIfAbsent(i,
k -> new HashSet<>());
Set<Id> nodes = prevLayerNodes.computeIfAbsent(i, HashSet::new);
nodes.add(target);
return true;
}
Expand All @@ -186,7 +184,7 @@ private void mergeSameLayerIncrRanks(Set<Id> sameLayerNodesV, double incr,
}
}

private void contributePrevLayers(List<Map<Id, Double>> ranks, double incr,
private void contributePrevLayers(List<Ranks> ranks, double incr,
Map<Integer, Set<Id>> prevLayerNodesV) {
for (Map.Entry<Integer, Set<Id>> e : prevLayerNodesV.entrySet()) {
Map<Id, Double> prevLayerRanks = ranks.get(e.getKey());
Expand All @@ -206,11 +204,10 @@ private void contributeLastLayer(Map<Id, Double> rankIncrs,
}
}

private Map<Id, Double> contributeNewLayer(
MultivaluedMap<Id, Node> adjacencies,
Map<Id, Double> lastLayerRanks,
int capacity) {
Map<Id, Double> newLayerRanks = new OrderLimitMap<>(capacity);
private Ranks contributeNewLayer(MultivaluedMap<Id, Node> adjacencies,
Map<Id, Double> lastLayerRanks,
Linary marked this conversation as resolved.
Show resolved Hide resolved
int capacity) {
Ranks newLayerRanks = new Ranks(capacity);
for (Map.Entry<Id, List<Node>> entry : adjacencies.entrySet()) {
Id parent = entry.getKey();
long size = entry.getValue().size();
Expand All @@ -223,16 +220,15 @@ private Map<Id, Double> contributeNewLayer(
return newLayerRanks;
}

private List<Map<Id, Double>> topRanks(List<Map<Id, Double>> ranks,
private List<Map<Id, Double>> topRanks(List<Ranks> ranks,
List<Step> steps) {
assert ranks.size() > 0;
List<Map<Id, Double>> results = new ArrayList<>(ranks.size());
// The first layer is root node so skip i=0
results.add(ranks.get(0));
for (int i = 1; i < ranks.size(); i++) {
Step step = steps.get(i - 1);
OrderLimitMap<Id, Double> origin = (OrderLimitMap<Id, Double>)
ranks.get(i);
Ranks origin = ranks.get(i);
if (origin.size() > step.top) {
results.add(origin.topN(step.top));
} else {
Expand All @@ -259,4 +255,17 @@ public Step(Directions direction, Map<Id, String> labels,
this.capacity = DEFAULT_CAPACITY_PER_LAYER;
}
}

private static class Ranks extends OrderLimitMap<Id, Double> {

public Ranks(int capacity) {
super(capacity);
}

public static Ranks of(Id key, Double value) {
Ranks ranks = new Ranks(1);
ranks.put(key, value);
return ranks;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,17 @@ public PersonalRankTraverser(HugeGraph graph, double alpha,
}

public Map<Id, Double> personalRank(Id source, String label) {
E.checkArgumentNotNull(source, "The source vertex id can't be null");
E.checkArgumentNotNull(label, "The edge label can't be null");

Map<Id, Double> ranks = new HashMap<>();
ranks.put(source, 1.0);

Id labelId = this.graph().edgeLabel(label).id();
Linary marked this conversation as resolved.
Show resolved Hide resolved
Directions dir = this.getStartDirection(source, label);
long degree = this.degreeOfVertex(source, dir, labelId);
if (degree <= 0) {
return ImmutableMap.of(source, 1.0);
return ranks;
}

Set<Id> outSeeds = new HashSet<>();
Expand All @@ -70,9 +76,6 @@ public Map<Id, Double> personalRank(Id source, String label) {
inSeeds.add(source);
}

Map<Id, Double> ranks = new HashMap<>();
ranks.put(source, 1.0);

for (long i = 0; i < this.maxDepth; i++) {
Map<Id, Double> incrRanks = this.getIncrRanks(outSeeds, inSeeds,
labelId, ranks);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class CoreVersion {

public static void check() {
// Check version of hugegraph-common
VersionUtil.check(CommonVersion.VERSION, "1.5.0", "1.6",
VersionUtil.check(CommonVersion.VERSION, "1.6.0", "1.7",
CommonVersion.NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
EdgeIdTest.class,
AnalyzerTest.class,
JsonUtilTest.class,
OrderLimitMapTest.class,

RocksDBSessionsTest.class,
RocksDBCountersTest.class
Expand Down
Loading