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 2015/09/09 18:32:51 UTC

[07/33] incubator-tinkerpop git commit: Hide partition key in traversal output where possible.

Hide partition key in traversal output where possible.

This isn't perfect.  A lambda or direct access to Element will still yield the partition key.  Not sure there's much we can do about that using the TraversalStrategy model.


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

Branch: refs/heads/hadoop_split
Commit: 2ce45763609924e0d25cdff8e672bd6b304a90da
Parents: 321f5cc
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Mon Aug 31 15:25:06 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Mon Aug 31 15:25:06 2015 -0400

----------------------------------------------------------------------
 .../strategy/decoration/PartitionStrategy.java  |  27 ++++-
 .../PartitionStrategyProcessTest.java           | 104 +++++++++++++++++++
 2 files changed, 128 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2ce45763/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/PartitionStrategy.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/PartitionStrategy.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/PartitionStrategy.java
index 108a2c6..f52715c 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/PartitionStrategy.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/PartitionStrategy.java
@@ -48,6 +48,7 @@ import org.apache.tinkerpop.gremlin.structure.Property;
 import org.apache.tinkerpop.gremlin.structure.PropertyType;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
+import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
 
 import java.io.Serializable;
 import java.util.ArrayList;
@@ -127,6 +128,10 @@ public final class PartitionStrategy extends AbstractTraversalStrategy<Traversal
         if (includeMetaProperties) {
             final List<PropertiesStep> propertiesSteps = TraversalHelper.getStepsOfAssignableClass(PropertiesStep.class, traversal);
             propertiesSteps.forEach(step -> {
+                // check length first because keyExists will return true otherwise
+                if (step.getPropertyKeys().length > 0 && ElementHelper.keyExists(partitionKey, step.getPropertyKeys()))
+                    throw new IllegalStateException("Cannot explicitly request the partitionKey in the traversal");
+
                 if (step.getReturnType() == PropertyType.PROPERTY) {
                     // check the following step to see if it is a has(partitionKey, *) - if so then this strategy was
                     // already applied down below via g.V().values() which injects a properties() step
@@ -137,7 +142,7 @@ public final class PartitionStrategy extends AbstractTraversalStrategy<Traversal
                         final Traversal choose = __.choose(
                                 __.filter(new TypeChecker<>(VertexProperty.class)),
                                 __.has(partitionKey, P.within(new ArrayList<>(readPartitions))),
-                                __.__());
+                                __.__()).filter(new PartitionKeyHider());
                         TraversalHelper.insertTraversal(step, choose.asAdmin(), traversal);
                     }
                 } else if (step.getReturnType() == PropertyType.VALUE) {
@@ -145,8 +150,8 @@ public final class PartitionStrategy extends AbstractTraversalStrategy<Traversal
                     // if not, pass it through otherwise explode g.V().values() to g.V().properties().has().value()
                     final Traversal choose = __.choose(
                             __.filter(new TypeChecker<>(Vertex.class)),
-                            __.properties(step.getPropertyKeys()).has(partitionKey, P.within(new ArrayList<>(readPartitions))).value(),
-                            __.__());
+                            __.properties(step.getPropertyKeys()).has(partitionKey, P.within(new ArrayList<>(readPartitions))).filter(new PartitionKeyHider()).value(),
+                            __.__().filter(new PartitionKeyHider()));
                     TraversalHelper.insertTraversal(step, choose.asAdmin(), traversal);
                     traversal.removeStep(step);
                 } else {
@@ -157,6 +162,10 @@ public final class PartitionStrategy extends AbstractTraversalStrategy<Traversal
 
             final List<PropertyMapStep> propertyMapSteps = TraversalHelper.getStepsOfAssignableClass(PropertyMapStep.class, traversal);
             propertyMapSteps.forEach(step -> {
+                // check length first because keyExists will return true otherwise
+                if (step.getPropertyKeys().length > 0 && ElementHelper.keyExists(partitionKey, step.getPropertyKeys()))
+                    throw new IllegalStateException("Cannot explicitly request the partitionKey in the traversal");
+
                 if (step.getReturnType() == PropertyType.PROPERTY) {
                     // via map() filter out properties that aren't in the partition if it is a PropertyVertex,
                     // otherwise just let them pass through
@@ -232,6 +241,18 @@ public final class PartitionStrategy extends AbstractTraversalStrategy<Traversal
         }
     }
 
+    public final class PartitionKeyHider<A extends Property> implements Predicate<Traverser<A>>, Serializable {
+        @Override
+        public boolean test(final Traverser<A> traverser) {
+            return !traverser.get().key().equals(partitionKey);
+        }
+
+        @Override
+        public String toString() {
+            return "remove(" + partitionKey + ")";
+        }
+    }
+
     /**
      * Takes the result of a {@link Map} containing {@link Property} lists and if the property is a
      * {@link VertexProperty} it applies a filter based on the current partitioning.  If is not a

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2ce45763/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/PartitionStrategyProcessTest.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/PartitionStrategyProcessTest.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/PartitionStrategyProcessTest.java
index 137790f..ced5094 100644
--- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/PartitionStrategyProcessTest.java
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/PartitionStrategyProcessTest.java
@@ -26,9 +26,12 @@ import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSo
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.apache.tinkerpop.gremlin.structure.Graph;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.apache.tinkerpop.gremlin.structure.VertexProperty;
 import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
 import org.junit.Test;
 
+import java.util.List;
+
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.*;
 
@@ -78,6 +81,7 @@ public class PartitionStrategyProcessTest extends AbstractGremlinProcessTest {
         assertEquals("thing", v.property("any").value());
         assertEquals("A", v.property(partition).value());
         assertThat(v.property("any").properties().hasNext(), is(false));
+
     }
 
     @Test
@@ -145,6 +149,106 @@ public class PartitionStrategyProcessTest extends AbstractGremlinProcessTest {
         assertThat(gOverB.V(v).valueMap().hasNext(), is(false));
     }
 
+    @Test(expected = IllegalStateException.class)
+    @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
+    public void shouldHidePartitionKeyForValues() {
+        final GraphTraversalSource gOverA = create(PartitionStrategy.build()
+                .includeMetaProperties(true)
+                .partitionKey(partition).writePartition("A").addReadPartition("A").create());
+        final Vertex v = gOverA.addV().property("any", "thing").next();
+
+        gOverA.V(v).values(partition).next();
+    }
+
+    @Test
+    @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
+    public void shouldHidePartitionKeyForValuesWithEmptyKeys() {
+        final GraphTraversalSource gOverA = create(PartitionStrategy.build()
+                .includeMetaProperties(true)
+                .partitionKey(partition).writePartition("A").addReadPartition("A").create());
+        final Vertex v = gOverA.addV().property("any", "thing").next();
+
+        assertEquals(1l, (long) gOverA.V(v).values().count().next());
+        assertEquals("thing", gOverA.V(v).values().next());
+    }
+
+    @Test(expected = IllegalStateException.class)
+    @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
+    public void shouldHidePartitionKeyForProperties() {
+        final GraphTraversalSource gOverA = create(PartitionStrategy.build()
+                .includeMetaProperties(true)
+                .partitionKey(partition).writePartition("A").addReadPartition("A").create());
+        final Vertex v = gOverA.addV().property("any", "thing").next();
+
+        gOverA.V(v).properties(partition).next();
+    }
+
+    @Test
+    @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
+    public void shouldHidePartitionKeyForPropertiesWithEmptyKeys() {
+        final GraphTraversalSource gOverA = create(PartitionStrategy.build()
+                .includeMetaProperties(true)
+                .partitionKey(partition).writePartition("A").addReadPartition("A").create());
+        final Vertex v = gOverA.addV().property("any", "thing").next();
+
+        assertEquals(1l, (long) gOverA.V(v).properties().count().next());
+        assertEquals("thing", gOverA.V(v).properties().value().next());
+    }
+
+    @Test(expected = IllegalStateException.class)
+    @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
+    public void shouldHidePartitionKeyForPropertyMap() {
+        final GraphTraversalSource gOverA = create(PartitionStrategy.build()
+                .includeMetaProperties(true)
+                .partitionKey(partition).writePartition("A").addReadPartition("A").create());
+        final Vertex v = gOverA.addV().property("any", "thing").next();
+
+        gOverA.V(v).propertyMap(partition).next();
+    }
+
+    @Test
+    @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
+    public void shouldHidePartitionKeyForPropertyMapWithEmptyKeys() {
+        final GraphTraversalSource gOverA = create(PartitionStrategy.build()
+                .includeMetaProperties(true)
+                .partitionKey(partition).writePartition("A").addReadPartition("A").create());
+        final Vertex v = gOverA.addV().property("any", "thing").next();
+
+        assertEquals(1l, (long) gOverA.V(v).propertyMap().count().next());
+        assertEquals("thing", ((List<VertexProperty>) gOverA.V(v).propertyMap().next().get("any")).get(0).value());
+    }
+
+    @Test(expected = IllegalStateException.class)
+    @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
+    public void shouldHidePartitionKeyForValueMap() {
+        final GraphTraversalSource gOverA = create(PartitionStrategy.build()
+                .includeMetaProperties(true)
+                .partitionKey(partition).writePartition("A").addReadPartition("A").create());
+        final Vertex v = gOverA.addV().property("any", "thing").next();
+
+        gOverA.V(v).valueMap(partition).next();
+    }
+
+    @Test
+    @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
+    public void shouldHidePartitionKeyForValueMapWithEmptyKeys() {
+        final GraphTraversalSource gOverA = create(PartitionStrategy.build()
+                .includeMetaProperties(true)
+                .partitionKey(partition).writePartition("A").addReadPartition("A").create());
+        final Vertex v = gOverA.addV().property("any", "thing").next();
+
+        assertEquals(1l, (long) gOverA.V(v).valueMap().count().next());
+        assertEquals("thing", ((List) gOverA.V(v).valueMap().next().get("any")).get(0));
+    }
+
     @Test
     @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE)
     public void shouldAppendPartitionToEdge() {