You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hugegraph.apache.org by je...@apache.org on 2022/06/13 16:06:10 UTC

[incubator-hugegraph] branch count-is-0-apply-graph-missing created (now 4910d9d02)

This is an automated email from the ASF dual-hosted git repository.

jermy pushed a change to branch count-is-0-apply-graph-missing
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git


      at 4910d9d02 fix: Traversal.graph is empty in StepStrategy.apply() with `count().is(0)`

This branch includes the following new commits:

     new 4910d9d02 fix: Traversal.graph is empty in StepStrategy.apply() with `count().is(0)`

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[incubator-hugegraph] 01/01: fix: Traversal.graph is empty in StepStrategy.apply() with `count().is(0)`

Posted by je...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jermy pushed a commit to branch count-is-0-apply-graph-missing
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git

commit 4910d9d022bb9996453fb29224ace9cfdc54b806
Author: Zhangmei Li <li...@baidu.com>
AuthorDate: Tue Jun 14 00:03:32 2022 +0800

    fix: Traversal.graph is empty in StepStrategy.apply() with `count().is(0)`
    
    Change-Id: I0ca670a1df852504dcc8e8d0cfd8c0de39987e60
---
 .../baidu/hugegraph/auth/HugeGraphAuthProxy.java   | 25 ++++++++++---------
 .../traversal/optimize/HugeVertexStepStrategy.java | 23 +++++++++++++-----
 .../traversal/optimize/TraversalUtil.java          | 28 +++++++++++++++-------
 3 files changed, 50 insertions(+), 26 deletions(-)

diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java
index d3efb5aa3..8d7515c2b 100644
--- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java
+++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java
@@ -38,11 +38,6 @@ import java.util.function.Supplier;
 
 import javax.security.sasl.AuthenticationException;
 
-import com.baidu.hugegraph.iterator.MapperIterator;
-import com.baidu.hugegraph.traversal.optimize.HugeScriptTraversal;
-import jakarta.ws.rs.ForbiddenException;
-import jakarta.ws.rs.NotAuthorizedException;
-
 import org.apache.commons.configuration2.Configuration;
 import org.apache.tinkerpop.gremlin.process.computer.GraphComputer;
 import org.apache.tinkerpop.gremlin.process.traversal.Bytecode;
@@ -81,6 +76,7 @@ import com.baidu.hugegraph.config.HugeConfig;
 import com.baidu.hugegraph.config.TypedOption;
 import com.baidu.hugegraph.exception.NotSupportException;
 import com.baidu.hugegraph.iterator.FilterIterator;
+import com.baidu.hugegraph.iterator.MapperIterator;
 import com.baidu.hugegraph.rpc.RpcServiceConfig4Client;
 import com.baidu.hugegraph.rpc.RpcServiceConfig4Server;
 import com.baidu.hugegraph.schema.EdgeLabel;
@@ -98,6 +94,7 @@ import com.baidu.hugegraph.task.HugeTask;
 import com.baidu.hugegraph.task.TaskManager;
 import com.baidu.hugegraph.task.TaskScheduler;
 import com.baidu.hugegraph.task.TaskStatus;
+import com.baidu.hugegraph.traversal.optimize.HugeScriptTraversal;
 import com.baidu.hugegraph.type.HugeType;
 import com.baidu.hugegraph.type.Nameable;
 import com.baidu.hugegraph.type.define.GraphMode;
@@ -107,6 +104,9 @@ import com.baidu.hugegraph.util.E;
 import com.baidu.hugegraph.util.Log;
 import com.baidu.hugegraph.util.RateLimiter;
 
+import jakarta.ws.rs.ForbiddenException;
+import jakarta.ws.rs.NotAuthorizedException;
+
 public final class HugeGraphAuthProxy implements HugeGraph {
 
     static {
@@ -1638,7 +1638,7 @@ public final class HugeGraphAuthProxy implements HugeGraph {
             return new MapperIterator<TraversalStrategy<?>,
                                       TraversalStrategy<?>>(
                        this.strategies.iterator(), (strategy) -> {
-                           return new TraversalStrategyProxy(strategy);
+                           return new TraversalStrategyProxy<>(strategy);
                        });
         }
 
@@ -1678,8 +1678,11 @@ public final class HugeGraphAuthProxy implements HugeGraph {
         }
     }
 
-    private final class TraversalStrategyProxy<T extends TraversalStrategy>
+    private final class TraversalStrategyProxy<T extends TraversalStrategy<?>>
                   implements TraversalStrategy<T> {
+
+        private static final long serialVersionUID = 2071829024642435735L;
+
         private final TraversalStrategy<T> origin;
 
         public TraversalStrategyProxy(TraversalStrategy<T> origin) {
@@ -1687,7 +1690,7 @@ public final class HugeGraphAuthProxy implements HugeGraph {
         }
 
         @Override
-        public void apply(Traversal.Admin traversal) {
+        public void apply(Traversal.Admin<?, ?> traversal) {
             String script;
             if (traversal instanceof HugeScriptTraversal) {
                 script = ((HugeScriptTraversal<?, ?>) traversal).script();
@@ -1740,9 +1743,9 @@ public final class HugeGraphAuthProxy implements HugeGraph {
         }
 
         @Override
-        public int compareTo(Class<? extends TraversalStrategy>
-                                             otherTraversalCategory) {
-            return this.origin.compareTo(otherTraversalCategory);
+        public int compareTo(@SuppressWarnings("rawtypes")
+                             Class<? extends TraversalStrategy> otherCategory) {
+            return this.origin.compareTo(otherCategory);
         }
 
         @Override
diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/HugeVertexStepStrategy.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/HugeVertexStepStrategy.java
index 6931bb0a3..9321db703 100644
--- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/HugeVertexStepStrategy.java
+++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/HugeVertexStepStrategy.java
@@ -31,6 +31,8 @@ import org.apache.tinkerpop.gremlin.process.traversal.strategy.AbstractTraversal
 import org.apache.tinkerpop.gremlin.process.traversal.util.EmptyTraversal;
 import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
 
+import com.baidu.hugegraph.HugeGraph;
+
 public final class HugeVertexStepStrategy
              extends AbstractTraversalStrategy<ProviderOptimizationStrategy>
              implements ProviderOptimizationStrategy {
@@ -59,16 +61,25 @@ public final class HugeVertexStepStrategy
         if (!steps.isEmpty()) {
             boolean withPath = HugeVertexStepStrategy.containsPath(traversal);
             boolean withTree = HugeVertexStepStrategy.containsTree(traversal);
-            boolean supportIn = TraversalUtil.getGraph(steps.get(0))
-                                             .backendStoreFeatures()
-                                             .supportsQueryWithInCondition();
+            /*
+             * The graph of traversal may be null when `__` step is followed
+             * by `count().is(0)` step, like the following gremlin:
+             * `g.V(id).repeat(in()).until(or(inE().count().is(0), loops().is(2)))`
+             * TODO: remove this `graph!=null` check after fixed the bug #1699
+             */
+            boolean supportIn = false;
+            HugeGraph graph = TraversalUtil.tryGetGraph(steps.get(0));
+            if (graph != null) {
+                supportIn = graph.backendStoreFeatures()
+                                 .supportsQueryWithInCondition();
+            }
             batchOptimize = !withTree && !withPath && supportIn;
         }
 
         for (VertexStep originStep : steps) {
             HugeVertexStep<?> newStep = batchOptimize ?
-                              new HugeVertexStepByBatch<>(originStep) :
-                              new HugeVertexStep<>(originStep);
+                                        new HugeVertexStepByBatch<>(originStep) :
+                                        new HugeVertexStep<>(originStep);
             TraversalHelper.replaceStep(originStep, newStep, traversal);
 
             TraversalUtil.extractHasContainer(newStep, traversal);
@@ -107,7 +118,7 @@ public final class HugeVertexStepStrategy
      */
     protected static boolean containsTree(Traversal.Admin<?, ?> traversal) {
         boolean hasTree = TraversalHelper.getStepsOfClass(
-                TreeStep.class, traversal).size() > 0;
+                          TreeStep.class, traversal).size() > 0;
         if (hasTree) {
             return true;
         } else if (traversal instanceof EmptyTraversal) {
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 15e762906..ae2c333e8 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
@@ -60,11 +60,12 @@ import org.apache.tinkerpop.gremlin.process.traversal.util.OrP;
 import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.apache.tinkerpop.gremlin.structure.Element;
+import org.apache.tinkerpop.gremlin.structure.Graph;
 import org.apache.tinkerpop.gremlin.structure.Property;
 import org.apache.tinkerpop.gremlin.structure.PropertyType;
 import org.apache.tinkerpop.gremlin.structure.T;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
-import org.slf4j.Logger;
+import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;
 
 import com.baidu.hugegraph.HugeException;
 import com.baidu.hugegraph.HugeGraph;
@@ -91,17 +92,27 @@ import com.baidu.hugegraph.util.CollectionUtil;
 import com.baidu.hugegraph.util.DateUtil;
 import com.baidu.hugegraph.util.E;
 import com.baidu.hugegraph.util.JsonUtil;
-import com.baidu.hugegraph.util.Log;
 import com.google.common.collect.ImmutableList;
 
 public final class TraversalUtil {
 
-    private static final Logger LOG = Log.logger(HugeGraph.class);
-
     public static final String P_CALL = "P.";
 
     public static HugeGraph getGraph(Step<?, ?> step) {
-        return (HugeGraph) step.getTraversal().getGraph().get();
+        HugeGraph graph = tryGetGraph(step);
+        if (graph != null) {
+            return graph;
+        }
+        throw new IllegalArgumentException("There is no graph in step: " + step);
+    }
+
+    public static HugeGraph tryGetGraph(Step<?, ?> step) {
+        Graph graph = step.getTraversal().getGraph().get();
+        if (graph instanceof HugeGraph) {
+            return (HugeGraph) graph;
+        }
+        assert graph == null || graph instanceof EmptyGraph;
+        return null;
     }
 
     public static void extractHasContainer(HugeGraphStep<?, ?> newStep,
@@ -572,10 +583,9 @@ public final class TraversalUtil {
                       TraversalHelper.getStepsOfAssignableClassRecursively(
                       HasStep.class, traversal);
         /*
-         * The graph may be null.
-         * For example:
-         *   g.V().hasLabel('person').union(__.<Vertex>has("birth", dates[0]))
-         * Here "__.has" will create a new traversal, but the graph is null
+         * The graph in traversal may be null, for example:
+         *   `g.V().hasLabel('person').union(__.has('name', 'tom'))`
+         * Here `__.has()` will create a new traversal, but the graph is null
          */
         if (steps.isEmpty() || !traversal.getGraph().isPresent()) {
             return;