chore: unify to call SchemaLabel.getLabelId() (#2458)

Change-Id: I31bcc0d1ee99f3c443f8f4f0d458e06ca89977ef
This commit is contained in:
Jermy Li 2024-02-24 22:45:23 +08:00 committed by GitHub
parent bc421bb197
commit eef3b355f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 39 additions and 34 deletions

View File

@ -26,6 +26,7 @@ import java.util.Set;
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 @@ public class Vertices {
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 @@ public class Vertices {
} 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()) {

View File

@ -379,7 +379,7 @@ public abstract class AbstractAlgorithm implements Algorithm {
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);
}

View File

@ -26,6 +26,7 @@ import org.apache.hugegraph.backend.id.Id;
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 @@ public class BetweennessCentralityAlgorithmV2 extends AbstractCentAlgorithm {
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,

View File

@ -81,10 +81,7 @@ public class ClosenessCentralityAlgorithmV2 extends AbstractCentAlgorithm {
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,

View File

@ -73,7 +73,7 @@ public class DegreeCentralityAlgorithm extends AbstractCentAlgorithm {
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;

View File

@ -60,7 +60,7 @@ public class StressCentralityAlgorithmV2 extends AbstractCentAlgorithm {
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 @@ public class StressCentralityAlgorithmV2 extends AbstractCentAlgorithm {
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,

View File

@ -178,7 +178,7 @@ public class LpaAlgorithm extends AbstractCommAlgorithm {
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);

View File

@ -177,4 +177,12 @@ public abstract class SchemaLabel extends SchemaElement
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);
}
}

View File

@ -112,7 +112,7 @@ public class FusiformSimilarityTraverser extends HugeTraverser {
// 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);

View File

@ -620,18 +620,18 @@ public class HugeTraverser {
}
}
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) {

View File

@ -54,7 +54,7 @@ public class JaccardSimilarTraverser extends OltpTraverser {
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));

View File

@ -46,7 +46,7 @@ public class KneighborTraverser extends OltpTraverser {
checkDegree(degree);
checkLimit(limit);
Id labelId = this.getEdgeLabelId(label);
Id labelId = this.getEdgeLabelIdOrNull(label);
KneighborRecords records = new KneighborRecords(true, sourceV, true);

View File

@ -58,7 +58,7 @@ public class KoutTraverser extends OltpTraverser {
capacity, limit);
}
Id labelId = this.getEdgeLabelId(label);
Id labelId = this.getEdgeLabelIdOrNull(label);
Set<Id> sources = newIdSet();
Set<Id> neighbors = newIdSet();

View File

@ -59,7 +59,7 @@ public class PathsTraverser extends HugeTraverser {
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

View File

@ -55,7 +55,7 @@ public class PersonalRankTraverser extends HugeTraverser {
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();

View File

@ -45,7 +45,7 @@ public class SameNeighborTraverser extends HugeTraverser {
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 @@ public class SameNeighborTraverser extends HugeTraverser {
List<Id> labelsId = new ArrayList<>();
if (labels != null) {
for (String label : labels) {
labelsId.add(this.getEdgeLabelId(label));
labelsId.add(this.getEdgeLabelIdOrNull(label));
}
}

View File

@ -61,7 +61,7 @@ public class ShortestPathTraverser extends HugeTraverser {
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 @@ public class ShortestPathTraverser extends HugeTraverser {
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);

View File

@ -58,7 +58,7 @@ public class SingleSourceShortestPathTraverser extends HugeTraverser {
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 @@ public class SingleSourceShortestPathTraverser extends HugeTraverser {
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);

View File

@ -81,7 +81,7 @@ public class SubGraphTraverser extends HugeTraverser {
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);