Skip to content

Commit 554715f

Browse files
committed
cleanup code
1 parent 2d8b7b2 commit 554715f

File tree

3 files changed

+68
-77
lines changed

3 files changed

+68
-77
lines changed

src/main/java/dev/zarr/zarrjava/v3/codec/core/ZstdCodec.java

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,87 +4,83 @@
44
import com.fasterxml.jackson.annotation.JsonProperty;
55
import com.github.luben.zstd.Zstd;
66
import com.github.luben.zstd.ZstdCompressCtx;
7-
import com.github.luben.zstd.ZstdInputStream;
8-
import com.github.luben.zstd.ZstdOutputStream;
97
import dev.zarr.zarrjava.ZarrException;
10-
import dev.zarr.zarrjava.utils.Utils;
118
import dev.zarr.zarrjava.v3.ArrayMetadata;
129
import dev.zarr.zarrjava.v3.codec.BytesBytesCodec;
13-
import java.io.ByteArrayInputStream;
14-
import java.io.ByteArrayOutputStream;
10+
11+
import javax.annotation.Nonnull;
1512
import java.io.IOException;
1613
import java.io.InputStream;
1714
import java.io.OutputStream;
1815
import java.nio.ByteBuffer;
19-
import javax.annotation.Nonnull;
2016

2117
public class ZstdCodec extends BytesBytesCodec {
2218

23-
public final String name = "zstd";
24-
@Nonnull
25-
public final Configuration configuration;
26-
27-
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
28-
public ZstdCodec(
29-
@Nonnull @JsonProperty(value = "configuration", required = true) Configuration configuration) {
30-
this.configuration = configuration;
31-
}
32-
33-
private void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
34-
byte[] buffer = new byte[4096];
35-
int len;
36-
while ((len = inputStream.read(buffer)) > 0) {
37-
outputStream.write(buffer, 0, len);
19+
public final String name = "zstd";
20+
@Nonnull
21+
public final Configuration configuration;
22+
23+
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
24+
public ZstdCodec(
25+
@Nonnull @JsonProperty(value = "configuration", required = true) Configuration configuration) {
26+
this.configuration = configuration;
27+
}
28+
29+
private void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
30+
byte[] buffer = new byte[4096];
31+
int len;
32+
while ((len = inputStream.read(buffer)) > 0) {
33+
outputStream.write(buffer, 0, len);
34+
}
3835
}
39-
}
4036

41-
@Override
42-
public ByteBuffer decode(ByteBuffer compressedBytes) throws ZarrException {
43-
byte[] compressedArray = compressedBytes.array();
37+
@Override
38+
public ByteBuffer decode(ByteBuffer compressedBytes) throws ZarrException {
39+
byte[] compressedArray = compressedBytes.array();
40+
41+
long originalSize = Zstd.decompressedSize(compressedArray);
42+
if (originalSize == 0) {
43+
throw new ZarrException("Failed to get decompressed size");
44+
}
4445

45-
long originalSize = Zstd.decompressedSize(compressedArray);
46-
if (originalSize == 0) {
47-
throw new ZarrException("Failed to get decompressed size");
46+
byte[] decompressed = Zstd.decompress(compressedArray, (int) originalSize);
47+
return ByteBuffer.wrap(decompressed);
4848
}
4949

50-
byte[] decompressed = Zstd.decompress(compressedArray, (int) originalSize);
51-
return ByteBuffer.wrap(decompressed);
52-
}
53-
54-
@Override
55-
public ByteBuffer encode(ByteBuffer chunkBytes) throws ZarrException {
56-
byte[] arr = chunkBytes.array();
57-
byte[] compressed;
58-
try (ZstdCompressCtx ctx = new ZstdCompressCtx()) {
59-
ctx.setLevel(configuration.level);
60-
ctx.setChecksum(configuration.checksum);
61-
compressed = ctx.compress(arr);
50+
@Override
51+
public ByteBuffer encode(ByteBuffer chunkBytes) throws ZarrException {
52+
byte[] arr = chunkBytes.array();
53+
byte[] compressed;
54+
try (ZstdCompressCtx ctx = new ZstdCompressCtx()) {
55+
ctx.setLevel(configuration.level);
56+
ctx.setChecksum(configuration.checksum);
57+
compressed = ctx.compress(arr);
58+
}
59+
return ByteBuffer.wrap(compressed);
6260
}
63-
return ByteBuffer.wrap(compressed);
64-
}
6561

66-
@Override
67-
public long computeEncodedSize(long inputByteLength,
68-
ArrayMetadata.CoreArrayMetadata arrayMetadata) throws ZarrException {
69-
throw new ZarrException("Not implemented for Zstd codec.");
70-
}
62+
@Override
63+
public long computeEncodedSize(long inputByteLength,
64+
ArrayMetadata.CoreArrayMetadata arrayMetadata) throws ZarrException {
65+
throw new ZarrException("Not implemented for Zstd codec.");
66+
}
7167

72-
public static final class Configuration {
68+
public static final class Configuration {
7369

74-
public final int level;
75-
public final boolean checksum;
70+
public final int level;
71+
public final boolean checksum;
7672

77-
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
78-
public Configuration(@JsonProperty(value = "level", defaultValue = "5") int level,
79-
@JsonProperty(value = "checksum", defaultValue = "true") boolean checksum)
80-
throws ZarrException {
81-
if (level < -131072 || level > 22) {
82-
throw new ZarrException("'level' needs to be between -131072 and 22.");
83-
}
84-
this.level = level;
85-
this.checksum = checksum;
73+
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
74+
public Configuration(@JsonProperty(value = "level", defaultValue = "5") int level,
75+
@JsonProperty(value = "checksum", defaultValue = "true") boolean checksum)
76+
throws ZarrException {
77+
if (level < -131072 || level > 22) {
78+
throw new ZarrException("'level' needs to be between -131072 and 22.");
79+
}
80+
this.level = level;
81+
this.checksum = checksum;
82+
}
8683
}
87-
}
8884
}
8985

9086

src/test/java/dev/zarr/zarrjava/ZarrTest.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
import com.amazonaws.auth.AnonymousAWSCredentials;
55
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
66
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.github.luben.zstd.Zstd;
78
import com.github.luben.zstd.ZstdCompressCtx;
8-
import com.github.luben.zstd.ZstdOutputStream;
99
import dev.zarr.zarrjava.store.FilesystemStore;
1010
import dev.zarr.zarrjava.store.HttpStore;
1111
import dev.zarr.zarrjava.store.S3Store;
1212
import dev.zarr.zarrjava.store.StoreHandle;
1313
import dev.zarr.zarrjava.utils.MultiArrayUtils;
14-
import dev.zarr.zarrjava.utils.Utils;
1514
import dev.zarr.zarrjava.v3.*;
1615
import dev.zarr.zarrjava.v3.codec.core.TransposeCodec;
1716
import org.junit.jupiter.api.Assertions;
@@ -20,12 +19,10 @@
2019
import org.junit.jupiter.params.ParameterizedTest;
2120
import org.junit.jupiter.params.provider.CsvSource;
2221
import org.junit.jupiter.params.provider.ValueSource;
23-
import com.github.luben.zstd.Zstd;
2422
import ucar.ma2.MAMath;
2523

26-
import java.io.FileOutputStream;
27-
import java.nio.ByteBuffer;
2824
import java.io.*;
25+
import java.nio.ByteBuffer;
2926
import java.nio.file.Files;
3027
import java.nio.file.Path;
3128
import java.nio.file.Paths;
@@ -121,7 +118,7 @@ public void testZstdLibrary(int clevel, boolean checksumFlag) throws IOException
121118
Assertions.assertEquals(number, ByteBuffer.wrap(decompressed).getInt());
122119

123120
//write compressed to file
124-
String compressedDataPath =TESTOUTPUT.resolve("compressed" + clevel + checksumFlag + ".bin").toString();
121+
String compressedDataPath = TESTOUTPUT.resolve("compressed" + clevel + checksumFlag + ".bin").toString();
125122
try (FileOutputStream fos = new FileOutputStream(compressedDataPath)) {
126123
fos.write(compressed);
127124
}
@@ -363,8 +360,8 @@ public void testV3ShardingReadCutout() throws IOException, ZarrException {
363360
Array array = Array.open(new FilesystemStore(TESTDATA).resolve("l4_sample", "color", "1"));
364361

365362
ucar.ma2.Array outArray = array.read(new long[]{0, 3073, 3073, 513}, new int[]{1, 64, 64, 64});
366-
assertEquals(outArray.getSize(), 64 * 64 * 64);
367-
assertEquals(outArray.getByte(0), -98);
363+
Assertions.assertEquals(outArray.getSize(), 64 * 64 * 64);
364+
Assertions.assertEquals(outArray.getByte(0), -98);
368365
}
369366

370367
@Test
@@ -374,8 +371,8 @@ public void testV3Access() throws IOException, ZarrException {
374371
ucar.ma2.Array outArray = readArray.access().withOffset(0, 3073, 3073, 513)
375372
.withShape(1, 64, 64, 64)
376373
.read();
377-
assertEquals(outArray.getSize(), 64 * 64 * 64);
378-
assertEquals(outArray.getByte(0), -98);
374+
Assertions.assertEquals(outArray.getSize(), 64 * 64 * 64);
375+
Assertions.assertEquals(outArray.getByte(0), -98);
379376

380377
Array writeArray = Array.create(
381378
new FilesystemStore(TESTOUTPUT).resolve("l4_sample_2", "color", "1"),
@@ -449,9 +446,9 @@ public void testV3ArrayMetadataBuilder() throws ZarrException {
449446

450447
@Test
451448
public void testV3FillValue() throws ZarrException {
452-
assertEquals((int) ArrayMetadata.parseFillValue(0, DataType.UINT32), 0);
453-
assertEquals((int) ArrayMetadata.parseFillValue("0x00010203", DataType.UINT32), 50462976);
454-
assertEquals((byte) ArrayMetadata.parseFillValue("0b00000010", DataType.UINT8), 2);
449+
Assertions.assertEquals((int) ArrayMetadata.parseFillValue(0, DataType.UINT32), 0);
450+
Assertions.assertEquals((int) ArrayMetadata.parseFillValue("0x00010203", DataType.UINT32), 50462976);
451+
Assertions.assertEquals((byte) ArrayMetadata.parseFillValue("0b00000010", DataType.UINT8), 2);
455452
assert Double.isNaN((double) ArrayMetadata.parseFillValue("NaN", DataType.FLOAT64));
456453
assert Double.isInfinite((double) ArrayMetadata.parseFillValue("-Infinity", DataType.FLOAT64));
457454
}
@@ -469,9 +466,7 @@ public void testV3Group() throws IOException, ZarrException {
469466
);
470467
array.write(new long[]{2, 2}, ucar.ma2.Array.factory(ucar.ma2.DataType.UBYTE, new int[]{8, 8}));
471468

472-
assertArrayEquals(
473-
((Array) ((Group) group.listAsArray()[0]).listAsArray()[0]).metadata.chunkShape(),
474-
new int[]{5, 5});
469+
Assertions.assertArrayEquals(((Array) ((Group) group.listAsArray()[0]).listAsArray()[0]).metadata.chunkShape(), new int[]{5, 5});
475470
}
476471

477472
@Test

src/test/python-scripts/zarrita_write.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
elif codec_string == "bytes":
1515
codec = [zarrita.codecs.bytes_codec()]
1616
elif codec_string == "transpose":
17-
codec = [zarrita.codecs.transpose_codec([0, 1]), zarrita.codecs.bytes_codec()]
17+
codec = [zarrita.codecs.transpose_codec((0, 1)), zarrita.codecs.bytes_codec()]
1818
elif codec_string == "sharding_start":
1919
codec = [zarrita.codecs.sharding_codec(chunk_shape=(1, 2), codecs=[zarrita.codecs.bytes_codec()], index_location=zarrita.metadata.ShardingCodecIndexLocation.start)]
2020
elif codec_string == "sharding_end":

0 commit comments

Comments
 (0)