Makes Script type writeable

Used to be Streamable. Left-over of the PROTOTYPE related refactoring by
@nik9000

Closes #17753
This commit is contained in:
Isabel Drost-Fromm 2016-04-21 12:40:29 +02:00
parent a6c0a813b5
commit 233fe86ee4
18 changed files with 42 additions and 74 deletions

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

@ -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<Script> {
/**
* 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.
@ -149,7 +132,11 @@ public class Script implements ToXContent, Streamable {
}
@Override
public final void readFrom(StreamInput in) throws IOException {
public final Script readFrom(StreamInput in) throws IOException {
return new Script(in);
}
public Script(StreamInput in) throws IOException {
script = in.readString();
if (in.readBoolean()) {
type = ScriptType.readFrom(in);
@ -158,11 +145,6 @@ public class Script implements ToXContent, Streamable {
if (in.readBoolean()) {
params = in.readMap();
}
doReadFrom(in);
}
protected void doReadFrom(StreamInput in) throws IOException {
// For sub-classes to Override
}
@Override
@ -181,10 +163,8 @@ public class Script implements ToXContent, Streamable {
}
doWriteTo(out);
}
protected void doWriteTo(StreamOutput out) throws IOException {
// For sub-classes to Override
}
protected void doWriteTo(StreamOutput out) throws IOException {};
@Override
public final XContentBuilder toXContent(XContentBuilder builder, Params builderParams) throws IOException {
@ -210,12 +190,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,9 +40,12 @@ public class Template extends Script {
public static final String DEFAULT_LANG = "mustache";
private XContentType contentType;
public Template() {
super();
public Template(StreamInput in) throws IOException {
super(in);
if (in.readBoolean()) {
this.contentType = XContentType.readFrom(in);
}
}
/**
@ -88,13 +91,6 @@ public class Template extends Script {
return contentType;
}
@Override
protected void doReadFrom(StreamInput in) throws IOException {
if (in.readBoolean()) {
this.contentType = XContentType.readFrom(in);
}
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
boolean hasContentType = contentType != null;
@ -116,9 +112,7 @@ public class Template extends Script {
}
public static Template readTemplate(StreamInput in) throws IOException {
Template template = new Template();
template.readFrom(in);
return template;
return new Template(in);
}
public static Script parse(Map<String, Object> config, boolean removeMatchedEntries, ParseFieldMatcher 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

@ -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