Decouple XContentType from StreamInput/Output (#28927)

This removes the readFrom and writeTo methods from XContentType, instead using
the more generic `readEnum` and `writeEnum` methods. Luckily they are both
encoded exactly the same way, so there is no compatibility layer needed for
backwards compatibility.

Relates to #28504
This commit is contained in:
Lee Hinman 2018-03-07 14:50:30 -07:00 committed by GitHub
parent 2d1d6503a4
commit 818920a281
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 30 additions and 40 deletions

View File

@ -274,7 +274,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
} }
if (documents.isEmpty() == false) { if (documents.isEmpty() == false) {
if (in.getVersion().onOrAfter(Version.V_5_3_0)) { if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
documentXContentType = XContentType.readFrom(in); documentXContentType = in.readEnum(XContentType.class);
} else { } else {
documentXContentType = XContentFactory.xContentType(documents.iterator().next()); documentXContentType = XContentFactory.xContentType(documents.iterator().next());
} }
@ -331,7 +331,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
out.writeOptionalBytesReference(doc); out.writeOptionalBytesReference(doc);
} }
if (documents.isEmpty() == false && out.getVersion().onOrAfter(Version.V_5_3_0)) { if (documents.isEmpty() == false && out.getVersion().onOrAfter(Version.V_5_3_0)) {
documentXContentType.writeTo(out); out.writeEnum(documentXContentType);
} }
} }

View File

@ -123,7 +123,7 @@ public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptR
id = in.readOptionalString(); id = in.readOptionalString();
content = in.readBytesReference(); content = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) { if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType = XContentType.readFrom(in); xContentType = in.readEnum(XContentType.class);
} else { } else {
xContentType = XContentFactory.xContentType(content); xContentType = XContentFactory.xContentType(content);
} }
@ -145,7 +145,7 @@ public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptR
out.writeOptionalString(id); out.writeOptionalString(id);
out.writeBytesReference(content); out.writeBytesReference(content);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) { if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out); out.writeEnum(xContentType);
} }
if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha2)) { if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha2)) {
out.writeOptionalString(context); out.writeOptionalString(context);

View File

@ -542,7 +542,11 @@ public class IndexRequest extends ReplicatedWriteRequest<IndexRequest> implement
pipeline = in.readOptionalString(); pipeline = in.readOptionalString();
isRetry = in.readBoolean(); isRetry = in.readBoolean();
autoGeneratedTimestamp = in.readLong(); autoGeneratedTimestamp = in.readLong();
contentType = in.readOptionalWriteable(XContentType::readFrom); if (in.readBoolean()) {
contentType = in.readEnum(XContentType.class);
} else {
contentType = null;
}
} }
@Override @Override
@ -566,7 +570,12 @@ public class IndexRequest extends ReplicatedWriteRequest<IndexRequest> implement
out.writeOptionalString(pipeline); out.writeOptionalString(pipeline);
out.writeBoolean(isRetry); out.writeBoolean(isRetry);
out.writeLong(autoGeneratedTimestamp); out.writeLong(autoGeneratedTimestamp);
out.writeOptionalWriteable(contentType); if (contentType != null) {
out.writeBoolean(true);
out.writeEnum(contentType);
} else {
out.writeBoolean(false);
}
} }
@Override @Override

View File

@ -81,7 +81,7 @@ public class PutPipelineRequest extends AcknowledgedRequest<PutPipelineRequest>
id = in.readString(); id = in.readString();
source = in.readBytesReference(); source = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) { if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType = XContentType.readFrom(in); xContentType = in.readEnum(XContentType.class);
} else { } else {
xContentType = XContentFactory.xContentType(source); xContentType = XContentFactory.xContentType(source);
} }
@ -93,7 +93,7 @@ public class PutPipelineRequest extends AcknowledgedRequest<PutPipelineRequest>
out.writeString(id); out.writeString(id);
out.writeBytesReference(source); out.writeBytesReference(source);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) { if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out); out.writeEnum(xContentType);
} }
} }
} }

View File

@ -76,7 +76,7 @@ public class SimulatePipelineRequest extends ActionRequest {
verbose = in.readBoolean(); verbose = in.readBoolean();
source = in.readBytesReference(); source = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) { if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType = XContentType.readFrom(in); xContentType = in.readEnum(XContentType.class);
} else { } else {
xContentType = XContentFactory.xContentType(source); xContentType = XContentFactory.xContentType(source);
} }
@ -123,7 +123,7 @@ public class SimulatePipelineRequest extends ActionRequest {
out.writeBoolean(verbose); out.writeBoolean(verbose);
out.writeBytesReference(source); out.writeBytesReference(source);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) { if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out); out.writeEnum(xContentType);
} }
} }

View File

@ -516,7 +516,7 @@ public class TermVectorsRequest extends SingleShardRequest<TermVectorsRequest> i
if (in.readBoolean()) { if (in.readBoolean()) {
doc = in.readBytesReference(); doc = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) { if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType = XContentType.readFrom(in); xContentType = in.readEnum(XContentType.class);
} else { } else {
xContentType = XContentFactory.xContentType(doc); xContentType = XContentFactory.xContentType(doc);
} }
@ -561,7 +561,7 @@ public class TermVectorsRequest extends SingleShardRequest<TermVectorsRequest> i
if (doc != null) { if (doc != null) {
out.writeBytesReference(doc); out.writeBytesReference(doc);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) { if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out); out.writeEnum(xContentType);
} }
} }
out.writeOptionalString(routing); out.writeOptionalString(routing);

View File

@ -19,8 +19,7 @@
package org.elasticsearch.common.xcontent; package org.elasticsearch.common.xcontent;
import org.elasticsearch.common.lease.Releasable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.util.List; import java.util.List;
@ -37,7 +36,7 @@ import java.util.Map;
* NamedXContentRegistry.EMPTY, ParserField."{\"key\" : \"value\"}"); * NamedXContentRegistry.EMPTY, ParserField."{\"key\" : \"value\"}");
* </pre> * </pre>
*/ */
public interface XContentParser extends Releasable { public interface XContentParser extends Closeable {
enum Token { enum Token {
START_OBJECT { START_OBJECT {

View File

@ -19,22 +19,18 @@
package org.elasticsearch.common.xcontent; package org.elasticsearch.common.xcontent;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.cbor.CborXContent; import org.elasticsearch.common.xcontent.cbor.CborXContent;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.common.xcontent.smile.SmileXContent; import org.elasticsearch.common.xcontent.smile.SmileXContent;
import org.elasticsearch.common.xcontent.yaml.YamlXContent; import org.elasticsearch.common.xcontent.yaml.YamlXContent;
import java.io.IOException;
import java.util.Locale; import java.util.Locale;
import java.util.Objects; import java.util.Objects;
/** /**
* The content type of {@link org.elasticsearch.common.xcontent.XContent}. * The content type of {@link org.elasticsearch.common.xcontent.XContent}.
*/ */
public enum XContentType implements Writeable { public enum XContentType {
/** /**
* A JSON based content type. * A JSON based content type.
@ -183,18 +179,4 @@ public enum XContentType implements Writeable {
public abstract String mediaTypeWithoutParameters(); public abstract String mediaTypeWithoutParameters();
public static XContentType readFrom(StreamInput in) throws IOException {
int index = in.readVInt();
for (XContentType contentType : values()) {
if (index == contentType.index) {
return contentType;
}
}
throw new IllegalStateException("Unknown XContentType with index [" + index + "]");
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(index);
}
} }

View File

@ -221,7 +221,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
if (in.readBoolean()) { if (in.readBoolean()) {
doc = (BytesReference) in.readGenericValue(); doc = (BytesReference) in.readGenericValue();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) { if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType = XContentType.readFrom(in); xContentType = in.readEnum(XContentType.class);
} else { } else {
xContentType = XContentFactory.xContentType(doc); xContentType = XContentFactory.xContentType(doc);
} }
@ -243,7 +243,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
if (doc != null) { if (doc != null) {
out.writeGenericValue(doc); out.writeGenericValue(doc);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) { if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out); out.writeEnum(xContentType);
} }
} else { } else {
out.writeString(id); out.writeString(id);

View File

@ -120,7 +120,7 @@ public final class PipelineConfiguration extends AbstractDiffable<PipelineConfig
public static PipelineConfiguration readFrom(StreamInput in) throws IOException { public static PipelineConfiguration readFrom(StreamInput in) throws IOException {
if (in.getVersion().onOrAfter(Version.V_5_3_0)) { if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
return new PipelineConfiguration(in.readString(), in.readBytesReference(), XContentType.readFrom(in)); return new PipelineConfiguration(in.readString(), in.readBytesReference(), in.readEnum(XContentType.class));
} else { } else {
final String id = in.readString(); final String id = in.readString();
final BytesReference config = in.readBytesReference(); final BytesReference config = in.readBytesReference();
@ -137,7 +137,7 @@ public final class PipelineConfiguration extends AbstractDiffable<PipelineConfig
out.writeString(id); out.writeString(id);
out.writeBytesReference(config); out.writeBytesReference(config);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) { if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out); out.writeEnum(xContentType);
} }
} }

View File

@ -507,7 +507,7 @@ public final class Script implements ToXContentObject, Writeable {
if (in.readBoolean()) { if (in.readBoolean()) {
this.options = new HashMap<>(); this.options = new HashMap<>();
XContentType contentType = XContentType.readFrom(in); XContentType contentType = in.readEnum(XContentType.class);
this.options.put(CONTENT_TYPE_OPTION, contentType.mediaType()); this.options.put(CONTENT_TYPE_OPTION, contentType.mediaType());
} else if (type == ScriptType.INLINE) { } else if (type == ScriptType.INLINE) {
options = new HashMap<>(); options = new HashMap<>();
@ -571,7 +571,7 @@ public final class Script implements ToXContentObject, Writeable {
if (options != null && options.containsKey(CONTENT_TYPE_OPTION)) { if (options != null && options.containsKey(CONTENT_TYPE_OPTION)) {
XContentType contentType = XContentType.fromMediaTypeOrFormat(options.get(CONTENT_TYPE_OPTION)); XContentType contentType = XContentType.fromMediaTypeOrFormat(options.get(CONTENT_TYPE_OPTION));
out.writeBoolean(true); out.writeBoolean(true);
contentType.writeTo(out); out.writeEnum(contentType);
} else { } else {
out.writeBoolean(false); out.writeBoolean(false);
} }