Skip to content

Commit 137e388

Browse files
committed
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
1 parent d7a9fcd commit 137e388

File tree

11 files changed

+30
-40
lines changed

11 files changed

+30
-40
lines changed

modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public PercolateQueryBuilder(String field, String documentType, String indexedDo
274274
}
275275
if (documents.isEmpty() == false) {
276276
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
277-
documentXContentType = XContentType.readFrom(in);
277+
documentXContentType = in.readEnum(XContentType.class);
278278
} else {
279279
documentXContentType = XContentFactory.xContentType(documents.iterator().next());
280280
}
@@ -331,7 +331,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
331331
out.writeOptionalBytesReference(doc);
332332
}
333333
if (documents.isEmpty() == false && out.getVersion().onOrAfter(Version.V_5_3_0)) {
334-
documentXContentType.writeTo(out);
334+
out.writeEnum(documentXContentType);
335335
}
336336
}
337337

server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void readFrom(StreamInput in) throws IOException {
123123
id = in.readOptionalString();
124124
content = in.readBytesReference();
125125
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
126-
xContentType = XContentType.readFrom(in);
126+
xContentType = in.readEnum(XContentType.class);
127127
} else {
128128
xContentType = XContentFactory.xContentType(content);
129129
}
@@ -145,7 +145,7 @@ public void writeTo(StreamOutput out) throws IOException {
145145
out.writeOptionalString(id);
146146
out.writeBytesReference(content);
147147
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
148-
xContentType.writeTo(out);
148+
out.writeEnum(xContentType);
149149
}
150150
if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha2)) {
151151
out.writeOptionalString(context);

server/src/main/java/org/elasticsearch/action/index/IndexRequest.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,11 @@ public void readFrom(StreamInput in) throws IOException {
542542
pipeline = in.readOptionalString();
543543
isRetry = in.readBoolean();
544544
autoGeneratedTimestamp = in.readLong();
545-
contentType = in.readOptionalWriteable(XContentType::readFrom);
545+
if (in.readBoolean()) {
546+
contentType = in.readEnum(XContentType.class);
547+
} else {
548+
contentType = null;
549+
}
546550
}
547551

548552
@Override
@@ -566,7 +570,12 @@ public void writeTo(StreamOutput out) throws IOException {
566570
out.writeOptionalString(pipeline);
567571
out.writeBoolean(isRetry);
568572
out.writeLong(autoGeneratedTimestamp);
569-
out.writeOptionalWriteable(contentType);
573+
if (contentType != null) {
574+
out.writeBoolean(true);
575+
out.writeEnum(contentType);
576+
} else {
577+
out.writeBoolean(false);
578+
}
570579
}
571580

572581
@Override

server/src/main/java/org/elasticsearch/action/ingest/PutPipelineRequest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void readFrom(StreamInput in) throws IOException {
8181
id = in.readString();
8282
source = in.readBytesReference();
8383
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
84-
xContentType = XContentType.readFrom(in);
84+
xContentType = in.readEnum(XContentType.class);
8585
} else {
8686
xContentType = XContentFactory.xContentType(source);
8787
}
@@ -93,7 +93,7 @@ public void writeTo(StreamOutput out) throws IOException {
9393
out.writeString(id);
9494
out.writeBytesReference(source);
9595
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
96-
xContentType.writeTo(out);
96+
out.writeEnum(xContentType);
9797
}
9898
}
9999
}

server/src/main/java/org/elasticsearch/action/ingest/SimulatePipelineRequest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void readFrom(StreamInput in) throws IOException {
105105
verbose = in.readBoolean();
106106
source = in.readBytesReference();
107107
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
108-
xContentType = XContentType.readFrom(in);
108+
xContentType = in.readEnum(XContentType.class);
109109
} else {
110110
xContentType = XContentFactory.xContentType(source);
111111
}
@@ -118,7 +118,7 @@ public void writeTo(StreamOutput out) throws IOException {
118118
out.writeBoolean(verbose);
119119
out.writeBytesReference(source);
120120
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
121-
xContentType.writeTo(out);
121+
out.writeEnum(xContentType);
122122
}
123123
}
124124

server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsRequest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ public void readFrom(StreamInput in) throws IOException {
517517
if (in.readBoolean()) {
518518
doc = in.readBytesReference();
519519
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
520-
xContentType = XContentType.readFrom(in);
520+
xContentType = in.readEnum(XContentType.class);
521521
} else {
522522
xContentType = XContentFactory.xContentType(doc);
523523
}
@@ -562,7 +562,7 @@ public void writeTo(StreamOutput out) throws IOException {
562562
if (doc != null) {
563563
out.writeBytesReference(doc);
564564
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
565-
xContentType.writeTo(out);
565+
out.writeEnum(xContentType);
566566
}
567567
}
568568
out.writeOptionalString(routing);

server/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
package org.elasticsearch.common.xcontent;
2121

22-
import org.elasticsearch.common.lease.Releasable;
23-
22+
import java.io.Closeable;
2423
import java.io.IOException;
2524
import java.nio.CharBuffer;
2625
import java.util.List;
@@ -37,7 +36,7 @@
3736
* NamedXContentRegistry.EMPTY, ParserField."{\"key\" : \"value\"}");
3837
* </pre>
3938
*/
40-
public interface XContentParser extends Releasable {
39+
public interface XContentParser extends Closeable {
4140

4241
enum Token {
4342
START_OBJECT {

server/src/main/java/org/elasticsearch/common/xcontent/XContentType.java

+1-19
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,18 @@
1919

2020
package org.elasticsearch.common.xcontent;
2121

22-
import org.elasticsearch.common.io.stream.StreamInput;
23-
import org.elasticsearch.common.io.stream.StreamOutput;
24-
import org.elasticsearch.common.io.stream.Writeable;
2522
import org.elasticsearch.common.xcontent.cbor.CborXContent;
2623
import org.elasticsearch.common.xcontent.json.JsonXContent;
2724
import org.elasticsearch.common.xcontent.smile.SmileXContent;
2825
import org.elasticsearch.common.xcontent.yaml.YamlXContent;
2926

30-
import java.io.IOException;
3127
import java.util.Locale;
3228
import java.util.Objects;
3329

3430
/**
3531
* The content type of {@link org.elasticsearch.common.xcontent.XContent}.
3632
*/
37-
public enum XContentType implements Writeable {
33+
public enum XContentType {
3834

3935
/**
4036
* A JSON based content type.
@@ -183,18 +179,4 @@ public String mediaType() {
183179

184180
public abstract String mediaTypeWithoutParameters();
185181

186-
public static XContentType readFrom(StreamInput in) throws IOException {
187-
int index = in.readVInt();
188-
for (XContentType contentType : values()) {
189-
if (index == contentType.index) {
190-
return contentType;
191-
}
192-
}
193-
throw new IllegalStateException("Unknown XContentType with index [" + index + "]");
194-
}
195-
196-
@Override
197-
public void writeTo(StreamOutput out) throws IOException {
198-
out.writeVInt(index);
199-
}
200182
}

server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public Item(@Nullable String index, @Nullable String type, XContentBuilder doc)
222222
if (in.readBoolean()) {
223223
doc = (BytesReference) in.readGenericValue();
224224
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
225-
xContentType = XContentType.readFrom(in);
225+
xContentType = in.readEnum(XContentType.class);
226226
} else {
227227
xContentType = XContentFactory.xContentType(doc);
228228
}
@@ -244,7 +244,7 @@ public void writeTo(StreamOutput out) throws IOException {
244244
if (doc != null) {
245245
out.writeGenericValue(doc);
246246
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
247-
xContentType.writeTo(out);
247+
out.writeEnum(xContentType);
248248
}
249249
} else {
250250
out.writeString(id);

server/src/main/java/org/elasticsearch/ingest/PipelineConfiguration.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
120120

121121
public static PipelineConfiguration readFrom(StreamInput in) throws IOException {
122122
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
123-
return new PipelineConfiguration(in.readString(), in.readBytesReference(), XContentType.readFrom(in));
123+
return new PipelineConfiguration(in.readString(), in.readBytesReference(), in.readEnum(XContentType.class));
124124
} else {
125125
final String id = in.readString();
126126
final BytesReference config = in.readBytesReference();
@@ -137,7 +137,7 @@ public void writeTo(StreamOutput out) throws IOException {
137137
out.writeString(id);
138138
out.writeBytesReference(config);
139139
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
140-
xContentType.writeTo(out);
140+
out.writeEnum(xContentType);
141141
}
142142
}
143143

server/src/main/java/org/elasticsearch/script/Script.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ public Script(StreamInput in) throws IOException {
507507

508508
if (in.readBoolean()) {
509509
this.options = new HashMap<>();
510-
XContentType contentType = XContentType.readFrom(in);
510+
XContentType contentType = in.readEnum(XContentType.class);
511511
this.options.put(CONTENT_TYPE_OPTION, contentType.mediaType());
512512
} else if (type == ScriptType.INLINE) {
513513
options = new HashMap<>();
@@ -571,7 +571,7 @@ public void writeTo(StreamOutput out) throws IOException {
571571
if (options != null && options.containsKey(CONTENT_TYPE_OPTION)) {
572572
XContentType contentType = XContentType.fromMediaTypeOrFormat(options.get(CONTENT_TYPE_OPTION));
573573
out.writeBoolean(true);
574-
contentType.writeTo(out);
574+
out.writeEnum(contentType);
575575
} else {
576576
out.writeBoolean(false);
577577
}

0 commit comments

Comments
 (0)