Merge pull request #17908 from MaineC/enhancement/make-script-writeable

Makes Script type writeable
This commit is contained in:
Isabel Drost-Fromm 2016-04-22 08:39:01 +02:00
commit 1939425378
21 changed files with 73 additions and 114 deletions

View File

@ -63,7 +63,7 @@ public class RenderSearchTemplateRequest extends ActionRequest<RenderSearchTempl
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
if (in.readBoolean()) {
template = Template.readTemplate(in);
template = new Template(in);
}
}
}

View File

@ -326,7 +326,7 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic
indicesOptions = IndicesOptions.readIndicesOptions(in);
requestCache = in.readOptionalBoolean();
template = in.readOptionalStreamable(Template::new);
template = in.readOptionalWriteable(Template::new);
}
@Override
@ -357,6 +357,6 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic
out.writeStringArray(types);
indicesOptions.writeIndicesOptions(out);
out.writeOptionalBoolean(requestCache);
out.writeOptionalStreamable(template);
out.writeOptionalWriteable(template);
}
}

View File

@ -727,7 +727,7 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
routing = in.readOptionalString();
parent = in.readOptionalString();
if (in.readBoolean()) {
script = Script.readScript(in);
script = new Script(in);
}
retryOnConflict = in.readVInt();
refresh = in.readBoolean();

View File

@ -68,7 +68,7 @@ public class ScriptQueryBuilder extends AbstractQueryBuilder<ScriptQueryBuilder>
*/
public ScriptQueryBuilder(StreamInput in) throws IOException {
super(in);
script = Script.readScript(in);
script = new Script(in);
}
@Override

View File

@ -102,7 +102,7 @@ public class TemplateQueryBuilder extends AbstractQueryBuilder<TemplateQueryBuil
*/
public TemplateQueryBuilder(StreamInput in) throws IOException {
super(in);
template = Template.readTemplate(in);
template = new Template(in);
}
@Override

View File

@ -65,7 +65,7 @@ public class ScriptScoreFunctionBuilder extends ScoreFunctionBuilder<ScriptScore
*/
public ScriptScoreFunctionBuilder(StreamInput in) throws IOException {
super(in);
script = Script.readScript(in);
script = new Script(in);
}
@Override

View File

@ -25,7 +25,7 @@ import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.logging.LoggerMessageFormat;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -34,23 +34,12 @@ import org.elasticsearch.script.ScriptService.ScriptType;
import java.io.IOException;
import java.util.Map;
import java.util.function.Supplier;
/**
* Script holds all the parameters necessary to compile or find in cache and then execute a script.
*/
public class Script implements ToXContent, Streamable {
public class Script implements ToXContent, Writeable {
/**
* A {@link Supplier} implementation for use when reading a {@link Script}
* using {@link StreamInput#readOptionalStreamable(Supplier)}
*/
public static final Supplier<Script> SUPPLIER = new Supplier<Script>() {
@Override
public Script get() {
return new Script();
}
};
public static final ScriptType DEFAULT_TYPE = ScriptType.INLINE;
private static final ScriptParser PARSER = new ScriptParser();
@ -59,12 +48,6 @@ public class Script implements ToXContent, Streamable {
private @Nullable String lang;
private @Nullable Map<String, Object> params;
/**
* For Serialization
*/
Script() {
}
/**
* Constructor for simple inline script. The script will have no lang or
* params set.
@ -111,6 +94,36 @@ public class Script implements ToXContent, Streamable {
this.params = (Map<String, Object>)params;
}
public Script(StreamInput in) throws IOException {
script = in.readString();
if (in.readBoolean()) {
type = ScriptType.readFrom(in);
}
lang = in.readOptionalString();
if (in.readBoolean()) {
params = in.readMap();
}
}
@Override
public final void writeTo(StreamOutput out) throws IOException {
out.writeString(script);
boolean hasType = type != null;
out.writeBoolean(hasType);
if (hasType) {
ScriptType.writeTo(type, out);
}
out.writeOptionalString(lang);
boolean hasParams = params != null;
out.writeBoolean(hasParams);
if (hasParams) {
out.writeMap(params);
}
doWriteTo(out);
}
protected void doWriteTo(StreamOutput out) throws IOException {};
/**
* Method for getting the script.
* @return The cache key of the script to be compiled/executed. For dynamic scripts this is the actual
@ -148,44 +161,6 @@ public class Script implements ToXContent, Streamable {
return params;
}
@Override
public final void readFrom(StreamInput in) throws IOException {
script = in.readString();
if (in.readBoolean()) {
type = ScriptType.readFrom(in);
}
lang = in.readOptionalString();
if (in.readBoolean()) {
params = in.readMap();
}
doReadFrom(in);
}
protected void doReadFrom(StreamInput in) throws IOException {
// For sub-classes to Override
}
@Override
public final void writeTo(StreamOutput out) throws IOException {
out.writeString(script);
boolean hasType = type != null;
out.writeBoolean(hasType);
if (hasType) {
ScriptType.writeTo(type, out);
}
out.writeOptionalString(lang);
boolean hasParams = params != null;
out.writeBoolean(hasParams);
if (hasParams) {
out.writeMap(params);
}
doWriteTo(out);
}
protected void doWriteTo(StreamOutput out) throws IOException {
// For sub-classes to Override
}
@Override
public final XContentBuilder toXContent(XContentBuilder builder, Params builderParams) throws IOException {
if (type == null) {
@ -210,12 +185,6 @@ public class Script implements ToXContent, Streamable {
return builder;
}
public static Script readScript(StreamInput in) throws IOException {
Script script = new Script();
script.readFrom(in);
return script;
}
public static Script parse(Map<String, Object> config, boolean removeMatchedEntries, ParseFieldMatcher parseFieldMatcher) {
return PARSER.parse(config, removeMatchedEntries, parseFieldMatcher);
}

View File

@ -40,11 +40,7 @@ public class Template extends Script {
public static final String DEFAULT_LANG = "mustache";
private XContentType contentType;
public Template() {
super();
}
/**
* Constructor for simple inline template. The template will have no lang,
* content type or params set.
@ -79,17 +75,8 @@ public class Template extends Script {
this.contentType = xContentType;
}
/**
* Method for getting the {@link XContentType} of the template.
*
* @return The {@link XContentType} of the template.
*/
public XContentType getContentType() {
return contentType;
}
@Override
protected void doReadFrom(StreamInput in) throws IOException {
public Template(StreamInput in) throws IOException {
super(in);
if (in.readBoolean()) {
this.contentType = XContentType.readFrom(in);
}
@ -104,6 +91,15 @@ public class Template extends Script {
}
}
/**
* Method for getting the {@link XContentType} of the template.
*
* @return The {@link XContentType} of the template.
*/
public XContentType getContentType() {
return contentType;
}
@Override
protected XContentBuilder scriptFieldToXContent(String template, ScriptType type, XContentBuilder builder, Params builderParams)
throws IOException {
@ -115,12 +111,6 @@ public class Template extends Script {
return builder;
}
public static Template readTemplate(StreamInput in) throws IOException {
Template template = new Template();
template.readFrom(in);
return template;
}
public static Script parse(Map<String, Object> config, boolean removeMatchedEntries, ParseFieldMatcher parseFieldMatcher) {
return new TemplateParser(Collections.emptyMap(), DEFAULT_LANG).parse(config, removeMatchedEntries, parseFieldMatcher);
}

View File

@ -68,7 +68,7 @@ public class ScriptHeuristic extends SignificanceHeuristic {
* Read from a stream.
*/
public ScriptHeuristic(StreamInput in) throws IOException {
this(Script.readScript(in));
this(new Script(in));
}
@Override

View File

@ -121,7 +121,7 @@ public class InternalScriptedMetric extends InternalMetricsAggregation implement
@Override
protected void doReadFrom(StreamInput in) throws IOException {
if (in.readBoolean()) {
reduceScript = Script.readScript(in);
reduceScript = new Script(in);
}
aggregation = in.readGenericValue();
}

View File

@ -67,10 +67,10 @@ public class ScriptedMetricAggregatorBuilder extends AggregatorBuilder<ScriptedM
*/
public ScriptedMetricAggregatorBuilder(StreamInput in) throws IOException {
super(in, InternalScriptedMetric.TYPE);
initScript = in.readOptionalStreamable(Script.SUPPLIER);
mapScript = in.readOptionalStreamable(Script.SUPPLIER);
combineScript = in.readOptionalStreamable(Script.SUPPLIER);
reduceScript = in.readOptionalStreamable(Script.SUPPLIER);
initScript = in.readOptionalWriteable(Script::new);
mapScript = in.readOptionalWriteable(Script::new);
combineScript = in.readOptionalWriteable(Script::new);
reduceScript = in.readOptionalWriteable(Script::new);
if (in.readBoolean()) {
params = in.readMap();
}
@ -78,10 +78,10 @@ public class ScriptedMetricAggregatorBuilder extends AggregatorBuilder<ScriptedM
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeOptionalStreamable(initScript);
out.writeOptionalStreamable(mapScript);
out.writeOptionalStreamable(combineScript);
out.writeOptionalStreamable(reduceScript);
out.writeOptionalWriteable(initScript);
out.writeOptionalWriteable(mapScript);
out.writeOptionalWriteable(combineScript);
out.writeOptionalWriteable(reduceScript);
boolean hasParams = params != null;
out.writeBoolean(hasParams);
if (hasParams) {

View File

@ -147,7 +147,7 @@ public class BucketScriptPipelineAggregator extends PipelineAggregator {
@SuppressWarnings("unchecked")
@Override
protected void doReadFrom(StreamInput in) throws IOException {
script = Script.readScript(in);
script = new Script(in);
formatter = in.readNamedWriteable(DocValueFormat.class);
gapPolicy = GapPolicy.readFrom(in);
bucketsPathsMap = (Map<String, String>) in.readGenericValue();

View File

@ -76,7 +76,7 @@ public class BucketScriptPipelineAggregatorBuilder extends PipelineAggregatorBui
for (int i = 0; i < mapSize; i++) {
bucketsPathsMap.put(in.readString(), in.readString());
}
script = Script.readScript(in);
script = new Script(in);
format = in.readOptionalString();
gapPolicy = GapPolicy.readFrom(in);
}

View File

@ -129,7 +129,7 @@ public class BucketSelectorPipelineAggregator extends PipelineAggregator {
@SuppressWarnings("unchecked")
@Override
protected void doReadFrom(StreamInput in) throws IOException {
script = Script.readScript(in);
script = new Script(in);
gapPolicy = GapPolicy.readFrom(in);
bucketsPathsMap = (Map<String, String>) in.readGenericValue();
}

View File

@ -73,7 +73,7 @@ public class BucketSelectorPipelineAggregatorBuilder extends PipelineAggregatorB
for (int i = 0; i < mapSize; i++) {
bucketsPathsMap.put(in.readString(), in.readString());
}
script = Script.readScript(in);
script = new Script(in);
gapPolicy = GapPolicy.readFrom(in);
}

View File

@ -126,7 +126,7 @@ public abstract class ValuesSourceAggregatorBuilder<VS extends ValuesSource, AB
private void read(StreamInput in) throws IOException {
field = in.readOptionalString();
if (in.readBoolean()) {
script = Script.readScript(in);
script = new Script(in);
}
if (in.readBoolean()) {
valueType = ValueType.readFromStream(in);

View File

@ -1281,7 +1281,7 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ
*/
public ScriptField(StreamInput in) throws IOException {
fieldName = in.readString();
script = Script.readScript(in);
script = new Script(in);
ignoreFailure = in.readBoolean();
}

View File

@ -183,7 +183,7 @@ public class ShardSearchLocalRequest implements ShardSearchRequest {
types = in.readStringArray();
filteringAliases = in.readStringArray();
nowInMillis = in.readVLong();
template = in.readOptionalStreamable(Template::new);
template = in.readOptionalWriteable(Template::new);
requestCache = in.readOptionalBoolean();
}
@ -212,7 +212,7 @@ public class ShardSearchLocalRequest implements ShardSearchRequest {
out.writeVLong(nowInMillis);
}
out.writeOptionalStreamable(template);
out.writeOptionalWriteable(template);
out.writeOptionalBoolean(requestCache);
}

View File

@ -113,7 +113,7 @@ public class ScriptSortBuilder extends SortBuilder<ScriptSortBuilder> {
* Read from a stream.
*/
public ScriptSortBuilder(StreamInput in) throws IOException {
script = Script.readScript(in);
script = new Script(in);
type = ScriptSortType.readFromStream(in);
order = SortOrder.readFromStream(in);
sortMode = in.readOptionalWriteable(SortMode::readFromStream);

View File

@ -135,7 +135,7 @@ public class PhraseSuggestionBuilder extends SuggestionBuilder<PhraseSuggestionB
postTag = in.readOptionalString();
separator = in.readString();
if (in.readBoolean()) {
collateQuery = Template.readTemplate(in);
collateQuery = new Template(in);
}
collateParams = in.readMap();
collatePrune = in.readOptionalBoolean();

View File

@ -60,14 +60,14 @@ public abstract class AbstractBulkIndexByScrollRequest<Self extends AbstractBulk
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
if (in.readBoolean()) {
script = Script.readScript(in);
script = new Script(in);
}
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeOptionalStreamable(script);
out.writeOptionalWriteable(script);
}
@Override