From 97b600126cf69da3ea2b36b01cabd134a752f854 Mon Sep 17 00:00:00 2001 From: Zhangmei Li Date: Thu, 29 Jul 2021 18:06:53 +0800 Subject: [PATCH] use list instead of set for ids since no duplicate id Change-Id: I4e775b77964980644b08d8569315b4ab9af0701f --- .../hugegraph/api/traversers/KneighborAPI.java | 18 +++++++++++------- .../hugegraph/api/traversers/KoutAPI.java | 18 +++++++++++------- .../hugegraph/serializer/JsonSerializer.java | 7 +++---- .../baidu/hugegraph/serializer/Serializer.java | 5 ++--- .../algorithm/records/KneighborRecords.java | 6 +++--- .../algorithm/records/KoutRecords.java | 6 +++--- 6 files changed, 33 insertions(+), 27 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KneighborAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KneighborAPI.java index addd1f6fbe..4c721a13e5 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KneighborAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KneighborAPI.java @@ -24,6 +24,7 @@ import java.util.HashSet; import java.util.Iterator; +import java.util.List; import java.util.Set; import javax.inject.Singleton; @@ -44,6 +45,7 @@ import com.baidu.hugegraph.api.graph.EdgeAPI; import com.baidu.hugegraph.api.graph.VertexAPI; import com.baidu.hugegraph.backend.id.Id; +import com.baidu.hugegraph.backend.query.Query; import com.baidu.hugegraph.backend.query.QueryResults; import com.baidu.hugegraph.core.GraphManager; import com.baidu.hugegraph.server.RestServer; @@ -57,7 +59,7 @@ import com.baidu.hugegraph.util.Log; import com.codahale.metrics.annotation.Timed; import com.fasterxml.jackson.annotation.JsonProperty; -import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableList; @Path("graphs/{graph}/traversers/kneighbor") @Singleton @@ -132,18 +134,20 @@ public String post(@Context GraphManager manager, request.limit); } - int size = results.size(); - Set neighbors = request.countOnly ? - ImmutableSet.of() : results.ids(request.limit); + long size = results.size(); + if (request.limit != Query.NO_LIMIT && size > request.limit) { + size = request.limit; + } + List neighbors = request.countOnly ? + ImmutableList.of() : results.ids(request.limit); HugeTraverser.PathSet paths = new HugeTraverser.PathSet(); if (request.withPath) { paths.addAll(results.paths(request.limit)); } Iterator iter = QueryResults.emptyIterator(); - if (request.withVertex) { - Set ids = new HashSet<>(); - ids.addAll(results.ids(request.limit)); + if (request.withVertex && !request.countOnly) { + Set ids = new HashSet<>(neighbors); if (request.withPath) { for (HugeTraverser.Path p : paths) { ids.addAll(p.vertices()); diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KoutAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KoutAPI.java index 9f98ed82cb..40bab89193 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KoutAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KoutAPI.java @@ -25,6 +25,7 @@ import java.util.HashSet; import java.util.Iterator; +import java.util.List; import java.util.Set; import javax.inject.Singleton; @@ -45,6 +46,7 @@ import com.baidu.hugegraph.api.graph.EdgeAPI; import com.baidu.hugegraph.api.graph.VertexAPI; import com.baidu.hugegraph.backend.id.Id; +import com.baidu.hugegraph.backend.query.Query; import com.baidu.hugegraph.backend.query.QueryResults; import com.baidu.hugegraph.core.GraphManager; import com.baidu.hugegraph.server.RestServer; @@ -58,7 +60,7 @@ import com.baidu.hugegraph.util.Log; import com.codahale.metrics.annotation.Timed; import com.fasterxml.jackson.annotation.JsonProperty; -import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableList; @Path("graphs/{graph}/traversers/kout") @Singleton @@ -141,18 +143,20 @@ public String post(@Context GraphManager manager, request.limit); } - int size = results.size(); - Set neighbors = request.countOnly ? - ImmutableSet.of() : results.ids(request.limit); + long size = results.size(); + if (request.limit != Query.NO_LIMIT && size > request.limit) { + size = request.limit; + } + List neighbors = request.countOnly ? + ImmutableList.of() : results.ids(request.limit); HugeTraverser.PathSet paths = new HugeTraverser.PathSet(); if (request.withPath) { paths.addAll(results.paths(request.limit)); } Iterator iter = QueryResults.emptyIterator(); - if (request.withVertex) { - Set ids = new HashSet<>(); - ids.addAll(results.ids(request.limit)); + if (request.withVertex && !request.countOnly) { + Set ids = new HashSet<>(neighbors); if (request.withPath) { for (HugeTraverser.Path p : paths) { ids.addAll(p.vertices()); diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java index a9f41ccef7..03d6cd88c3 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java @@ -25,7 +25,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Set; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; import org.apache.tinkerpop.gremlin.structure.Edge; @@ -311,9 +310,9 @@ public String writeWeightedPaths(WeightedPaths paths, } @Override - public String writeNodesWithPath(String name, Set nodes, int size, + public String writeNodesWithPath(String name, List nodes, long size, Collection paths, - Iterator iterator) { + Iterator vertices) { List> pathList = new ArrayList<>(); for (HugeTraverser.Path path : paths) { pathList.add(path.toMap(false)); @@ -321,7 +320,7 @@ public String writeNodesWithPath(String name, Set nodes, int size, Map results; results = ImmutableMap.of(name, nodes, "size", size, - "paths", pathList, "vertices", iterator); + "paths", pathList, "vertices", vertices); return JsonUtil.toJson(results); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/Serializer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/Serializer.java index 011b419b50..a7544d0368 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/Serializer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/Serializer.java @@ -23,7 +23,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Set; import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Vertex; @@ -101,7 +100,7 @@ public String writeWeightedPath(NodeWithWeight path, public String writeWeightedPaths(WeightedPaths paths, Iterator vertices); - public String writeNodesWithPath(String name, Set nodes, int size, + public String writeNodesWithPath(String name, List nodes, long size, Collection paths, - Iterator iterator); + Iterator vertices); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KneighborRecords.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KneighborRecords.java index df7eeb4fa0..2b2233c008 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KneighborRecords.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KneighborRecords.java @@ -21,7 +21,7 @@ import static com.baidu.hugegraph.backend.query.Query.NO_LIMIT; -import java.util.Set; +import java.util.List; import java.util.Stack; import com.baidu.hugegraph.backend.id.Id; @@ -44,8 +44,8 @@ public int size() { return (int) this.accessed(); } - public Set ids(long limit) { - Set ids = CollectionFactory.newIdSet(CollectionType.EC); + public List ids(long limit) { + List ids = CollectionFactory.newList(CollectionType.EC); Stack records = this.records(); // Not include record(i=0) to ignore source vertex for (int i = 1; i < records.size(); i++) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KoutRecords.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KoutRecords.java index da58b6cd3b..f48fe2c0f7 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KoutRecords.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KoutRecords.java @@ -21,7 +21,7 @@ import static com.baidu.hugegraph.backend.query.Query.NO_LIMIT; -import java.util.Set; +import java.util.List; import java.util.Stack; import com.baidu.hugegraph.backend.id.Id; @@ -44,9 +44,9 @@ public int size() { return this.currentRecord().size(); } - public Set ids(long limit) { + public List ids(long limit) { + List ids = CollectionFactory.newList(CollectionType.EC); IntIterator iterator = this.records().peek().keys(); - Set ids = CollectionFactory.newIdSet(CollectionType.EC); while ((limit == NO_LIMIT || limit-- > 0L) && iterator.hasNext()) { ids.add(this.id(iterator.next())); }