From 89b34b34fabb2c08127348c019a3ee2edc599eb3 Mon Sep 17 00:00:00 2001 From: M <1216063060@qq.com> Date: Sun, 6 Aug 2023 21:31:37 +0800 Subject: [PATCH] chore tiny improve chore: keep Iterator type chore chore tiny improve tiny changes tiny changes update version --- hugegraph-server/hugegraph-api/pom.xml | 2 +- .../api/traversers/EdgeExistenceAPI.java | 37 ++++++++---- .../apache/hugegraph/version/ApiVersion.java | 3 +- .../algorithm/EdgeExistenceTraverser.java | 56 ++++++++++--------- 4 files changed, 57 insertions(+), 41 deletions(-) diff --git a/hugegraph-server/hugegraph-api/pom.xml b/hugegraph-server/hugegraph-api/pom.xml index d365ce22f8..2da9fafda8 100644 --- a/hugegraph-server/hugegraph-api/pom.xml +++ b/hugegraph-server/hugegraph-api/pom.xml @@ -188,7 +188,7 @@ - 0.69.0.0 + 0.70.0.0 diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/EdgeExistenceAPI.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/EdgeExistenceAPI.java index cb3623d0ba..c2ffb0738a 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/EdgeExistenceAPI.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/traversers/EdgeExistenceAPI.java @@ -1,9 +1,25 @@ +/* + * 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 com.google.common.collect.Lists; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.inject.Singleton; import jakarta.ws.rs.*; @@ -19,9 +35,8 @@ import org.slf4j.Logger; import java.util.Iterator; -import java.util.List; -@Path("graphs/{graph}/traversers/edgeexistence") +@Path("graphs/{graph}/traversers/edgeexist") @Singleton @Tag(name = "EdgeExistenceAPI") public class EdgeExistenceAPI extends TraverserAPI { @@ -36,26 +51,24 @@ public String get(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("source") String source, @QueryParam("target") String target, - @QueryParam("edgelabel") String edgeLabel, - @QueryParam("sortValues") + @QueryParam("label") String edgeLabel, + @QueryParam("sort_values") @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); + "source '{}', target '{}', edgeLabel '{}', sortValue '{}', 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); - + HugeGraph hugegraph = graph(manager, graph); + EdgeExistenceTraverser traverser = new EdgeExistenceTraverser(hugegraph); Iterator edges = traverser.queryEdgeExistence(sourceId, targetId, edgeLabel, sortValues, limit); - List all = Lists.newArrayList(edges); - return manager.serializer(hugeGraph).writeList("edges", all); + return manager.serializer(hugegraph).writeEdges(edges, false); } } diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/version/ApiVersion.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/version/ApiVersion.java index 969e9a3d6a..fb03007e01 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/version/ApiVersion.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/version/ApiVersion.java @@ -114,12 +114,13 @@ public final class ApiVersion { * [0.67] Issue-1065: Support dynamically add/remove graph * [0.68] Issue-1763: Support adamic-adar & resource-allocation API * [0.69] Issue-1748: Support Cypher query RESTful API + * [0.70] Issue-2242: Add edge-existence RESTful API */ /** * The second parameter of Version.of() is for IDE running without JAR */ - public static final Version VERSION = Version.of(ApiVersion.class, "0.69"); + public static final Version VERSION = Version.of(ApiVersion.class, "0.70"); public static void check() { // Check version of hugegraph-core. Firstly do check from version 0.3 diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/EdgeExistenceTraverser.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/EdgeExistenceTraverser.java index bb09934e03..5048f6cd35 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/EdgeExistenceTraverser.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/EdgeExistenceTraverser.java @@ -1,62 +1,64 @@ +/* + * 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 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.iterator.FilterIterator; 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 queryEdgeExistence(Id sourceId, Id targetId, String label, String sortValues, long limit) { + // If no label provided, fallback to slow query by filtering if (label == null || label.isEmpty()) { - return queryByNeighbor(sourceId, targetId, limit); + return queryByNeighbors(sourceId, targetId, limit); } Id edgeLabelId = getEdgeLabelId(label); - EdgeLabel edgeLabel = EdgeLabel.undefined(graph(), edgeLabelId); - List sortKeys = edgeLabel.sortKeys(); - + EdgeLabel edgeLabel = graph().edgeLabel(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.eq(HugeKeys.SORT_VALUES, sortValues); conditionQuery.limit(limit); - if (sortKeys != null) { - List names = graph().mapPkId2Name(sortKeys); - conditionQuery.key(HugeKeys.SORT_KEYS, names); + if (edgeLabel.existSortKeys()) { + conditionQuery.eq(HugeKeys.SORT_VALUES, sortValues); + } else { + conditionQuery.eq(HugeKeys.SORT_VALUES, ""); } return graph().edges(conditionQuery); } - private Iterator queryByNeighbor(Id sourceId, Id targetId, long limit) { - Iterator edges = this.edgesOfVertex(sourceId, Directions.OUT, (Id) null, limit); - List 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; - res.add(edge); - } - return res.iterator(); + private Iterator queryByNeighbors(Id sourceId, Id targetId, long limit) { + return new FilterIterator<>(edgesOfVertex(sourceId, Directions.OUT, (Id) null, limit) + , edge -> targetId.equals(edge.inVertex().id())); } }