Skip to content

Commit

Permalink
Decouple XContentType from StreamInput/Output (elastic#28927)
Browse files Browse the repository at this point in the history
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 elastic#28504
  • Loading branch information
dakrone committed Mar 7, 2018
1 parent f13a9f7 commit 41a7a94
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 40 deletions.
Expand Up @@ -274,7 +274,7 @@ public PercolateQueryBuilder(String field, String documentType, String indexedDo
}
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());
}
Expand Down Expand Up @@ -331,7 +331,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
out.writeOptionalBytesReference(doc);
}
if (documents.isEmpty() == false && out.getVersion().onOrAfter(Version.V_5_3_0)) {
documentXContentType.writeTo(out);
out.writeEnum(documentXContentType);
}
}

Expand Down
Expand Up @@ -123,7 +123,7 @@ public void readFrom(StreamInput in) throws IOException {
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);
}
Expand All @@ -145,7 +145,7 @@ public void writeTo(StreamOutput out) throws IOException {
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);
Expand Down
Expand Up @@ -542,7 +542,11 @@ public void readFrom(StreamInput in) throws IOException {
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
Expand All @@ -566,7 +570,12 @@ public void writeTo(StreamOutput out) throws IOException {
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
Expand Down
Expand Up @@ -81,7 +81,7 @@ public void readFrom(StreamInput in) throws IOException {
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);
}
Expand All @@ -93,7 +93,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(id);
out.writeBytesReference(source);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out);
out.writeEnum(xContentType);
}
}
}
Expand Up @@ -76,7 +76,7 @@ public SimulatePipelineRequest(BytesReference source, XContentType xContentType)
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);
}
Expand Down Expand Up @@ -123,7 +123,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(verbose);
out.writeBytesReference(source);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out);
out.writeEnum(xContentType);
}
}

Expand Down
Expand Up @@ -516,7 +516,7 @@ public void readFrom(StreamInput in) throws IOException {
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);
}
Expand Down Expand Up @@ -561,7 +561,7 @@ public void writeTo(StreamOutput out) throws IOException {
if (doc != null) {
out.writeBytesReference(doc);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out);
out.writeEnum(xContentType);
}
}
out.writeOptionalString(routing);
Expand Down
Expand Up @@ -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;
Expand All @@ -37,7 +36,7 @@
* NamedXContentRegistry.EMPTY, ParserField."{\"key\" : \"value\"}");
* </pre>
*/
public interface XContentParser extends Releasable {
public interface XContentParser extends Closeable {

enum Token {
START_OBJECT {
Expand Down
Expand Up @@ -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.
Expand Down Expand Up @@ -183,18 +179,4 @@ public String mediaType() {

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);
}
}
Expand Up @@ -221,7 +221,7 @@ public Item(@Nullable String index, @Nullable String type, XContentBuilder doc)
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);
}
Expand All @@ -243,7 +243,7 @@ public void writeTo(StreamOutput out) throws IOException {
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);
Expand Down
Expand Up @@ -120,7 +120,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

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();
Expand All @@ -137,7 +137,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(id);
out.writeBytesReference(config);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out);
out.writeEnum(xContentType);
}
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/main/java/org/elasticsearch/script/Script.java
Expand Up @@ -507,7 +507,7 @@ public Script(StreamInput in) throws IOException {

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<>();
Expand Down Expand Up @@ -571,7 +571,7 @@ public void writeTo(StreamOutput out) throws IOException {
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);
}
Expand Down

0 comments on commit 41a7a94

Please sign in to comment.