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 aScriptEngineinstance. It is designed to host any JSR-223 enabledScriptEngineand assumes such engines are designed to be thread-safe in the evaluation. Script evaluation functions return aCompletableFuturewhere scripts may timeout if their evaluation takes too long. The default timeout is 8000ms. By default, theGremlinExecutorinitializes 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 returnedCompletableFutureto 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 classGremlinExecutor.Builderstatic classGremlinExecutor.LifeCycleThe 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.Builderbuild()Create aBuilderwith the gremlin-groovy ScriptEngine configured.voidclose()CompletableFuture<Void>closeAsync()Executors are only closed if they were not supplied externally in theGremlinExecutor.BuilderOptional<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 transformFunctionthat will transform the result after script evaluates but before transaction commit and before the returnedCompletableFutureis 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 transformFunctionthat will transform the result after script evaluates but before transaction commit and before the returnedCompletableFutureis 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 aConsumerthat will take the result for additional processing after the script evaluates and after theCompletableFutureis 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 aMapof bindings.CompletableFuture<Object>eval(String script, Bindings boundVars)Evaluate a script with specified bindings.Traversal.Admineval(Bytecode bytecode, Bindings boundVars, String language, String traversalSource)Evaluates bytecode with bindings for a specific language into aTraversal.ExecutorServicegetExecutorService()ScheduledExecutorServicegetScheduledExecutorService()GremlinScriptEngineManagergetScriptEngineManager()
-
-
-
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 theScriptEngineimplementation implementsCompilable. In the event that the defaultScriptEnginedoes 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 theScriptEngineimplementation implementsCompilable. In the event that the requestedScriptEnginedoes 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 aMapof 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 aMapto 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 transformFunctionthat will transform the result after script evaluates but before transaction commit and before the returnedCompletableFutureis 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- aFunctionthat 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 transformFunctionthat will transform the result after script evaluates but before transaction commit and before the returnedCompletableFutureis 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- aFunctionthat 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 aConsumerthat will take the result for additional processing after the script evaluates and after theCompletableFutureis 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- aConsumerthat 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 transformFunctionandConsumer. TheFunctionwill transform the result after script evaluates but before transaction commit and before the returnedCompletableFutureis completed. TheConsumerwill take the result for additional processing after the script evaluates and after theCompletableFutureis 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 transformFunctionandConsumer. TheFunctionwill transform the result after script evaluates but before transaction commit and before the returnedCompletableFutureis completed. TheConsumerwill take the result for additional processing after the script evaluates and after theCompletableFutureis 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- aFunctionthat transforms the result - can benullwithResult- aConsumerthat 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 ExceptionExecutors are only closed if they were not supplied externally in theGremlinExecutor.Builder- Specified by:
closein 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 aBuilderwith the gremlin-groovy ScriptEngine configured.
-
-