Skip to content

Commit 8037b9a

Browse files
committed
feat: add test compress
Signed-off-by: kaixuan xu <[email protected]>
1 parent 345c1c6 commit 8037b9a

File tree

16 files changed

+509
-6
lines changed

16 files changed

+509
-6
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2024 openGemini Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opengemini.client.api;
18+
19+
public enum CompressMethod {
20+
GZIP("gzip"),
21+
SNAPPY("snappy"),
22+
ZSTD("zstd");
23+
24+
private final String value;
25+
26+
CompressMethod(String value) {
27+
this.value = value;
28+
}
29+
30+
public String getValue() {
31+
return value;
32+
}
33+
}

opengemini-client-api/src/main/java/io/opengemini/client/api/Configuration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public class Configuration {
3737

3838
BatchConfig batchConfig;
3939

40+
ContentType contentType;
41+
42+
CompressMethod compressMethod;
43+
44+
// deprecated, will use compressMethod and contentType
4045
boolean gzipEnabled;
4146

4247
HttpClientConfig httpConfig;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2024 openGemini Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opengemini.client.api;
18+
19+
public enum ContentType {
20+
JSON("application/json"),
21+
MSGPACK("application/msgpack");
22+
23+
private final String value;
24+
25+
ContentType(String value) {
26+
this.value = value;
27+
}
28+
29+
public String getValue() {
30+
return value;
31+
}
32+
}

opengemini-client-common/src/main/java/io/opengemini/client/common/BaseClient.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public BaseClient(Configuration conf) {
5555
contentEncodingHeader.add("gzip");
5656
headers.put("Content-Encoding", contentEncodingHeader);
5757
}
58+
59+
applyCodec(conf, headers);
60+
5861
String httpPrefix;
5962
if (conf.getHttpConfig().tlsConfig() != null) {
6063
httpPrefix = "https://";
@@ -77,6 +80,36 @@ public BaseClient(Configuration conf) {
7780
scheduler.ifPresent(this::startHealthCheck);
7881
}
7982

83+
private void applyCodec(Configuration config, Map<String, List<String>> headers) {
84+
if (config.getContentType() != null) {
85+
List<String> acceptHeader = new ArrayList<>();
86+
switch (config.getContentType()) {
87+
case MSGPACK:
88+
acceptHeader.add("application/msgpack");
89+
break;
90+
case JSON:
91+
acceptHeader.add("application/json");
92+
break;
93+
}
94+
headers.put("Accept", acceptHeader);
95+
}
96+
if (config.getCompressMethod() != null) {
97+
List<String> acceptEncodingHeader = new ArrayList<>();
98+
switch (config.getCompressMethod()) {
99+
case GZIP:
100+
acceptEncodingHeader.add("gzip");
101+
break;
102+
case ZSTD:
103+
acceptEncodingHeader.add("zstd");
104+
break;
105+
case SNAPPY:
106+
acceptEncodingHeader.add("snappy");
107+
break;
108+
}
109+
headers.put("Accept-Encoding", acceptEncodingHeader);
110+
}
111+
}
112+
80113
/**
81114
* Health Check
82115
* Start schedule task(period 10s) to ping all server url
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2024 openGemini Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opengemini.client.common.compress;
18+
19+
public interface Compressor {
20+
21+
byte[] compress(byte[] data);
22+
23+
byte[] decompress(byte[] data);
24+
25+
String getName();
26+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2024 openGemini Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opengemini.client.common.compress;
18+
19+
import io.opengemini.client.api.CompressMethod;
20+
import lombok.AllArgsConstructor;
21+
22+
import java.io.ByteArrayInputStream;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.IOException;
25+
import java.util.zip.GZIPInputStream;
26+
import java.util.zip.GZIPOutputStream;
27+
28+
@AllArgsConstructor
29+
public class GzipCompressor implements Compressor {
30+
31+
@Override
32+
public byte[] compress(byte[] data) {
33+
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
34+
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream)) {
35+
gzipOutputStream.write(data);
36+
gzipOutputStream.finish();
37+
return byteArrayOutputStream.toByteArray();
38+
} catch (IOException e) {
39+
throw new RuntimeException("Failed to compress data", e);
40+
}
41+
}
42+
43+
@Override
44+
public byte[] decompress(byte[] data) {
45+
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
46+
GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream);
47+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
48+
byte[] buffer = new byte[1024];
49+
int len;
50+
while ((len = gzipInputStream.read(buffer)) != -1) {
51+
byteArrayOutputStream.write(buffer, 0, len);
52+
}
53+
return byteArrayOutputStream.toByteArray();
54+
} catch (IOException e) {
55+
throw new RuntimeException("Failed to decompress data", e);
56+
}
57+
}
58+
59+
@Override
60+
public String getName() {
61+
return CompressMethod.GZIP.getValue();
62+
}
63+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 openGemini Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opengemini.client.common.compress;
18+
19+
import io.opengemini.client.api.CompressMethod;
20+
import lombok.AllArgsConstructor;
21+
import org.xerial.snappy.Snappy;
22+
23+
import java.io.IOException;
24+
25+
@AllArgsConstructor
26+
public class SnappyCompressor implements Compressor {
27+
28+
@Override
29+
public byte[] compress(byte[] data) {
30+
try {
31+
return Snappy.compress(data);
32+
} catch (IOException e) {
33+
throw new RuntimeException("Failed to compress data", e);
34+
}
35+
}
36+
37+
@Override
38+
public byte[] decompress(byte[] data) {
39+
try {
40+
return Snappy.uncompress(data);
41+
} catch (IOException e) {
42+
throw new RuntimeException("Failed to decompress data", e);
43+
}
44+
}
45+
46+
@Override
47+
public String getName() {
48+
return CompressMethod.SNAPPY.getValue();
49+
}
50+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2024 openGemini Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opengemini.client.common.compress;
18+
19+
import com.github.luben.zstd.Zstd;
20+
import io.opengemini.client.api.CompressMethod;
21+
22+
public class ZstdCompressor implements Compressor {
23+
24+
@Override
25+
public byte[] compress(byte[] data) {
26+
return Zstd.compress(data);
27+
}
28+
29+
@Override
30+
public byte[] decompress(byte[] data) {
31+
try {
32+
long decompressedSize = Zstd.decompressedSize(data);
33+
byte[] decompressedData = new byte[(int) decompressedSize];
34+
Zstd.decompress(decompressedData, data);
35+
return decompressedData;
36+
} catch (Exception e) {
37+
throw new RuntimeException("Failed to decompress data", e);
38+
}
39+
}
40+
41+
@Override
42+
public String getName() {
43+
return CompressMethod.ZSTD.getValue();
44+
}
45+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright 2024 openGemini Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opengemini.client.common.compress;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2024 openGemini Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opengemini.client.common.compress;
18+
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
class GzipCompressorTest {
23+
24+
private final GzipCompressor gzipCompressor = new GzipCompressor();
25+
26+
@Test
27+
void testCompressAndDecompress() {
28+
String originalString = "This is a test string for GZIP compression";
29+
byte[] originalData = originalString.getBytes();
30+
31+
// Compress the data
32+
byte[] compressedData = gzipCompressor.compress(originalData);
33+
Assertions.assertNotNull(compressedData);
34+
Assertions.assertNotEquals(0, compressedData.length);
35+
36+
// Decompress the data
37+
byte[] decompressedData = gzipCompressor.decompress(compressedData);
38+
Assertions.assertNotNull(decompressedData);
39+
Assertions.assertArrayEquals(originalData, decompressedData);
40+
41+
// Verify the decompressed string is the same as the original
42+
String decompressedString = new String(decompressedData);
43+
Assertions.assertEquals(originalString, decompressedString);
44+
}
45+
}

0 commit comments

Comments
 (0)