Skip to content

Commit

Permalink
Scripting: Make contexts available to ScriptEngine construction (#24896)
Browse files Browse the repository at this point in the history
This commit adds collection of all contexts to the parameters of
getScriptEngine. This will allow script engines like painless to
precache extra information about the contexts.
  • Loading branch information
rjernst authored May 25, 2017
1 parent 9a11869 commit 8eab1fe
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
*/
package org.elasticsearch.plugins;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.script.ScriptContext;
Expand All @@ -31,9 +33,11 @@
public interface ScriptPlugin {

/**
* Returns a {@link ScriptEngine} instance or <code>null</code> if this plugin doesn't add a new script engine
* Returns a {@link ScriptEngine} instance or <code>null</code> if this plugin doesn't add a new script engine.
* @param settings Node settings
* @param contexts The contexts that {@link ScriptEngine#compile(String, String, ScriptContext, Map)} may be called with
*/
default ScriptEngine getScriptEngine(Settings settings) {
default ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>> contexts) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public ScriptModule(Settings settings, List<ScriptPlugin> scriptPlugins) {
throw new IllegalArgumentException("Context name [" + context.name + "] defined twice");
}
}
ScriptEngine engine = plugin.getScriptEngine(settings);
}
for (ScriptPlugin plugin : scriptPlugins) {
ScriptEngine engine = plugin.getScriptEngine(settings, contexts.values());
if (engine != null) {
ScriptEngine existing = engines.put(engine.getType(), engine);
if (existing != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class ExplainableScriptIT extends ESIntegTestCase {

public static class ExplainableScriptPlugin extends Plugin implements ScriptPlugin {
@Override
public ScriptEngine getScriptEngine(Settings settings) {
public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>> contexts) {
return new ScriptEngine() {
@Override
public String getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {

public static class DummyTemplatePlugin extends Plugin implements ScriptPlugin {
@Override
public ScriptEngine getScriptEngine(Settings settings) {
public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>> contexts) {
return new DummyTemplateScriptEngine();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@

package org.elasticsearch.script.expression;

import java.util.Collection;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptEngine;

public class ExpressionPlugin extends Plugin implements ScriptPlugin {

@Override
public ScriptEngine getScriptEngine(Settings settings) {
public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>> contexts) {
return new ExpressionScriptEngine(settings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@
import org.elasticsearch.plugins.SearchPlugin;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptEngine;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;

public class MustachePlugin extends Plugin implements ScriptPlugin, ActionPlugin, SearchPlugin {

@Override
public ScriptEngine getScriptEngine(Settings settings) {
public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>>contexts) {
return new MustacheScriptEngine();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptEngine;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
Expand All @@ -40,7 +42,7 @@ public final class PainlessPlugin extends Plugin implements ScriptPlugin {
}

@Override
public ScriptEngine getScriptEngine(Settings settings) {
public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>> contexts) {
return new PainlessScriptEngine(settings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;

Expand All @@ -45,7 +46,7 @@
public class ExpertScriptPlugin extends Plugin implements ScriptPlugin {

@Override
public ScriptEngine getScriptEngine(Settings settings) {
public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>> contexts) {
return new MyExpertScriptEngine();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin;

import java.util.Collection;
import java.util.Map;
import java.util.function.Function;

Expand All @@ -34,7 +35,7 @@ public abstract class MockScriptPlugin extends Plugin implements ScriptPlugin {
public static final String NAME = MockScriptEngine.NAME;

@Override
public ScriptEngine getScriptEngine(Settings settings) {
public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>> contexts) {
return new MockScriptEngine(pluginScriptLang(), pluginScripts());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.script.MockScriptEngine;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptEngine;
import org.elasticsearch.script.ScriptModule;
import org.elasticsearch.script.ScriptService;
Expand Down Expand Up @@ -1195,7 +1196,7 @@ public static TestAnalysis createTestAnalysis(IndexSettings indexSettings, Setti
public static ScriptModule newTestScriptModule() {
return new ScriptModule(Settings.EMPTY, singletonList(new ScriptPlugin() {
@Override
public ScriptEngine getScriptEngine(Settings settings) {
public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>> contexts) {
return new MockScriptEngine(MockScriptEngine.NAME, Collections.singletonMap("1", script -> "1"));
}
}));
Expand Down

0 comments on commit 8eab1fe

Please sign in to comment.