Skip to content

Commit 418bc64

Browse files
committed
moved code handling encoding to compressed entity class
1 parent 003a5f0 commit 418bc64

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

client-v2/src/main/java/com/clickhouse/client/api/internal/CompressedEntity.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public class CompressedEntity implements HttpEntity {
1717
private HttpEntity httpEntity;
1818
private final boolean isResponse;
1919
private final CompressorStreamFactory compressorStreamFactory;
20-
private final String contentEncoding;
20+
private final String compressionAlgo;
2121

22-
CompressedEntity(HttpEntity httpEntity, boolean isResponse, CompressorStreamFactory compressorStreamFactory, String contentEncoding) {
22+
CompressedEntity(HttpEntity httpEntity, boolean isResponse, CompressorStreamFactory compressorStreamFactory) {
2323
this.httpEntity = httpEntity;
2424
this.isResponse = isResponse;
2525
this.compressorStreamFactory = compressorStreamFactory;
26-
this.contentEncoding = contentEncoding;
26+
this.compressionAlgo = getCompressionAlgoName(httpEntity.getContentEncoding());
2727
}
2828

2929
@Override
@@ -38,7 +38,7 @@ public InputStream getContent() throws IOException, UnsupportedOperationExceptio
3838
}
3939

4040
try {
41-
return compressorStreamFactory.createCompressorInputStream(contentEncoding, httpEntity.getContent());
41+
return compressorStreamFactory.createCompressorInputStream(compressionAlgo, httpEntity.getContent());
4242
} catch (CompressorException e) {
4343
throw new IOException("Failed to create decompressing input stream", e);
4444
}
@@ -52,7 +52,7 @@ public void writeTo(OutputStream outStream) throws IOException {
5252
}
5353

5454
try {
55-
httpEntity.writeTo(compressorStreamFactory.createCompressorOutputStream(contentEncoding, outStream));
55+
httpEntity.writeTo(compressorStreamFactory.createCompressorOutputStream(compressionAlgo, outStream));
5656
} catch (CompressorException e) {
5757
throw new IOException("Failed to create compressing output stream", e);
5858
}
@@ -97,4 +97,14 @@ public boolean isChunked() {
9797
public Set<String> getTrailerNames() {
9898
return httpEntity.getTrailerNames();
9999
}
100+
101+
private String getCompressionAlgoName(String contentEncoding) {
102+
String algo = contentEncoding;
103+
if (algo.equalsIgnoreCase("gzip")) {
104+
algo = CompressorStreamFactory.GZIP;
105+
} else if (algo.equalsIgnoreCase("lz4")) {
106+
algo = CompressorStreamFactory.LZ4_FRAMED;
107+
}
108+
return algo;
109+
}
100110
}

client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,7 @@ private HttpEntity wrapRequestEntity(HttpEntity httpEntity, LZ4Factory lz4Factor
644644

645645
if (httpEntity.getContentEncoding() != null && !appCompressedData) {
646646
// http header is set and data is not compressed
647-
String algo = getCompressionAlgoName(httpEntity.getContentEncoding());
648-
return new CompressedEntity(httpEntity, false, CompressorStreamFactory.getSingleton(), algo);
647+
return new CompressedEntity(httpEntity, false, CompressorStreamFactory.getSingleton());
649648
} else if (clientCompression && !appCompressedData) {
650649
int buffSize = ClientConfigProperties.COMPRESSION_LZ4_UNCOMPRESSED_BUF_SIZE.getOrDefault(requestConfig);
651650
return new LZ4Entity(httpEntity, useHttpCompression, false, true,
@@ -664,8 +663,7 @@ private HttpEntity wrapResponseEntity(HttpEntity httpEntity, int httpStatus, LZ4
664663

665664
if (httpEntity.getContentEncoding() != null) {
666665
// http compressed response
667-
String algo = getCompressionAlgoName(httpEntity.getContentEncoding());
668-
return new CompressedEntity(httpEntity, true, CompressorStreamFactory.getSingleton(), algo);
666+
return new CompressedEntity(httpEntity, true, CompressorStreamFactory.getSingleton());
669667
}
670668

671669
// data compression
@@ -677,16 +675,6 @@ private HttpEntity wrapResponseEntity(HttpEntity httpEntity, int httpStatus, LZ4
677675
return httpEntity;
678676
}
679677

680-
private String getCompressionAlgoName(String contentEncoding) {
681-
String algo = contentEncoding;
682-
if (algo.equalsIgnoreCase("gzip")) {
683-
algo = CompressorStreamFactory.GZIP;
684-
} else if (algo.equalsIgnoreCase("lz4")) {
685-
algo = CompressorStreamFactory.LZ4_FRAMED;
686-
}
687-
return algo;
688-
}
689-
690678
public static int getHeaderInt(Header header, int defaultValue) {
691679
return getHeaderVal(header, defaultValue, Integer::parseInt);
692680
}

client-v2/src/test/java/com/clickhouse/client/insert/InsertClientHttpCompressionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public Object[][] insertRawDataCompressedProvider() {
6464
{ "lz4" },
6565
{ "zstd" },
6666
{ "deflate" },
67-
{ "gz" },
67+
{ "gzip" },
6868
};
6969
}
7070
}

0 commit comments

Comments
 (0)