Skip to content

Commit 3264d9e

Browse files
authored
Revert "CompressorFactory.compressor (elastic#131655)" (elastic#131866)
This should have waited for approval from the breaking change committee. I'll un-revert it when that approval arrives. Reverts elastic#131655
1 parent 71957ca commit 3264d9e

File tree

6 files changed

+11
-37
lines changed

6 files changed

+11
-37
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.compressorForUnknownXContentType(request.bytes());
133+
final Compressor compressor = CompressorFactory.compressor(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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public CompressedXContent(ToXContent xcontent, ToXContent.Params params) throws
135135
* that may already be compressed.
136136
*/
137137
public CompressedXContent(BytesReference data) throws IOException {
138-
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(data);
138+
Compressor compressor = CompressorFactory.compressor(data);
139139
if (compressor != null) {
140140
// already compressed...
141141
this.bytes = BytesReference.toBytes(data);
@@ -148,7 +148,7 @@ public CompressedXContent(BytesReference data) throws IOException {
148148
}
149149

150150
private void assertConsistent() {
151-
assert CompressorFactory.compressorForUnknownXContentType(new BytesArray(bytes)) != null;
151+
assert CompressorFactory.compressor(new BytesArray(bytes)) != null;
152152
assert this.sha256.equals(sha256(uncompressed()));
153153
assert this.sha256.equals(sha256FromCompressed(bytes));
154154
}

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class CompressorFactory {
2222
public static final Compressor COMPRESSOR = new DeflateCompressor();
2323

2424
public static boolean isCompressed(BytesReference bytes) {
25-
return compressorForUnknownXContentType(bytes) != null;
25+
return compressor(bytes) != null;
2626
}
2727

2828
@Nullable
@@ -34,15 +34,6 @@ public static Compressor compressor(BytesReference bytes) {
3434
assert XContentHelper.xContentType(bytes) == null;
3535
return COMPRESSOR;
3636
}
37-
return null;
38-
}
39-
40-
@Nullable
41-
public static Compressor compressorForUnknownXContentType(BytesReference bytes) {
42-
Compressor compressor = compressor(bytes);
43-
if (compressor != null) {
44-
return compressor;
45-
}
4637

4738
XContentType contentType = XContentHelper.xContentType(bytes);
4839
if (contentType == null) {
@@ -65,13 +56,13 @@ private static boolean isAncient(BytesReference bytes) {
6556
* @throws NullPointerException a NullPointerException will be thrown when bytes is null
6657
*/
6758
public static BytesReference uncompressIfNeeded(BytesReference bytes) throws IOException {
68-
Compressor compressor = compressorForUnknownXContentType(Objects.requireNonNull(bytes, "the BytesReference must not be null"));
59+
Compressor compressor = compressor(Objects.requireNonNull(bytes, "the BytesReference must not be null"));
6960
return compressor == null ? bytes : compressor.uncompress(bytes);
7061
}
7162

7263
/** Decompress the provided {@link BytesReference}. */
7364
public static BytesReference uncompress(BytesReference bytes) throws IOException {
74-
Compressor compressor = compressorForUnknownXContentType(bytes);
65+
Compressor compressor = compressor(bytes);
7566
if (compressor == null) {
7667
throw new NotCompressedException();
7768
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static XContentParser createParser(NamedXContentRegistry registry, Deprec
6767
*/
6868
@Deprecated
6969
public static XContentParser createParser(XContentParserConfiguration config, BytesReference bytes) throws IOException {
70-
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(bytes);
70+
Compressor compressor = CompressorFactory.compressor(bytes);
7171
if (compressor != null) {
7272
InputStream compressedInput = compressor.threadLocalInputStream(bytes.streamInput());
7373
if (compressedInput.markSupported() == false) {
@@ -568,7 +568,7 @@ public interface CustomMerge {
568568
@Deprecated
569569
public static void writeRawField(String field, BytesReference source, XContentBuilder builder, ToXContent.Params params)
570570
throws IOException {
571-
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(source);
571+
Compressor compressor = CompressorFactory.compressor(source);
572572
if (compressor != null) {
573573
try (InputStream compressedStreamInput = compressor.threadLocalInputStream(source.streamInput())) {
574574
builder.rawField(field, compressedStreamInput);
@@ -592,7 +592,7 @@ public static void writeRawField(
592592
ToXContent.Params params
593593
) throws IOException {
594594
Objects.requireNonNull(xContentType);
595-
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(source);
595+
Compressor compressor = CompressorFactory.compressor(source);
596596
if (compressor != null) {
597597
try (InputStream compressedStreamInput = compressor.threadLocalInputStream(source.streamInput())) {
598598
builder.rawField(field, compressedStreamInput, xContentType);
@@ -677,7 +677,7 @@ public static BytesReference toXContent(ChunkedToXContent toXContent, XContentTy
677677
*/
678678
@Deprecated
679679
public static XContentType xContentTypeMayCompressed(BytesReference bytes) {
680-
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(bytes);
680+
Compressor compressor = CompressorFactory.compressor(bytes);
681681
if (compressor != null) {
682682
try {
683683
InputStream compressedStreamInput = compressor.threadLocalInputStream(bytes.streamInput());

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.compressorForUnknownXContentType(request.bytes());
148+
final Compressor compressor = CompressorFactory.compressor(request.bytes());
149149
if (compressor != null) {
150150
in = compressor.threadLocalStreamInput(in);
151151
}

server/src/test/java/org/elasticsearch/common/xcontent/support/XContentHelperTests.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
import org.elasticsearch.common.bytes.BytesArray;
1313
import org.elasticsearch.common.bytes.BytesReference;
1414
import org.elasticsearch.common.compress.CompressedXContent;
15-
import org.elasticsearch.common.compress.NotXContentException;
1615
import org.elasticsearch.common.util.Maps;
1716
import org.elasticsearch.common.xcontent.XContentHelper;
1817
import org.elasticsearch.test.ESTestCase;
1918
import org.elasticsearch.xcontent.ToXContent;
2019
import org.elasticsearch.xcontent.ToXContentObject;
2120
import org.elasticsearch.xcontent.XContentBuilder;
22-
import org.elasticsearch.xcontent.XContentParseException;
2321
import org.elasticsearch.xcontent.XContentParser;
2422
import org.elasticsearch.xcontent.XContentParserConfiguration;
2523
import org.elasticsearch.xcontent.XContentType;
@@ -423,19 +421,4 @@ public void testParseToType() throws IOException {
423421

424422
assertThat(names, equalTo(Set.of("a", "c")));
425423
}
426-
427-
public void testGetParserWithInvalidInput() throws IOException {
428-
assertThrows(
429-
"Should detect bad JSON",
430-
NotXContentException.class,
431-
() -> XContentHelper.createParser(XContentParserConfiguration.EMPTY, new BytesArray("not actually XContent"))
432-
);
433-
XContentParser parser = XContentHelper.createParser(
434-
XContentParserConfiguration.EMPTY,
435-
new BytesArray("not actually XContent"),
436-
XContentType.JSON
437-
);
438-
assertNotNull("Should not detect bad JSON", parser); // This is more like assertNotThrows
439-
assertThrows("Should detect bad JSON at parse time", XContentParseException.class, parser::numberValue);
440-
}
441424
}

0 commit comments

Comments
 (0)