Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
Change-Id: If41ae158bef48b3cc2dc7cfe821d1fd501438cea
  • Loading branch information
Linary committed Apr 10, 2019
1 parent f91c73b commit 5936537
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 366 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ public String neighborRank(@Context GraphManager manager,
@PathParam("graph") String graph,
RankRequest request) {
LOG.debug("Graph [{}] get neighbor rank from '{}' with steps '{}', " +
"alpha '{}', and sorted '{}'",
graph, request.source, request.steps, request.alpha);
"alpha '{}', capacity '{}', limit '{}'",
graph, request.source, request.steps, request.alpha,
request.capacity, request.limit);

E.checkArgumentNotNull(request, "The rank request body can't be null");
E.checkArgumentNotNull(request.source,
Expand All @@ -78,8 +79,8 @@ public String neighborRank(@Context GraphManager manager,
"specified or as null, but can't be specified " +
"as empty");
}
E.checkArgument(request.alpha >= 0.0 && request.alpha <= 1.0,
"The alpha of rank request must between [0, 1], " +
E.checkArgument(request.alpha > 0 && request.alpha <= 1.0,
"The alpha of rank request must belong (0, 1], " +
"but got '%s'", request.alpha);

Id sourceId = VertexAPI.checkAndParseVertexId(request.source);
Expand All @@ -89,8 +90,8 @@ public String neighborRank(@Context GraphManager manager,

RankTraverser traverser = new RankTraverser(g, request.alpha);
List<Map<Id, Double>> ranks = traverser.neighborRank(sourceId, steps,
request.capacity,
request.limit);
request.capacity,
request.limit);
return JsonUtil.toJson(ranks);
}

Expand Down Expand Up @@ -119,8 +120,9 @@ private static class RankRequest {
@Override
public String toString() {
return String.format("RankRequest{source=%s,steps=%s," +
"alpha=%s}",
this.source, this.steps, this.alpha);
"alpha=%s,capacity=%s,limit=%s}",
this.source, this.steps, this.alpha,
this.capacity, this.limit);
}
}

Expand All @@ -132,18 +134,15 @@ private static class Step {
public Directions direction;
@JsonProperty("labels")
public List<String> labels;
@JsonProperty("properties")
public Map<String, Object> properties;
@JsonProperty("degree")
public long degree = Long.valueOf(DEFAULT_DEGREE);
@JsonProperty("number")
public int number = MAX_NUMBER;

@Override
public String toString() {
return String.format("Step{direction=%s,labels=%s,properties=%s," +
"degree=%s}", this.direction, this.labels,
this.properties, this.degree);
return String.format("Step{direction=%s,labels=%s,degree=%s}",
this.direction, this.labels, this.degree);
}

private RankTraverser.Step jsonToStep(HugeGraph graph) {
Expand All @@ -153,7 +152,7 @@ private RankTraverser.Step jsonToStep(HugeGraph graph) {
E.checkArgument(this.degree == NO_LIMIT,
"Degree must be greater than or equal to sample, " +
"but got degree %s", degree);
E.checkArgument(this.number > 0 && this.number <= 1000,
E.checkArgument(this.number > 0 && this.number <= MAX_NUMBER,
"The recommended number of each layer cannot " +
"exceed '%s'", MAX_NUMBER);
Map<Id, String> labelIds = new HashMap<>();
Expand All @@ -164,8 +163,7 @@ private RankTraverser.Step jsonToStep(HugeGraph graph) {
}
}
return new RankTraverser.Step(this.direction, labelIds,
this.properties, this.degree,
this.number);
this.degree, this.number);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.HugeGraph;
Expand Down Expand Up @@ -166,12 +165,6 @@ public Set<Id> kneighbor(Id sourceV, Directions dir,
return all;
}

protected Set<Id> adjacentVertices(Id sourceV, Directions dir, Id label) {
// TODO: may OOM, replace with paged iterate or async task
return this.adjacentVertices(ImmutableSet.of(sourceV), dir, label,
null, NO_LIMIT, NO_LIMIT);
}

private Set<Id> adjacentVertices(Set<Id> vertices, Directions dir,
Id label, Set<Id> excluded,
long degree, long limit) {
Expand Down Expand Up @@ -248,11 +241,6 @@ protected Iterator<Edge> edgesOfVertex(Id source, Directions dir,
return g.limit(limit);
}

protected long degreeOfVertex(Id source, Directions dir, Id label) {
return IteratorUtils.count(this.edgesOfVertex(source, dir, label,
NO_LIMIT));
}

protected Id getEdgeLabelId(Object label) {
if (label == null) {
return null;
Expand Down
Loading

0 comments on commit 5936537

Please sign in to comment.