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:
parent
2d1d6503a4
commit
818920a281
|
@ -274,7 +274,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
|
|||
}
|
||||
if (documents.isEmpty() == false) {
|
||||
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
|
||||
documentXContentType = XContentType.readFrom(in);
|
||||
documentXContentType = in.readEnum(XContentType.class);
|
||||
} else {
|
||||
documentXContentType = XContentFactory.xContentType(documents.iterator().next());
|
||||
}
|
||||
|
@ -331,7 +331,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
|
|||
out.writeOptionalBytesReference(doc);
|
||||
}
|
||||
if (documents.isEmpty() == false && out.getVersion().onOrAfter(Version.V_5_3_0)) {
|
||||
documentXContentType.writeTo(out);
|
||||
out.writeEnum(documentXContentType);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -123,7 +123,7 @@ public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptR
|
|||
id = in.readOptionalString();
|
||||
content = in.readBytesReference();
|
||||
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
|
||||
xContentType = XContentType.readFrom(in);
|
||||
xContentType = in.readEnum(XContentType.class);
|
||||
} else {
|
||||
xContentType = XContentFactory.xContentType(content);
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptR
|
|||
out.writeOptionalString(id);
|
||||
out.writeBytesReference(content);
|
||||
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)) {
|
||||
out.writeOptionalString(context);
|
||||
|
|
|
@ -542,7 +542,11 @@ public class IndexRequest extends ReplicatedWriteRequest<IndexRequest> implement
|
|||
pipeline = in.readOptionalString();
|
||||
isRetry = in.readBoolean();
|
||||
autoGeneratedTimestamp = in.readLong();
|
||||
contentType = in.readOptionalWriteable(XContentType::readFrom);
|
||||
if (in.readBoolean()) {
|
||||
contentType = in.readEnum(XContentType.class);
|
||||
} else {
|
||||
contentType = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -566,7 +570,12 @@ public class IndexRequest extends ReplicatedWriteRequest<IndexRequest> implement
|
|||
out.writeOptionalString(pipeline);
|
||||
out.writeBoolean(isRetry);
|
||||
out.writeLong(autoGeneratedTimestamp);
|
||||
out.writeOptionalWriteable(contentType);
|
||||
if (contentType != null) {
|
||||
out.writeBoolean(true);
|
||||
out.writeEnum(contentType);
|
||||
} else {
|
||||
out.writeBoolean(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -81,7 +81,7 @@ public class PutPipelineRequest extends AcknowledgedRequest<PutPipelineRequest>
|
|||
id = in.readString();
|
||||
source = in.readBytesReference();
|
||||
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
|
||||
xContentType = XContentType.readFrom(in);
|
||||
xContentType = in.readEnum(XContentType.class);
|
||||
} else {
|
||||
xContentType = XContentFactory.xContentType(source);
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ public class PutPipelineRequest extends AcknowledgedRequest<PutPipelineRequest>
|
|||
out.writeString(id);
|
||||
out.writeBytesReference(source);
|
||||
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
|
||||
xContentType.writeTo(out);
|
||||
out.writeEnum(xContentType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,7 +76,7 @@ public class SimulatePipelineRequest extends ActionRequest {
|
|||
verbose = in.readBoolean();
|
||||
source = in.readBytesReference();
|
||||
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
|
||||
xContentType = XContentType.readFrom(in);
|
||||
xContentType = in.readEnum(XContentType.class);
|
||||
} else {
|
||||
xContentType = XContentFactory.xContentType(source);
|
||||
}
|
||||
|
@ -123,7 +123,7 @@ public class SimulatePipelineRequest extends ActionRequest {
|
|||
out.writeBoolean(verbose);
|
||||
out.writeBytesReference(source);
|
||||
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
|
||||
xContentType.writeTo(out);
|
||||
out.writeEnum(xContentType);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -516,7 +516,7 @@ public class TermVectorsRequest extends SingleShardRequest<TermVectorsRequest> i
|
|||
if (in.readBoolean()) {
|
||||
doc = in.readBytesReference();
|
||||
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
|
||||
xContentType = XContentType.readFrom(in);
|
||||
xContentType = in.readEnum(XContentType.class);
|
||||
} else {
|
||||
xContentType = XContentFactory.xContentType(doc);
|
||||
}
|
||||
|
@ -561,7 +561,7 @@ public class TermVectorsRequest extends SingleShardRequest<TermVectorsRequest> i
|
|||
if (doc != null) {
|
||||
out.writeBytesReference(doc);
|
||||
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
|
||||
xContentType.writeTo(out);
|
||||
out.writeEnum(xContentType);
|
||||
}
|
||||
}
|
||||
out.writeOptionalString(routing);
|
||||
|
|
|
@ -19,8 +19,7 @@
|
|||
|
||||
package org.elasticsearch.common.xcontent;
|
||||
|
||||
import org.elasticsearch.common.lease.Releasable;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.nio.CharBuffer;
|
||||
import java.util.List;
|
||||
|
@ -37,7 +36,7 @@ import java.util.Map;
|
|||
* NamedXContentRegistry.EMPTY, ParserField."{\"key\" : \"value\"}");
|
||||
* </pre>
|
||||
*/
|
||||
public interface XContentParser extends Releasable {
|
||||
public interface XContentParser extends Closeable {
|
||||
|
||||
enum Token {
|
||||
START_OBJECT {
|
||||
|
|
|
@ -19,22 +19,18 @@
|
|||
|
||||
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.json.JsonXContent;
|
||||
import org.elasticsearch.common.xcontent.smile.SmileXContent;
|
||||
import org.elasticsearch.common.xcontent.yaml.YamlXContent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* The content type of {@link org.elasticsearch.common.xcontent.XContent}.
|
||||
*/
|
||||
public enum XContentType implements Writeable {
|
||||
public enum XContentType {
|
||||
|
||||
/**
|
||||
* A JSON based content type.
|
||||
|
@ -183,18 +179,4 @@ public enum XContentType implements Writeable {
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -221,7 +221,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
|
|||
if (in.readBoolean()) {
|
||||
doc = (BytesReference) in.readGenericValue();
|
||||
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
|
||||
xContentType = XContentType.readFrom(in);
|
||||
xContentType = in.readEnum(XContentType.class);
|
||||
} else {
|
||||
xContentType = XContentFactory.xContentType(doc);
|
||||
}
|
||||
|
@ -243,7 +243,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
|
|||
if (doc != null) {
|
||||
out.writeGenericValue(doc);
|
||||
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
|
||||
xContentType.writeTo(out);
|
||||
out.writeEnum(xContentType);
|
||||
}
|
||||
} else {
|
||||
out.writeString(id);
|
||||
|
|
|
@ -120,7 +120,7 @@ public final class PipelineConfiguration extends AbstractDiffable<PipelineConfig
|
|||
|
||||
public static PipelineConfiguration readFrom(StreamInput in) throws IOException {
|
||||
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 {
|
||||
final String id = in.readString();
|
||||
final BytesReference config = in.readBytesReference();
|
||||
|
@ -137,7 +137,7 @@ public final class PipelineConfiguration extends AbstractDiffable<PipelineConfig
|
|||
out.writeString(id);
|
||||
out.writeBytesReference(config);
|
||||
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
|
||||
xContentType.writeTo(out);
|
||||
out.writeEnum(xContentType);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -507,7 +507,7 @@ public final class Script implements ToXContentObject, Writeable {
|
|||
|
||||
if (in.readBoolean()) {
|
||||
this.options = new HashMap<>();
|
||||
XContentType contentType = XContentType.readFrom(in);
|
||||
XContentType contentType = in.readEnum(XContentType.class);
|
||||
this.options.put(CONTENT_TYPE_OPTION, contentType.mediaType());
|
||||
} else if (type == ScriptType.INLINE) {
|
||||
options = new HashMap<>();
|
||||
|
@ -571,7 +571,7 @@ public final class Script implements ToXContentObject, Writeable {
|
|||
if (options != null && options.containsKey(CONTENT_TYPE_OPTION)) {
|
||||
XContentType contentType = XContentType.fromMediaTypeOrFormat(options.get(CONTENT_TYPE_OPTION));
|
||||
out.writeBoolean(true);
|
||||
contentType.writeTo(out);
|
||||
out.writeEnum(contentType);
|
||||
} else {
|
||||
out.writeBoolean(false);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue