Skip to content

Commit

Permalink
chore: unify to call SchemaLabel.getLabelId()
Browse files Browse the repository at this point in the history
Change-Id: I31bcc0d1ee99f3c443f8f4f0d458e06ca89977ef
  • Loading branch information
javeme committed Feb 24, 2024
1 parent bc421bb commit 0e4a123
Show file tree
Hide file tree
Showing 19 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.backend.id.Id;
import org.apache.hugegraph.backend.query.ConditionQuery;
import org.apache.hugegraph.schema.SchemaLabel;
import org.apache.hugegraph.structure.HugeVertex;
import org.apache.hugegraph.traversal.optimize.TraversalUtil;
import org.apache.hugegraph.type.HugeType;
Expand All @@ -51,6 +52,10 @@ public Iterator<Vertex> vertices(HugeGraph g) {
this.label == null), "No source vertices provided");
Iterator<Vertex> iterator;
if (this.ids != null && !this.ids.isEmpty()) {
E.checkArgument(this.label == null,
"Just provide one of ids or label of source vertices");
E.checkArgument(props == null || props.isEmpty(),
"Just provide one of ids or properties of source vertices");
List<Id> sourceIds = new ArrayList<>(this.ids.size());
for (Object id : this.ids) {
sourceIds.add(HugeVertex.getIdValue(id));
Expand All @@ -62,7 +67,7 @@ public Iterator<Vertex> vertices(HugeGraph g) {
} else {
ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
if (this.label != null) {
Id label = g.vertexLabel(this.label).id();
Id label = SchemaLabel.getVertexLabelId(g, this.label);
query.eq(HugeKeys.LABEL, label);
}
if (props != null && !props.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ protected Iterator<Vertex> vertices(Object label, long limit) {
ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
query.capacity(Query.NO_CAPACITY);
query.limit(limit);
query.eq(HugeKeys.LABEL, this.getVertexLabelId(label));
query.eq(HugeKeys.LABEL, this.getVertexLabelIdOrNull(label));

Check warning on line 382 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/AbstractAlgorithm.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/AbstractAlgorithm.java#L382

Added line #L382 was not covered by tests
return this.graph().vertices(query);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.hugegraph.backend.query.Query;
import org.apache.hugegraph.job.UserJob;
import org.apache.hugegraph.job.algorithm.BfsTraverser;
import org.apache.hugegraph.schema.SchemaLabel;
import org.apache.hugegraph.structure.HugeVertex;
import org.apache.hugegraph.traversal.algorithm.HugeTraverser;
import org.apache.hugegraph.type.define.Directions;
Expand Down Expand Up @@ -80,10 +81,7 @@ private Object betweenessCentrality(Directions direction,
assert topN >= 0L || topN == NO_LIMIT;

this.globalBetweennesses = new HashMap<>();
Id edgeLabelId = null;
if (label != null) {
edgeLabelId = this.graph().edgeLabel(label).id();
}
Id edgeLabelId = this.getEdgeLabelIdOrNull(label);

Check warning on line 84 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/BetweennessCentralityAlgorithmV2.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/BetweennessCentralityAlgorithmV2.java#L84

Added line #L84 was not covered by tests

// TODO: sample the startVertices
Iterator<Vertex> startVertices = this.vertices(sourceLabel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ private Object closenessCentrality(Directions direction,
assert degree > 0L || degree == NO_LIMIT;
assert topN >= 0L || topN == NO_LIMIT;

Id edgeLabelId = null;
if (label != null) {
edgeLabelId = this.graph().edgeLabel(label).id();
}
Id edgeLabelId = this.getEdgeLabelIdOrNull(label);

Check warning on line 84 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithmV2.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithmV2.java#L84

Added line #L84 was not covered by tests

// TODO: sample the startVertices
Iterator<Vertex> startVertices = this.vertices(sourceLabel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Object degreeCentrality(Directions direction,
JsonMap degrees = new JsonMap();
TopMap<Id> tops = new TopMap<>(topN);
Id vertex = null;
Id labelId = this.getEdgeLabelId(label);
Id labelId = this.getEdgeLabelIdOrNull(label);

Check warning on line 76 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/DegreeCentralityAlgorithm.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/DegreeCentralityAlgorithm.java#L76

Added line #L76 was not covered by tests
long degree = 0L;
long totalEdges = 0L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Object call(UserJob<Object> job, Map<String, Object> parameters) {

private static class Traverser extends BfsTraverser<StressNode> {

private Map<Id, MutableLong> globalStresses;
private final Map<Id, MutableLong> globalStresses;

private Traverser(UserJob<Object> job) {
super(job);
Expand All @@ -80,10 +80,7 @@ private Object stressCentrality(Directions direction,
assert degree > 0L || degree == NO_LIMIT;
assert topN >= 0L || topN == NO_LIMIT;

Id edgeLabelId = null;
if (label != null) {
edgeLabelId = this.graph().edgeLabel(label).id();
}
Id edgeLabelId = this.getEdgeLabelIdOrNull(label);

Check warning on line 83 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/StressCentralityAlgorithmV2.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/StressCentralityAlgorithmV2.java#L83

Added line #L83 was not covered by tests

// TODO: sample the startVertices
Iterator<Vertex> startVertices = this.vertices(sourceLabel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private String voteCommunityOfVertex(Vertex vertex, String edgeLabel,
Directions dir, long degree) {
// neighbors of source vertex v
Id source = (Id) vertex.id();
Id labelId = this.getEdgeLabelId(edgeLabel);
Id labelId = this.getEdgeLabelIdOrNull(edgeLabel);

Check warning on line 181 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/comm/LpaAlgorithm.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/comm/LpaAlgorithm.java#L181

Added line #L181 was not covered by tests
Iterator<Id> neighbors = this.adjacentVertices(source, dir,
labelId, degree);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,12 @@ public static Id getLabelId(HugeGraph graph, HugeType type, Object label) {
label.getClass());
}
}

public static Id getVertexLabelId(HugeGraph graph, Object label) {
return SchemaLabel.getLabelId(graph, HugeType.VERTEX, label);
}

public static Id getEdgeLabelId(HugeGraph graph, Object label) {
return SchemaLabel.getLabelId(graph, HugeType.EDGE, label);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private Set<Similar> fusiformSimilarityForVertex(
// Ignore current vertex if its neighbors number is not enough
return ImmutableSet.of();
}
Id labelId = this.getEdgeLabelId(label);
Id labelId = this.getEdgeLabelIdOrNull(label);
// Get similar nodes and counts
Iterator<Edge> edges = this.edgesOfVertex(vertex.id(), direction,
labelId, degree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,18 +620,18 @@ protected long edgesCount(Id source, EdgeStep edgeStep) {
}
}

protected Object getVertexLabelId(Object label) {
protected Object getVertexLabelIdOrNull(Object label) {
if (label == null) {
return null;
}
return SchemaLabel.getLabelId(this.graph, HugeType.VERTEX, label);
return SchemaLabel.getVertexLabelId(this.graph, label);

Check warning on line 627 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/HugeTraverser.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/HugeTraverser.java#L627

Added line #L627 was not covered by tests
}

protected Id getEdgeLabelId(Object label) {
protected Id getEdgeLabelIdOrNull(Object label) {
if (label == null) {
return null;
}
return SchemaLabel.getLabelId(this.graph, HugeType.EDGE, label);
return SchemaLabel.getEdgeLabelId(this.graph, label);
}

protected void checkVertexExist(Id vertexId, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public double jaccardSimilarity(Id vertex, Id other, Directions dir,
E.checkNotNull(dir, "direction");
checkDegree(degree);

Id labelId = this.getEdgeLabelId(label);
Id labelId = this.getEdgeLabelIdOrNull(label);

Set<Id> sourceNeighbors = IteratorUtils.set(this.adjacentVertices(
vertex, dir, labelId, degree));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Set<Id> kneighbor(Id sourceV, Directions dir,
checkDegree(degree);
checkLimit(limit);

Id labelId = this.getEdgeLabelId(label);
Id labelId = this.getEdgeLabelIdOrNull(label);

KneighborRecords records = new KneighborRecords(true, sourceV, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Set<Id> kout(Id sourceV, Directions dir, String label,
capacity, limit);
}

Id labelId = this.getEdgeLabelId(label);
Id labelId = this.getEdgeLabelIdOrNull(label);

Set<Id> sources = newIdSet();
Set<Id> neighbors = newIdSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public PathSet paths(Id sourceV, Directions sourceDir,
return PathSet.EMPTY;
}

Id labelId = this.getEdgeLabelId(label);
Id labelId = this.getEdgeLabelIdOrNull(label);
Traverser traverser = new Traverser(sourceV, targetV, labelId,
degree, capacity, limit);
// We should stop early if walk backtrace or reach limit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Map<Id, Double> personalRank(Id source, String label,
Map<Id, Double> ranks = newMap();
ranks.put(source, 1.0);

Id labelId = this.graph().edgeLabel(label).id();
Id labelId = this.getEdgeLabelIdOrNull(label);
Directions dir = this.getStartDirection(source, label);

Set<Id> outSeeds = newIdSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Set<Id> sameNeighbors(Id vertex, Id other, Directions direction,
checkDegree(degree);
checkLimit(limit);

Id labelId = this.getEdgeLabelId(label);
Id labelId = this.getEdgeLabelIdOrNull(label);

Set<Id> sourceNeighbors = IteratorUtils.set(this.adjacentVertices(
vertex, direction, labelId, degree));
Expand Down Expand Up @@ -80,7 +80,7 @@ public Set<Id> sameNeighbors(List<Id> vertexIds, Directions direction,
List<Id> labelsId = new ArrayList<>();
if (labels != null) {
for (String label : labels) {
labelsId.add(this.getEdgeLabelId(label));
labelsId.add(this.getEdgeLabelIdOrNull(label));

Check warning on line 83 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SameNeighborTraverser.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SameNeighborTraverser.java#L83

Added line #L83 was not covered by tests
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Path shortestPath(Id sourceV, Id targetV, Directions dir,

Map<Id, String> labelMap = newMap(labels.size());
for (String label : labels) {
labelMap.put(this.getEdgeLabelId(label), label);
labelMap.put(this.getEdgeLabelIdOrNull(label), label);

Check warning on line 64 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/ShortestPathTraverser.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/ShortestPathTraverser.java#L64

Added line #L64 was not covered by tests
}
Traverser traverser = new Traverser(sourceV, targetV, dir, labelMap,
degree, skipDegree, capacity);
Expand Down Expand Up @@ -122,7 +122,7 @@ public PathSet allShortestPaths(Id sourceV, Id targetV, Directions dir,

Map<Id, String> labelMap = newMap(labels.size());
for (String label : labels) {
labelMap.put(this.getEdgeLabelId(label), label);
labelMap.put(this.getEdgeLabelIdOrNull(label), label);

Check warning on line 125 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/ShortestPathTraverser.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/ShortestPathTraverser.java#L125

Added line #L125 was not covered by tests
}
Traverser traverser = new Traverser(sourceV, targetV, dir, labelMap,
degree, skipDegree, capacity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public WeightedPaths singleSourceShortestPaths(Id sourceV, Directions dir,
checkSkipDegree(skipDegree, degree, capacity);
checkLimit(limit);

Id labelId = this.getEdgeLabelId(label);
Id labelId = this.getEdgeLabelIdOrNull(label);
Traverser traverser = new Traverser(sourceV, dir, labelId, weight,
degree, skipDegree, capacity, limit);
while (true) {
Expand Down Expand Up @@ -94,7 +94,7 @@ public NodeWithWeight weightedShortestPath(Id sourceV, Id targetV,
checkCapacity(capacity);
checkSkipDegree(skipDegree, degree, capacity);

Id labelId = this.getEdgeLabelId(label);
Id labelId = this.getEdgeLabelIdOrNull(label);
Traverser traverser = new Traverser(sourceV, dir, labelId, weight,
degree, skipDegree, capacity,
NO_LIMIT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private PathSet subGraphPaths(Id sourceV, Directions dir, String label,
checkCapacity(capacity);
checkLimit(limit);

Id labelId = this.getEdgeLabelId(label);
Id labelId = this.getEdgeLabelIdOrNull(label);
Traverser traverser = new Traverser(sourceV, labelId, depth, degree,
capacity, limit, rings,
sourceInRing);
Expand Down

0 comments on commit 0e4a123

Please sign in to comment.