Repository: tinkerpop
Updated Branches:
  refs/heads/tp32 ffd65437b -> 46888b1a0


fixed a bug in Bytecode bindings where anonymous traversals were not aware of 
parent Bindings. The solution sorta sucks...but I don't know how else to do it. 
Added test cases to verify anonymous traversal binding usage.


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

Branch: refs/heads/tp32
Commit: cba782ae2632c4e0c10fcf0131f5751a6fd61dd3
Parents: 9e82b4d
Author: Marko A. Rodriguez <okramma...@gmail.com>
Authored: Tue Nov 29 09:07:22 2016 -0700
Committer: Marko A. Rodriguez <okramma...@gmail.com>
Committed: Tue Nov 29 09:07:22 2016 -0700

----------------------------------------------------------------------
 CHANGELOG.asciidoc                              |  1 +
 .../gremlin/process/traversal/Bytecode.java     | 17 ++++++------
 .../gremlin/process/traversal/BytecodeTest.java | 28 ++++++++++++++++++++
 3 files changed, 37 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/cba782ae/CHANGELOG.asciidoc
----------------------------------------------------------------------
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 9791b64..10a0604 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -26,6 +26,7 @@ 
image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
 TinkerPop 3.2.4 (Release Date: NOT OFFICIALLY RELEASED YET)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+* Fixed a bug in Gremlin-Java `Bytecode` where anonymous traversals were not 
aware of parent bindings.
 * Fixed a bug in Gremlin-Java GraphSON deserialization around `P.within()` and 
`P.without()`.
 * Converted Spark process suite tests to "integration" tests.
 * Fixed a bug in `InlineFilterStrategy` having to do with folding 
`HasContainers` into `VertexStep`.

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/cba782ae/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Bytecode.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Bytecode.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Bytecode.java
index da09362..377195f 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Bytecode.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Bytecode.java
@@ -52,7 +52,7 @@ public final class Bytecode implements Cloneable, 
Serializable {
 
     private List<Instruction> sourceInstructions = new ArrayList<>();
     private List<Instruction> stepInstructions = new ArrayList<>();
-    private transient Bindings bindings = null;
+    private static transient ThreadLocal<Bindings> BINDINGS = new 
ThreadLocal<>();
 
     /**
      * Add a {@link TraversalSource} instruction to the bytecode.
@@ -62,8 +62,7 @@ public final class Bytecode implements Cloneable, 
Serializable {
      */
     public void addSource(final String sourceName, final Object... arguments) {
         if (sourceName.equals(TraversalSource.Symbols.withBindings)) {
-            this.bindings = (Bindings) arguments[0];
-            this.bindings.clear();
+            BINDINGS.set((Bindings) arguments[0]);
         } else if 
(sourceName.equals(TraversalSource.Symbols.withoutStrategies)) {
             final Class<TraversalStrategy>[] classes = new 
Class[arguments.length];
             for (int i = 0; i < arguments.length; i++) {
@@ -74,8 +73,8 @@ public final class Bytecode implements Cloneable, 
Serializable {
             this.sourceInstructions.add(new Instruction(sourceName, classes));
         } else {
             this.sourceInstructions.add(new Instruction(sourceName, 
flattenArguments(arguments)));
-            if (null != this.bindings) this.bindings.clear();
         }
+        if (null != BINDINGS.get()) BINDINGS.get().clear();
     }
 
     /**
@@ -86,7 +85,7 @@ public final class Bytecode implements Cloneable, 
Serializable {
      */
     public void addStep(final String stepName, final Object... arguments) {
         this.stepInstructions.add(new Instruction(stepName, 
flattenArguments(arguments)));
-        if (null != this.bindings) this.bindings.clear();
+        if (null != BINDINGS.get()) BINDINGS.get().clear();
     }
 
     /**
@@ -118,9 +117,9 @@ public final class Bytecode implements Cloneable, 
Serializable {
     }
 
     /**
-     * Get all the bindings (in a nested, recurssive manner) from all the 
arguments of all the instructions of this bytecode.
+     * Get all the BINDINGS (in a nested, recurssive manner) from all the 
arguments of all the instructions of this bytecode.
      *
-     * @return a map of string variable and object value bindings
+     * @return a map of string variable and object value BINDINGS
      */
     public Map<String, Object> getBindings() {
         final Map<String, Object> bindingsMap = new HashMap<>();
@@ -273,8 +272,8 @@ public final class Bytecode implements Cloneable, 
Serializable {
     }
 
     private final Object convertArgument(final Object argument, final boolean 
searchBindings) {
-        if (searchBindings && null != this.bindings) {
-            final String variable = this.bindings.getBoundVariable(argument);
+        if (searchBindings && null != BINDINGS.get()) {
+            final String variable = BINDINGS.get().getBoundVariable(argument);
             if (null != variable)
                 return new Binding<>(variable, convertArgument(argument, 
false));
         }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/cba782ae/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/BytecodeTest.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/BytecodeTest.java
 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/BytecodeTest.java
index 1b655ca..ea81bdc 100644
--- 
a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/BytecodeTest.java
+++ 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/BytecodeTest.java
@@ -21,6 +21,7 @@ package org.apache.tinkerpop.gremlin.process.traversal;
 
 import org.apache.tinkerpop.gremlin.process.computer.Computer;
 import 
org.apache.tinkerpop.gremlin.process.computer.traversal.strategy.decoration.VertexProgramStrategy;
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
 import 
org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
 import 
org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.SubgraphStrategy;
@@ -30,6 +31,8 @@ import 
org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;
 import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
 import org.junit.Test;
 
+import java.util.List;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
 
@@ -99,6 +102,31 @@ public class BytecodeTest {
     }
 
     @Test
+    public void shouldIncludeBindingsInNestedTraversals() {
+        final Bindings b = new Bindings();
+        final GraphTraversalSource g = 
EmptyGraph.instance().traversal().withBindings(b);
+        final Bytecode bytecode = 
g.V().in(b.of("a","created")).where(__.out(b.of("b","knows")).has("age",b.of("c",P.gt(32))).map(__.values(b.of("d","name")))).asAdmin().getBytecode();
+        assertEquals(4, bytecode.getBindings().size());
+        assertEquals("created", bytecode.getBindings().get("a"));
+        assertEquals("knows", bytecode.getBindings().get("b"));
+        assertEquals(P.gt(32), bytecode.getBindings().get("c"));
+        assertEquals("name", bytecode.getBindings().get("d"));
+        //
+        Bytecode.Binding binding = 
(Bytecode.Binding)((List<Bytecode.Instruction>)bytecode.getStepInstructions()).get(1).getArguments()[0];
+        assertEquals("a", binding.variable());
+        assertEquals("created", binding.value());
+        binding = (Bytecode.Binding) 
((List<Bytecode.Instruction>)((Bytecode)((List<Bytecode.Instruction>)bytecode.getStepInstructions()).get(2).getArguments()[0]).getStepInstructions()).get(0).getArguments()[0];
+        assertEquals("b", binding.variable());
+        assertEquals("knows", binding.value());
+        binding = (Bytecode.Binding) 
((List<Bytecode.Instruction>)((Bytecode)((List<Bytecode.Instruction>)bytecode.getStepInstructions()).get(2).getArguments()[0]).getStepInstructions()).get(1).getArguments()[1];
+        assertEquals("c", binding.variable());
+        assertEquals(P.gt(32), binding.value());
+        binding = (Bytecode.Binding) 
((List<Bytecode.Instruction>)((Bytecode)((List<Bytecode.Instruction>)((Bytecode)((List<Bytecode.Instruction>)bytecode.getStepInstructions()).get(2).getArguments()[0]).getStepInstructions()).get(2).getArguments()[0]).getStepInstructions()).get(0).getArguments()[0];
+        assertEquals("d", binding.variable());
+        assertEquals("name", binding.value());
+    }
+
+    @Test
     public void shouldConvertStrategies() {
         final GraphTraversalSource g = EmptyGraph.instance().traversal();
         Bytecode bytecode = 
g.withStrategies(ReadOnlyStrategy.instance()).getBytecode();

Reply via email to