chore: unify to call SchemaLabel.getLabelId() (#2458)
Change-Id: I31bcc0d1ee99f3c443f8f4f0d458e06ca89977ef
diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/Vertices.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/Vertices.java
index bf231ca..d5be694 100644
--- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/Vertices.java
+++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/Vertices.java
@@ -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;
@@ -51,6 +52,10 @@
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));
@@ -62,7 +67,7 @@
} 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()) {
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/AbstractAlgorithm.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/AbstractAlgorithm.java
index 0e249eb..b30d3ac 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/AbstractAlgorithm.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/AbstractAlgorithm.java
@@ -379,7 +379,7 @@
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));
return this.graph().vertices(query);
}
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/BetweennessCentralityAlgorithmV2.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/BetweennessCentralityAlgorithmV2.java
index d33dc54..bc4c3ae 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/BetweennessCentralityAlgorithmV2.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/BetweennessCentralityAlgorithmV2.java
@@ -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;
@@ -80,10 +81,7 @@
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);
// TODO: sample the startVertices
Iterator<Vertex> startVertices = this.vertices(sourceLabel,
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithmV2.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithmV2.java
index e95eae5..1519a2a 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithmV2.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithmV2.java
@@ -81,10 +81,7 @@
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);
// TODO: sample the startVertices
Iterator<Vertex> startVertices = this.vertices(sourceLabel,
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/DegreeCentralityAlgorithm.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/DegreeCentralityAlgorithm.java
index 5a995d6..19e68c0 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/DegreeCentralityAlgorithm.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/DegreeCentralityAlgorithm.java
@@ -73,7 +73,7 @@
JsonMap degrees = new JsonMap();
TopMap<Id> tops = new TopMap<>(topN);
Id vertex = null;
- Id labelId = this.getEdgeLabelId(label);
+ Id labelId = this.getEdgeLabelIdOrNull(label);
long degree = 0L;
long totalEdges = 0L;
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/StressCentralityAlgorithmV2.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/StressCentralityAlgorithmV2.java
index 5817e03..d1c3da6 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/StressCentralityAlgorithmV2.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/StressCentralityAlgorithmV2.java
@@ -60,7 +60,7 @@
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);
@@ -80,10 +80,7 @@
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);
// TODO: sample the startVertices
Iterator<Vertex> startVertices = this.vertices(sourceLabel,
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/comm/LpaAlgorithm.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/comm/LpaAlgorithm.java
index 289ebca..4b2e30d 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/comm/LpaAlgorithm.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/comm/LpaAlgorithm.java
@@ -178,7 +178,7 @@
Directions dir, long degree) {
// neighbors of source vertex v
Id source = (Id) vertex.id();
- Id labelId = this.getEdgeLabelId(edgeLabel);
+ Id labelId = this.getEdgeLabelIdOrNull(edgeLabel);
Iterator<Id> neighbors = this.adjacentVertices(source, dir,
labelId, degree);
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/SchemaLabel.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/SchemaLabel.java
index e20a3f8..09de43e 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/SchemaLabel.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/SchemaLabel.java
@@ -177,4 +177,12 @@
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);
+ }
}
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/FusiformSimilarityTraverser.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/FusiformSimilarityTraverser.java
index 55ee91b..741a6ef 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/FusiformSimilarityTraverser.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/FusiformSimilarityTraverser.java
@@ -112,7 +112,7 @@
// 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);
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/HugeTraverser.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/HugeTraverser.java
index e75d016..57c7c0a 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/HugeTraverser.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/HugeTraverser.java
@@ -620,18 +620,18 @@
}
}
- 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);
}
- 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) {
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/JaccardSimilarTraverser.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/JaccardSimilarTraverser.java
index 51c81b9..296e89a 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/JaccardSimilarTraverser.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/JaccardSimilarTraverser.java
@@ -54,7 +54,7 @@
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));
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/KneighborTraverser.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/KneighborTraverser.java
index cc2a399..2b284b2 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/KneighborTraverser.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/KneighborTraverser.java
@@ -46,7 +46,7 @@
checkDegree(degree);
checkLimit(limit);
- Id labelId = this.getEdgeLabelId(label);
+ Id labelId = this.getEdgeLabelIdOrNull(label);
KneighborRecords records = new KneighborRecords(true, sourceV, true);
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/KoutTraverser.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/KoutTraverser.java
index 61ec0a4..8cf3c15 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/KoutTraverser.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/KoutTraverser.java
@@ -58,7 +58,7 @@
capacity, limit);
}
- Id labelId = this.getEdgeLabelId(label);
+ Id labelId = this.getEdgeLabelIdOrNull(label);
Set<Id> sources = newIdSet();
Set<Id> neighbors = newIdSet();
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/PathsTraverser.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/PathsTraverser.java
index 76c2fa6..5746533 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/PathsTraverser.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/PathsTraverser.java
@@ -59,7 +59,7 @@
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
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/PersonalRankTraverser.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/PersonalRankTraverser.java
index 972eff7..2c3bafa 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/PersonalRankTraverser.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/PersonalRankTraverser.java
@@ -55,7 +55,7 @@
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();
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SameNeighborTraverser.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SameNeighborTraverser.java
index 036ec55..af16231 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SameNeighborTraverser.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SameNeighborTraverser.java
@@ -45,7 +45,7 @@
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));
@@ -80,7 +80,7 @@
List<Id> labelsId = new ArrayList<>();
if (labels != null) {
for (String label : labels) {
- labelsId.add(this.getEdgeLabelId(label));
+ labelsId.add(this.getEdgeLabelIdOrNull(label));
}
}
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/ShortestPathTraverser.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/ShortestPathTraverser.java
index a34f872..4a2f325 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/ShortestPathTraverser.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/ShortestPathTraverser.java
@@ -61,7 +61,7 @@
Map<Id, String> labelMap = newMap(labels.size());
for (String label : labels) {
- labelMap.put(this.getEdgeLabelId(label), label);
+ labelMap.put(this.getEdgeLabelIdOrNull(label), label);
}
Traverser traverser = new Traverser(sourceV, targetV, dir, labelMap,
degree, skipDegree, capacity);
@@ -122,7 +122,7 @@
Map<Id, String> labelMap = newMap(labels.size());
for (String label : labels) {
- labelMap.put(this.getEdgeLabelId(label), label);
+ labelMap.put(this.getEdgeLabelIdOrNull(label), label);
}
Traverser traverser = new Traverser(sourceV, targetV, dir, labelMap,
degree, skipDegree, capacity);
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java
index ead4365..0c67228 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java
@@ -58,7 +58,7 @@
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) {
@@ -94,7 +94,7 @@
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);
diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SubGraphTraverser.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SubGraphTraverser.java
index d5c49ea..f76f3e3 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SubGraphTraverser.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SubGraphTraverser.java
@@ -81,7 +81,7 @@
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);