From 8bfe4d55557e0a9918fb1b34891dea0fd7770b1c Mon Sep 17 00:00:00 2001 From: imbajin Date: Fri, 29 Oct 2021 16:58:54 +0800 Subject: [PATCH 1/8] feat(api): Support adamic-adar & resource-allocation algorithms --- .../java/com/baidu/hugegraph/api/API.java | 2 +- .../api/traversers/PredictionAPI.java | 122 ++++++++++++++++++ .../baidu/hugegraph/version/ApiVersion.java | 5 +- .../algorithm/PredictionTraverser.java | 75 +++++++++++ .../traversal/algorithm/steps/EdgeStep.java | 1 + 5 files changed, 202 insertions(+), 3 deletions(-) create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PredictionAPI.java create mode 100644 hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java index 398f0a686d..ff51da3c9f 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java @@ -45,7 +45,7 @@ public class API { - private static final Logger LOG = Log.logger(RestServer.class); + public static final Logger LOG = Log.logger(RestServer.class); public static final String CHARSET = "UTF-8"; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PredictionAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PredictionAPI.java new file mode 100644 index 0000000000..728a2d8a73 --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PredictionAPI.java @@ -0,0 +1,122 @@ +/* + * Copyright 2022 HugeGraph Authors + * + * 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 com.baidu.hugegraph.api.traversers; + +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_ELEMENTS_LIMIT; +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_MAX_DEGREE; + +import javax.inject.Singleton; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; + +import com.baidu.hugegraph.HugeGraph; +import com.baidu.hugegraph.api.API; +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.core.GraphManager; +import com.baidu.hugegraph.traversal.algorithm.PredictionTraverser; +import com.baidu.hugegraph.type.define.Directions; +import com.baidu.hugegraph.util.E; +import com.baidu.hugegraph.util.JsonUtil; +import com.codahale.metrics.annotation.Timed; +import com.google.common.collect.ImmutableMap; + +/** + * This API include similar prediction algorithms, now include: + * - Adamic Adar + * - Resource Allocation + * + * Could add more prediction algorithms in future + */ +@Path("graphs/{graph}/traversers/") +@Singleton +public class PredictionAPI extends API { + + @GET + @Timed + @Path("adamicadar") + @Produces(APPLICATION_JSON_WITH_CHARSET) + public String get(@Context GraphManager manager, + @PathParam("graph") String graph, + @QueryParam("vertex") String current, + @QueryParam("other") String other, + @QueryParam("direction") String direction, + @QueryParam("label") String edgeLabel, + @QueryParam("max_degree") + @DefaultValue(DEFAULT_MAX_DEGREE) long maxDegree, + @QueryParam("limit") + @DefaultValue(DEFAULT_ELEMENTS_LIMIT) long limit) { + LOG.debug("Graph [{}] get adamic adar between '{}' and '{}' with " + + "direction {}, edge label {}, max degree '{}' and limit '{}'", + graph, current, other, direction, edgeLabel, maxDegree, + limit); + + Id sourceId = VertexAPI.checkAndParseVertexId(current); + Id targetId = VertexAPI.checkAndParseVertexId(other); + E.checkArgument(!current.equals(other), + "The source and target vertex id can't be same"); + Directions dir = Directions.convert(EdgeAPI.parseDirection(direction)); + + HugeGraph g = graph(manager, graph); + PredictionTraverser traverser = new PredictionTraverser(g); + double score = traverser.adamicAdar(sourceId, targetId, dir, + edgeLabel, maxDegree, limit); + return JsonUtil.toJson(ImmutableMap.of("adamic_adar", score)); + } + + @GET + @Timed + @Path("resourceallocation") + @Produces(APPLICATION_JSON_WITH_CHARSET) + public String create(@Context GraphManager manager, + @PathParam("graph") String graph, + @QueryParam("vertex") String current, + @QueryParam("other") String other, + @QueryParam("direction") String direction, + @QueryParam("label") String edgeLabel, + @QueryParam("max_degree") + @DefaultValue(DEFAULT_MAX_DEGREE) long maxDegree, + @QueryParam("limit") + @DefaultValue(DEFAULT_ELEMENTS_LIMIT) long limit) { + LOG.debug("Graph [{}] get resource allocation between '{}' and '{}' " + + "with direction {}, edge label {}, max degree '{}' and " + + "limit '{}'", graph, current, other, direction, edgeLabel, + maxDegree, limit); + + Id sourceId = VertexAPI.checkAndParseVertexId(current); + Id targetId = VertexAPI.checkAndParseVertexId(other); + E.checkArgument(!current.equals(other), + "The source and target vertex id can't be same"); + Directions dir = Directions.convert(EdgeAPI.parseDirection(direction)); + + HugeGraph g = graph(manager, graph); + PredictionTraverser traverser = new PredictionTraverser(g); + double score = traverser.resourceAllocation(sourceId, targetId, dir, + edgeLabel, maxDegree, + limit); + return JsonUtil.toJson(ImmutableMap.of("resource_allocation", score)); + } +} diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java index ad1f14b76a..78870f4577 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java @@ -117,12 +117,13 @@ public final class ApiVersion { * [0.65] Issue-1506: Support olap property key * [0.66] Issue-1567: Support get schema RESTful API * [0.67] Issue-1065: Support dynamically add/remove graph + * [0.68] Issue-1741: Support adamic-adar & resource-allocation API */ // The second parameter of Version.of() is for IDE running without JAR - public static final Version VERSION = Version.of(ApiVersion.class, "0.67"); + public static final Version VERSION = Version.of(ApiVersion.class, "0.68"); - public static final void check() { + public static void check() { // Check version of hugegraph-core. Firstly do check from version 0.3 VersionUtil.check(CoreVersion.VERSION, "0.12", "0.13", CoreVersion.NAME); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java new file mode 100644 index 0000000000..21b902c33e --- /dev/null +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java @@ -0,0 +1,75 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * 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 com.baidu.hugegraph.traversal.algorithm; + +import static java.lang.Math.log; + +import java.util.Set; + +import com.baidu.hugegraph.HugeGraph; +import com.baidu.hugegraph.backend.id.Id; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; +import com.baidu.hugegraph.type.define.Directions; +import com.baidu.hugegraph.util.E; +import com.google.common.collect.ImmutableList; + +public class PredictionTraverser extends OltpTraverser { + + public PredictionTraverser(HugeGraph graph) { + super(graph); + } + + public double adamicAdar(Id source, Id target, Directions dir, + String label, long degree, long limit) { + Set neighbors = checkAndGetCommonNeighbors(source, target, dir, + label, degree, limit); + EdgeStep step = label == null ? new EdgeStep(graph(), dir) : + new EdgeStep(graph(), dir, ImmutableList.of(label)); + + return neighbors.stream() + .mapToDouble(vid -> 1.0 / log(edgesCount(vid, step))) + .sum(); + } + + public double resourceAllocation(Id source, Id target, Directions dir, + String label, long degree, long limit) { + Set neighbors = checkAndGetCommonNeighbors(source, target, dir, + label, degree, limit); + EdgeStep step = label == null ? new EdgeStep(graph(), dir) : + new EdgeStep(graph(), dir, ImmutableList.of(label)); + + return neighbors.stream() + .mapToDouble(vid -> 1.0 / edgesCount(vid, step)) + .sum(); + } + + private Set checkAndGetCommonNeighbors(Id source, Id target, + Directions dir, String label, + long degree, long limit) { + E.checkNotNull(source, "source id"); + E.checkNotNull(target, "the target id"); + this.checkVertexExist(source, "source"); + this.checkVertexExist(target, "target"); + E.checkNotNull(dir, "direction"); + checkDegree(degree); + SameNeighborTraverser traverser = new SameNeighborTraverser(graph()); + return traverser.sameNeighbors(source, target, dir, label, degree, limit); + } +} diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java index b1d7ad1dad..dc2bb06990 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java @@ -72,6 +72,7 @@ public EdgeStep(HugeGraph g, Directions direction, List labels, public EdgeStep(HugeGraph g, Directions direction, List labels, Map properties, long degree, long skipDegree) { + E.checkNotNull(g, "The graph can't be null"); E.checkArgument(degree == NO_LIMIT || degree > 0L, "The max degree must be > 0 or == -1, but got: %s", degree); From f7ce0f3d0414b4bc2d39eb0f6e270612636fd96a Mon Sep 17 00:00:00 2001 From: imbajin Date: Thu, 27 Jan 2022 17:59:02 +0800 Subject: [PATCH 2/8] split into 2 class --- .../java/com/baidu/hugegraph/api/API.java | 2 +- .../api/traversers/AdamicAdarAPI.java | 88 +++++++++++++++++++ ...ionAPI.java => ResourceAllocationAPI.java} | 37 +------- .../algorithm/PredictionTraverser.java | 18 ++-- 4 files changed, 101 insertions(+), 44 deletions(-) create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java rename hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/{PredictionAPI.java => ResourceAllocationAPI.java} (68%) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java index ff51da3c9f..91fbef80cf 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java @@ -45,7 +45,7 @@ public class API { - public static final Logger LOG = Log.logger(RestServer.class); + protected static final Logger LOG = Log.logger(RestServer.class); public static final String CHARSET = "UTF-8"; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java new file mode 100644 index 0000000000..cc1f4c6237 --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java @@ -0,0 +1,88 @@ +/* + * Copyright 2022 HugeGraph Authors + * + * 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 com.baidu.hugegraph.api.traversers; + +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_ELEMENTS_LIMIT; +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_MAX_DEGREE; + +import javax.inject.Singleton; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; + +import com.baidu.hugegraph.HugeGraph; +import com.baidu.hugegraph.api.API; +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.core.GraphManager; +import com.baidu.hugegraph.traversal.algorithm.PredictionTraverser; +import com.baidu.hugegraph.type.define.Directions; +import com.baidu.hugegraph.util.E; +import com.baidu.hugegraph.util.JsonUtil; +import com.codahale.metrics.annotation.Timed; +import com.google.common.collect.ImmutableMap; + +/** + * This API include similar prediction algorithms, now include: + * - Adamic Adar + * - Resource Allocation + * + * Could add more prediction algorithms in future + */ +@Path("graphs/{graph}/traversers/adamicadar") +@Singleton +public class AdamicAdarAPI extends API { + + @GET + @Timed + @Produces(APPLICATION_JSON_WITH_CHARSET) + public String get(@Context GraphManager manager, + @PathParam("graph") String graph, + @QueryParam("vertex") String current, + @QueryParam("other") String other, + @QueryParam("direction") String direction, + @QueryParam("label") String edgeLabel, + @QueryParam("max_degree") + @DefaultValue(DEFAULT_MAX_DEGREE) long maxDegree, + @QueryParam("limit") + @DefaultValue(DEFAULT_ELEMENTS_LIMIT) long limit) { + LOG.debug("Graph [{}] get adamic adar between '{}' and '{}' with " + + "direction {}, edge label {}, max degree '{}' and limit '{}'", + graph, current, other, direction, edgeLabel, maxDegree, + limit); + + Id sourceId = VertexAPI.checkAndParseVertexId(current); + Id targetId = VertexAPI.checkAndParseVertexId(other); + E.checkArgument(!current.equals(other), + "The source and target vertex id can't be same"); + Directions dir = Directions.convert(EdgeAPI.parseDirection(direction)); + + HugeGraph g = graph(manager, graph); + PredictionTraverser traverser = new PredictionTraverser(g); + double score = traverser.adamicAdar(sourceId, targetId, dir, + edgeLabel, maxDegree, limit); + return JsonUtil.toJson(ImmutableMap.of("adamic_adar", score)); + } +} diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PredictionAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java similarity index 68% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PredictionAPI.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java index 728a2d8a73..f476faddcf 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PredictionAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java @@ -51,45 +51,12 @@ * * Could add more prediction algorithms in future */ -@Path("graphs/{graph}/traversers/") +@Path("graphs/{graph}/traversers/resourceallocation") @Singleton -public class PredictionAPI extends API { +public class ResourceAllocationAPI extends API { @GET @Timed - @Path("adamicadar") - @Produces(APPLICATION_JSON_WITH_CHARSET) - public String get(@Context GraphManager manager, - @PathParam("graph") String graph, - @QueryParam("vertex") String current, - @QueryParam("other") String other, - @QueryParam("direction") String direction, - @QueryParam("label") String edgeLabel, - @QueryParam("max_degree") - @DefaultValue(DEFAULT_MAX_DEGREE) long maxDegree, - @QueryParam("limit") - @DefaultValue(DEFAULT_ELEMENTS_LIMIT) long limit) { - LOG.debug("Graph [{}] get adamic adar between '{}' and '{}' with " + - "direction {}, edge label {}, max degree '{}' and limit '{}'", - graph, current, other, direction, edgeLabel, maxDegree, - limit); - - Id sourceId = VertexAPI.checkAndParseVertexId(current); - Id targetId = VertexAPI.checkAndParseVertexId(other); - E.checkArgument(!current.equals(other), - "The source and target vertex id can't be same"); - Directions dir = Directions.convert(EdgeAPI.parseDirection(direction)); - - HugeGraph g = graph(manager, graph); - PredictionTraverser traverser = new PredictionTraverser(g); - double score = traverser.adamicAdar(sourceId, targetId, dir, - edgeLabel, maxDegree, limit); - return JsonUtil.toJson(ImmutableMap.of("adamic_adar", score)); - } - - @GET - @Timed - @Path("resourceallocation") @Produces(APPLICATION_JSON_WITH_CHARSET) public String create(@Context GraphManager manager, @PathParam("graph") String graph, diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java index 21b902c33e..231fe5cddf 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java @@ -19,8 +19,6 @@ package com.baidu.hugegraph.traversal.algorithm; -import static java.lang.Math.log; - import java.util.Set; import com.baidu.hugegraph.HugeGraph; @@ -43,9 +41,11 @@ public double adamicAdar(Id source, Id target, Directions dir, EdgeStep step = label == null ? new EdgeStep(graph(), dir) : new EdgeStep(graph(), dir, ImmutableList.of(label)); - return neighbors.stream() - .mapToDouble(vid -> 1.0 / log(edgesCount(vid, step))) - .sum(); + double sum = 0.0; + for (Id vid : neighbors) { + sum += 1.0 / Math.log(this.edgesCount(vid, step)); + } + return sum; } public double resourceAllocation(Id source, Id target, Directions dir, @@ -55,9 +55,11 @@ public double resourceAllocation(Id source, Id target, Directions dir, EdgeStep step = label == null ? new EdgeStep(graph(), dir) : new EdgeStep(graph(), dir, ImmutableList.of(label)); - return neighbors.stream() - .mapToDouble(vid -> 1.0 / edgesCount(vid, step)) - .sum(); + double sum = 0.0; + for (Id vid : neighbors) { + sum += 1.0 / this.edgesCount(vid, step); + } + return sum; } private Set checkAndGetCommonNeighbors(Id source, Id target, From 1e9635cd22260a8804dfdce2e739307ae6fc151d Mon Sep 17 00:00:00 2001 From: imbajin Date: Thu, 10 Feb 2022 19:28:16 +0800 Subject: [PATCH 3/8] add test & format --- .../hugegraph/auth/StandardAuthenticator.java | 2 +- .../serializer/BinaryBackendEntry.java | 5 +- .../backend/serializer/BytesBuffer.java | 6 +- .../backend/serializer/SerializerFactory.java | 14 +++-- .../backend/serializer/TableBackendEntry.java | 3 +- .../backend/serializer/TextBackendEntry.java | 5 +- .../com/baidu/hugegraph/api/BaseApiTest.java | 6 +- .../api/traversers/AdamicAdarAPITest.java | 62 +++++++++++++++++++ .../traversers/ResourceAllocationAPITest.java | 62 +++++++++++++++++++ .../traversers/TraversersApiTestSuite.java | 4 +- 10 files changed, 146 insertions(+), 23 deletions(-) create mode 100644 hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java create mode 100644 hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java index b28a0b1911..5d56169f0e 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java @@ -82,7 +82,7 @@ private String inputPassword() { String notEmptyPrompt = "The admin password can't be empty"; Console console = System.console(); while (true) { - String password = ""; + String password; if (console != null) { char[] chars = console.readPassword(inputPrompt); password = new String(chars); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinaryBackendEntry.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinaryBackendEntry.java index bbcd963622..6c33e6d802 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinaryBackendEntry.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinaryBackendEntry.java @@ -191,10 +191,7 @@ public boolean equals(Object obj) { if (this.columns.size() != other.columns.size()) { return false; } - if (!this.columns.containsAll(other.columns)) { - return false; - } - return true; + return this.columns.containsAll(other.columns); } protected static final class BinaryId implements Id { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BytesBuffer.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BytesBuffer.java index 19b6bfb684..abeea5f046 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BytesBuffer.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BytesBuffer.java @@ -300,8 +300,7 @@ public BytesBuffer writeBytes(byte[] bytes) { public byte[] readBytes() { int length = this.readVInt(); assert length >= 0; - byte[] bytes = this.read(length); - return bytes; + return this.read(length); } public BytesBuffer writeBigBytes(byte[] bytes) { @@ -317,8 +316,7 @@ public BytesBuffer writeBigBytes(byte[] bytes) { public byte[] readBigBytes() { int length = this.readVInt(); assert length >= 0; - byte[] bytes = this.read(length); - return bytes; + return this.read(length); } public BytesBuffer writeStringRaw(String val) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/SerializerFactory.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/SerializerFactory.java index f0a2d227f1..86e1fedaa7 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/SerializerFactory.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/SerializerFactory.java @@ -34,12 +34,14 @@ public class SerializerFactory { public static AbstractSerializer serializer(String name) { name = name.toLowerCase(); - if ("binary".equals(name)) { - return new BinarySerializer(); - } else if ("binaryscatter".equals(name)) { - return new BinaryScatterSerializer(); - } else if ("text".equals(name)) { - return new TextSerializer(); + switch (name) { + case "binary": + return new BinarySerializer(); + case "binaryscatter": + return new BinaryScatterSerializer(); + case "text": + return new TextSerializer(); + default: } Class clazz = serializers.get(name); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableBackendEntry.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableBackendEntry.java index 0c281de24c..216d3ef24a 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableBackendEntry.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableBackendEntry.java @@ -130,7 +130,7 @@ public String toString() { private final List subRows; // NOTE: selfChanged is false when the row has not changed but subRows has. - private boolean selfChanged = true; + private boolean selfChanged; private boolean olap = false; public TableBackendEntry(Id id) { @@ -195,6 +195,7 @@ public void olap(boolean olap) { this.olap = olap; } + @Override public boolean olap() { return this.olap; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TextBackendEntry.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TextBackendEntry.java index c5cd9fd9bb..98c428642d 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TextBackendEntry.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TextBackendEntry.java @@ -187,7 +187,7 @@ public void append(TextBackendEntry entry) { } // TODO: ensure the old value is a list and json format (for index) - if (oldValue.equals("[]")) { + if ("[]".equals(oldValue)) { this.column(col.getKey(), newValue); continue; } @@ -222,12 +222,11 @@ public void eliminate(TextBackendEntry entry) { } // TODO: ensure the old value is a list and json format (for index) - List values = new ArrayList<>(); @SuppressWarnings("unchecked") List oldValues = JsonUtil.fromJson(oldValue, List.class); @SuppressWarnings("unchecked") List newValues = JsonUtil.fromJson(newValue, List.class); - values.addAll(oldValues); + List values = new ArrayList<>(oldValues); values.removeAll(newValues); // Update the old value this.column(col.getKey(), JsonUtil.toJson(values)); diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/BaseApiTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/BaseApiTest.java index ac44371254..cde7511ede 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/BaseApiTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/BaseApiTest.java @@ -55,8 +55,8 @@ public class BaseApiTest { - private static String BASE_URL = "http://127.0.0.1:8080"; - private static String GRAPH = "hugegraph"; + private static final String BASE_URL = "http://127.0.0.1:8080"; + private static final String GRAPH = "hugegraph"; private static final String USERNAME = "admin"; private static final String PASSWORD = "pa"; @@ -626,7 +626,7 @@ public static T assertMapContains(Map map, String key) { break; } } - Assert.assertTrue(message, found != null); + Assert.assertNotNull(message, found); return found; } } diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java new file mode 100644 index 0000000000..cf7b54af60 --- /dev/null +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2022 HugeGraph Authors + * + * 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 com.baidu.hugegraph.api.traversers; + +import java.util.Map; + +import javax.ws.rs.core.Response; + +import org.junit.Before; +import org.junit.Test; + +import com.baidu.hugegraph.api.BaseApiTest; +import com.google.common.collect.ImmutableMap; + +public class AdamicAdarAPITest extends BaseApiTest { + + private final static String PATH = TRAVERSERS_API + "/adamicadar"; + + @Before + public void prepareSchema() { + BaseApiTest.initPropertyKey(); + BaseApiTest.initVertexLabel(); + BaseApiTest.initEdgeLabel(); + BaseApiTest.initVertex(); + BaseApiTest.initEdge(); + } + + @Test + public void testGet() { + Map name2Ids = listAllVertexName2Ids(); + name2Ids.entrySet().forEach(System.out::println); + + String markoId = name2Ids.get("marko"); + String joshId = name2Ids.get("josh"); + String peterId = name2Ids.get("peter"); + Response r = client().get(PATH, ImmutableMap.of("vertex", + id2Json(markoId), + "other", + id2Json(joshId))); + String content = assertResponseStatus(200, r); + assertJsonContains(content, "adamic_adar"); + //Assert.assertFalse(sameNeighbors.isEmpty()); + //Assert.assertTrue(sameNeighbors.contains(peterId)); + } +} diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java new file mode 100644 index 0000000000..fecf392903 --- /dev/null +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2022 HugeGraph Authors + * + * 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 com.baidu.hugegraph.api.traversers; + +import java.util.Map; + +import javax.ws.rs.core.Response; + +import org.junit.Before; +import org.junit.Test; + +import com.baidu.hugegraph.api.BaseApiTest; +import com.google.common.collect.ImmutableMap; + +public class ResourceAllocationAPITest extends BaseApiTest { + + private final static String PATH = TRAVERSERS_API + "/adamicadar"; + + @Before + public void prepareSchema() { + BaseApiTest.initPropertyKey(); + BaseApiTest.initVertexLabel(); + BaseApiTest.initEdgeLabel(); + BaseApiTest.initVertex(); + BaseApiTest.initEdge(); + } + + @Test + public void testGet() { + Map name2Ids = listAllVertexName2Ids(); + name2Ids.entrySet().forEach(System.out::println); + + String markoId = name2Ids.get("marko"); + String joshId = name2Ids.get("josh"); + String peterId = name2Ids.get("peter"); + Response r = client().get(PATH, ImmutableMap.of("vertex", + id2Json(markoId), + "other", + id2Json(joshId))); + String content = assertResponseStatus(200, r); + assertJsonContains(content, "adamic_adar"); + //Assert.assertFalse(sameNeighbors.isEmpty()); + //Assert.assertTrue(sameNeighbors.contains(peterId)); + } +} diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/TraversersApiTestSuite.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/TraversersApiTestSuite.java index 5ca1bef647..b29b685500 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/TraversersApiTestSuite.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/TraversersApiTestSuite.java @@ -43,7 +43,9 @@ ShortestPathApiTest.class, SingleSourceShortestPathApiTest.class, TemplatePathsApiTest.class, - WeightedShortestPathApiTest.class + WeightedShortestPathApiTest.class, + AdamicAdarAPITest.class, + ResourceAllocationAPITest.class }) public class TraversersApiTestSuite { } From b9f7910de2bfcceb872c29ea13fdfea8838557c9 Mon Sep 17 00:00:00 2001 From: imbajin Date: Thu, 10 Feb 2022 22:49:09 +0800 Subject: [PATCH 4/8] enhance a string of code logic --- .../java/com/baidu/hugegraph/StandardHugeGraph.java | 2 +- .../com/baidu/hugegraph/auth/EntityManager.java | 6 ++---- .../baidu/hugegraph/auth/RelationshipManager.java | 6 ++---- .../baidu/hugegraph/backend/cache/LevelCache.java | 2 +- .../java/com/baidu/hugegraph/backend/id/IdUtil.java | 5 +++-- .../hugegraph/backend/query/ConditionQuery.java | 12 ++++++------ .../backend/query/ConditionQueryFlatten.java | 4 +--- .../com/baidu/hugegraph/backend/query/Query.java | 3 +-- .../baidu/hugegraph/backend/query/QueryResults.java | 5 +---- .../backend/serializer/TableBackendEntry.java | 3 ++- .../baidu/hugegraph/backend/store/ram/RamTable.java | 1 + .../hugegraph/backend/tx/GraphIndexTransaction.java | 5 ++--- .../hugegraph/backend/tx/GraphTransaction.java | 1 + .../com/baidu/hugegraph/io/HugeGraphIoRegistry.java | 4 ++-- .../hugegraph/schema/builder/EdgeLabelBuilder.java | 2 +- .../schema/builder/VertexLabelBuilder.java | 9 ++------- .../com/baidu/hugegraph/structure/HugeVertex.java | 7 ++----- .../baidu/hugegraph/task/StandardTaskScheduler.java | 2 +- .../java/com/baidu/hugegraph/task/TaskStatus.java | 2 +- .../traversal/algorithm/steps/EdgeStep.java | 6 ++---- .../traversal/optimize/HugeScriptTraversal.java | 7 +++++-- .../hugegraph/traversal/optimize/TraversalUtil.java | 13 +++++-------- .../hugegraph/api/traversers/AdamicAdarAPITest.java | 1 - .../api/traversers/ResourceAllocationAPITest.java | 5 ++--- 24 files changed, 47 insertions(+), 66 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java index 6b4b205ede..1e6d8d5473 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java @@ -933,7 +933,7 @@ public void drop() { */ this.close(); } catch (Throwable e) { - LOG.warn("Failed to close graph {}", e, this); + LOG.warn("Failed to close graph {} {}", e, this); } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/EntityManager.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/EntityManager.java index 53b4ba152d..c4b668e619 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/EntityManager.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/EntityManager.java @@ -118,9 +118,7 @@ public boolean exists(Id id) { Iterator vertices = this.tx().queryVertices(id); if (vertices.hasNext()) { Vertex vertex = vertices.next(); - if (this.label.equals(vertex.label())) { - return true; - } + return this.label.equals(vertex.label()); } return false; } @@ -145,7 +143,7 @@ protected List toList(Iterator vertices) { } private Iterator queryById(List ids) { - Object[] idArray = ids.toArray(new Id[ids.size()]); + Object[] idArray = ids.toArray(new Id[0]); return this.tx().queryVertices(idArray); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/RelationshipManager.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/RelationshipManager.java index 07adb6714b..1fb3232fd5 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/RelationshipManager.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/RelationshipManager.java @@ -122,9 +122,7 @@ public boolean exists(Id id) { Iterator edges = this.tx().queryEdges(id); if (edges.hasNext()) { Edge edge = edges.next(); - if (this.label.equals(edge.label())) { - return true; - } + return this.label.equals(edge.label()); } return false; } @@ -161,7 +159,7 @@ protected List toList(Iterator edges) { } private Iterator queryById(List ids) { - Object[] idArray = ids.toArray(new Id[ids.size()]); + Object[] idArray = ids.toArray(new Id[0]); return this.tx().queryEdges(idArray); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/LevelCache.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/LevelCache.java index ab29371869..287f0b85a3 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/LevelCache.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/LevelCache.java @@ -29,7 +29,7 @@ public final class LevelCache extends AbstractCache { // For multi-layer caches - private final AbstractCache caches[]; + private final AbstractCache[] caches; @SuppressWarnings("unchecked") public LevelCache(AbstractCache lavel1, diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/IdUtil.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/IdUtil.java index 0e3295a76e..60111f57c2 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/IdUtil.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/IdUtil.java @@ -134,12 +134,13 @@ public static String escape(char splitor, char escape, String... values) { public static String[] unescape(String id, String splitor, String escape) { /* - * Note that the `splitor`/`escape` maybe special characters in regular + * Note that the `splitter`/`escape` maybe special characters in regular * expressions, but this is a frequently called method, for faster * execution, we forbid the use of special characters as delimiter * or escape sign. + * * The `limit` param -1 in split method can ensure empty string be - * splited to a part. + * split to a part. */ String[] parts = id.split("(?"); - public static final String INDEX_VALUE_EMPTY = new String(""); + public static final String INDEX_VALUE_NULL = ""; + public static final String INDEX_VALUE_EMPTY = ""; public static final Set IGNORE_SYM_SET; static { @@ -659,7 +659,7 @@ public static String concatValues(Object value) { return concatValues((List) value); } else if (needConvertNumber(value)) { return LongEncoding.encodeNumber(value); - } else if (value == INDEX_VALUE_NULL) { + } else if (value.equals(INDEX_VALUE_NULL)) { return INDEX_SYM_NULL; } else { return escapeSpecialValueIfNeeded(value.toString()); @@ -675,7 +675,7 @@ private static String escapeSpecialValueIfNeeded(String value) { if (value.isEmpty()) { // Escape empty String to INDEX_SYM_EMPTY (char `\u0002`) value = INDEX_SYM_EMPTY; - } else if (value == INDEX_VALUE_EMPTY) { + } else if (value.equals(INDEX_VALUE_EMPTY)) { value = ""; } else { char ch = value.charAt(0); @@ -822,8 +822,8 @@ private static boolean numberEquals(Object number1, Object number2) { // Otherwise convert to BigDecimal to make two numbers comparable Number n1 = NumericUtil.convertToNumber(number1); Number n2 = NumericUtil.convertToNumber(number2); - BigDecimal b1 = new BigDecimal(n1.doubleValue()); - BigDecimal b2 = new BigDecimal(n2.doubleValue()); + BigDecimal b1 = BigDecimal.valueOf(n1.doubleValue()); + BigDecimal b2 = BigDecimal.valueOf(n2.doubleValue()); return b1.compareTo(b2) == 0; } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQueryFlatten.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQueryFlatten.java index b33d6d3089..d112a73ec8 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQueryFlatten.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQueryFlatten.java @@ -91,9 +91,7 @@ public static List flatten(ConditionQuery query, continue; } ConditionQuery cq = newQueryFromRelations(query, relations); - if (cq != null) { - queries.add(cq); - } + queries.add(cq); } return queries; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java index a46b92da07..4d4c87e7fd 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java @@ -282,8 +282,7 @@ public long limit() { } public void limit(long limit) { - E.checkArgument(limit >= 0L || limit == NO_LIMIT, - "Invalid limit %s", limit); + E.checkArgument(limit >= 0L, "Invalid limit %s", limit); this.limit = limit; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/QueryResults.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/QueryResults.java index 10f47f6623..c1cf47aa1a 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/QueryResults.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/QueryResults.java @@ -22,7 +22,6 @@ import java.util.Collection; import java.util.Collections; import java.util.Iterator; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; @@ -132,9 +131,7 @@ public Iterator keepInputOrderIfNeeded( ids = map.keySet(); } - return new MapperIterator<>(ids.iterator(), id -> { - return map.get(id); - }); + return new MapperIterator<>(ids.iterator(), map::get); } private boolean mustSortByInputIds() { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableBackendEntry.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableBackendEntry.java index 216d3ef24a..a87c134fa2 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableBackendEntry.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableBackendEntry.java @@ -131,7 +131,7 @@ public String toString() { // NOTE: selfChanged is false when the row has not changed but subRows has. private boolean selfChanged; - private boolean olap = false; + private boolean olap; public TableBackendEntry(Id id) { this(null, id); @@ -149,6 +149,7 @@ public TableBackendEntry(Row row) { this.row = row; this.subRows = new ArrayList<>(); this.selfChanged = true; + this.olap = false; } @Override diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/ram/RamTable.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/ram/RamTable.java index 1388f0a689..e78d341f7c 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/ram/RamTable.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/ram/RamTable.java @@ -278,6 +278,7 @@ public boolean matched(Query query) { for (Condition cond : cq.conditions()) { if (cond.equals(BOTH_COND)) { direction = Directions.BOTH; + break; } } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java index d01d1bf207..24f6ea62a8 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java @@ -283,7 +283,7 @@ protected void updateIndex(Id ilId, HugeElement element, boolean removed) { break; case UNIQUE: value = ConditionQuery.concatValues(allPropValues); - assert !value.equals(""); + assert !"".equals(value); Id id = element.id(); // TODO: add lock for updating unique index if (!removed && this.existUniqueValue(indexLabel, value, id)) { @@ -665,8 +665,7 @@ private IdHolder doIndexQueryBatch(IndexLabel indexLabel, // Iterate one batch, and keep iterator position Set ids = InsertionOrderUtil.newSet(); - while ((ids.size() < batch || batch == Query.NO_LIMIT) && - entries.hasNext()) { + while (ids.size() < batch && entries.hasNext()) { HugeIndex index = this.serializer.readIndex(graph(), query, entries.next()); this.removeExpiredIndexIfNeeded(index, query.showExpired()); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java index 994476378d..0adbaa3124 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java @@ -2010,6 +2010,7 @@ private void traverseByLabel(SchemaLabel label, if (label.enableLabelIndex()) { // Support label index, query by label index by paging + assert query instanceof ConditionQuery; ((ConditionQuery) query).eq(HugeKeys.LABEL, label.id()); Iterator iter = fetcher.apply(query); try { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/io/HugeGraphIoRegistry.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/io/HugeGraphIoRegistry.java index f6dac37bff..0ae0d59c86 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/io/HugeGraphIoRegistry.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/io/HugeGraphIoRegistry.java @@ -24,11 +24,11 @@ public class HugeGraphIoRegistry extends AbstractIoRegistry { - private static final HugeGraphIoRegistry instance = + private static final HugeGraphIoRegistry INSTANCE = new HugeGraphIoRegistry(); public static HugeGraphIoRegistry instance() { - return instance; + return INSTANCE; } private HugeGraphIoRegistry() { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/EdgeLabelBuilder.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/EdgeLabelBuilder.java index ffca6e66ac..338b45fb2e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/EdgeLabelBuilder.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/EdgeLabelBuilder.java @@ -200,7 +200,7 @@ private boolean hasSameProperties(EdgeLabel existedEdgeLabel) { return false; } } else { // this false - if (existedEdgeLabel.enableLabelIndex() == true) { + if (existedEdgeLabel.enableLabelIndex()) { return false; } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/VertexLabelBuilder.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/VertexLabelBuilder.java index fdeafbca11..75fb3285c1 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/VertexLabelBuilder.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/VertexLabelBuilder.java @@ -200,15 +200,10 @@ private boolean hasSameProperties(VertexLabel existedVertexLabel) { // this.enableLabelIndex == null, it means true. if (this.enableLabelIndex == null || this.enableLabelIndex) { - if (!existedVertexLabel.enableLabelIndex()) { - return false; - } + return existedVertexLabel.enableLabelIndex(); } else { // this.enableLabelIndex is false - if (existedVertexLabel.enableLabelIndex() == true) { - return false; - } + return !existedVertexLabel.enableLabelIndex(); } - return true; } @Override diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java index 59d3645e8d..228df2191b 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java @@ -279,7 +279,7 @@ public HugeEdge constructEdge(String label, HugeVertex vertex, if (elemKeys.id() != null) { throw Edge.Exceptions.userSuppliedIdsNotSupported(); } - Id id = HugeElement.getIdValue(elemKeys.id()); + Id id = null; // Check target vertex E.checkArgumentNotNull(vertex, "Target vertex can't be null"); @@ -323,10 +323,7 @@ public HugeEdge constructEdge(String label, HugeVertex vertex, ElementHelper.attachProperties(edge, keyValues); // Set id if it not exists - if (id == null) { - edge.assignId(); - } - + edge.assignId(); return edge; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/StandardTaskScheduler.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/StandardTaskScheduler.java index 0db34f6fdc..b51e9ce867 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/StandardTaskScheduler.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/StandardTaskScheduler.java @@ -707,7 +707,7 @@ private Iterator> queryTask(Map conditions, private Iterator> queryTask(List ids) { return this.call(() -> { - Object[] idArray = ids.toArray(new Id[ids.size()]); + Object[] idArray = ids.toArray(new Id[0]); Iterator vertices = this.tx().queryVertices(idArray); Iterator> tasks = new MapperIterator<>(vertices, HugeTask::fromVertex); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/TaskStatus.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/TaskStatus.java index 3107e13ee3..ec5053d31f 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/TaskStatus.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/TaskStatus.java @@ -48,7 +48,7 @@ public enum TaskStatus implements SerialEnum { public static final Set COMPLETED_STATUSES = ImmutableSet.of( TaskStatus.SUCCESS, TaskStatus.CANCELLED, TaskStatus.FAILED); - private byte status = 0; + private byte status; private String name; static { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java index dc2bb06990..88ecfc549e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java @@ -123,8 +123,7 @@ public long skipDegree() { public Id[] edgeLabels() { int elsSize = this.labels.size(); - Id[] edgeLabels = this.labels.keySet().toArray(new Id[elsSize]); - return edgeLabels; + return this.labels.keySet().toArray(new Id[elsSize]); } public void swithDirection() { @@ -132,8 +131,7 @@ public void swithDirection() { } public long limit() { - long limit = this.skipDegree > 0L ? this.skipDegree : this.degree; - return limit; + return this.skipDegree > 0L ? this.skipDegree : this.degree; } @Override diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/HugeScriptTraversal.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/HugeScriptTraversal.java index 8ca8191c94..5bd45b303e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/HugeScriptTraversal.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/HugeScriptTraversal.java @@ -35,8 +35,11 @@ import com.baidu.hugegraph.HugeException; /** - * ScriptTraversal encapsulates a {@link ScriptEngine} and a script which is compiled into a {@link Traversal} at {@link Admin#applyStrategies()}. - * This is useful for serializing traversals as the compilation can happen on the remote end where the traversal will ultimately be processed. + * ScriptTraversal encapsulates a {@link ScriptEngine} and a script which is + * compiled into a {@link Traversal} at {@link Admin#applyStrategies()}. + * + * This is useful for serializing traversals as the compilation can happen on + * the remote end where the traversal will ultimately be processed. * * @author Marko A. Rodriguez (http://markorodriguez.com) */ diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java index e9d9c49c54..54207d5dc0 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java @@ -453,6 +453,7 @@ public static Condition convIn2Relation(HugeGraph graph, String originKey = has.getKey(); if (values.size() > 1) { + // TODO: what we check now? the condition is always true? E.checkArgument(!originKey.equals(T.key) && !originKey.equals(T.value), "Not support hasKey() or hasValue() with " + @@ -557,11 +558,8 @@ public static Iterator filterResult(Vertex vertex, Directions dir, Iterator edges) { return new FilterIterator<>(edges, edge -> { - if (dir == Directions.OUT && vertex.equals(edge.outVertex()) || - dir == Directions.IN && vertex.equals(edge.inVertex())) { - return true; - } - return false; + return dir == Directions.OUT && vertex.equals(edge.outVertex()) || + dir == Directions.IN && vertex.equals(edge.inVertex()); }); } @@ -672,8 +670,7 @@ private static V validPropertyValue(V value, PropertyKey pkey) { E.checkArgument(false, "Invalid data type of query value in %s, " + "expect %s for '%s', actual got %s", - value, pkey.dataType(), pkey.name(), - value == null ? null : classes); + value, pkey.dataType(), pkey.name(), classes); } @SuppressWarnings("unchecked") @@ -927,7 +924,7 @@ private static Number[] predicateNumbers(String value, int count) { throw new HugeException( "Invalid value '%s', expect a list of number", value); } - return values.toArray(new Number[values.size()]); + return values.toArray(new Number[0]); } @SuppressWarnings("unchecked") diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java index cf7b54af60..e211d7d8ef 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java @@ -45,7 +45,6 @@ public void prepareSchema() { @Test public void testGet() { Map name2Ids = listAllVertexName2Ids(); - name2Ids.entrySet().forEach(System.out::println); String markoId = name2Ids.get("marko"); String joshId = name2Ids.get("josh"); diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java index fecf392903..ccd773e0c5 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java @@ -31,7 +31,7 @@ public class ResourceAllocationAPITest extends BaseApiTest { - private final static String PATH = TRAVERSERS_API + "/adamicadar"; + private final static String PATH = TRAVERSERS_API + "/resourceallocation"; @Before public void prepareSchema() { @@ -45,7 +45,6 @@ public void prepareSchema() { @Test public void testGet() { Map name2Ids = listAllVertexName2Ids(); - name2Ids.entrySet().forEach(System.out::println); String markoId = name2Ids.get("marko"); String joshId = name2Ids.get("josh"); @@ -55,7 +54,7 @@ public void testGet() { "other", id2Json(joshId))); String content = assertResponseStatus(200, r); - assertJsonContains(content, "adamic_adar"); + assertJsonContains(content, "resource_allocation"); //Assert.assertFalse(sameNeighbors.isEmpty()); //Assert.assertTrue(sameNeighbors.contains(peterId)); } From 34322cd065cea9a2f5f770e653f0ea272ae29e48 Mon Sep 17 00:00:00 2001 From: imbajin Date: Mon, 21 Feb 2022 19:32:16 +0800 Subject: [PATCH 5/8] adopt more code --- .../baidu/hugegraph/api/graph/VertexAPI.java | 4 +-- .../api/traversers/KneighborAPI.java | 3 +- .../hugegraph/api/traversers/KoutAPI.java | 4 +-- .../baidu/hugegraph/StandardHugeGraph.java | 2 +- .../baidu/hugegraph/auth/HugeResource.java | 29 +++++-------------- .../hugegraph/backend/query/Condition.java | 2 +- .../backend/query/ConditionQuery.java | 8 ++--- .../backend/tx/GraphIndexTransaction.java | 3 +- .../baidu/hugegraph/structure/HugeVertex.java | 5 ++-- .../algorithm/records/KneighborRecords.java | 6 ++-- .../traversal/optimize/TraversalUtil.java | 19 +++++------- .../api/traversers/AdamicAdarAPITest.java | 2 -- .../traversers/ResourceAllocationAPITest.java | 2 -- 13 files changed, 33 insertions(+), 56 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/graph/VertexAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/graph/VertexAPI.java index 6b48bea0dd..13b8ad8f11 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/graph/VertexAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/graph/VertexAPI.java @@ -405,7 +405,7 @@ private static void checkUpdate(BatchVertexRequest req) { E.checkArgument(req.updateStrategies != null && !req.updateStrategies.isEmpty(), "Parameter 'update_strategies' can't be empty"); - E.checkArgument(req.createIfNotExist == true, + E.checkArgument(req.createIfNotExist, "Parameter 'create_if_not_exist' " + "dose not support false now"); } @@ -462,7 +462,7 @@ public Object[] properties() { } if (this.id != null) { newProps[appendIndex++] = T.id; - newProps[appendIndex++] = this.id; + newProps[appendIndex] = this.id; } return newProps; } 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 4c721a13e5..4ec3ecfd22 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 @@ -45,7 +45,6 @@ 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; @@ -135,7 +134,7 @@ public String post(@Context GraphManager manager, } long size = results.size(); - if (request.limit != Query.NO_LIMIT && size > request.limit) { + if (size > request.limit) { size = request.limit; } List neighbors = request.countOnly ? 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 40bab89193..2f743a985e 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 @@ -46,7 +46,6 @@ 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; @@ -144,7 +143,7 @@ public String post(@Context GraphManager manager, } long size = results.size(); - if (request.limit != Query.NO_LIMIT && size > request.limit) { + if (size > request.limit) { size = request.limit; } List neighbors = request.countOnly ? @@ -154,6 +153,7 @@ public String post(@Context GraphManager manager, if (request.withPath) { paths.addAll(results.paths(request.limit)); } + Iterator iter = QueryResults.emptyIterator(); if (request.withVertex && !request.countOnly) { Set ids = new HashSet<>(neighbors); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java index 1e6d8d5473..fe548149e9 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java @@ -933,7 +933,7 @@ public void drop() { */ this.close(); } catch (Throwable e) { - LOG.warn("Failed to close graph {} {}", e, this); + LOG.warn("Failed to close graph {} {}", this, e); } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/HugeResource.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/HugeResource.java index 027386bde3..e0b5e6993e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/HugeResource.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/HugeResource.java @@ -138,9 +138,7 @@ public boolean filter(ResourceObject resourceObject) { private boolean filter(AuthElement element) { assert this.type.match(element.type()); if (element instanceof Namifiable) { - if (!this.filter((Namifiable) element)) { - return false; - } + return this.filter((Namifiable) element); } return true; } @@ -149,10 +147,7 @@ private boolean filter(Namifiable element) { assert !(element instanceof Typifiable) || this.type.match( ResourceType.from(((Typifiable) element).type())); - if (!this.matchLabel(element.name())) { - return false; - } - return true; + return this.matchLabel(element.name()); } private boolean filter(HugeElement element) { @@ -193,10 +188,7 @@ private boolean matchLabel(String other) { return false; } // It's ok if wildcard match or regular match - if (!this.label.equals(ANY) && !other.matches(this.label)) { - return false; - } - return true; + return this.label.equals(ANY) || other.matches(this.label); } private boolean matchProperties(Map other) { @@ -229,10 +221,7 @@ protected boolean contains(HugeResource other) { if (!this.matchLabel(other.label)) { return false; } - if (!this.matchProperties(other.properties)) { - return false; - } - return true; + return this.matchProperties(other.properties); } @Override @@ -260,9 +249,7 @@ public static boolean allowed(ResourceObject resourceObject) { // Allowed to access system(hidden) schema by anyone if (resourceObject.type().isSchema()) { Namifiable schema = (Namifiable) resourceObject.operated(); - if (Hidden.isHidden(schema.name())) { - return true; - } + return Hidden.isHidden(schema.name()); } return false; @@ -339,19 +326,19 @@ public HugeResource deserialize(JsonParser parser, HugeResource res = new HugeResource(); while (parser.nextToken() != JsonToken.END_OBJECT) { String key = parser.getCurrentName(); - if (key.equals("type")) { + if ("type".equals(key)) { if (parser.nextToken() != JsonToken.VALUE_NULL) { res.type = ctxt.readValue(parser, ResourceType.class); } else { res.type = null; } - } else if (key.equals("label")) { + } else if ("label".equals(key)) { if (parser.nextToken() != JsonToken.VALUE_NULL) { res.label = parser.getValueAsString(); } else { res.label = null; } - } else if (key.equals("properties")) { + } else if ("properties".equals(key)) { if (parser.nextToken() != JsonToken.VALUE_NULL) { @SuppressWarnings("unchecked") Map prop = ctxt.readValue(parser, diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Condition.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Condition.java index d3a512c85d..ecf7ac87a2 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Condition.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Condition.java @@ -107,7 +107,7 @@ public enum RelationType implements BiPredicate { assert v2 != null; /* * TODO: we still have no way to determine accurately, since - * some backends may scan with token(column) like cassandra. + * some backends may scan with token(column) like cassandra. */ return true; }); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java index 48bf152a0e..07e65c57cc 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java @@ -60,8 +60,8 @@ public class ConditionQuery extends IdQuery { public static final String INDEX_SYM_EMPTY = "\u0002"; public static final char INDEX_SYM_MAX = '\u0003'; - public static final String INDEX_VALUE_NULL = ""; - public static final String INDEX_VALUE_EMPTY = ""; + public static final String INDEX_VALUE_NULL = new String(""); + public static final String INDEX_VALUE_EMPTY = new String(""); public static final Set IGNORE_SYM_SET; static { @@ -74,7 +74,7 @@ public class ConditionQuery extends IdQuery { private static final List EMPTY_CONDITIONS = ImmutableList.of(); - // Conditions will be concated with `and` by default + // Conditions will be contacted with `and` by default private List conditions = EMPTY_CONDITIONS; private OptimizedType optimizedType = OptimizedType.NONE; @@ -659,7 +659,7 @@ public static String concatValues(Object value) { return concatValues((List) value); } else if (needConvertNumber(value)) { return LongEncoding.encodeNumber(value); - } else if (value.equals(INDEX_VALUE_NULL)) { + } else if (value == INDEX_VALUE_NULL) { return INDEX_SYM_NULL; } else { return escapeSpecialValueIfNeeded(value.toString()); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java index 24f6ea62a8..d497cb9e6a 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java @@ -665,7 +665,8 @@ private IdHolder doIndexQueryBatch(IndexLabel indexLabel, // Iterate one batch, and keep iterator position Set ids = InsertionOrderUtil.newSet(); - while (ids.size() < batch && entries.hasNext()) { + while ((batch == Query.NO_LIMIT || ids.size() < batch) && + entries.hasNext()) { HugeIndex index = this.serializer.readIndex(graph(), query, entries.next()); this.removeExpiredIndexIfNeeded(index, query.showExpired()); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java index 228df2191b..9285bf5a52 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java @@ -283,6 +283,7 @@ public HugeEdge constructEdge(String label, HugeVertex vertex, // Check target vertex E.checkArgumentNotNull(vertex, "Target vertex can't be null"); + // TODO: Why we check it? the param is HugeVertex already? E.checkArgument(vertex instanceof HugeVertex, "Target vertex must be an instance of HugeVertex"); @@ -322,7 +323,6 @@ public HugeEdge constructEdge(String label, HugeVertex vertex, // Set properties ElementHelper.attachProperties(edge, keyValues); - // Set id if it not exists edge.assignId(); return edge; } @@ -609,7 +609,8 @@ public boolean valid() { public HugeVertex prepareRemoved() { // NOTE: clear edges/properties of the cloned vertex and return HugeVertex vertex = this.clone(); - vertex.removed(true); /* Remove self */ + // Remove self + vertex.removed(true); vertex.resetEdges(); vertex.resetProperties(); return vertex; 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 71725caa6a..e84128ee98 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 @@ -19,8 +19,6 @@ package com.baidu.hugegraph.traversal.algorithm.records; -import static com.baidu.hugegraph.backend.query.Query.NO_LIMIT; - import java.util.List; import java.util.Stack; @@ -51,7 +49,7 @@ public List ids(long limit) { // Not include record(i=0) to ignore source vertex for (int i = 1; i < records.size(); i++) { IntIterator iterator = records.get(i).keys(); - while ((limit == NO_LIMIT || limit > 0L) && iterator.hasNext()) { + while ((limit > 0L) && iterator.hasNext()) { ids.add(this.id(iterator.next())); limit--; } @@ -65,7 +63,7 @@ public PathSet paths(long limit) { Stack records = this.records(); for (int i = 1; i < records.size(); i++) { IntIterator iterator = records.get(i).keys(); - while ((limit == NO_LIMIT || limit > 0L) && iterator.hasNext()) { + while ((limit > 0L) && iterator.hasNext()) { paths.add(this.linkPath(i, iterator.next())); limit--; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java index 54207d5dc0..890d281600 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java @@ -453,9 +453,8 @@ public static Condition convIn2Relation(HugeGraph graph, String originKey = has.getKey(); if (values.size() > 1) { - // TODO: what we check now? the condition is always true? - E.checkArgument(!originKey.equals(T.key) && - !originKey.equals(T.value), + E.checkArgument(!originKey.equals(T.key.getAccessor()) && + !originKey.equals(T.value.getAccessor()), "Not support hasKey() or hasValue() with " + "multiple values"); } @@ -700,13 +699,9 @@ private static V validPropertyValue(V value, PropertyKey pkey) { public static void retriveSysprop(List hasContainers, Function func) { - for (Iterator iter = hasContainers.iterator(); - iter.hasNext();) { - HasContainer container = iter.next(); - if (container.getKey().startsWith("~") && func.apply(container)) { - iter.remove(); - } - } + hasContainers.removeIf(container -> { + return container.getKey().startsWith("~") && func.apply(container); + }); } public static String page(GraphTraversal traversal) { @@ -902,7 +897,7 @@ private static Number predicateNumber(String value) { } private static Number[] predicateNumbers(String value, int count) { - List values = predicateArgs(value); + List values = predicateArgs(value); if (values.size() != count) { throw new HugeException("Invalid numbers size %s, expect %s", values.size(), count); @@ -918,7 +913,7 @@ private static Number[] predicateNumbers(String value, int count) { // pass } if (v instanceof Number) { - values.set(i, (Number) v); + values.set(i, v); continue; } throw new HugeException( diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java index e211d7d8ef..04a1b67ab5 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java @@ -55,7 +55,5 @@ public void testGet() { id2Json(joshId))); String content = assertResponseStatus(200, r); assertJsonContains(content, "adamic_adar"); - //Assert.assertFalse(sameNeighbors.isEmpty()); - //Assert.assertTrue(sameNeighbors.contains(peterId)); } } diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java index ccd773e0c5..ede6b7031e 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java @@ -55,7 +55,5 @@ public void testGet() { id2Json(joshId))); String content = assertResponseStatus(200, r); assertJsonContains(content, "resource_allocation"); - //Assert.assertFalse(sameNeighbors.isEmpty()); - //Assert.assertTrue(sameNeighbors.contains(peterId)); } } From 19a75b0077249d926ad7c4c962e7444fc44af773 Mon Sep 17 00:00:00 2001 From: imbajin Date: Tue, 22 Feb 2022 15:32:44 +0800 Subject: [PATCH 6/8] tiny improve --- .../java/com/baidu/hugegraph/api/graph/VertexAPI.java | 3 ++- .../baidu/hugegraph/api/traversers/KneighborAPI.java | 3 ++- .../com/baidu/hugegraph/api/traversers/KoutAPI.java | 3 ++- .../java/com/baidu/hugegraph/version/ApiVersion.java | 1 - .../baidu/hugegraph/backend/query/ConditionQuery.java | 8 ++++---- .../java/com/baidu/hugegraph/backend/query/Query.java | 1 + .../java/com/baidu/hugegraph/structure/HugeVertex.java | 7 +++---- .../hugegraph/traversal/optimize/TraversalUtil.java | 10 +++++++--- 8 files changed, 21 insertions(+), 15 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/graph/VertexAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/graph/VertexAPI.java index 13b8ad8f11..0ab4f638f3 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/graph/VertexAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/graph/VertexAPI.java @@ -462,7 +462,8 @@ public Object[] properties() { } if (this.id != null) { newProps[appendIndex++] = T.id; - newProps[appendIndex] = this.id; + // Keep value++ to avoid code trap + newProps[appendIndex++] = this.id; } return newProps; } 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 4ec3ecfd22..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 @@ -45,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; @@ -134,7 +135,7 @@ public String post(@Context GraphManager manager, } long size = results.size(); - if (size > request.limit) { + if (request.limit != Query.NO_LIMIT && size > request.limit) { size = request.limit; } List neighbors = request.countOnly ? 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 2f743a985e..3101d045a6 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 @@ -46,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; @@ -143,7 +144,7 @@ public String post(@Context GraphManager manager, } long size = results.size(); - if (size > request.limit) { + if (request.limit != Query.NO_LIMIT && size > request.limit) { size = request.limit; } List neighbors = request.countOnly ? diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java index 78870f4577..0031d739bd 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java @@ -117,7 +117,6 @@ public final class ApiVersion { * [0.65] Issue-1506: Support olap property key * [0.66] Issue-1567: Support get schema RESTful API * [0.67] Issue-1065: Support dynamically add/remove graph - * [0.68] Issue-1741: Support adamic-adar & resource-allocation API */ // The second parameter of Version.of() is for IDE running without JAR diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java index 07e65c57cc..fa34ccf4b1 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java @@ -60,6 +60,7 @@ public class ConditionQuery extends IdQuery { public static final String INDEX_SYM_EMPTY = "\u0002"; public static final char INDEX_SYM_MAX = '\u0003'; + // Note: here we use "new String" to distinguish normal string code public static final String INDEX_VALUE_NULL = new String(""); public static final String INDEX_VALUE_EMPTY = new String(""); @@ -654,13 +655,12 @@ public static String concatValues(List values) { public static String concatValues(Object value) { if (value instanceof String) { - return escapeSpecialValueIfNeeded((String) value); + return value == INDEX_VALUE_NULL ? INDEX_SYM_NULL : + escapeSpecialValueIfNeeded((String) value); } if (value instanceof List) { return concatValues((List) value); } else if (needConvertNumber(value)) { return LongEncoding.encodeNumber(value); - } else if (value == INDEX_VALUE_NULL) { - return INDEX_SYM_NULL; } else { return escapeSpecialValueIfNeeded(value.toString()); } @@ -675,7 +675,7 @@ private static String escapeSpecialValueIfNeeded(String value) { if (value.isEmpty()) { // Escape empty String to INDEX_SYM_EMPTY (char `\u0002`) value = INDEX_SYM_EMPTY; - } else if (value.equals(INDEX_VALUE_EMPTY)) { + } else if (value == INDEX_VALUE_EMPTY) { value = ""; } else { char ch = value.charAt(0); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java index 4d4c87e7fd..df15ce6dab 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java @@ -42,6 +42,7 @@ public class Query implements Cloneable { + // TODO: we should better not use Long.Max as the unify limit number public static final long NO_LIMIT = Long.MAX_VALUE; public static final long COMMIT_BATCH = 500L; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java index 9285bf5a52..bcb154fe97 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java @@ -283,9 +283,9 @@ public HugeEdge constructEdge(String label, HugeVertex vertex, // Check target vertex E.checkArgumentNotNull(vertex, "Target vertex can't be null"); - // TODO: Why we check it? the param is HugeVertex already? - E.checkArgument(vertex instanceof HugeVertex, - "Target vertex must be an instance of HugeVertex"); + // If we change the vertex type from HugeVertex to Vertex, uncomment it + //E.checkArgument(vertex instanceof HugeVertex, + // "Target vertex must be an instance of HugeVertex"); // Check label E.checkArgument(label != null && !label.isEmpty(), @@ -540,7 +540,6 @@ protected boolean ensureFilledProperties(boolean throwIfNotExist) { @Override public Iterator> properties(String... keys) { // TODO: Compatible with TinkerPop properties() (HugeGraph-742) - this.ensureFilledProperties(true); // Capacity should be about the following size diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java index 890d281600..311db733e1 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java @@ -699,9 +699,13 @@ private static V validPropertyValue(V value, PropertyKey pkey) { public static void retriveSysprop(List hasContainers, Function func) { - hasContainers.removeIf(container -> { - return container.getKey().startsWith("~") && func.apply(container); - }); + for (Iterator iter = hasContainers.iterator(); + iter.hasNext();) { + HasContainer container = iter.next(); + if (container.getKey().startsWith("~") && func.apply(container)) { + iter.remove(); + } + } } public static String page(GraphTraversal traversal) { From 5b894d41b726781aa111ae1a531d401fdfdff6b8 Mon Sep 17 00:00:00 2001 From: imbajin Date: Tue, 22 Feb 2022 17:57:46 +0800 Subject: [PATCH 7/8] keep NO_LIMIT now --- .../com/baidu/hugegraph/backend/query/ConditionQuery.java | 5 +++-- .../main/java/com/baidu/hugegraph/backend/query/Query.java | 3 ++- .../main/java/com/baidu/hugegraph/structure/HugeVertex.java | 4 ---- .../traversal/algorithm/records/KneighborRecords.java | 6 ++++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java index fa34ccf4b1..a2fda90dad 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java @@ -655,8 +655,7 @@ public static String concatValues(List values) { public static String concatValues(Object value) { if (value instanceof String) { - return value == INDEX_VALUE_NULL ? INDEX_SYM_NULL : - escapeSpecialValueIfNeeded((String) value); + return escapeSpecialValueIfNeeded((String) value); } if (value instanceof List) { return concatValues((List) value); } else if (needConvertNumber(value)) { @@ -677,6 +676,8 @@ private static String escapeSpecialValueIfNeeded(String value) { value = INDEX_SYM_EMPTY; } else if (value == INDEX_VALUE_EMPTY) { value = ""; + } else if (value == INDEX_VALUE_NULL) { + value = INDEX_SYM_NULL; } else { char ch = value.charAt(0); if (ch <= INDEX_SYM_MAX) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java index df15ce6dab..103a27f0b7 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java @@ -283,7 +283,8 @@ public long limit() { } public void limit(long limit) { - E.checkArgument(limit >= 0L, "Invalid limit %s", limit); + E.checkArgument(limit >= 0L || limit == NO_LIMIT, + "Invalid limit %s", limit); this.limit = limit; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java index bcb154fe97..f3570c3ebc 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java @@ -283,10 +283,6 @@ public HugeEdge constructEdge(String label, HugeVertex vertex, // Check target vertex E.checkArgumentNotNull(vertex, "Target vertex can't be null"); - // If we change the vertex type from HugeVertex to Vertex, uncomment it - //E.checkArgument(vertex instanceof HugeVertex, - // "Target vertex must be an instance of HugeVertex"); - // Check label E.checkArgument(label != null && !label.isEmpty(), "Edge label can't be null or empty"); 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 e84128ee98..71725caa6a 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 @@ -19,6 +19,8 @@ package com.baidu.hugegraph.traversal.algorithm.records; +import static com.baidu.hugegraph.backend.query.Query.NO_LIMIT; + import java.util.List; import java.util.Stack; @@ -49,7 +51,7 @@ public List ids(long limit) { // Not include record(i=0) to ignore source vertex for (int i = 1; i < records.size(); i++) { IntIterator iterator = records.get(i).keys(); - while ((limit > 0L) && iterator.hasNext()) { + while ((limit == NO_LIMIT || limit > 0L) && iterator.hasNext()) { ids.add(this.id(iterator.next())); limit--; } @@ -63,7 +65,7 @@ public PathSet paths(long limit) { Stack records = this.records(); for (int i = 1; i < records.size(); i++) { IntIterator iterator = records.get(i).keys(); - while ((limit > 0L) && iterator.hasNext()) { + while ((limit == NO_LIMIT || limit > 0L) && iterator.hasNext()) { paths.add(this.linkPath(i, iterator.next())); limit--; } From dda926c188983180039dd8f25fd225a1c4bb3f28 Mon Sep 17 00:00:00 2001 From: imbajin Date: Tue, 22 Feb 2022 21:34:52 +0800 Subject: [PATCH 8/8] Update EdgeStep.java --- .../com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java index 88ecfc549e..2c27eece39 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java @@ -72,7 +72,7 @@ public EdgeStep(HugeGraph g, Directions direction, List labels, public EdgeStep(HugeGraph g, Directions direction, List labels, Map properties, long degree, long skipDegree) { - E.checkNotNull(g, "The graph can't be null"); + E.checkArgumentNotNull(g, "The graph can't be null"); E.checkArgument(degree == NO_LIMIT || degree > 0L, "The max degree must be > 0 or == -1, but got: %s", degree);