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:50 UTC

[06/33] incubator-tinkerpop git commit: Worked in propertyMap and valueMap steps to PartitionStrategy.

Worked in propertyMap and valueMap steps to PartitionStrategy.


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

Branch: refs/heads/hadoop_split
Commit: 321f5cc4f0676d3cae87310526d6ca2ff3e8b14a
Parents: 92eeafc
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Mon Aug 31 14:04:49 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Mon Aug 31 14:04:49 2015 -0400

----------------------------------------------------------------------
 .../traversal/step/map/PropertyMapStep.java     |  8 ++
 .../strategy/decoration/PartitionStrategy.java  | 94 +++++++++++++++++++-
 .../PartitionStrategyProcessTest.java           | 14 +++
 3 files changed, 112 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/321f5cc4/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/PropertyMapStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/PropertyMapStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/PropertyMapStep.java
index fab4990..8e4e110 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/PropertyMapStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/PropertyMapStep.java
@@ -76,6 +76,14 @@ public class PropertyMapStep<E> extends MapStep<Element, Map<String, E>> {
         return this.returnType;
     }
 
+    public String[] getPropertyKeys() {
+        return propertyKeys;
+    }
+
+    public boolean isIncludeTokens() {
+        return includeTokens;
+    }
+
     public String toString() {
         return StringFactory.stepString(this, Arrays.asList(this.propertyKeys), this.returnType.name().toLowerCase());
     }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/321f5cc4/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 6c5139b..108a2c6 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
@@ -32,7 +32,9 @@ import org.apache.tinkerpop.gremlin.process.traversal.step.map.AddVertexStartSte
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.AddVertexStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep;
+import org.apache.tinkerpop.gremlin.process.traversal.step.map.LambdaMapStep;
 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.AddPropertyStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.GraphStep;
@@ -42,6 +44,7 @@ import org.apache.tinkerpop.gremlin.process.traversal.strategy.AbstractTraversal
 import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
 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.Vertex;
 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
@@ -49,10 +52,13 @@ import org.apache.tinkerpop.gremlin.structure.VertexProperty;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.function.Function;
 import java.util.function.Predicate;
 import java.util.stream.Collectors;
 
@@ -144,8 +150,30 @@ public final class PartitionStrategy extends AbstractTraversalStrategy<Traversal
                     TraversalHelper.insertTraversal(step, choose.asAdmin(), traversal);
                     traversal.removeStep(step);
                 } else {
-                    throw new IllegalStateException(String.format("%s is not accounting for a particular PropertyType %s",
-                            PartitionStrategy.class.getSimpleName(), step.getReturnType()));
+                    throw new IllegalStateException(String.format("%s is not accounting for a particular %s %s",
+                            PartitionStrategy.class.getSimpleName(), PropertyType.class.toString(), step.getReturnType()));
+                }
+            });
+
+            final List<PropertyMapStep> propertyMapSteps = TraversalHelper.getStepsOfAssignableClass(PropertyMapStep.class, traversal);
+            propertyMapSteps.forEach(step -> {
+                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
+                    TraversalHelper.insertAfterStep(new LambdaMapStep<>(traversal, new MapPropertiesFilter()), step, traversal);
+                } else if (step.getReturnType() == PropertyType.VALUE) {
+                    // as this is a value map, replace that step with propertiesMap() that returns PropertyType.VALUE.
+                    // from there, add the filter as shown above and then unwrap the properties as they would have
+                    // been done under valueMap()
+                    final PropertyMapStep propertyMapStep = new PropertyMapStep(traversal, step.isIncludeTokens(), PropertyType.PROPERTY, step.getPropertyKeys());
+                    TraversalHelper.replaceStep(step, propertyMapStep, traversal);
+
+                    final LambdaMapStep mapPropertiesFilterStep = new LambdaMapStep<>(traversal, new MapPropertiesFilter());
+                    TraversalHelper.insertAfterStep(mapPropertiesFilterStep, propertyMapStep, traversal);
+                    TraversalHelper.insertAfterStep(new LambdaMapStep<>(traversal, new MapPropertiesConverter()), mapPropertiesFilterStep, traversal);
+                } else {
+                    throw new IllegalStateException(String.format("%s is not accounting for a particular %s %s",
+                            PartitionStrategy.class.getSimpleName(), PropertyType.class.toString(), step.getReturnType()));
                 }
             });
         }
@@ -182,6 +210,10 @@ public final class PartitionStrategy extends AbstractTraversalStrategy<Traversal
         });
     }
 
+    /**
+     * A concrete lambda implementation that checks if the type passing through on the {@link Traverser} is
+     * of a specific {@link Element} type.
+     */
     public final class TypeChecker<A> implements Predicate<Traverser<A>>, Serializable {
         final Class<? extends Element> toCheck;
 
@@ -200,14 +232,68 @@ public final class PartitionStrategy extends AbstractTraversalStrategy<Traversal
         }
     }
 
+    /**
+     * 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
+     * {@link VertexProperty} the property is simply passed through.
+     */
+    public final class MapPropertiesFilter implements Function<Traverser<Map<String,List<Property>>>, Map<String,List<Property>>>, Serializable {
+        @Override
+        public Map<String, List<Property>> apply(final Traverser<Map<String, List<Property>>> mapTraverser) {
+            final Map<String,List<Property>> values = mapTraverser.get();
+            final Map<String,List<Property>> filtered = new HashMap<>();
+
+            values.entrySet().forEach(p -> {
+                final List l = p.getValue().stream().filter(property -> {
+                    if (property instanceof VertexProperty) {
+                        final Iterator<String> itty = ((VertexProperty) property).values(partitionKey);
+                        return itty.hasNext() && readPartitions.contains(itty.next());
+                    } else {
+                        return true;
+                    }
+                }).collect(Collectors.toList());
+                if (l.size() > 0) filtered.put(p.getKey(), l);
+            });
+
+            return filtered;
+        }
+
+        @Override
+        public String toString() {
+            return "applyPartitionFilter";
+        }
+    }
+
+    /**
+     * Takes a {@link Map} of a {@link List} of {@link Property} objects and unwraps the {@link Property#value()}.
+     */
+    public final class MapPropertiesConverter implements Function<Traverser<Map<String,List<Property>>>, Map<String,List<Property>>>, Serializable {
+        @Override
+        public Map<String, List<Property>> apply(final Traverser<Map<String, List<Property>>> mapTraverser) {
+            final Map<String,List<Property>> values = mapTraverser.get();
+            final Map<String,List<Property>> converted = new HashMap<>();
+
+            values.entrySet().forEach(p -> {
+                final List l = p.getValue().stream().map(property -> property.value()).collect(Collectors.toList());
+                converted.put(p.getKey(), l);
+            });
+
+            return converted;
+        }
+
+        @Override
+        public String toString() {
+            return "extractValuesInPropertiesMap";
+        }
+    }
+
     public final static class Builder {
         private String writePartition;
         private String partitionKey;
         private Set<String> readPartitions = new HashSet<>();
         private boolean includeMetaProperties = false;
 
-        Builder() {
-        }
+        Builder() { }
 
         /**
          * Set to {@code true} if the {@link VertexProperty} instances should get assigned to partitions.  This

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/321f5cc4/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 520d9d9..137790f 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
@@ -129,6 +129,20 @@ public class PartitionStrategyProcessTest extends AbstractGremlinProcessTest {
         assertThat(gOverA.V(v).values("any").hasNext(), is(true));
         assertThat(gOverB.V(v).values("any").hasNext(), is(false));
         assertThat(gOverB.V(v).values("that").hasNext(), is(false));
+
+        assertThat(gOverAB.V(v).propertyMap().next().containsKey("any"), is(true));
+        assertThat(gOverAB.V(v).propertyMap().next().containsKey("that"), is(true));
+        assertThat(gOverA.V(v).propertyMap().next().containsKey("that"), is(false));
+        assertThat(gOverA.V(v).propertyMap().next().containsKey("any"), is(true));
+        assertThat(gOverB.V(v).propertyMap().hasNext(), is(false));
+        assertThat(gOverB.V(v).propertyMap().hasNext(), is(false));
+
+        assertThat(gOverAB.V(v).valueMap().next().containsKey("any"), is(true));
+        assertThat(gOverAB.V(v).valueMap().next().containsKey("that"), is(true));
+        assertThat(gOverA.V(v).valueMap().next().containsKey("that"), is(false));
+        assertThat(gOverA.V(v).valueMap().next().containsKey("any"), is(true));
+        assertThat(gOverB.V(v).valueMap().hasNext(), is(false));
+        assertThat(gOverB.V(v).valueMap().hasNext(), is(false));
     }
 
     @Test