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/05/01 00:34:23 UTC

incubator-tinkerpop git commit: added support for Steps having multiple labels.

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/multi-label-steps [created] 95f370d7c


added support for Steps having multiple labels.


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

Branch: refs/heads/multi-label-steps
Commit: 95f370d7c2c0a5f1a750e2903acf169dbe311180
Parents: be4bb3e
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Thu Apr 30 16:34:12 2015 -0600
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Thu Apr 30 16:34:12 2015 -0600

----------------------------------------------------------------------
 .../tinkerpop/gremlin/process/traversal/Step.java  |  4 ++--
 .../traversal/dsl/graph/GraphTraversal.java        |  5 +----
 .../process/traversal/step/filter/WhereStep.java   | 14 +++++++-------
 .../traversal/step/map/match/MatchStep.java        |  6 +++---
 .../process/traversal/step/util/AbstractStep.java  | 17 +++++++++--------
 .../process/traversal/step/util/EmptyStep.java     |  6 +++---
 .../optimization/IdentityRemovalStrategy.java      |  2 +-
 .../verification/LabeledEndStepStrategy.java       |  2 +-
 .../traverser/B_O_P_S_SE_SL_Traverser.java         | 10 ++++------
 .../process/traversal/util/TraversalHelper.java    | 14 ++++++--------
 .../traversal/step/sideEffect/TinkerGraphStep.java |  3 +--
 .../optimization/TinkerGraphStepStrategy.java      |  6 +-----
 12 files changed, 39 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95f370d7/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Step.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Step.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Step.java
index 642a4ea..d447175 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Step.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Step.java
@@ -121,14 +121,14 @@ public interface Step<S, E> extends Iterator<Traverser<E>>, Serializable, Clonea
      *
      * @return the optional label of the step
      */
-    public Optional<String> getLabel();
+    public Set<String> getLabels();
 
     /**
      * Set the label of this step.
      *
      * @param label the label for this step
      */
-    public void setLabel(final String label);
+    public void addLabel(final String label);
 
     /**
      * Get the unique id of the step.

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95f370d7/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.java
index 11a036b..32f04d1 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.java
@@ -707,10 +707,7 @@ public interface GraphTraversal<S, E> extends Traversal<S, E> {
 
     public default GraphTraversal<S, E> as(final String stepLabel) {
         if (this.asAdmin().getSteps().size() == 0) this.asAdmin().addStep(new StartStep<>(this.asAdmin()));
-        final Step<?, E> endStep = this.asAdmin().getEndStep();
-        if (endStep.getLabel().isPresent())
-            throw new IllegalStateException("The previous step has already been labeled: " + endStep);
-        endStep.setLabel(stepLabel);
+        this.asAdmin().getEndStep().addLabel(stepLabel);
         return this;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95f370d7/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereStep.java
index 8414001..9fbf91f 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereStep.java
@@ -76,14 +76,14 @@ public final class WhereStep<E> extends FilterStep<Map<String, E>> implements Tr
                 endStep = endStep.getPreviousStep();
 
             final Map<String, E> map = traverser.get();
-            if (!map.containsKey(startStep.getLabel().get()))
-                throw new IllegalArgumentException("The provided key is not in the current map: " + startStep.getLabel().get());
-            final Object startObject = map.get(startStep.getLabel().get());
+            if (!map.containsKey(startStep.getLabels().iterator().next()))
+                throw new IllegalArgumentException("The provided key is not in the current map: " + startStep.getLabels().iterator().next());
+            final Object startObject = map.get(startStep.getLabels().iterator().next());
             final Object endObject;
-            if (endStep.getLabel().isPresent()) {
-                if (!map.containsKey(endStep.getLabel().get()))
-                    throw new IllegalArgumentException("The provided key is not in the current map: " + endStep.getLabel().get());
-                endObject = map.get(endStep.getLabel().get());
+            if (!endStep.getLabels().isEmpty()) {
+                if (!map.containsKey(endStep.getLabels().iterator().next()))
+                    throw new IllegalArgumentException("The provided key is not in the current map: " + endStep.getLabels().iterator().next());
+                endObject = map.get(endStep.getLabels().iterator().next());
             } else
                 endObject = null;
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95f370d7/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/match/MatchStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/match/MatchStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/match/MatchStep.java
index 70e2ac3..31112ea 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/match/MatchStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/match/MatchStep.java
@@ -181,9 +181,9 @@ public final class MatchStep<S, E> extends AbstractStep<S, Map<String, E>> imple
 
     private void addTraversalPrivate(final Traversal<S, S> traversal) {
 
-        String startAs = traversal.asAdmin().getStartStep().getLabel().orElseThrow(()
-                -> new IllegalArgumentException("All match traversals must have their start step labeled with as()"));
-        String endAs = traversal.asAdmin().getEndStep().getLabel().orElse(null);
+        String startAs = traversal.asAdmin().getStartStep().getLabels().iterator().next();
+        //        -> new IllegalArgumentException("All match traversals must have their start step labeled with as()"));
+        String endAs = traversal.asAdmin().getEndStep().getLabels().stream().findFirst().orElse(null);
         checkAs(startAs);
         if (null == endAs) {
             endAs = createAnonymousAs();

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95f370d7/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/AbstractStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/AbstractStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/AbstractStep.java
index 14c3b36..bb70e67 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/AbstractStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/AbstractStep.java
@@ -23,17 +23,19 @@ import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
 import org.apache.tinkerpop.gremlin.process.traversal.Traverser;
 import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
 
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 import java.util.Objects;
-import java.util.Optional;
+import java.util.Set;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
 public abstract class AbstractStep<S, E> implements Step<S, E> {
 
-    protected String label = null;
+    protected Set<String> labels = new HashSet<>();
     protected String id = Traverser.Admin.HALT;
     protected Traversal.Admin traversal;
     protected ExpandableStepIterator<S> starts;
@@ -60,14 +62,13 @@ public abstract class AbstractStep<S, E> implements Step<S, E> {
     }
 
     @Override
-    public void setLabel(final String label) {
-        Objects.nonNull(label);
-        this.label = label;
+    public void addLabel(final String label) {
+        this.labels.add(label);
     }
 
     @Override
-    public Optional<String> getLabel() {
-        return Optional.ofNullable(this.label);
+    public Set<String> getLabels() {
+        return Collections.unmodifiableSet(this.labels);
     }
 
     @Override
@@ -176,7 +177,7 @@ public abstract class AbstractStep<S, E> implements Step<S, E> {
 
     private final Traverser<E> prepareTraversalForNextStep(final Traverser<E> traverser) {
         if (!this.traverserStepIdSetByChild) ((Traverser.Admin<E>) traverser).setStepId(this.nextStep.getId());
-        if (null != this.label) traverser.path().addLabel(this.label);
+        this.labels.forEach(label -> traverser.path().addLabel(label));
         return traverser;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95f370d7/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/EmptyStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/EmptyStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/EmptyStep.java
index ee6a5d6..1a9c2d9 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/EmptyStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/EmptyStep.java
@@ -97,12 +97,12 @@ public final class EmptyStep<S, E> implements Step<S, E>, TraversalParent {
     }
 
     @Override
-    public Optional<String> getLabel() {
-        return Optional.empty();
+    public Set<String> getLabels() {
+        return Collections.emptySet();
     }
 
     @Override
-    public void setLabel(final String label) {
+    public void addLabel(final String label) {
 
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95f370d7/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IdentityRemovalStrategy.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IdentityRemovalStrategy.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IdentityRemovalStrategy.java
index 729e2b9..a66aeff 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IdentityRemovalStrategy.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/IdentityRemovalStrategy.java
@@ -39,7 +39,7 @@ public final class IdentityRemovalStrategy extends AbstractTraversalStrategy {
             return;
 
         TraversalHelper.getStepsOfClass(IdentityStep.class, traversal).stream()
-                .filter(step -> !step.getLabel().isPresent())
+                .filter(step -> step.getLabels().isEmpty())
                 .forEach(traversal::removeStep);
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95f370d7/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/verification/LabeledEndStepStrategy.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/verification/LabeledEndStepStrategy.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/verification/LabeledEndStepStrategy.java
index c9ecd6f..8ba86f9 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/verification/LabeledEndStepStrategy.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/verification/LabeledEndStepStrategy.java
@@ -35,7 +35,7 @@ public final class LabeledEndStepStrategy extends AbstractTraversalStrategy {
 
     @Override
     public void apply(final Traversal.Admin<?, ?> traversal) {
-        if (traversal.getEndStep().getLabel().isPresent())
+        if (!traversal.getEndStep().getLabels().isEmpty())
             traversal.addStep(new MarkerIdentityStep<>(traversal));
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95f370d7/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/traverser/B_O_P_S_SE_SL_Traverser.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/traverser/B_O_P_S_SE_SL_Traverser.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/traverser/B_O_P_S_SE_SL_Traverser.java
index c976c69..3daa672 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/traverser/B_O_P_S_SE_SL_Traverser.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/traverser/B_O_P_S_SE_SL_Traverser.java
@@ -40,10 +40,8 @@ public class B_O_P_S_SE_SL_Traverser<T> extends B_O_S_SE_SL_Traverser<T> {
 
     public B_O_P_S_SE_SL_Traverser(final T t, final Step<T, ?> step, final long initialBulk) {
         super(t, step, initialBulk);
-        final Optional<String> stepLabel = step.getLabel();
-        this.path = stepLabel.isPresent() ?
-                ImmutablePath.make().extend(t, stepLabel.get()) :
-                ImmutablePath.make().extend(t);
+        final String[] stepLabels = step.getLabels().toArray(new String[step.getLabels().size()]);
+        this.path = ImmutablePath.make().extend(t, stepLabels);
     }
 
     /////////////////
@@ -81,8 +79,8 @@ public class B_O_P_S_SE_SL_Traverser<T> extends B_O_S_SE_SL_Traverser<T> {
     public <R> Traverser.Admin<R> split(final R r, final Step<T, R> step) {
 
             final B_O_P_S_SE_SL_Traverser<R> clone = (B_O_P_S_SE_SL_Traverser<R>) super.split(r,step);
-            final Optional<String> stepLabel = step.getLabel();
-            clone.path = stepLabel.isPresent() ? clone.path.clone().extend(r, stepLabel.get()) : clone.path.clone().extend(r);
+            final String[] stepLabels = step.getLabels().toArray(new String[step.getLabels().size()]);
+            clone.path = clone.path.clone().extend(r, stepLabels);
             //clone.sack = null == clone.sack ? null : clone.sideEffects.getSackSplitOperator().orElse(UnaryOperator.identity()).apply(clone.sack);
             return clone;
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95f370d7/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 0ffe53f..be70805 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,12 +20,12 @@ 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.step.TraversalParent;
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep;
-import org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyStep;
-import org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent;
 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;
@@ -48,8 +48,7 @@ public final class TraversalHelper {
 
     public static <S, E> Step<S, E> getStepByLabel(final String label, final Traversal.Admin<?, ?> traversal) {
         return traversal.getSteps().stream()
-                .filter(step -> step.getLabel().isPresent())
-                .filter(step -> label.equals(step.getLabel().get()))
+                .filter(step -> step.getLabels().contains(label))
                 .findAny()
                 .orElseThrow(() -> new IllegalArgumentException("The provided step label does not exist: " + label));
     }
@@ -63,8 +62,7 @@ public final class TraversalHelper {
 
     public static boolean hasLabel(final String label, final Traversal.Admin<?, ?> traversal) {
         return traversal.getSteps().stream()
-                .filter(step -> step.getLabel().isPresent())
-                .filter(step -> label.equals(step.getLabel().get()))
+                .filter(step -> step.getLabels().contains(label))
                 .findAny().isPresent();
     }
 
@@ -72,7 +70,7 @@ public final class TraversalHelper {
         final List<String> labels = new ArrayList<>();
         for (final Step<?, ?> temp : traversal.getSteps()) {
             if (temp == step) break;
-            temp.getLabel().ifPresent(labels::add);
+            temp.getLabels().forEach(labels::add);
         }
         return labels;
     }
@@ -164,7 +162,7 @@ public final class TraversalHelper {
             builder.append(String.join(",", strings));
             builder.append(')');
         }
-        step.getLabel().ifPresent(label -> builder.append('@').append(label));
+        if (!step.getLabels().isEmpty()) builder.append('@').append(step.getLabels());
         //builder.append("^").append(step.getId());
         return builder.toString();
     }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95f370d7/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/step/sideEffect/TinkerGraphStep.java
----------------------------------------------------------------------
diff --git a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/step/sideEffect/TinkerGraphStep.java b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/step/sideEffect/TinkerGraphStep.java
index a3cd192..8ae4344 100644
--- a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/step/sideEffect/TinkerGraphStep.java
+++ b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/step/sideEffect/TinkerGraphStep.java
@@ -46,8 +46,7 @@ public class TinkerGraphStep<S extends Element> extends GraphStep<S> implements
 
     public TinkerGraphStep(final GraphStep<S> originalGraphStep) {
         super(originalGraphStep.getTraversal(), originalGraphStep.getReturnClass(), originalGraphStep.getIds());
-        if (originalGraphStep.getLabel().isPresent())
-            this.setLabel(originalGraphStep.getLabel().get());
+        originalGraphStep.getLabels().forEach(this::addLabel);
         //No need to do anything if the first element is an Element, all elements are guaranteed to be an element and will be return as is
         if ((this.ids.length == 0 || !(this.ids[0] instanceof Element)))
             this.setIteratorSupplier(() -> (Iterator<S>) (Vertex.class.isAssignableFrom(this.returnClass) ? this.vertices() : this.edges()));

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95f370d7/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/strategy/optimization/TinkerGraphStepStrategy.java
----------------------------------------------------------------------
diff --git a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/strategy/optimization/TinkerGraphStepStrategy.java b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/strategy/optimization/TinkerGraphStepStrategy.java
index 9a1832a..4403693 100644
--- a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/strategy/optimization/TinkerGraphStepStrategy.java
+++ b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/strategy/optimization/TinkerGraphStepStrategy.java
@@ -52,11 +52,7 @@ public class TinkerGraphStepStrategy extends AbstractTraversalStrategy {
             while (true) {
                 if (currentStep instanceof HasContainerHolder) {
                     tinkerGraphStep.hasContainers.addAll(((HasContainerHolder) currentStep).getHasContainers());
-                    if (currentStep.getLabel().isPresent()) {
-                        final IdentityStep identityStep = new IdentityStep<>(traversal);
-                        identityStep.setLabel(currentStep.getLabel().get());
-                        TraversalHelper.insertAfterStep(identityStep, currentStep, traversal);
-                    }
+                    currentStep.getLabels().forEach(tinkerGraphStep::addLabel);
                     traversal.removeStep(currentStep);
                 } else if (currentStep instanceof IdentityStep) {
                     // do nothing