Class GremlinExecutor
- java.lang.Object
-
- org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor
-
- All Implemented Interfaces:
AutoCloseable
public class GremlinExecutor extends Object implements AutoCloseable
Execute Gremlin scripts against aScriptEngine
instance. It is designed to host any JSR-223 enabledScriptEngine
and assumes such engines are designed to be thread-safe in the evaluation. Script evaluation functions return aCompletableFuture
where scripts may timeout if their evaluation takes too long. The default timeout is 8000ms. By default, theGremlinExecutor
initializes itself to use a shared thread pool initialized with four threads. This default thread pool is shared for both the task of executing script evaluations and for scheduling timeouts. It is worth noting that a timeout simply triggers the returnedCompletableFuture
to abort, but the thread processing the script will continue to evaluate until completion. This offers only marginal protection against run-away scripts.- Author:
- Stephen Mallette (http://stephen.genoprime.com)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
GremlinExecutor.Builder
static class
GremlinExecutor.LifeCycle
The lifecycle of execution within the#eval(String, String, Bindings, LifeCycle)
method.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static GremlinExecutor.Builder
build()
Create aBuilder
with the gremlin-groovy ScriptEngine configured.void
close()
CompletableFuture<Void>
closeAsync()
Executors are only closed if they were not supplied externally in theGremlinExecutor.Builder
Optional<CompiledScript>
compile(String script)
Attempts to compile a script and cache it in the defaultScriptEngine
.Optional<CompiledScript>
compile(String script, Optional<String> language)
Attempts to compile a script and cache it in the requestScriptEngine
.CompletableFuture<Object>
eval(Object gremlin, String language, Map<String,Object> boundVars, Long timeOut, Function<Object,Object> transformResult)
Evaluate a script and allow for the submission of a transformFunction
that will transform the result after script evaluates but before transaction commit and before the returnedCompletableFuture
is completed.CompletableFuture<Object>
eval(Object gremlin, String language, Map<String,Object> boundVars, Function<Object,Object> transformResult)
Evaluate a script and allow for the submission of a transformFunction
that will transform the result after script evaluates but before transaction commit and before the returnedCompletableFuture
is completed.CompletableFuture<Object>
eval(Object gremlin, String language, Bindings boundVars, Long timeOut, Function<Object,Object> transformResult, Consumer<Object> withResult)
CompletableFuture<Object>
eval(Object gremlin, String language, Bindings boundVars, Function<Object,Object> transformResult, Consumer<Object> withResult)
CompletableFuture<Object>
eval(Object gremlin, String language, Bindings boundVars, GremlinExecutor.LifeCycle lifeCycle)
Evaluate a script and allow for the submission of alteration to the entire evaluation execution lifecycle.CompletableFuture<Object>
eval(String script)
Evaluate a script with empty bindings.CompletableFuture<Object>
eval(String script, String language, Map<String,Object> boundVars)
Evaluate a script.CompletableFuture<Object>
eval(String script, String language, Map<String,Object> boundVars, Consumer<Object> withResult)
Evaluate a script and allow for the submission of aConsumer
that will take the result for additional processing after the script evaluates and after theCompletableFuture
is completed, but before the transaction is committed.CompletableFuture<Object>
eval(String script, String language, Bindings boundVars)
Evaluate a script.CompletableFuture<Object>
eval(String script, Map<String,Object> boundVars)
Evaluate a script with aMap
of bindings.CompletableFuture<Object>
eval(String script, Bindings boundVars)
Evaluate a script with specified bindings.Traversal.Admin
eval(Bytecode bytecode, Bindings boundVars, String language, String traversalSource)
Evaluates bytecode with bindings for a specific language into aTraversal
.ExecutorService
getExecutorService()
ScheduledExecutorService
getScheduledExecutorService()
GremlinScriptEngineManager
getScriptEngineManager()
-
-
-
Method Detail
-
compile
public Optional<CompiledScript> compile(String script) throws ScriptException
Attempts to compile a script and cache it in the defaultScriptEngine
. This is only possible if theScriptEngine
implementation implementsCompilable
. In the event that the defaultScriptEngine
does not implement it, the method will return empty.- Throws:
ScriptException
-
compile
public Optional<CompiledScript> compile(String script, Optional<String> language) throws ScriptException
Attempts to compile a script and cache it in the requestScriptEngine
. This is only possible if theScriptEngine
implementation implementsCompilable
. In the event that the requestedScriptEngine
does not implement it, the method will return empty.- Throws:
ScriptException
-
eval
public CompletableFuture<Object> eval(String script)
Evaluate a script with empty bindings.
-
eval
public CompletableFuture<Object> eval(String script, Bindings boundVars)
Evaluate a script with specified bindings.
-
eval
public CompletableFuture<Object> eval(String script, Map<String,Object> boundVars)
Evaluate a script with aMap
of bindings.
-
eval
public CompletableFuture<Object> eval(String script, String language, Map<String,Object> boundVars)
Evaluate a script.- Parameters:
script
- the script to evaluatelanguage
- the language to evaluate it inboundVars
- the bindings as aMap
to evaluate in the context of the script
-
eval
public CompletableFuture<Object> eval(String script, String language, Bindings boundVars)
Evaluate a script.- Parameters:
script
- the script to evaluatelanguage
- the language to evaluate it inboundVars
- the bindings to evaluate in the context of the script
-
eval
public CompletableFuture<Object> eval(Object gremlin, String language, Map<String,Object> boundVars, Function<Object,Object> transformResult)
Evaluate a script and allow for the submission of a transformFunction
that will transform the result after script evaluates but before transaction commit and before the returnedCompletableFuture
is completed.- Parameters:
gremlin
- the script or bytecode to evaluatelanguage
- the language to evaluate it inboundVars
- the bindings to evaluate in the context of the scripttransformResult
- aFunction
that transforms the result - can benull
-
eval
public CompletableFuture<Object> eval(Object gremlin, String language, Map<String,Object> boundVars, Long timeOut, Function<Object,Object> transformResult)
Evaluate a script and allow for the submission of a transformFunction
that will transform the result after script evaluates but before transaction commit and before the returnedCompletableFuture
is completed.- Parameters:
gremlin
- the script or bytecode to evaluatelanguage
- the language to evaluate it inboundVars
- the bindings to evaluate in the context of the scripttimeOut
- optional override for evaluation timeouttransformResult
- aFunction
that transforms the result - can benull
-
eval
public CompletableFuture<Object> eval(String script, String language, Map<String,Object> boundVars, Consumer<Object> withResult)
Evaluate a script and allow for the submission of aConsumer
that will take the result for additional processing after the script evaluates and after theCompletableFuture
is completed, but before the transaction is committed.- Parameters:
script
- the script to evaluatelanguage
- the language to evaluate it inboundVars
- the bindings to evaluate in the context of the scriptwithResult
- aConsumer
that accepts the result - can benull
-
eval
public CompletableFuture<Object> eval(Object gremlin, String language, Bindings boundVars, Function<Object,Object> transformResult, Consumer<Object> withResult)
Evaluate a script and allow for the submission of both a transformFunction
andConsumer
. TheFunction
will transform the result after script evaluates but before transaction commit and before the returnedCompletableFuture
is completed. TheConsumer
will take the result for additional processing after the script evaluates and after theCompletableFuture
is completed, but before the transaction is committed.
-
eval
public CompletableFuture<Object> eval(Object gremlin, String language, Bindings boundVars, Long timeOut, Function<Object,Object> transformResult, Consumer<Object> withResult)
Evaluate a script and allow for the submission of both a transformFunction
andConsumer
. TheFunction
will transform the result after script evaluates but before transaction commit and before the returnedCompletableFuture
is completed. TheConsumer
will take the result for additional processing after the script evaluates and after theCompletableFuture
is completed, but before the transaction is committed.- Parameters:
gremlin
- the script or bytecode to evaluatelanguage
- the language to evaluate it inboundVars
- the bindings to evaluate in the context of the scripttimeOut
- optional override for evaluation timeouttransformResult
- aFunction
that transforms the result - can benull
withResult
- aConsumer
that accepts the result - can benull
-
eval
public CompletableFuture<Object> eval(Object gremlin, String language, Bindings boundVars, GremlinExecutor.LifeCycle lifeCycle)
Evaluate a script and allow for the submission of alteration to the entire evaluation execution lifecycle.- Parameters:
gremlin
- the script or bytecode to evaluatelanguage
- the language to evaluate it inboundVars
- the bindings to evaluate in the context of the scriptlifeCycle
- a set of functions that can be applied at various stages of the evaluation process
-
eval
public Traversal.Admin eval(Bytecode bytecode, Bindings boundVars, String language, String traversalSource) throws ScriptException
Evaluates bytecode with bindings for a specific language into aTraversal
.- Throws:
ScriptException
-
getScriptEngineManager
public GremlinScriptEngineManager getScriptEngineManager()
-
getExecutorService
public ExecutorService getExecutorService()
-
getScheduledExecutorService
public ScheduledExecutorService getScheduledExecutorService()
-
close
public void close() throws Exception
Executors are only closed if they were not supplied externally in theGremlinExecutor.Builder
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-
closeAsync
public CompletableFuture<Void> closeAsync() throws Exception
Executors are only closed if they were not supplied externally in theGremlinExecutor.Builder
- Throws:
Exception
-
build
public static GremlinExecutor.Builder build()
Create aBuilder
with the gremlin-groovy ScriptEngine configured.
-
-