You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by ok...@apache.org on 2016/02/06 20:54:44 UTC

[13/30] incubator-tinkerpop git commit: found a bug in TraversalUtil.isLocalStarGraph(). Added TraversalUtil.isLocalVertex() (for only checking properties -- no edge access). Added JavaDoc to new GraphComputer methods. Added verfication that the provided

found a bug in TraversalUtil.isLocalStarGraph(). Added TraversalUtil.isLocalVertex() (for only checking properties -- no edge access). Added JavaDoc to new GraphComputer methods. Added verfication that the provided traversals don't leave their respective boundaries.


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

Branch: refs/heads/master
Commit: 6cfb1f22f43fa82be10d04fc28e86e8f3db9d28e
Parents: 7023987
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Tue Feb 2 14:24:38 2016 -0700
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Tue Feb 2 14:24:38 2016 -0700

----------------------------------------------------------------------
 .../gremlin/process/computer/GraphComputer.java | 23 +++++++++++-
 .../gremlin/process/computer/GraphFilter.java   |  5 +++
 .../process/traversal/util/TraversalHelper.java | 39 ++++++++++++++------
 .../process/computer/GraphFilterTest.java       | 27 ++++++++++++--
 4 files changed, 77 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6cfb1f22/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/GraphComputer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/GraphComputer.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/GraphComputer.java
index 1494c28..7a0f593 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/GraphComputer.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/GraphComputer.java
@@ -107,9 +107,28 @@ public interface GraphComputer {
      */
     public GraphComputer workers(final int workers);
 
-    public GraphComputer vertices(final Traversal<Vertex, Vertex> vertexFilter);
+    /**
+     * Add a filter that will limit which vertices are loaded from the graph source.
+     * The provided {@link Traversal} can only check the vertex, its vertex properties, and the vertex property properties.
+     * The loaded graph will only have those vertices that pass through the provided filter.
+     *
+     * @param vertexFilter the traversal to verify whether or not to load the current vertex
+     * @return the updated GraphComputer with newly set vertex filter
+     * @throws IllegalArgumentException if the provided traversal attempts to access vertex edges
+     */
+    public GraphComputer vertices(final Traversal<Vertex, Vertex> vertexFilter) throws IllegalArgumentException;
 
-    public GraphComputer edges(final Traversal<Vertex, Edge> edgeFilter);
+    /**
+     * Add a filter that will limit which edges of the vertices are loaded from the graph source.
+     * The provided {@link Traversal} can only check the local star graph of the vertex and thus,
+     * can not access properties/labels of the adjacent vertices.
+     * The vertices of the loaded graph will only have those edges that pass through the provided filter.
+     *
+     * @param edgeFilter the traversal that determines which edges are loaded for each vertex
+     * @return the updated GraphComputer with newly set edge filter
+     * @throws IllegalArgumentException if the provided traversal goes attempts to access adjacent vertices
+     */
+    public GraphComputer edges(final Traversal<Vertex, Edge> edgeFilter) throws IllegalArgumentException;
 
     /**
      * Set an arbitrary configuration key/value for the underlying {@link org.apache.commons.configuration.Configuration} in the {@link GraphComputer}.

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6cfb1f22/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/GraphFilter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/GraphFilter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/GraphFilter.java
index f38ce90..f6c0726 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/GraphFilter.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/GraphFilter.java
@@ -21,6 +21,7 @@ package org.apache.tinkerpop.gremlin.process.computer;
 
 import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep;
+import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
 import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalUtil;
 import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Edge;
@@ -51,10 +52,14 @@ public final class GraphFilter implements Cloneable, Serializable {
     protected boolean allowAllRemainingEdges = false;
 
     public void setVertexFilter(final Traversal<Vertex, Vertex> vertexFilter) {
+        if (!TraversalHelper.isLocalVertex(vertexFilter.asAdmin()))
+            throw new IllegalArgumentException("The provided vertex filter must not leave the local vertex: " + edgeFilter);
         this.vertexFilter = vertexFilter.asAdmin().clone();
     }
 
     public void setEdgeFilter(final Traversal<Vertex, Edge> edgeFilter) {
+        if (!TraversalHelper.isLocalStarGraph(edgeFilter.asAdmin()))
+            throw new IllegalArgumentException("The provided edge filter must not leave the local star graph: " + edgeFilter);
         this.edgeFilter = edgeFilter.asAdmin().clone();
         if (this.edgeFilter.getStartStep() instanceof VertexStep) {
             this.allowedEdgeLabels.addAll(Arrays.asList(((VertexStep) this.edgeFilter.getStartStep()).getEdgeLabels()));

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6cfb1f22/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 b5c9fbc..cd7b740 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
@@ -20,8 +20,6 @@ package org.apache.tinkerpop.gremlin.process.traversal.util;
 
 import org.apache.tinkerpop.gremlin.process.traversal.Step;
 import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
-import org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal;
-import org.apache.tinkerpop.gremlin.process.traversal.lambda.TokenTraversal;
 import org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder;
 import org.apache.tinkerpop.gremlin.process.traversal.step.Scoping;
 import org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent;
@@ -33,7 +31,6 @@ import org.apache.tinkerpop.gremlin.process.traversal.step.filter.WhereTraversal
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.MatchStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep;
-import org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertyMapStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.StartStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet;
@@ -60,6 +57,28 @@ public final class TraversalHelper {
     private TraversalHelper() {
     }
 
+    public static boolean isLocalVertex(final Traversal.Admin<?, ?> traversal) {
+        for (final Step step : traversal.getSteps()) {
+            if (step instanceof RepeatStep &&
+                    ((RepeatStep<?>) step).getGlobalChildren().stream()
+                            .flatMap(t -> t.getSteps().stream())
+                            .filter(s -> s instanceof VertexStep)
+                            .findAny()
+                            .isPresent())  // TODO: is this sufficient?
+                return false;
+            else if (step instanceof VertexStep) {
+                return false;
+            } else if (step instanceof EdgeVertexStep) {
+                return false;
+            } else if (step instanceof TraversalParent) {
+                if (((TraversalParent) step).getLocalChildren().stream()
+                        .filter(t -> !isLocalVertex(t.asAdmin()))
+                        .findAny().isPresent()) return false;
+            }
+        }
+        return true;
+    }
+
     public static boolean isLocalStarGraph(final Traversal.Admin<?, ?> traversal) {
         return isLocalStarGraph(traversal, 'v');
     }
@@ -69,21 +88,17 @@ public final class TraversalHelper {
             if (step instanceof RepeatStep &&
                     ((RepeatStep<?>) step).getGlobalChildren().stream()
                             .flatMap(t -> t.getSteps().stream())
-                            .filter(temp -> temp instanceof VertexStep)
+                            .filter(s -> s instanceof VertexStep)
                             .findAny()
                             .isPresent())  // TODO: is this sufficient?
                 return false;
-            if (step instanceof PropertiesStep && state == 'u')
+            else if (step instanceof PropertiesStep && state == 'u')
                 return false;
             else if (step instanceof VertexStep) {
-                if (((VertexStep) step).returnsVertex()) {
-                    if (state == 'u') return false;
-                    if (state == 'v') state = 'u';
-                } else {
-                    state = 'e';
-                }
+                if (state == 'u') return false;
+                state = ((VertexStep) step).returnsVertex() ? 'u' : 'e';
             } else if (step instanceof EdgeVertexStep) {
-                if (state == 'e') state = 'u';
+                state = 'u';
             } else if (step instanceof HasContainerHolder && state == 'u') {
                 if (((HasContainerHolder) step).getHasContainers().stream()
                         .filter(c -> !c.getKey().equals(T.id.getAccessor())) // TODO: are labels available?

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6cfb1f22/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/computer/GraphFilterTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/computer/GraphFilterTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/computer/GraphFilterTest.java
index 11091a2..2fc3433 100644
--- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/computer/GraphFilterTest.java
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/computer/GraphFilterTest.java
@@ -27,6 +27,7 @@ import org.junit.Test;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
@@ -35,18 +36,20 @@ public class GraphFilterTest {
 
     @Test
     public void shouldHandlePreFilterCorrectly() {
-        final GraphFilter graphFilter = new GraphFilter();
+        GraphFilter graphFilter = new GraphFilter();
         graphFilter.setEdgeFilter(__.<Vertex>bothE().limit(0));
         assertTrue(graphFilter.allowedEdgeLabels.isEmpty());
         assertEquals(Direction.BOTH, graphFilter.allowedEdgeDirection);
         assertFalse(graphFilter.allowAllRemainingEdges);
         //
+        graphFilter = new GraphFilter();
         graphFilter.setEdgeFilter(__.<Vertex>bothE("knows").limit(0));
         assertEquals(1, graphFilter.allowedEdgeLabels.size());
         assertTrue(graphFilter.allowedEdgeLabels.contains("knows"));
         assertEquals(Direction.BOTH, graphFilter.allowedEdgeDirection);
         assertFalse(graphFilter.allowAllRemainingEdges);
         //
+        graphFilter = new GraphFilter();
         graphFilter.setEdgeFilter(__.<Vertex>outE("knows", "created"));
         assertEquals(2, graphFilter.allowedEdgeLabels.size());
         assertTrue(graphFilter.allowedEdgeLabels.contains("knows"));
@@ -54,6 +57,7 @@ public class GraphFilterTest {
         assertEquals(Direction.OUT, graphFilter.allowedEdgeDirection);
         assertTrue(graphFilter.allowAllRemainingEdges);
         //
+        graphFilter = new GraphFilter();
         graphFilter.setEdgeFilter(__.<Vertex>inE("knows", "created", "likes"));
         assertEquals(3, graphFilter.allowedEdgeLabels.size());
         assertTrue(graphFilter.allowedEdgeLabels.contains("knows"));
@@ -62,13 +66,30 @@ public class GraphFilterTest {
         assertEquals(Direction.IN, graphFilter.allowedEdgeDirection);
         assertTrue(graphFilter.allowAllRemainingEdges);
         //
-        graphFilter.setEdgeFilter(__.<Vertex>inE("knows", "created", "likes").has("weight", 1));
+        graphFilter = new GraphFilter();
+        graphFilter.setEdgeFilter(__.<Vertex>inE("knows", "created", "likes"));
         assertEquals(3, graphFilter.allowedEdgeLabels.size());
         assertTrue(graphFilter.allowedEdgeLabels.contains("knows"));
         assertTrue(graphFilter.allowedEdgeLabels.contains("created"));
         assertTrue(graphFilter.allowedEdgeLabels.contains("likes"));
         assertEquals(Direction.IN, graphFilter.allowedEdgeDirection);
-        assertFalse(graphFilter.allowAllRemainingEdges);
+        assertTrue(graphFilter.allowAllRemainingEdges);
+        //
+        graphFilter = new GraphFilter();
+        try {
+            graphFilter.setEdgeFilter(__.<Vertex>inE("likes").inV().outE().has("weight", 1));    // cannot leave local star graph
+            fail();
+        } catch (final IllegalArgumentException e) {
+            assertTrue(e.getMessage().contains("local star graph"));
+        }
+        //
+        graphFilter = new GraphFilter();
+        try {
+            graphFilter.setVertexFilter(__.out("likes"));    // cannot leave local vertex
+            fail();
+        } catch (final IllegalArgumentException e) {
+            assertTrue(e.getMessage().contains("local vertex"));
+        }
     }
 
     /*@Test