You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@hugegraph.apache.org by "javeme (via GitHub)" <gi...@apache.org> on 2024/02/03 11:33:55 UTC

Re: [PR] feat(algorithm): support single source shortest path algorithm [incubator-hugegraph-computer]

javeme commented on code in PR #285:
URL: https://github.com/apache/incubator-hugegraph-computer/pull/285#discussion_r1477049484


##########
computer-api/src/main/java/org/apache/hugegraph/computer/core/graph/id/IdFactory.java:
##########
@@ -80,6 +80,23 @@ public static Id createId(IdType type) {
         }
     }
 
+    public static Id parseId(IdType type, Object value) {
+        try {
+            switch (type) {
+                case LONG:
+                    return (Id) BYTES_ID_LONG_METHOD.invoke(null, value);
+                case UTF8:
+                    return (Id) BYTES_ID_STRING_METHOD.invoke(null, value);
+                case UUID:
+                    return (Id) BYTES_ID_UUID_METHOD.invoke(null, value);
+                default:
+                    throw new ComputerException("Can't parse Id for %s", type.name());

Review Comment:
   prefer "Unexpect id type %s"



##########
computer-test/src/main/java/org/apache/hugegraph/computer/core/util/IdUtilTest.java:
##########
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.computer.core.util;
+
+import org.apache.hugegraph.computer.core.graph.id.IdCategory;
+import org.apache.hugegraph.computer.core.graph.id.IdType;
+import org.apache.hugegraph.testutil.Assert;
+import org.junit.Test;
+
+public class IdUtilTest {
+
+    @Test
+    public void testParseId() {
+        String uuid = "3b676b77-c484-4ba6-b627-8c040bc42863";
+        Assert.assertEquals(IdType.LONG, IdUtil.parseId("222").idType());
+        Assert.assertEquals(IdType.UTF8, IdUtil.parseId("aaa222").idType());
+        Assert.assertEquals(IdType.UUID, IdUtil.parseId(uuid).idType());
+
+        Assert.assertEquals(IdType.LONG, IdUtil.parseId(IdCategory.NUMBER, "222").idType());
+        Assert.assertEquals(IdType.UTF8, IdUtil.parseId(IdCategory.STRING, "aaa222").idType());

Review Comment:
   can also test IdUtil.parseId(IdCategory.STRING, "222").idType());



##########
computer-api/src/main/java/org/apache/hugegraph/computer/core/graph/id/IdCategory.java:
##########
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.computer.core.graph.id;
+
+import org.apache.commons.lang3.StringUtils;
+
+public enum IdCategory {

Review Comment:
   not sure why not reuse IdType



##########
computer-api/src/main/java/org/apache/hugegraph/computer/core/graph/id/IdFactory.java:
##########
@@ -80,6 +80,23 @@ public static Id createId(IdType type) {
         }
     }
 
+    public static Id parseId(IdType type, Object value) {
+        try {
+            switch (type) {
+                case LONG:
+                    return (Id) BYTES_ID_LONG_METHOD.invoke(null, value);
+                case UTF8:
+                    return (Id) BYTES_ID_STRING_METHOD.invoke(null, value);
+                case UUID:
+                    return (Id) BYTES_ID_UUID_METHOD.invoke(null, value);
+                default:
+                    throw new ComputerException("Can't parse Id for %s", type.name());
+            }
+        } catch (Exception e) {
+            throw new ComputerException("Failed to parse Id", e);

Review Comment:
   prefer ("Failed to parse %s Id: '%s'", e, type , value)



##########
computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java:
##########
@@ -103,23 +107,30 @@ public String name() {
 
     @Override
     public void init(Config config) {
-        this.sourceId = config.getString(OPTION_SOURCE_ID, "");
-        if (StringUtils.isBlank(this.sourceId)) {
+        this.vertexIdTypeStr = config.getString(OPTION_VERTEX_ID_TYPE, "");
+        this.vertexIdType = IdCategory.parse(this.vertexIdTypeStr);
+
+        this.sourceIdStr = config.getString(OPTION_SOURCE_ID, "");
+        if (StringUtils.isBlank(this.sourceIdStr)) {
             throw new ComputerException("The param '%s' must not be blank", OPTION_SOURCE_ID);
         }
+        this.sourceId = IdUtil.parseId(this.vertexIdType, this.sourceIdStr);
 
-        this.targetId = config.getString(OPTION_TARGET_ID, "");
-        if (StringUtils.isBlank(this.targetId)) {
+        this.targetIdStr = config.getString(OPTION_TARGET_ID, "");
+        if (StringUtils.isBlank(this.targetIdStr)) {
             throw new ComputerException("The param '%s' must not be blank", OPTION_TARGET_ID);
         }
-
         // remove spaces
-        this.targetId = Arrays.stream(this.targetId.split(","))
-                              .map(e -> e.trim())
-                              .collect(Collectors.joining(","));
-        // cache targetId
-        this.targetIdSet = new HashSet<>(Arrays.asList(this.targetId.split(",")));
+        this.targetIdStr = Arrays.stream(this.targetIdStr.split(","))
+                                 .map(e -> e.trim())
+                                 .collect(Collectors.joining(","));
         this.targetQuantityType = this.getQuantityType();
+        if (this.targetQuantityType != QuantityType.ALL) {
+            this.targetIdSet = new IdSet();
+            for (String targetIdStr : this.targetIdStr.split(",")) {
+                targetIdSet.add(IdUtil.parseId(this.vertexIdType, targetIdStr));

Review Comment:
   seems the targets id should be different types? like [111, "abc", "222", 333, U"uuid..."]



##########
computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java:
##########
@@ -43,31 +42,36 @@ public class SingleSourceShortestPath implements Computation<SingleSourceShortes
 
     private static final Logger LOG = Log.logger(SingleSourceShortestPath.class);
 
+    public static final String OPTION_VERTEX_ID_TYPE = "single_source_shortest_path.vertex_id_type";
     public static final String OPTION_SOURCE_ID = "single_source_shortest_path.source_id";
     public static final String OPTION_TARGET_ID = "single_source_shortest_path.target_id";
     public static final String OPTION_WEIGHT_PROPERTY =
             "single_source_shortest_path.weight_property";
     public static final String OPTION_DEFAULT_WEIGHT =
             "single_source_shortest_path.default_weight";
 
+    /**
+     * id type of vertex.
+     * string|number|uuid
+     */
+    // todo improve: automatic inference
+    private String vertexIdTypeStr;

Review Comment:
   don't need to store as a member var?



##########
computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java:
##########
@@ -43,31 +42,36 @@ public class SingleSourceShortestPath implements Computation<SingleSourceShortes
 
     private static final Logger LOG = Log.logger(SingleSourceShortestPath.class);
 
+    public static final String OPTION_VERTEX_ID_TYPE = "single_source_shortest_path.vertex_id_type";
     public static final String OPTION_SOURCE_ID = "single_source_shortest_path.source_id";
     public static final String OPTION_TARGET_ID = "single_source_shortest_path.target_id";
     public static final String OPTION_WEIGHT_PROPERTY =
             "single_source_shortest_path.weight_property";
     public static final String OPTION_DEFAULT_WEIGHT =
             "single_source_shortest_path.default_weight";
 
+    /**
+     * id type of vertex.
+     * string|number|uuid
+     */
+    // todo improve: automatic inference
+    private String vertexIdTypeStr;
+    private IdCategory vertexIdType;
+
     /**
      * source vertex id
      */
-    private String sourceId;
+    private String sourceIdStr;

Review Comment:
   don't need to store as a member var?



##########
computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java:
##########
@@ -43,31 +42,36 @@ public class SingleSourceShortestPath implements Computation<SingleSourceShortes
 
     private static final Logger LOG = Log.logger(SingleSourceShortestPath.class);
 
+    public static final String OPTION_VERTEX_ID_TYPE = "single_source_shortest_path.vertex_id_type";
     public static final String OPTION_SOURCE_ID = "single_source_shortest_path.source_id";
     public static final String OPTION_TARGET_ID = "single_source_shortest_path.target_id";
     public static final String OPTION_WEIGHT_PROPERTY =
             "single_source_shortest_path.weight_property";
     public static final String OPTION_DEFAULT_WEIGHT =
             "single_source_shortest_path.default_weight";
 
+    /**
+     * id type of vertex.
+     * string|number|uuid
+     */
+    // todo improve: automatic inference
+    private String vertexIdTypeStr;
+    private IdCategory vertexIdType;
+
     /**
      * source vertex id
      */
-    private String sourceId;
+    private String sourceIdStr;
+    private Id sourceId;
 
     /**
      * target vertex id.
      * 1. single target: one vertex id
      * 2. multiple target: multiple vertex ids separated by comma
      * 3. all: *
      */
-    private String targetId;
-
-    /**
-     * cache of targetId
-     */
-    private Set<String> targetIdSet;
-
+    private String targetIdStr;

Review Comment:
   don't need to store as a member var?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org