Skip to content

Commit 0e4a123

Browse files
committed
chore: unify to call SchemaLabel.getLabelId()
Change-Id: I31bcc0d1ee99f3c443f8f4f0d458e06ca89977ef
1 parent bc421bb commit 0e4a123

19 files changed

+39
-34
lines changed

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/Vertices.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.hugegraph.HugeGraph;
2727
import org.apache.hugegraph.backend.id.Id;
2828
import org.apache.hugegraph.backend.query.ConditionQuery;
29+
import org.apache.hugegraph.schema.SchemaLabel;
2930
import org.apache.hugegraph.structure.HugeVertex;
3031
import org.apache.hugegraph.traversal.optimize.TraversalUtil;
3132
import org.apache.hugegraph.type.HugeType;
@@ -51,6 +52,10 @@ public Iterator<Vertex> vertices(HugeGraph g) {
5152
this.label == null), "No source vertices provided");
5253
Iterator<Vertex> iterator;
5354
if (this.ids != null && !this.ids.isEmpty()) {
55+
E.checkArgument(this.label == null,
56+
"Just provide one of ids or label of source vertices");
57+
E.checkArgument(props == null || props.isEmpty(),
58+
"Just provide one of ids or properties of source vertices");
5459
List<Id> sourceIds = new ArrayList<>(this.ids.size());
5560
for (Object id : this.ids) {
5661
sourceIds.add(HugeVertex.getIdValue(id));
@@ -62,7 +67,7 @@ public Iterator<Vertex> vertices(HugeGraph g) {
6267
} else {
6368
ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
6469
if (this.label != null) {
65-
Id label = g.vertexLabel(this.label).id();
70+
Id label = SchemaLabel.getVertexLabelId(g, this.label);
6671
query.eq(HugeKeys.LABEL, label);
6772
}
6873
if (props != null && !props.isEmpty()) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ protected Iterator<Vertex> vertices(Object label, long limit) {
379379
ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
380380
query.capacity(Query.NO_CAPACITY);
381381
query.limit(limit);
382-
query.eq(HugeKeys.LABEL, this.getVertexLabelId(label));
382+
query.eq(HugeKeys.LABEL, this.getVertexLabelIdOrNull(label));
383383
return this.graph().vertices(query);
384384
}
385385

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.hugegraph.backend.query.Query;
2727
import org.apache.hugegraph.job.UserJob;
2828
import org.apache.hugegraph.job.algorithm.BfsTraverser;
29+
import org.apache.hugegraph.schema.SchemaLabel;
2930
import org.apache.hugegraph.structure.HugeVertex;
3031
import org.apache.hugegraph.traversal.algorithm.HugeTraverser;
3132
import org.apache.hugegraph.type.define.Directions;
@@ -80,10 +81,7 @@ private Object betweenessCentrality(Directions direction,
8081
assert topN >= 0L || topN == NO_LIMIT;
8182

8283
this.globalBetweennesses = new HashMap<>();
83-
Id edgeLabelId = null;
84-
if (label != null) {
85-
edgeLabelId = this.graph().edgeLabel(label).id();
86-
}
84+
Id edgeLabelId = this.getEdgeLabelIdOrNull(label);
8785

8886
// TODO: sample the startVertices
8987
Iterator<Vertex> startVertices = this.vertices(sourceLabel,

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ private Object closenessCentrality(Directions direction,
8181
assert degree > 0L || degree == NO_LIMIT;
8282
assert topN >= 0L || topN == NO_LIMIT;
8383

84-
Id edgeLabelId = null;
85-
if (label != null) {
86-
edgeLabelId = this.graph().edgeLabel(label).id();
87-
}
84+
Id edgeLabelId = this.getEdgeLabelIdOrNull(label);
8885

8986
// TODO: sample the startVertices
9087
Iterator<Vertex> startVertices = this.vertices(sourceLabel,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Object degreeCentrality(Directions direction,
7373
JsonMap degrees = new JsonMap();
7474
TopMap<Id> tops = new TopMap<>(topN);
7575
Id vertex = null;
76-
Id labelId = this.getEdgeLabelId(label);
76+
Id labelId = this.getEdgeLabelIdOrNull(label);
7777
long degree = 0L;
7878
long totalEdges = 0L;
7979

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public Object call(UserJob<Object> job, Map<String, Object> parameters) {
6060

6161
private static class Traverser extends BfsTraverser<StressNode> {
6262

63-
private Map<Id, MutableLong> globalStresses;
63+
private final Map<Id, MutableLong> globalStresses;
6464

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

83-
Id edgeLabelId = null;
84-
if (label != null) {
85-
edgeLabelId = this.graph().edgeLabel(label).id();
86-
}
83+
Id edgeLabelId = this.getEdgeLabelIdOrNull(label);
8784

8885
// TODO: sample the startVertices
8986
Iterator<Vertex> startVertices = this.vertices(sourceLabel,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private String voteCommunityOfVertex(Vertex vertex, String edgeLabel,
178178
Directions dir, long degree) {
179179
// neighbors of source vertex v
180180
Id source = (Id) vertex.id();
181-
Id labelId = this.getEdgeLabelId(edgeLabel);
181+
Id labelId = this.getEdgeLabelIdOrNull(edgeLabel);
182182
Iterator<Id> neighbors = this.adjacentVertices(source, dir,
183183
labelId, degree);
184184

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/SchemaLabel.java

+8
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,12 @@ public static Id getLabelId(HugeGraph graph, HugeType type, Object label) {
177177
label.getClass());
178178
}
179179
}
180+
181+
public static Id getVertexLabelId(HugeGraph graph, Object label) {
182+
return SchemaLabel.getLabelId(graph, HugeType.VERTEX, label);
183+
}
184+
185+
public static Id getEdgeLabelId(HugeGraph graph, Object label) {
186+
return SchemaLabel.getLabelId(graph, HugeType.EDGE, label);
187+
}
180188
}

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/FusiformSimilarityTraverser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private Set<Similar> fusiformSimilarityForVertex(
112112
// Ignore current vertex if its neighbors number is not enough
113113
return ImmutableSet.of();
114114
}
115-
Id labelId = this.getEdgeLabelId(label);
115+
Id labelId = this.getEdgeLabelIdOrNull(label);
116116
// Get similar nodes and counts
117117
Iterator<Edge> edges = this.edgesOfVertex(vertex.id(), direction,
118118
labelId, degree);

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -620,18 +620,18 @@ protected long edgesCount(Id source, EdgeStep edgeStep) {
620620
}
621621
}
622622

623-
protected Object getVertexLabelId(Object label) {
623+
protected Object getVertexLabelIdOrNull(Object label) {
624624
if (label == null) {
625625
return null;
626626
}
627-
return SchemaLabel.getLabelId(this.graph, HugeType.VERTEX, label);
627+
return SchemaLabel.getVertexLabelId(this.graph, label);
628628
}
629629

630-
protected Id getEdgeLabelId(Object label) {
630+
protected Id getEdgeLabelIdOrNull(Object label) {
631631
if (label == null) {
632632
return null;
633633
}
634-
return SchemaLabel.getLabelId(this.graph, HugeType.EDGE, label);
634+
return SchemaLabel.getEdgeLabelId(this.graph, label);
635635
}
636636

637637
protected void checkVertexExist(Id vertexId, String name) {

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/JaccardSimilarTraverser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public double jaccardSimilarity(Id vertex, Id other, Directions dir,
5454
E.checkNotNull(dir, "direction");
5555
checkDegree(degree);
5656

57-
Id labelId = this.getEdgeLabelId(label);
57+
Id labelId = this.getEdgeLabelIdOrNull(label);
5858

5959
Set<Id> sourceNeighbors = IteratorUtils.set(this.adjacentVertices(
6060
vertex, dir, labelId, degree));

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/KneighborTraverser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Set<Id> kneighbor(Id sourceV, Directions dir,
4646
checkDegree(degree);
4747
checkLimit(limit);
4848

49-
Id labelId = this.getEdgeLabelId(label);
49+
Id labelId = this.getEdgeLabelIdOrNull(label);
5050

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

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/KoutTraverser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Set<Id> kout(Id sourceV, Directions dir, String label,
5858
capacity, limit);
5959
}
6060

61-
Id labelId = this.getEdgeLabelId(label);
61+
Id labelId = this.getEdgeLabelIdOrNull(label);
6262

6363
Set<Id> sources = newIdSet();
6464
Set<Id> neighbors = newIdSet();

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/PathsTraverser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public PathSet paths(Id sourceV, Directions sourceDir,
5959
return PathSet.EMPTY;
6060
}
6161

62-
Id labelId = this.getEdgeLabelId(label);
62+
Id labelId = this.getEdgeLabelIdOrNull(label);
6363
Traverser traverser = new Traverser(sourceV, targetV, labelId,
6464
degree, capacity, limit);
6565
// We should stop early if walk backtrace or reach limit

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/PersonalRankTraverser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Map<Id, Double> personalRank(Id source, String label,
5555
Map<Id, Double> ranks = newMap();
5656
ranks.put(source, 1.0);
5757

58-
Id labelId = this.graph().edgeLabel(label).id();
58+
Id labelId = this.getEdgeLabelIdOrNull(label);
5959
Directions dir = this.getStartDirection(source, label);
6060

6161
Set<Id> outSeeds = newIdSet();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Set<Id> sameNeighbors(Id vertex, Id other, Directions direction,
4545
checkDegree(degree);
4646
checkLimit(limit);
4747

48-
Id labelId = this.getEdgeLabelId(label);
48+
Id labelId = this.getEdgeLabelIdOrNull(label);
4949

5050
Set<Id> sourceNeighbors = IteratorUtils.set(this.adjacentVertices(
5151
vertex, direction, labelId, degree));
@@ -80,7 +80,7 @@ public Set<Id> sameNeighbors(List<Id> vertexIds, Directions direction,
8080
List<Id> labelsId = new ArrayList<>();
8181
if (labels != null) {
8282
for (String label : labels) {
83-
labelsId.add(this.getEdgeLabelId(label));
83+
labelsId.add(this.getEdgeLabelIdOrNull(label));
8484
}
8585
}
8686

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public Path shortestPath(Id sourceV, Id targetV, Directions dir,
6161

6262
Map<Id, String> labelMap = newMap(labels.size());
6363
for (String label : labels) {
64-
labelMap.put(this.getEdgeLabelId(label), label);
64+
labelMap.put(this.getEdgeLabelIdOrNull(label), label);
6565
}
6666
Traverser traverser = new Traverser(sourceV, targetV, dir, labelMap,
6767
degree, skipDegree, capacity);
@@ -122,7 +122,7 @@ public PathSet allShortestPaths(Id sourceV, Id targetV, Directions dir,
122122

123123
Map<Id, String> labelMap = newMap(labels.size());
124124
for (String label : labels) {
125-
labelMap.put(this.getEdgeLabelId(label), label);
125+
labelMap.put(this.getEdgeLabelIdOrNull(label), label);
126126
}
127127
Traverser traverser = new Traverser(sourceV, targetV, dir, labelMap,
128128
degree, skipDegree, capacity);

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public WeightedPaths singleSourceShortestPaths(Id sourceV, Directions dir,
5858
checkSkipDegree(skipDegree, degree, capacity);
5959
checkLimit(limit);
6060

61-
Id labelId = this.getEdgeLabelId(label);
61+
Id labelId = this.getEdgeLabelIdOrNull(label);
6262
Traverser traverser = new Traverser(sourceV, dir, labelId, weight,
6363
degree, skipDegree, capacity, limit);
6464
while (true) {
@@ -94,7 +94,7 @@ public NodeWithWeight weightedShortestPath(Id sourceV, Id targetV,
9494
checkCapacity(capacity);
9595
checkSkipDegree(skipDegree, degree, capacity);
9696

97-
Id labelId = this.getEdgeLabelId(label);
97+
Id labelId = this.getEdgeLabelIdOrNull(label);
9898
Traverser traverser = new Traverser(sourceV, dir, labelId, weight,
9999
degree, skipDegree, capacity,
100100
NO_LIMIT);

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/SubGraphTraverser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private PathSet subGraphPaths(Id sourceV, Directions dir, String label,
8181
checkCapacity(capacity);
8282
checkLimit(limit);
8383

84-
Id labelId = this.getEdgeLabelId(label);
84+
Id labelId = this.getEdgeLabelIdOrNull(label);
8585
Traverser traverser = new Traverser(sourceV, labelId, depth, degree,
8686
capacity, limit, rings,
8787
sourceInRing);

0 commit comments

Comments
 (0)