javeme commented on code in PR #2242: URL: https://github.com/apache/incubator-hugegraph/pull/2242#discussion_r1390426185
########## hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/EdgeExistenceAPI.java: ########## @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package org.apache.hugegraph.api.traversers; + +import static org.apache.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_LIMIT; + +import com.codahale.metrics.annotation.Timed; +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; + +@Path("graphs/{graph}/traversers/edgeexist") +@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") Review Comment: expect the style `label`/`sort_values` like this: https://github.com/apache/incubator-hugegraph/blob/bce6d058f5d1f108b4c19e18eb7a65ab0604a526/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/SingleSourceShortestPathAPI.java#L66 ########## hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/EdgeExistenceAPI.java: ########## @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package org.apache.hugegraph.api.traversers; + +import static org.apache.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_LIMIT; + +import com.codahale.metrics.annotation.Timed; +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; + +@Path("graphs/{graph}/traversers/edgeexist") +@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); Review Comment: align with `"` : ```java LOG.debug("Graph [{}] get edgeexistence with " + "source '{}', target '{}', edgeLabel '{}', sortValue '{}'and limit '{}'", graph, source, target, edgeLabel, sortValues, limit); ``` ########## hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/EdgeExistenceTraverser.java: ########## @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package org.apache.hugegraph.traversal.algorithm; + +import org.apache.hugegraph.HugeGraph; +import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.backend.query.ConditionQuery; +import org.apache.hugegraph.backend.query.QueryResults; +import org.apache.hugegraph.iterator.ListIterator; +import org.apache.hugegraph.schema.EdgeLabel; +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.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 queryByNeighbors(sourceId, targetId, limit); + } + Id edgeLabelId = getEdgeLabelId(label); + EdgeLabel edgeLabel = EdgeLabel.undefined(graph(), edgeLabelId); + 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.limit(limit); + if (edgeLabel.existSortKeys()) { + conditionQuery.eq(HugeKeys.SORT_VALUES, sortValues); + } else { + conditionQuery.eq(HugeKeys.SORT_VALUES, ""); + } + return graph().edges(conditionQuery); + } + + private Iterator<Edge> queryByNeighbors(Id sourceId, Id targetId, long limit) { + Iterator<Edge> edges = this.edgesOfVertex(sourceId, Directions.OUT, (Id) null, limit); + List<Edge> results = newList(); + while (edges.hasNext()){ Review Comment: we can use FilterIterator instead: https://github.com/apache/incubator-hugegraph/blob/bce6d058f5d1f108b4c19e18eb7a65ab0604a526/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/OltpTraverser.java#L239 ########## hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/EdgeExistenceTraverser.java: ########## @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package org.apache.hugegraph.traversal.algorithm; + +import org.apache.hugegraph.HugeGraph; +import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.backend.query.ConditionQuery; +import org.apache.hugegraph.backend.query.QueryResults; +import org.apache.hugegraph.iterator.ListIterator; +import org.apache.hugegraph.schema.EdgeLabel; +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.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 queryByNeighbors(sourceId, targetId, limit); + } + Id edgeLabelId = getEdgeLabelId(label); + EdgeLabel edgeLabel = EdgeLabel.undefined(graph(), edgeLabelId); Review Comment: unused var? ########## hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/EdgeExistenceTraverser.java: ########## @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package org.apache.hugegraph.traversal.algorithm; + +import org.apache.hugegraph.HugeGraph; +import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.backend.query.ConditionQuery; +import org.apache.hugegraph.backend.query.QueryResults; +import org.apache.hugegraph.iterator.ListIterator; +import org.apache.hugegraph.schema.EdgeLabel; +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.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 queryByNeighbors(sourceId, targetId, limit); + } + Id edgeLabelId = getEdgeLabelId(label); + EdgeLabel edgeLabel = EdgeLabel.undefined(graph(), edgeLabelId); + 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.limit(limit); + if (edgeLabel.existSortKeys()) { + conditionQuery.eq(HugeKeys.SORT_VALUES, sortValues); + } else { + conditionQuery.eq(HugeKeys.SORT_VALUES, ""); + } + return graph().edges(conditionQuery); + } + + private Iterator<Edge> queryByNeighbors(Id sourceId, Id targetId, long limit) { + Iterator<Edge> edges = this.edgesOfVertex(sourceId, Directions.OUT, (Id) null, limit); + List<Edge> results = newList(); + while (edges.hasNext()){ + Edge edge = edges.next(); + if (targetId.compareTo((Id) edge.inVertex().id()) == 0) { Review Comment: we can call `targetId.equals(edge.otherVertexId())` -- 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]
