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

View File

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

View File

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

View File

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

View File

@ -25,7 +25,7 @@ import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; 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.logging.LoggerMessageFormat;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -34,23 +34,12 @@ import org.elasticsearch.script.ScriptService.ScriptType;
import java.io.IOException; import java.io.IOException;
import java.util.Map; 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. * 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; public static final ScriptType DEFAULT_TYPE = ScriptType.INLINE;
private static final ScriptParser PARSER = new ScriptParser(); private static final ScriptParser PARSER = new ScriptParser();
@ -59,12 +48,6 @@ public class Script implements ToXContent, Streamable {
private @Nullable String lang; private @Nullable String lang;
private @Nullable Map<String, Object> params; private @Nullable Map<String, Object> params;
/**
* For Serialization
*/
Script() {
}
/** /**
* Constructor for simple inline script. The script will have no lang or * Constructor for simple inline script. The script will have no lang or
* params set. * params set.
@ -149,7 +132,11 @@ public class Script implements ToXContent, Streamable {
} }
@Override @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(); script = in.readString();
if (in.readBoolean()) { if (in.readBoolean()) {
type = ScriptType.readFrom(in); type = ScriptType.readFrom(in);
@ -158,11 +145,6 @@ public class Script implements ToXContent, Streamable {
if (in.readBoolean()) { if (in.readBoolean()) {
params = in.readMap(); params = in.readMap();
} }
doReadFrom(in);
}
protected void doReadFrom(StreamInput in) throws IOException {
// For sub-classes to Override
} }
@Override @Override
@ -182,9 +164,7 @@ public class Script implements ToXContent, Streamable {
doWriteTo(out); doWriteTo(out);
} }
protected void doWriteTo(StreamOutput out) throws IOException { protected void doWriteTo(StreamOutput out) throws IOException {};
// For sub-classes to Override
}
@Override @Override
public final XContentBuilder toXContent(XContentBuilder builder, Params builderParams) throws IOException { public final XContentBuilder toXContent(XContentBuilder builder, Params builderParams) throws IOException {
@ -210,12 +190,6 @@ public class Script implements ToXContent, Streamable {
return builder; 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) { public static Script parse(Map<String, Object> config, boolean removeMatchedEntries, ParseFieldMatcher parseFieldMatcher) {
return PARSER.parse(config, removeMatchedEntries, parseFieldMatcher); return PARSER.parse(config, removeMatchedEntries, parseFieldMatcher);
} }

View File

@ -41,8 +41,11 @@ public class Template extends Script {
private XContentType contentType; private XContentType contentType;
public Template() { public Template(StreamInput in) throws IOException {
super(); super(in);
if (in.readBoolean()) {
this.contentType = XContentType.readFrom(in);
}
} }
/** /**
@ -88,13 +91,6 @@ public class Template extends Script {
return contentType; return contentType;
} }
@Override
protected void doReadFrom(StreamInput in) throws IOException {
if (in.readBoolean()) {
this.contentType = XContentType.readFrom(in);
}
}
@Override @Override
protected void doWriteTo(StreamOutput out) throws IOException { protected void doWriteTo(StreamOutput out) throws IOException {
boolean hasContentType = contentType != null; boolean hasContentType = contentType != null;
@ -116,9 +112,7 @@ public class Template extends Script {
} }
public static Template readTemplate(StreamInput in) throws IOException { public static Template readTemplate(StreamInput in) throws IOException {
Template template = new Template(); return new Template(in);
template.readFrom(in);
return template;
} }
public static Script parse(Map<String, Object> config, boolean removeMatchedEntries, ParseFieldMatcher parseFieldMatcher) { 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. * Read from a stream.
*/ */
public ScriptHeuristic(StreamInput in) throws IOException { public ScriptHeuristic(StreamInput in) throws IOException {
this(Script.readScript(in)); this(new Script(in));
} }
@Override @Override

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -73,7 +73,7 @@ public class BucketSelectorPipelineAggregatorBuilder extends PipelineAggregatorB
for (int i = 0; i < mapSize; i++) { for (int i = 0; i < mapSize; i++) {
bucketsPathsMap.put(in.readString(), in.readString()); bucketsPathsMap.put(in.readString(), in.readString());
} }
script = Script.readScript(in); script = new Script(in);
gapPolicy = GapPolicy.readFrom(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 { private void read(StreamInput in) throws IOException {
field = in.readOptionalString(); field = in.readOptionalString();
if (in.readBoolean()) { if (in.readBoolean()) {
script = Script.readScript(in); script = new Script(in);
} }
if (in.readBoolean()) { if (in.readBoolean()) {
valueType = ValueType.readFromStream(in); valueType = ValueType.readFromStream(in);

View File

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

View File

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

View File

@ -113,7 +113,7 @@ public class ScriptSortBuilder extends SortBuilder<ScriptSortBuilder> {
* Read from a stream. * Read from a stream.
*/ */
public ScriptSortBuilder(StreamInput in) throws IOException { public ScriptSortBuilder(StreamInput in) throws IOException {
script = Script.readScript(in); script = new Script(in);
type = ScriptSortType.readFromStream(in); type = ScriptSortType.readFromStream(in);
order = SortOrder.readFromStream(in); order = SortOrder.readFromStream(in);
sortMode = in.readOptionalWriteable(SortMode::readFromStream); 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 { public void readFrom(StreamInput in) throws IOException {
super.readFrom(in); super.readFrom(in);
if (in.readBoolean()) { if (in.readBoolean()) {
script = Script.readScript(in); script = new Script(in);
} }
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
out.writeOptionalStreamable(script); out.writeOptionalWriteable(script);
} }
@Override @Override