Skip to content

Commit 99d79b1

Browse files
committed
Stop detecting XContent type when nobody asked
1 parent c32284f commit 99d79b1

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

server/src/main/java/org/elasticsearch/cluster/coordination/PublicationTransportHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private void handleIncomingPublishRequest(
130130
ActionListener<PublishWithJoinResponse> publishResponseListener
131131
) throws IOException {
132132
assert ThreadPool.assertCurrentThreadPool(GENERIC);
133-
final Compressor compressor = CompressorFactory.compressor(request.bytes());
133+
final Compressor compressor = CompressorFactory.compressorAndCheckXContentType(request.bytes());
134134
StreamInput in = request.bytes().streamInput();
135135
try {
136136
if (compressor != null) {

server/src/main/java/org/elasticsearch/common/compress/CompressedXContent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,15 @@ public CompressedXContent(BytesReference data) throws IOException {
141141
this.bytes = BytesReference.toBytes(data);
142142
this.sha256 = sha256FromCompressed(this.bytes);
143143
} else {
144+
CompressorFactory.checkXContentType(data);
144145
this.bytes = BytesReference.toBytes(CompressorFactory.COMPRESSOR.compress(data));
145146
this.sha256 = sha256(data);
146147
}
147148
assertConsistent();
148149
}
149150

150151
private void assertConsistent() {
151-
assert CompressorFactory.compressor(new BytesArray(bytes)) != null;
152+
assert CompressorFactory.compressorAndCheckXContentType(new BytesArray(bytes)) != null;
152153
assert this.sha256.equals(sha256(uncompressed()));
153154
assert this.sha256.equals(sha256FromCompressed(bytes));
154155
}

server/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,35 @@
1515
import org.elasticsearch.xcontent.XContentType;
1616

1717
import java.io.IOException;
18-
import java.util.Objects;
18+
19+
import static java.util.Objects.requireNonNull;
1920

2021
public class CompressorFactory {
2122

2223
public static final Compressor COMPRESSOR = new DeflateCompressor();
2324

2425
public static boolean isCompressed(BytesReference bytes) {
25-
return compressor(bytes) != null;
26+
return COMPRESSOR.isCompressed(bytes);
27+
}
28+
29+
/**
30+
* @deprecated Use {@link #compressor} and {@link #checkXContentType} as appropriate.
31+
*/
32+
@Deprecated
33+
@Nullable
34+
public static Compressor compressorAndCheckXContentType(BytesReference bytes) {
35+
Compressor compressor = compressor(bytes);
36+
if (compressor != null) {
37+
return compressor;
38+
}
39+
40+
checkXContentType(bytes);
41+
return null;
2642
}
2743

44+
/**
45+
* @return the {@link Compressor} if {@code bytes} are compressed; otherwise null
46+
*/
2847
@Nullable
2948
public static Compressor compressor(BytesReference bytes) {
3049
if (COMPRESSOR.isCompressed(bytes)) {
@@ -34,16 +53,24 @@ public static Compressor compressor(BytesReference bytes) {
3453
assert XContentHelper.xContentType(bytes) == null;
3554
return COMPRESSOR;
3655
}
56+
return null;
57+
}
3758

59+
/**
60+
* Throw if {@code bytes} do not represent valid XContent.
61+
*
62+
* @deprecated XContent type detection is deprecated; see {@link XContentHelper#xContentType}.
63+
*/
64+
@Deprecated
65+
public static XContentType checkXContentType(BytesReference bytes) {
3866
XContentType contentType = XContentHelper.xContentType(bytes);
3967
if (contentType == null) {
4068
if (isAncient(bytes)) {
4169
throw new IllegalStateException("unsupported compression: index was created before v2.0.0.beta1 and wasn't upgraded?");
4270
}
4371
throw new NotXContentException("Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes");
4472
}
45-
46-
return null;
73+
return requireNonNull(contentType);
4774
}
4875

4976
/** true if the bytes were compressed with LZF: only used before elasticsearch 2.0 */
@@ -56,7 +83,7 @@ private static boolean isAncient(BytesReference bytes) {
5683
* @throws NullPointerException a NullPointerException will be thrown when bytes is null
5784
*/
5885
public static BytesReference uncompressIfNeeded(BytesReference bytes) throws IOException {
59-
Compressor compressor = compressor(Objects.requireNonNull(bytes, "the BytesReference must not be null"));
86+
Compressor compressor = compressorAndCheckXContentType(requireNonNull(bytes, "the BytesReference must not be null"));
6087
return compressor == null ? bytes : compressor.uncompress(bytes);
6188
}
6289

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static XContentParser createParser(XContentParserConfiguration config, By
7676
final XContentType contentType = XContentFactory.xContentType(compressedInput);
7777
return XContentFactory.xContent(contentType).createParser(config, compressedInput);
7878
} else {
79-
return createParserNotCompressed(config, bytes, xContentType(bytes));
79+
return createParserNotCompressed(config, bytes, CompressorFactory.checkXContentType(bytes));
8080
}
8181
}
8282

@@ -574,6 +574,7 @@ public static void writeRawField(String field, BytesReference source, XContentBu
574574
builder.rawField(field, compressedStreamInput);
575575
}
576576
} else {
577+
CompressorFactory.checkXContentType(source);
577578
try (InputStream stream = source.streamInput()) {
578579
builder.rawField(field, stream);
579580
}
@@ -690,7 +691,7 @@ public static XContentType xContentTypeMayCompressed(BytesReference bytes) {
690691
throw new UncheckedIOException(e);
691692
}
692693
} else {
693-
return XContentHelper.xContentType(bytes);
694+
return CompressorFactory.checkXContentType(bytes);
694695
}
695696
}
696697

server/src/test/java/org/elasticsearch/cluster/coordination/PublicationTransportHandlerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private static boolean isDiff(BytesTransportRequest request, TransportVersion ve
145145
StreamInput in = null;
146146
try {
147147
in = request.bytes().streamInput();
148-
final Compressor compressor = CompressorFactory.compressor(request.bytes());
148+
final Compressor compressor = CompressorFactory.compressorAndCheckXContentType(request.bytes());
149149
if (compressor != null) {
150150
in = compressor.threadLocalStreamInput(in);
151151
}

0 commit comments

Comments
 (0)