You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by dk...@apache.org on 2015/05/06 18:28:48 UTC

[1/2] incubator-tinkerpop git commit: added new TraversalHelper method: hasStepOfAssignableClassRecursively

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/master 16b1a8e37 -> e1642e44f


added new TraversalHelper method: hasStepOfAssignableClassRecursively


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/168c0b4a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/168c0b4a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/168c0b4a

Branch: refs/heads/master
Commit: 168c0b4af767557684cd731b4998512716dd3826
Parents: 16b1a8e
Author: Daniel Kuppitz <da...@hotmail.com>
Authored: Wed May 6 18:27:11 2015 +0200
Committer: Daniel Kuppitz <da...@hotmail.com>
Committed: Wed May 6 18:27:11 2015 +0200

----------------------------------------------------------------------
 .../process/traversal/util/TraversalHelper.java | 24 ++++++++++++++------
 1 file changed, 17 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/168c0b4a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/TraversalHelper.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/TraversalHelper.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/TraversalHelper.java
index be70805..ad2b6a5 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/TraversalHelper.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/TraversalHelper.java
@@ -28,13 +28,7 @@ import org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet;
 import org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyStep;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
@@ -216,6 +210,22 @@ public final class TraversalHelper {
         return false;
     }
 
+    public static boolean hasStepOfAssignableClassRecursively(final Class stepClass, final Traversal.Admin<?, ?> traversal) {
+        for (final Step<?, ?> step : traversal.getSteps()) {
+            if (stepClass.isAssignableFrom(step.getClass())) {
+                return true;
+            }
+            if (step instanceof TraversalParent) {
+                for (final Traversal.Admin<?, ?> globalChild : ((TraversalParent) step).getGlobalChildren()) {
+                    if (hasStepOfAssignableClassRecursively(stepClass, globalChild)) {
+                        return true;
+                    }
+                }
+            }
+        }
+        return false;
+    }
+
     public static <S> void addToCollection(final Collection<S> collection, final S s, final long bulk) {
         if (collection instanceof BulkSet) {
             ((BulkSet<S>) collection).add(s, bulk);


[2/2] incubator-tinkerpop git commit: consider path- and lambda-steps in all child- and parent-traversals when applying IncidentToAdjacentStrategy

Posted by dk...@apache.org.
consider path- and lambda-steps in all child- and parent-traversals when applying IncidentToAdjacentStrategy


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/e1642e44
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/e1642e44
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/e1642e44

Branch: refs/heads/master
Commit: e1642e44fc7076d8a70eddc5a89359e76fca38c6
Parents: 168c0b4
Author: Daniel Kuppitz <da...@hotmail.com>
Authored: Wed May 6 18:28:27 2015 +0200
Committer: Daniel Kuppitz <da...@hotmail.com>
Committed: Wed May 6 18:28:27 2015 +0200

----------------------------------------------------------------------
 .../IncidentToAdjacentStrategy.java             | 22 +++++++++++++-------
 .../IncidentToAdjacentStrategyTest.java         |  3 ++-
 .../tinkergraph/structure/TinkerGraphTest.java  |  7 +------
 3 files changed, 17 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e1642e44/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IncidentToAdjacentStrategy.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IncidentToAdjacentStrategy.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IncidentToAdjacentStrategy.java
index 53c3001..185a277 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IncidentToAdjacentStrategy.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IncidentToAdjacentStrategy.java
@@ -31,10 +31,13 @@ import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
 import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
 import org.javatuples.Pair;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
 
 /**
  * This strategy looks for <code>.outE().inV()</code>, <code>.inE().outV()</code> and <code>.bothE().otherV()</code>
@@ -53,27 +56,30 @@ public final class IncidentToAdjacentStrategy extends AbstractTraversalStrategy<
         implements TraversalStrategy.OptimizationStrategy {
 
     private static final IncidentToAdjacentStrategy INSTANCE = new IncidentToAdjacentStrategy();
+    private static final Set<Class> InvalidatingStepClasses = new HashSet<Class>() {{
+        add(PathStep.class);
+        add(LambdaHolder.class);
+    }};
 
     private IncidentToAdjacentStrategy() {
     }
 
     @Override
     public void apply(Traversal.Admin<?, ?> traversal) {
-        final int size = traversal.getSteps().size() - 1;
-        boolean isOptimizable = true;
+        final Traversal.Admin root = TraversalHelper.getRootTraversal(traversal);
+        if (IteratorUtils.anyMatch(InvalidatingStepClasses.iterator(), stepClass ->
+                TraversalHelper.hasStepOfAssignableClassRecursively(stepClass, root))) return;
         final Collection<Pair<VertexStep, Step>> stepsToReplace = new ArrayList<>();
         Step prev = null;
-        for (int i = 0; i <= size && isOptimizable; i++) {
-            final Step curr = traversal.getSteps().get(i);
+        for (final Step curr : traversal.getSteps()) {
             if (isOptimizable(prev, curr)) {
                 stepsToReplace.add(Pair.with((VertexStep) prev, curr));
             }
-            isOptimizable = !(curr instanceof PathStep) && !(curr instanceof LambdaHolder);
             prev = curr;
         }
-        if (isOptimizable && !stepsToReplace.isEmpty()) {
-            for (final Pair<VertexStep, Step> steps : stepsToReplace) {
-                optimizeSteps(traversal, steps.getValue0(), steps.getValue1());
+        if (!stepsToReplace.isEmpty()) {
+            for (final Pair<VertexStep, Step> pair : stepsToReplace) {
+                optimizeSteps(traversal, pair.getValue0(), pair.getValue1());
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e1642e44/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IncidentToAdjacentStrategyTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IncidentToAdjacentStrategyTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IncidentToAdjacentStrategyTest.java
index 2816b3e..37b5ae4 100644
--- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IncidentToAdjacentStrategyTest.java
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IncidentToAdjacentStrategyTest.java
@@ -124,7 +124,8 @@ public class IncidentToAdjacentStrategyTest {
                     {__.bothE().outV(), __.bothE().outV()},
                     {__.outE().as("a").inV(), __.outE().as("a").inV()}, // todo: this can be optimized, but requires a lot more checks
                     {__.outE().inV().path(), __.outE().inV().path()},
-                    {__.outE().inV().map(e -> e), __.outE().inV().map(e -> e)}});
+                    {__.outE().inV().map(e -> e), __.outE().inV().map(e -> e)},
+                    {__.union(__.outE().inV(), __.inE().outV()).path(), __.union(__.outE().inV(), __.inE().outV()).path()}});
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e1642e44/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphTest.java
----------------------------------------------------------------------
diff --git a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphTest.java b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphTest.java
index b6fc864..d6dbddc 100644
--- a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphTest.java
+++ b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphTest.java
@@ -142,12 +142,7 @@ public class TinkerGraphTest {
     public void testPlayDK() throws Exception {
         final Graph graph = TinkerFactory.createModern();
         final GraphTraversalSource g = graph.traversal();
-        Traversal traversal = g.V().outE().inV().has(__.out("created")).has(__.outE("knows").inV().has("name", "josh"));
-        System.out.println(traversal.toString());
-        traversal.forEachRemaining(System.out::println);
-        System.out.println(traversal.toString());
-        System.out.println("--");
-        traversal = g.V().outE().inV().has(__.out("created")).has(__.outE("knows").inV());
+        Traversal traversal = g.V().union(__.outE().inV(), __.inE().outV()).union(__.identity(), __.path());
         System.out.println(traversal.toString());
         traversal.forEachRemaining(System.out::println);
         System.out.println(traversal.toString());