abstract away script execution

This commit is contained in:
kimchy 2010-09-03 01:49:06 +03:00
parent cd28afe950
commit 575f5b406b
6 changed files with 207 additions and 26 deletions

View File

@ -39,6 +39,7 @@ import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.SourceFieldMapper;
import org.elasticsearch.index.mapper.SourceFieldSelector;
import org.elasticsearch.script.CompiledScript;
import org.elasticsearch.script.ScriptService;
import javax.annotation.Nullable;
@ -67,7 +68,7 @@ public class ScriptFieldsFunction implements FieldsFunction {
final ScriptService scriptService;
final Object script;
final CompiledScript script;
final DocMap docMap;

View File

@ -0,0 +1,43 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.script;
/**
* @author kimchy (shay.banon)
*/
public class CompiledScript {
private final String type;
private final Object compiled;
public CompiledScript(String type, Object compiled) {
this.type = type;
this.compiled = compiled;
}
public String type() {
return type;
}
public Object compiled() {
return compiled;
}
}

View File

@ -0,0 +1,34 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.script;
import java.util.Map;
/**
* @author kimchy (shay.banon)
*/
public interface ScriptEngineService {
String type();
Object compile(String script);
public Object execute(Object compiledScript, Map vars);
}

View File

@ -19,14 +19,35 @@
package org.elasticsearch.script;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.multibindings.Multibinder;
import org.elasticsearch.script.mvel.MvelScriptEngineService;
import java.util.List;
/**
* @author kimchy (shay.banon)
*/
public class ScriptModule extends AbstractModule {
private List<Class<? extends ScriptEngineService>> scriptEngines = Lists.newArrayList();
public void addScriptEngine(Class<? extends ScriptEngineService> scriptEngine) {
scriptEngines.add(scriptEngine);
}
@Override protected void configure() {
Multibinder<ScriptEngineService> multibinder = Multibinder.newSetBinder(binder(), ScriptEngineService.class);
try {
multibinder.addBinding().to(MvelScriptEngineService.class);
} catch (Throwable t) {
// no MVEL
}
for (Class<? extends ScriptEngineService> scriptEngine : scriptEngines) {
multibinder.addBinding().to(scriptEngine);
}
bind(ScriptService.class).asEagerSingleton();
}
}

View File

@ -19,17 +19,17 @@
package org.elasticsearch.script;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.collect.ImmutableSet;
import org.elasticsearch.common.collect.MapMaker;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.math.UnboxedMathUtils;
import org.elasticsearch.common.mvel2.MVEL;
import org.elasticsearch.common.mvel2.ParserContext;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.script.mvel.MvelScriptEngineService;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
/**
@ -37,28 +37,37 @@ import java.util.concurrent.ConcurrentMap;
*/
public class ScriptService extends AbstractComponent {
private final ConcurrentMap<String, Object> cache = new MapMaker().softValues().makeMap();
private final String defaultType;
private final ParserContext parserContext;
private final ImmutableMap<String, ScriptEngineService> scriptEngines;
@Inject public ScriptService(Settings settings) {
super(settings);
private final ConcurrentMap<String, CompiledScript> cache = new MapMaker().softValues().makeMap();
parserContext = new ParserContext();
parserContext.addPackageImport("java.util");
parserContext.addPackageImport("org.elasticsearch.util.gnu.trove");
parserContext.addPackageImport("org.elasticsearch.common.joda");
parserContext.addImport("time", MVEL.getStaticMethod(System.class, "currentTimeMillis", new Class[0]));
// unboxed version of Math, better performance since conversion from boxed to unboxed my mvel is not needed
for (Method m : UnboxedMathUtils.class.getMethods()) {
if ((m.getModifiers() & Modifier.STATIC) > 0) {
parserContext.addImport(m.getName(), m);
}
}
public ScriptService(Settings settings) {
this(settings, ImmutableSet.<ScriptEngineService>builder()
.add(new MvelScriptEngineService(settings))
.build()
);
}
public Object compile(String script) {
Object compiled = cache.get(script);
@Inject public ScriptService(Settings settings, Set<ScriptEngineService> scriptEngines) {
super(settings);
this.defaultType = componentSettings.get("default_type", "mvel");
ImmutableMap.Builder<String, ScriptEngineService> builder = ImmutableMap.builder();
for (ScriptEngineService scriptEngine : scriptEngines) {
builder.put(scriptEngine.type(), scriptEngine);
}
this.scriptEngines = builder.build();
}
public CompiledScript compile(String script) {
return compile(defaultType, script);
}
public CompiledScript compile(String type, String script) {
CompiledScript compiled = cache.get(script);
if (compiled != null) {
return compiled;
}
@ -67,17 +76,22 @@ public class ScriptService extends AbstractComponent {
if (compiled != null) {
return compiled;
}
compiled = MVEL.compileExpression(script, parserContext);
ScriptEngineService service = scriptEngines.get(type);
if (service == null) {
throw new ElasticSearchIllegalArgumentException("script_type not supported [" + type + "]");
}
compiled = new CompiledScript(type, service.compile(script));
cache.put(script, compiled);
}
return compiled;
}
public Object execute(Object script, Map vars) {
return MVEL.executeExpression(script, vars);
public Object execute(CompiledScript compiledScript, Map vars) {
return scriptEngines.get(compiledScript.type()).execute(compiledScript.compiled(), vars);
}
public void clear() {
cache.clear();
}
}

View File

@ -0,0 +1,68 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.script.mvel;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.math.UnboxedMathUtils;
import org.elasticsearch.common.mvel2.MVEL;
import org.elasticsearch.common.mvel2.ParserContext;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.script.ScriptEngineService;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
/**
* @author kimchy (shay.banon)
*/
public class MvelScriptEngineService extends AbstractComponent implements ScriptEngineService {
private final ParserContext parserContext;
@Inject public MvelScriptEngineService(Settings settings) {
super(settings);
parserContext = new ParserContext();
parserContext.addPackageImport("java.util");
parserContext.addPackageImport("org.elasticsearch.common.trove");
parserContext.addPackageImport("org.elasticsearch.common.joda");
parserContext.addImport("time", MVEL.getStaticMethod(System.class, "currentTimeMillis", new Class[0]));
// unboxed version of Math, better performance since conversion from boxed to unboxed my mvel is not needed
for (Method m : UnboxedMathUtils.class.getMethods()) {
if ((m.getModifiers() & Modifier.STATIC) > 0) {
parserContext.addImport(m.getName(), m);
}
}
}
@Override public String type() {
return "mvel";
}
@Override public Object compile(String script) {
return MVEL.compileExpression(script, parserContext);
}
@Override public Object execute(Object compiledScript, Map vars) {
return MVEL.executeExpression(compiledScript, vars);
}
}