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 2015/03/05 20:34:32 UTC
incubator-tinkerpop git commit: Added compile() method to
GremlinExecutor.
Repository: incubator-tinkerpop
Updated Branches:
refs/heads/master 0b4c93add -> 37a16e09c
Added compile() method to GremlinExecutor.
This will cache the script in the requested ScriptEngine assuming it implements the Compileable interface.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/37a16e09
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/37a16e09
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/37a16e09
Branch: refs/heads/master
Commit: 37a16e09c86e138a728b73f09d4551dc000b0eef
Parents: 0b4c93a
Author: Stephen Mallette <sp...@apache.org>
Authored: Thu Mar 5 14:33:56 2015 -0500
Committer: Stephen Mallette <sp...@apache.org>
Committed: Thu Mar 5 14:33:56 2015 -0500
----------------------------------------------------------------------
CHANGELOG.asciidoc | 1 +
.../groovy/engine/GremlinExecutorTest.java | 10 ++++
.../gremlin/groovy/engine/GremlinExecutor.java | 27 +++++++++++
.../gremlin/groovy/engine/ScriptEngines.java | 48 ++++++++++++++++++++
4 files changed, 86 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/37a16e09/CHANGELOG.asciidoc
----------------------------------------------------------------------
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 01ad2d9..02a221f 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -25,6 +25,7 @@ image::http://www.tinkerpop.com/docs/current/images/gremlin-hindu.png[width=225]
TinkerPop 3.0.0.M8 (Release Date: NOT OFFICIALLY RELEASED YET)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+* Added `compile` method to `GremlinExecutor` and related classes.
* Renamed "Gremlin Kryo" to "Gryo".
* Implement profiling for nested traversals.
* Refactored `TinkerWorkerPool` to use `ExecutorService` so as to reuse threads when executing graph computer functions.
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/37a16e09/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
index 5076072..d516bab 100644
--- a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
@@ -30,6 +30,7 @@ import org.junit.Test;
import org.kohsuke.groovy.sandbox.GroovyInterceptor;
import javax.script.Bindings;
+import javax.script.CompiledScript;
import javax.script.SimpleBindings;
import java.sql.Time;
import java.util.Arrays;
@@ -39,6 +40,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
@@ -81,6 +83,14 @@ public class GremlinExecutorTest extends AbstractGremlinTest {
}
@Test
+ public void shouldCompileScript() throws Exception {
+ final GremlinExecutor gremlinExecutor = GremlinExecutor.build().create();
+ final CompiledScript script = gremlinExecutor.compile("1+1").get();
+ assertEquals(2, script.eval());
+ gremlinExecutor.close();
+ }
+
+ @Test
public void shouldEvalMultipleScripts() throws Exception {
final GremlinExecutor gremlinExecutor = GremlinExecutor.build().create();
assertEquals(2, gremlinExecutor.eval("1+1").get());
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/37a16e09/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
index 1a03753..8043b18 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
@@ -28,6 +28,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.script.Bindings;
+import javax.script.Compilable;
+import javax.script.CompiledScript;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
import java.io.File;
@@ -114,6 +116,31 @@ public class GremlinExecutor implements AutoCloseable {
this.suppliedScheduledExecutor = suppliedScheduledExecutor;
}
+ /**
+ * Attempts to compile a script and cache it in the default {@link javax.script.ScriptEngine}. This is only
+ * possible if the {@link javax.script.ScriptEngine} implementation implements {@link javax.script.Compilable}.
+ * In the event that the default {@link javax.script.ScriptEngine} does not implement it, the method will
+ * return empty.
+ */
+ public Optional<CompiledScript> compile(final String script) throws ScriptException {
+ return compile(script, Optional.empty());
+ }
+
+ /**
+ * Attempts to compile a script and cache it in the request {@link javax.script.ScriptEngine}. This is only
+ * possible if the {@link javax.script.ScriptEngine} implementation implements {@link javax.script.Compilable}.
+ * In the event that the requested {@link javax.script.ScriptEngine} does not implement it, the method will
+ * return empty.
+ */
+ public Optional<CompiledScript> compile(final String script, final Optional<String> language) throws ScriptException {
+ final String lang = language.orElse("gremlin-groovy");
+ try {
+ return Optional.of(scriptEngines.compile(script, lang));
+ } catch (UnsupportedOperationException uoe) {
+ return Optional.empty();
+ }
+ }
+
public CompletableFuture<Object> eval(final String script) {
return eval(script, Optional.empty(), new SimpleBindings());
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/37a16e09/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
index 2763ef0..f0b919c 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
@@ -30,6 +30,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.script.Bindings;
+import javax.script.Compilable;
+import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
@@ -107,6 +109,52 @@ public class ScriptEngines implements AutoCloseable {
}
/**
+ * Compiles a script without executing it.
+ *
+ * @throws java.lang.UnsupportedOperationException if the {@link ScriptEngine} implementation does not implement
+ * the {@link javax.script.Compilable} interface.
+ */
+ public CompiledScript compile(final String script, final String language) throws ScriptException {
+ if (!scriptEngines.containsKey(language))
+ throw new IllegalArgumentException("Language [%s] not supported");
+
+ try {
+ evaluationCount.incrementAndGet();
+ final ScriptEngine scriptEngine = scriptEngines.get(language);
+ if (!Compilable.class.isAssignableFrom(scriptEngine.getClass()))
+ throw new UnsupportedOperationException(String.format("ScriptEngine for %s does not implement %s", language, Compilable.class.getName()));
+
+ final Compilable compilable = (Compilable) scriptEngine;
+ return compilable.compile(script);
+ } finally {
+ evaluationCount.decrementAndGet();
+ }
+ }
+
+ /**
+ * Compiles a script without executing it.
+ *
+ * @throws java.lang.UnsupportedOperationException if the {@link ScriptEngine} implementation does not implement
+ * the {@link javax.script.Compilable} interface.
+ */
+ public CompiledScript compile(final Reader script, final String language) throws ScriptException {
+ if (!scriptEngines.containsKey(language))
+ throw new IllegalArgumentException("Language [%s] not supported");
+
+ try {
+ evaluationCount.incrementAndGet();
+ final ScriptEngine scriptEngine = scriptEngines.get(language);
+ if (scriptEngine instanceof Compilable)
+ throw new UnsupportedOperationException(String.format("ScriptEngine for %s does not implement %s", language, Compilable.class.getName()));
+
+ final Compilable compilable = (Compilable) scriptEngine;
+ return compilable.compile(script);
+ } finally {
+ evaluationCount.decrementAndGet();
+ }
+ }
+
+ /**
* Reload a {@code ScriptEngine} with fresh imports. Waits for any existing script evaluations to complete but
* then blocks other operations until complete.
*/