You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2016/08/16 11:32:12 UTC

tinkerpop git commit: Validate that TraversalOpProcessor aliases always alias to "g".

Repository: tinkerpop
Updated Branches:
  refs/heads/TINKERPOP-1278 272dae06b -> 35f666f92


Validate that TraversalOpProcessor aliases always alias to "g".


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

Branch: refs/heads/TINKERPOP-1278
Commit: 35f666f926272e0a7641718035a6cc15f79345f4
Parents: 272dae0
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Tue Aug 16 07:31:24 2016 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Tue Aug 16 07:31:24 2016 -0400

----------------------------------------------------------------------
 docs/src/dev/provider/index.asciidoc            | 10 ++++++--
 .../apache/tinkerpop/gremlin/driver/Tokens.java |  2 ++
 .../op/traversal/TraversalOpProcessor.java      | 26 +++++++++++---------
 3 files changed, 25 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/35f666f9/docs/src/dev/provider/index.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/dev/provider/index.asciidoc b/docs/src/dev/provider/index.asciidoc
index c9189b0..aaa039f 100644
--- a/docs/src/dev/provider/index.asciidoc
+++ b/docs/src/dev/provider/index.asciidoc
@@ -980,7 +980,10 @@ operation is available to clear the cache of a specific side-effect.
 to be aliased to different variable names for purposes of the current request.  The value represents the name of the
 global variable and its key represents the new binding name as it will be referenced in the Gremlin query.  For
 example, if the Gremlin Server defines two `TraversalSource` instances named `g1`, it would be possible
-to send an alias pair with key of "g" and value of "g1" and thus allow the script to refer to "g1" simply as "g".
+to send an alias pair with key of "g" and value of "g1" and thus allow the script to refer to "g1" simply as "g". Note
+that unlike users of `alias` in other contexts, in this case, the key can *only* be set to "g" and there can be only
+one key value pair present (since only one `Traversal` is being submitted, there is no sense to having more than a
+single alias).
 |=========================================================
 
 '`close` operation arguments'
@@ -1000,7 +1003,10 @@ to send an alias pair with key of "g" and value of "g1" and thus allow the scrip
 to be aliased to different variable names for purposes of the current request.  The value represents the name of the
 global variable and its key represents the new binding name as it will be referenced in the Gremlin query.  For
 example, if the Gremlin Server defines two `TraversalSource` instances named `g1`, it would be possible
-to send an alias pair with key of "g" and value of "g1" and thus allow the script to refer to "g1" simply as "g".
+to send an alias pair with key of "g" and value of "g1" and thus allow the script to refer to "g1" simply as "g". Note
+that unlike users of `alias` in other contexts, in this case, the key can *only* be set to "g" and there can be only
+one key value pair present (since only one `Traversal` is being submitted, there is no sense to having more than a
+single alias).
 |=========================================================
 
 When using "gather" it is important to note the metadata that is returned on the `ResponseMessage`. It returns both the

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/35f666f9/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Tokens.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Tokens.java b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Tokens.java
index acdb2cb..2d0f192 100644
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Tokens.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Tokens.java
@@ -151,4 +151,6 @@ public final class Tokens {
     public static final String VAL_AGGREGATE_TO_LIST = "list";
     public static final String VAL_AGGREGATE_TO_MAP = "map";
     public static final String VAL_AGGREGATE_TO_NONE = "none";
+
+    public static final String VAL_TRAVERSAL_SOURCE_ALIAS = "g";
 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/35f666f9/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/traversal/TraversalOpProcessor.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/traversal/TraversalOpProcessor.java b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/traversal/TraversalOpProcessor.java
index b041a32..74b7b7d 100644
--- a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/traversal/TraversalOpProcessor.java
+++ b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/traversal/TraversalOpProcessor.java
@@ -144,13 +144,7 @@ public class TraversalOpProcessor extends AbstractOpProcessor {
         final ThrowingConsumer<Context> op;
         switch (message.getOp()) {
             case Tokens.OPS_BYTECODE:
-                final Map<String, String> bytecodeAliases = validateTraversalRequest(message);
-                final Map.Entry<String, String> bytecodeKv = bytecodeAliases.entrySet().iterator().next();
-                if (!ctx.getGraphManager().getTraversalSources().containsKey(bytecodeKv.getValue())) {
-                    final String msg = String.format("The traversal source [%s] for alias [%s] is not configured on the server.", bytecodeKv.getValue(), bytecodeKv.getKey());
-                    throw new OpProcessorException(msg, ResponseMessage.build(message).code(ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS).statusMessage(msg).create());
-                }
-
+                validateTraversalSourceAlias(ctx, message, validateTraversalRequest(message));
                 op = this::iterateBytecodeTraversal;
                 break;
             case Tokens.OPS_GATHER:
@@ -166,7 +160,7 @@ public class TraversalOpProcessor extends AbstractOpProcessor {
                     throw new OpProcessorException(msg, ResponseMessage.build(message).code(ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS).statusMessage(msg).create());
                 }
 
-                validatedAliases(message);
+                validateTraversalSourceAlias(ctx, message, validatedAliases(message).get());
 
                 op = this::gatherSideEffect;
 
@@ -221,6 +215,14 @@ public class TraversalOpProcessor extends AbstractOpProcessor {
         return op;
     }
 
+    private static void validateTraversalSourceAlias(final Context ctx, final RequestMessage message, final Map<String, String> aliases) throws OpProcessorException {
+        final String traversalSourceBindingForAlias = aliases.values().iterator().next();
+        if (!ctx.getGraphManager().getTraversalSources().containsKey(traversalSourceBindingForAlias)) {
+            final String msg = String.format("The traversal source [%s] for alias [%s] is not configured on the server.", traversalSourceBindingForAlias, Tokens.VAL_TRAVERSAL_SOURCE_ALIAS);
+            throw new OpProcessorException(msg, ResponseMessage.build(message).code(ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS).statusMessage(msg).create());
+        }
+    }
+
     private static Map<String, String> validateTraversalRequest(final RequestMessage message) throws OpProcessorException {
         if (!message.optionalArgs(Tokens.ARGS_GREMLIN).isPresent()) {
             final String msg = String.format("A message with [%s] op code requires a [%s] argument.", Tokens.OPS_BYTECODE, Tokens.ARGS_GREMLIN);
@@ -239,10 +241,12 @@ public class TraversalOpProcessor extends AbstractOpProcessor {
             throw new OpProcessorException(msg, ResponseMessage.build(message).code(ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS).statusMessage(msg).create());
         }
 
-        if (aliases.get().size() != 1) {
-            final String msg = String.format("A message with [%s] op code requires the [%s] argument to be a Map containing one alias assignment.", Tokens.OPS_BYTECODE, Tokens.ARGS_ALIASES);
+        if (aliases.get().size() != 1 || !aliases.get().containsKey(Tokens.VAL_TRAVERSAL_SOURCE_ALIAS)) {
+            final String msg = String.format("A message with [%s] op code requires the [%s] argument to be a Map containing one alias assignment named '%s'.",
+                    Tokens.OPS_BYTECODE, Tokens.ARGS_ALIASES, Tokens.VAL_TRAVERSAL_SOURCE_ALIAS);
             throw new OpProcessorException(msg, ResponseMessage.build(message).code(ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS).statusMessage(msg).create());
         }
+
         return aliases;
     }
 
@@ -336,7 +340,7 @@ public class TraversalOpProcessor extends AbstractOpProcessor {
             else {
                 final ScriptEngines engines = context.getGremlinExecutor().getScriptEngines();
                 final SimpleBindings b = new SimpleBindings();
-                b.put("g", g);
+                b.put(Tokens.VAL_TRAVERSAL_SOURCE_ALIAS, g);
                 traversal = engines.eval(bytecode, b, lambdaLanguage.get());
             }
         } catch (Exception ex) {