diff options
author | Ryan Ernst <ryan@iernst.net> | 2017-05-24 14:29:02 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-24 14:29:02 -0700 |
commit | 1daacd97b07b601a8a27a8bf64aaf132d2a1931d (patch) | |
tree | 5321e4d094f9c435f23947134fa71a523e5c65ea /modules/lang-mustache/src/main | |
parent | 0ddd21942315092d76b43786b45c5ebb015849a5 (diff) |
Scripting: Add instance and compiled classes to script contexts (#24868)
This commit modifies the compile method of ScriptService to be context
aware. The ScriptContext is now a generic class which contains both the
instance type and compiled type for a script. Instance type may be
stateful (for example, pre loading field information for the index a
script will execute on, like in expressions), while the compiled type is
stateless and used to construct instance type instances. This change is
only a first step to cutover ScriptService to the new paradigm. It only
converts callers to the script service, and has a small shim to wrap
compilation from the script engines to support the current two fixed
instance types, SearchScript and ExecutableScript.
Diffstat (limited to 'modules/lang-mustache/src/main')
2 files changed, 8 insertions, 16 deletions
diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java index 28ba360025..e31b97099f 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java @@ -27,10 +27,7 @@ import org.apache.logging.log4j.util.Supplier; import org.elasticsearch.SpecialPermission; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.io.FastStringReader; -import org.elasticsearch.common.io.UTF8StreamWriter; -import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.logging.ESLoggerFactory; -import org.elasticsearch.script.CompiledScript; import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.GeneralScriptException; import org.elasticsearch.script.Script; @@ -40,7 +37,6 @@ import org.elasticsearch.search.lookup.SearchLookup; import java.io.Reader; import java.io.StringWriter; -import java.lang.ref.SoftReference; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Collections; @@ -86,14 +82,12 @@ public final class MustacheScriptEngine implements ScriptEngine { } @Override - public ExecutableScript executable(CompiledScript compiledScript, - @Nullable Map<String, Object> vars) { - return new MustacheExecutableScript(compiledScript, vars); + public ExecutableScript executable(Object compiledScript, @Nullable Map<String, Object> vars) { + return new MustacheExecutableScript((Mustache) compiledScript, vars); } @Override - public SearchScript search(CompiledScript compiledScript, SearchLookup lookup, - @Nullable Map<String, Object> vars) { + public SearchScript search(Object compiledScript, SearchLookup lookup, @Nullable Map<String, Object> vars) { throw new UnsupportedOperationException(); } @@ -102,7 +96,7 @@ public final class MustacheScriptEngine implements ScriptEngine { * */ private class MustacheExecutableScript implements ExecutableScript { /** Compiled template object wrapper. */ - private CompiledScript template; + private Mustache template; /** Parameters to fill above object with. */ private Map<String, Object> vars; @@ -110,7 +104,7 @@ public final class MustacheScriptEngine implements ScriptEngine { * @param template the compiled template object wrapper * @param vars the parameters to fill above object with **/ - MustacheExecutableScript(CompiledScript template, Map<String, Object> vars) { + MustacheExecutableScript(Mustache template, Map<String, Object> vars) { this.template = template; this.vars = vars == null ? Collections.emptyMap() : vars; } @@ -127,7 +121,7 @@ public final class MustacheScriptEngine implements ScriptEngine { // crazy reflection here SpecialPermission.check(); AccessController.doPrivileged((PrivilegedAction<Void>) () -> { - ((Mustache) template.compiled()).execute(writer, vars); + template.execute(writer, vars); return null; }); } catch (Exception e) { diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TransportSearchTemplateAction.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TransportSearchTemplateAction.java index 90678aba85..ca25147405 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TransportSearchTemplateAction.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TransportSearchTemplateAction.java @@ -27,7 +27,6 @@ import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.NamedXContentRegistry; @@ -39,14 +38,13 @@ import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.template.CompiledTemplate; -import org.elasticsearch.template.CompiledTemplate; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; import java.io.IOException; import java.util.Collections; -import static org.elasticsearch.script.ScriptContext.SEARCH; +import static org.elasticsearch.script.ScriptContext.EXECUTABLE; public class TransportSearchTemplateAction extends HandledTransportAction<SearchTemplateRequest, SearchTemplateResponse> { @@ -102,7 +100,7 @@ public class TransportSearchTemplateAction extends HandledTransportAction<Search NamedXContentRegistry xContentRegistry) throws IOException { Script script = new Script(searchTemplateRequest.getScriptType(), TEMPLATE_LANG, searchTemplateRequest.getScript(), searchTemplateRequest.getScriptParams() == null ? Collections.emptyMap() : searchTemplateRequest.getScriptParams()); - CompiledTemplate compiledScript = scriptService.compileTemplate(script, SEARCH); + CompiledTemplate compiledScript = scriptService.compileTemplate(script, EXECUTABLE); String source = compiledScript.run(script.getParams()); response.setSource(new BytesArray(source)); |