javeme commented on code in PR #2242:
URL:
https://github.com/apache/incubator-hugegraph/pull/2242#discussion_r1275968202
##########
hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/EdgeExistenceTraverser.java:
##########
@@ -0,0 +1,62 @@
+package org.apache.hugegraph.traversal.algorithm;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.hugegraph.HugeGraph;
+import org.apache.hugegraph.auth.HugeTarget;
+import org.apache.hugegraph.auth.HugeUser;
+import org.apache.hugegraph.backend.id.Id;
+import org.apache.hugegraph.backend.query.ConditionQuery;
+import org.apache.hugegraph.backend.query.IdQuery;
+import org.apache.hugegraph.backend.query.Query;
+import org.apache.hugegraph.schema.EdgeLabel;
+import org.apache.hugegraph.schema.PropertyKey;
+import org.apache.hugegraph.type.HugeType;
+import org.apache.hugegraph.type.define.Directions;
+import org.apache.hugegraph.type.define.HugeKeys;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+public class EdgeExistenceTraverser extends HugeTraverser {
+ public EdgeExistenceTraverser(HugeGraph graph) {
+ super(graph);
+ }
+
+ public Iterator<Edge> queryEdgeExistence(Id sourceId, Id targetId, String
label,
+ String sortValues, long limit) {
+ if (label == null || label.isEmpty()) {
+ return queryByNeighbor(sourceId, targetId, limit);
+ }
+ Id edgeLabelId = getEdgeLabelId(label);
+ EdgeLabel edgeLabel = EdgeLabel.undefined(graph(), edgeLabelId);
+ List<Id> sortKeys = edgeLabel.sortKeys();
+
+ ConditionQuery conditionQuery = new ConditionQuery(HugeType.EDGE);
+ conditionQuery.eq(HugeKeys.OWNER_VERTEX, sourceId);
+ conditionQuery.eq(HugeKeys.OTHER_VERTEX, targetId);
+ conditionQuery.eq(HugeKeys.LABEL, edgeLabelId);
+ conditionQuery.eq(HugeKeys.DIRECTION, Directions.OUT);
+ conditionQuery.eq(HugeKeys.SORT_VALUES, sortValues);
+ conditionQuery.limit(limit);
+ if (sortKeys != null) {
+ List<String> names = graph().mapPkId2Name(sortKeys);
+ conditionQuery.key(HugeKeys.SORT_KEYS, names);
Review Comment:
expect sortValues here?
##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/EdgeExistenceAPI.java:
##########
@@ -0,0 +1,61 @@
+package org.apache.hugegraph.api.traversers;
+
+import static
org.apache.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_LIMIT;
+
+import com.codahale.metrics.annotation.Timed;
+import com.google.common.collect.Lists;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import jakarta.inject.Singleton;
+import jakarta.ws.rs.*;
+import jakarta.ws.rs.core.Context;
+import org.apache.hugegraph.HugeGraph;
+import org.apache.hugegraph.backend.id.Id;
+import org.apache.hugegraph.core.GraphManager;
+import org.apache.hugegraph.structure.HugeVertex;
+import org.apache.hugegraph.traversal.algorithm.EdgeExistenceTraverser;
+import org.apache.hugegraph.util.E;
+import org.apache.hugegraph.util.Log;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.slf4j.Logger;
+
+import java.util.Iterator;
+import java.util.List;
+
+@Path("graphs/{graph}/traversers/edgeexistence")
+@Singleton
+@Tag(name = "EdgeExistenceAPI")
+public class EdgeExistenceAPI extends TraverserAPI {
+
+ private static final Logger LOG = Log.logger(EdgeExistenceAPI.class);
+ private static final String DEFAULT_EMPTY = "";
+
+ @GET
+ @Timed
+ @Produces(APPLICATION_JSON_WITH_CHARSET)
+ public String get(@Context GraphManager manager,
+ @PathParam("graph") String graph,
+ @QueryParam("source") String source,
+ @QueryParam("target") String target,
+ @QueryParam("edgelabel") String edgeLabel,
+ @QueryParam("sortValues")
+ @DefaultValue(DEFAULT_EMPTY) String sortValues,
+ @QueryParam("limit")
+ @DefaultValue(DEFAULT_LIMIT) long limit) {
+ LOG.debug("Graph [{}] get edgeexistence with " +
+ "source '{}', target '{}', edgeLabel '{}', sortValue '{}'and
limit '{}'",
+ graph, source, target, edgeLabel, sortValues, limit);
+
+ E.checkArgumentNotNull(source, "The source can't be null");
+ E.checkArgumentNotNull(target, "The target can't be null");
+
+ Id sourceId = HugeVertex.getIdValue(source);
+ Id targetId = HugeVertex.getIdValue(target);
+ HugeGraph hugeGraph = graph(manager, graph);
Review Comment:
hugeGraph => hugegraph
##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/EdgeExistenceAPI.java:
##########
@@ -0,0 +1,61 @@
+package org.apache.hugegraph.api.traversers;
+
+import static
org.apache.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_LIMIT;
+
+import com.codahale.metrics.annotation.Timed;
+import com.google.common.collect.Lists;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import jakarta.inject.Singleton;
+import jakarta.ws.rs.*;
+import jakarta.ws.rs.core.Context;
+import org.apache.hugegraph.HugeGraph;
+import org.apache.hugegraph.backend.id.Id;
+import org.apache.hugegraph.core.GraphManager;
+import org.apache.hugegraph.structure.HugeVertex;
+import org.apache.hugegraph.traversal.algorithm.EdgeExistenceTraverser;
+import org.apache.hugegraph.util.E;
+import org.apache.hugegraph.util.Log;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.slf4j.Logger;
+
+import java.util.Iterator;
+import java.util.List;
+
+@Path("graphs/{graph}/traversers/edgeexistence")
+@Singleton
+@Tag(name = "EdgeExistenceAPI")
+public class EdgeExistenceAPI extends TraverserAPI {
+
+ private static final Logger LOG = Log.logger(EdgeExistenceAPI.class);
+ private static final String DEFAULT_EMPTY = "";
+
+ @GET
+ @Timed
+ @Produces(APPLICATION_JSON_WITH_CHARSET)
+ public String get(@Context GraphManager manager,
+ @PathParam("graph") String graph,
+ @QueryParam("source") String source,
+ @QueryParam("target") String target,
+ @QueryParam("edgelabel") String edgeLabel,
+ @QueryParam("sortValues")
+ @DefaultValue(DEFAULT_EMPTY) String sortValues,
+ @QueryParam("limit")
+ @DefaultValue(DEFAULT_LIMIT) long limit) {
+ LOG.debug("Graph [{}] get edgeexistence with " +
+ "source '{}', target '{}', edgeLabel '{}', sortValue '{}'and
limit '{}'",
+ graph, source, target, edgeLabel, sortValues, limit);
+
+ E.checkArgumentNotNull(source, "The source can't be null");
+ E.checkArgumentNotNull(target, "The target can't be null");
+
+ Id sourceId = HugeVertex.getIdValue(source);
+ Id targetId = HugeVertex.getIdValue(target);
+ HugeGraph hugeGraph = graph(manager, graph);
+ EdgeExistenceTraverser traverser = new
EdgeExistenceTraverser(hugeGraph);
+
+ Iterator<Edge> edges = traverser.queryEdgeExistence(sourceId,
targetId, edgeLabel, sortValues, limit);
+
+ List<Edge> all = Lists.newArrayList(edges);
+ return manager.serializer(hugeGraph).writeList("edges", all);
Review Comment:
can we just serialize the iterator instead of a list
##########
hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/EdgeExistenceTraverser.java:
##########
@@ -0,0 +1,62 @@
+package org.apache.hugegraph.traversal.algorithm;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.hugegraph.HugeGraph;
+import org.apache.hugegraph.auth.HugeTarget;
+import org.apache.hugegraph.auth.HugeUser;
+import org.apache.hugegraph.backend.id.Id;
+import org.apache.hugegraph.backend.query.ConditionQuery;
+import org.apache.hugegraph.backend.query.IdQuery;
+import org.apache.hugegraph.backend.query.Query;
+import org.apache.hugegraph.schema.EdgeLabel;
+import org.apache.hugegraph.schema.PropertyKey;
+import org.apache.hugegraph.type.HugeType;
+import org.apache.hugegraph.type.define.Directions;
+import org.apache.hugegraph.type.define.HugeKeys;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+public class EdgeExistenceTraverser extends HugeTraverser {
+ public EdgeExistenceTraverser(HugeGraph graph) {
+ super(graph);
+ }
+
+ public Iterator<Edge> queryEdgeExistence(Id sourceId, Id targetId, String
label,
+ String sortValues, long limit) {
+ if (label == null || label.isEmpty()) {
+ return queryByNeighbor(sourceId, targetId, limit);
+ }
+ Id edgeLabelId = getEdgeLabelId(label);
+ EdgeLabel edgeLabel = EdgeLabel.undefined(graph(), edgeLabelId);
+ List<Id> sortKeys = edgeLabel.sortKeys();
+
+ ConditionQuery conditionQuery = new ConditionQuery(HugeType.EDGE);
+ conditionQuery.eq(HugeKeys.OWNER_VERTEX, sourceId);
+ conditionQuery.eq(HugeKeys.OTHER_VERTEX, targetId);
+ conditionQuery.eq(HugeKeys.LABEL, edgeLabelId);
+ conditionQuery.eq(HugeKeys.DIRECTION, Directions.OUT);
+ conditionQuery.eq(HugeKeys.SORT_VALUES, sortValues);
+ conditionQuery.limit(limit);
+ if (sortKeys != null) {
+ List<String> names = graph().mapPkId2Name(sortKeys);
+ conditionQuery.key(HugeKeys.SORT_KEYS, names);
+ }
+ return graph().edges(conditionQuery);
+ }
+
+ private Iterator<Edge> queryByNeighbor(Id sourceId, Id targetId, long
limit) {
+ Iterator<Edge> edges = this.edgesOfVertex(sourceId, Directions.OUT,
(Id) null, limit);
+ List<Edge> res = new ArrayList<>();
Review Comment:
use QueryResult.ListIterator?
##########
hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/EdgeExistenceTraverser.java:
##########
@@ -0,0 +1,62 @@
+package org.apache.hugegraph.traversal.algorithm;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.hugegraph.HugeGraph;
+import org.apache.hugegraph.auth.HugeTarget;
+import org.apache.hugegraph.auth.HugeUser;
+import org.apache.hugegraph.backend.id.Id;
+import org.apache.hugegraph.backend.query.ConditionQuery;
+import org.apache.hugegraph.backend.query.IdQuery;
+import org.apache.hugegraph.backend.query.Query;
+import org.apache.hugegraph.schema.EdgeLabel;
+import org.apache.hugegraph.schema.PropertyKey;
+import org.apache.hugegraph.type.HugeType;
+import org.apache.hugegraph.type.define.Directions;
+import org.apache.hugegraph.type.define.HugeKeys;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+public class EdgeExistenceTraverser extends HugeTraverser {
+ public EdgeExistenceTraverser(HugeGraph graph) {
+ super(graph);
+ }
+
+ public Iterator<Edge> queryEdgeExistence(Id sourceId, Id targetId, String
label,
+ String sortValues, long limit) {
+ if (label == null || label.isEmpty()) {
+ return queryByNeighbor(sourceId, targetId, limit);
+ }
+ Id edgeLabelId = getEdgeLabelId(label);
+ EdgeLabel edgeLabel = EdgeLabel.undefined(graph(), edgeLabelId);
+ List<Id> sortKeys = edgeLabel.sortKeys();
+
+ ConditionQuery conditionQuery = new ConditionQuery(HugeType.EDGE);
+ conditionQuery.eq(HugeKeys.OWNER_VERTEX, sourceId);
+ conditionQuery.eq(HugeKeys.OTHER_VERTEX, targetId);
+ conditionQuery.eq(HugeKeys.LABEL, edgeLabelId);
+ conditionQuery.eq(HugeKeys.DIRECTION, Directions.OUT);
+ conditionQuery.eq(HugeKeys.SORT_VALUES, sortValues);
+ conditionQuery.limit(limit);
+ if (sortKeys != null) {
+ List<String> names = graph().mapPkId2Name(sortKeys);
+ conditionQuery.key(HugeKeys.SORT_KEYS, names);
+ }
+ return graph().edges(conditionQuery);
+ }
+
+ private Iterator<Edge> queryByNeighbor(Id sourceId, Id targetId, long
limit) {
+ Iterator<Edge> edges = this.edgesOfVertex(sourceId, Directions.OUT,
(Id) null, limit);
+ List<Edge> res = new ArrayList<>();
+ String target = targetId.toString();
+ while (edges.hasNext()) {
+ Edge edge = edges.next();
+ String outVertexId = edge.inVertex().id().toString();
+ if (!target.equals(outVertexId)) continue;
Review Comment:
adjust the code style
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]