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 2023/09/08 17:08:47 UTC

[GitHub] [incubator-hugegraph] javeme commented on a diff in pull request #2295: feat(api&core): support label & property filtering for both edge and vertex & support kout dfs mode

javeme commented on code in PR #2295:
URL: https://github.com/apache/incubator-hugegraph/pull/2295#discussion_r1320132868


##########
hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/KoutTraverser.java:
##########
@@ -138,6 +138,38 @@ public KoutRecords customizedKout(Id source, EdgeStep step,
         return records;
     }
 
+    public KoutRecords DFSKout(Id source, Steps steps,
+                               int maxDepth, boolean nearest,
+                               long capacity, long limit) {
+        E.checkNotNull(source, "source vertex id");
+        this.checkVertexExist(source, "source vertex");
+        checkPositive(maxDepth, "k-out max_depth");

Review Comment:
   It's OK, I missed your reply



##########
hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/HugeTraverser.java:
##########
@@ -164,6 +171,17 @@ public static void checkSkipDegree(long skipDegree, long degree,
         }
     }
 
+    public static void checkTraverseMode(String traverseMode) {
+        E.checkArgument(traverseMode.compareToIgnoreCase(TRAVERSE_MODE_BFS) == 0 ||
+                        traverseMode.compareToIgnoreCase(TRAVERSE_MODE_DFS) == 0,
+                        "The traverse mode must be one of '%s' or '%s', but got '%s'",
+                        TRAVERSE_MODE_BFS, TRAVERSE_MODE_DFS, traverseMode);
+    }
+
+    public static boolean isDFSAlgorithm(String algorithm) {

Review Comment:
   isTraverseModeDFS



##########
hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/steps/Steps.java:
##########
@@ -0,0 +1,186 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.traversal.algorithm.steps;
+
+import static org.apache.hugegraph.traversal.algorithm.HugeTraverser.NO_LIMIT;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.hugegraph.HugeGraph;
+import org.apache.hugegraph.backend.id.Id;
+import org.apache.hugegraph.schema.EdgeLabel;
+import org.apache.hugegraph.schema.VertexLabel;
+import org.apache.hugegraph.traversal.algorithm.HugeTraverser;
+import org.apache.hugegraph.traversal.optimize.TraversalUtil;
+import org.apache.hugegraph.type.define.Directions;
+import org.apache.hugegraph.util.E;
+
+public class Steps {
+
+    protected final Map<Id, StepEntity> edgeSteps;
+    protected final Map<Id, StepEntity> vertexSteps;
+    protected final Directions direction;
+    protected final long degree;
+    protected final long skipDegree;
+
+    public Steps(HugeGraph graph, Directions direction,
+                 Map<String, Map<String, Object>> vSteps,
+                 Map<String, Map<String, Object>> eSteps,
+                 long degree, long skipDegree) {
+        E.checkArgument(degree == NO_LIMIT || degree > 0L,
+                        "The max degree must be > 0 or == -1, but got: %s", degree);
+        HugeTraverser.checkSkipDegree(skipDegree, degree, NO_LIMIT);
+
+        this.direction = direction;
+
+        // parse vertex steps
+        this.vertexSteps = new HashMap<>();
+        if (vSteps != null && !vSteps.isEmpty()) {
+            initVertexFilter(graph, vSteps);
+        }
+
+        // parse edge steps
+        this.edgeSteps = new HashMap<>();
+        if (eSteps != null && !eSteps.isEmpty()) {
+            initEdgeFilter(graph, eSteps);
+        }
+
+        this.degree = degree;
+        this.skipDegree = skipDegree;
+    }
+
+    private void initVertexFilter(HugeGraph graph, Map<String, Map<String, Object>> vSteps) {
+        for (Map.Entry<String, Map<String, Object>> entry : vSteps.entrySet()) {
+            if (checkEntryEmpty(entry)) {
+                continue;
+            }
+            E.checkArgument(entry.getKey() != null && !entry.getKey().isEmpty(),
+                            "The vertex step label could not be null");
+
+            VertexLabel vertexLabel = graph.vertexLabel(entry.getKey());
+            StepEntity stepEntity = handleStepEntity(graph, entry, vertexLabel.id());
+            this.vertexSteps.put(vertexLabel.id(), stepEntity);
+        }
+    }
+
+    private void initEdgeFilter(HugeGraph graph, Map<String, Map<String, Object>> eSteps) {
+        for (Map.Entry<String, Map<String, Object>> entry : eSteps.entrySet()) {
+            if (checkEntryEmpty(entry)) {
+                continue;
+            }
+            E.checkArgument(entry.getKey() != null && !entry.getKey().isEmpty(),
+                            "The edge step label could not be null");
+
+            EdgeLabel edgeLabel = graph.edgeLabel(entry.getKey());
+            StepEntity stepEntity = handleStepEntity(graph, entry, edgeLabel.id());
+            this.edgeSteps.put(edgeLabel.id(), stepEntity);
+        }
+    }
+
+    private StepEntity handleStepEntity(HugeGraph graph,
+                                        Map.Entry<String, Map<String, Object>> entry,
+                                        Id id) {
+        Map<Id, Object> properties = null;
+        if (entry.getValue() != null) {
+            properties = TraversalUtil.transProperties(graph, entry.getValue());
+        }
+        return new StepEntity(id, entry.getKey(), properties);
+    }
+
+    private boolean checkEntryEmpty(Map.Entry<String, Map<String, Object>> entry) {
+        return (entry.getKey() == null || entry.getKey().isEmpty()) &&
+               (entry.getValue() == null || entry.getValue().isEmpty());
+    }
+
+    public long degree() {
+        return degree;

Review Comment:
   add `this.` prefix for members access



##########
hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/steps/Steps.java:
##########
@@ -0,0 +1,186 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.traversal.algorithm.steps;
+
+import static org.apache.hugegraph.traversal.algorithm.HugeTraverser.NO_LIMIT;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.hugegraph.HugeGraph;
+import org.apache.hugegraph.backend.id.Id;
+import org.apache.hugegraph.schema.EdgeLabel;
+import org.apache.hugegraph.schema.VertexLabel;
+import org.apache.hugegraph.traversal.algorithm.HugeTraverser;
+import org.apache.hugegraph.traversal.optimize.TraversalUtil;
+import org.apache.hugegraph.type.define.Directions;
+import org.apache.hugegraph.util.E;
+
+public class Steps {
+
+    protected final Map<Id, StepEntity> edgeSteps;
+    protected final Map<Id, StepEntity> vertexSteps;
+    protected final Directions direction;
+    protected final long degree;
+    protected final long skipDegree;
+
+    public Steps(HugeGraph graph, Directions direction,
+                 Map<String, Map<String, Object>> vSteps,
+                 Map<String, Map<String, Object>> eSteps,
+                 long degree, long skipDegree) {
+        E.checkArgument(degree == NO_LIMIT || degree > 0L,
+                        "The max degree must be > 0 or == -1, but got: %s", degree);
+        HugeTraverser.checkSkipDegree(skipDegree, degree, NO_LIMIT);
+
+        this.direction = direction;
+
+        // parse vertex steps
+        this.vertexSteps = new HashMap<>();
+        if (vSteps != null && !vSteps.isEmpty()) {
+            initVertexFilter(graph, vSteps);
+        }
+
+        // parse edge steps
+        this.edgeSteps = new HashMap<>();
+        if (eSteps != null && !eSteps.isEmpty()) {
+            initEdgeFilter(graph, eSteps);
+        }
+
+        this.degree = degree;
+        this.skipDegree = skipDegree;
+    }
+
+    private void initVertexFilter(HugeGraph graph, Map<String, Map<String, Object>> vSteps) {
+        for (Map.Entry<String, Map<String, Object>> entry : vSteps.entrySet()) {
+            if (checkEntryEmpty(entry)) {
+                continue;
+            }
+            E.checkArgument(entry.getKey() != null && !entry.getKey().isEmpty(),
+                            "The vertex step label could not be null");
+
+            VertexLabel vertexLabel = graph.vertexLabel(entry.getKey());
+            StepEntity stepEntity = handleStepEntity(graph, entry, vertexLabel.id());
+            this.vertexSteps.put(vertexLabel.id(), stepEntity);
+        }
+    }
+
+    private void initEdgeFilter(HugeGraph graph, Map<String, Map<String, Object>> eSteps) {
+        for (Map.Entry<String, Map<String, Object>> entry : eSteps.entrySet()) {
+            if (checkEntryEmpty(entry)) {
+                continue;
+            }
+            E.checkArgument(entry.getKey() != null && !entry.getKey().isEmpty(),
+                            "The edge step label could not be null");
+
+            EdgeLabel edgeLabel = graph.edgeLabel(entry.getKey());
+            StepEntity stepEntity = handleStepEntity(graph, entry, edgeLabel.id());
+            this.edgeSteps.put(edgeLabel.id(), stepEntity);
+        }
+    }
+
+    private StepEntity handleStepEntity(HugeGraph graph,
+                                        Map.Entry<String, Map<String, Object>> entry,
+                                        Id id) {
+        Map<Id, Object> properties = null;
+        if (entry.getValue() != null) {
+            properties = TraversalUtil.transProperties(graph, entry.getValue());
+        }
+        return new StepEntity(id, entry.getKey(), properties);
+    }
+
+    private boolean checkEntryEmpty(Map.Entry<String, Map<String, Object>> entry) {
+        return (entry.getKey() == null || entry.getKey().isEmpty()) &&
+               (entry.getValue() == null || entry.getValue().isEmpty());
+    }
+
+    public long degree() {
+        return degree;
+    }
+
+    public Map<Id, StepEntity> edgeSteps() {
+        return edgeSteps;
+    }
+
+    public Map<Id, StepEntity> vertexSteps() {
+        return vertexSteps;
+    }
+
+    public long skipDegree() {
+        return skipDegree;
+    }
+
+    public Directions direction() {
+        return direction;
+    }
+
+    public long limit() {
+        return this.skipDegree > 0L ? this.skipDegree : this.degree;
+    }
+
+    public List<Id> edgeLabels() {
+        return new ArrayList<>(this.edgeSteps.keySet());
+    }
+
+    public boolean isVertexEmpty() {
+        return this.vertexSteps.isEmpty();
+    }
+
+    @Override
+    public String toString() {
+        return "Steps{" +
+               "edgeSteps=" + edgeSteps +
+               ", vertexSteps=" + vertexSteps +
+               ", direction=" + direction +
+               ", degree=" + degree +
+               ", skipDegree=" + skipDegree +
+               '}';
+    }
+
+    public static class StepEntity {

Review Comment:
   expect a blank line here



##########
hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java:
##########
@@ -1696,12 +1719,9 @@ private <T extends HugeElement> Iterator<T> filterUnmatchedRecords(
                 return false;
             }
             // Process results that query from left index or primary-key
-            if (query.resultType().isVertex() == elem.type().isVertex() &&
-                !rightResultFromIndexQuery(query, elem)) {
-                // Only index query will come here
-                return false;
-            }
-            return true;
+            // Only index query will come here

Review Comment:
   `// Only index query will come here` only when returning false, prefer to revert it 



##########
hugegraph-core/src/main/java/org/apache/hugegraph/traversal/algorithm/HugeTraverser.java:
##########
@@ -85,6 +89,9 @@ public class HugeTraverser {
     // Empirical value of scan limit, with which results can be returned in 3s
     public static final String DEFAULT_PAGE_LIMIT = "100000";
     public static final long NO_LIMIT = -1L;
+    // algorithms

Review Comment:
   we can remove the comment or add more details



-- 
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