Skip to content

Commit e49f016

Browse files
committed
add indexLocation in ShardingCodec.
1 parent cf105f0 commit e49f016

File tree

15 files changed

+184
-18
lines changed

15 files changed

+184
-18
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ build/
3636

3737

3838
### Custom ###
39-
/testdata
39+
/testdata/l4_sample
4040
/testoutput

src/main/java/dev/zarr/zarrjava/v3/codec/CodecBuilder.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,30 @@ public CodecBuilder withZstd(int clevel) {
109109
public CodecBuilder withSharding(int[] chunkShape) {
110110
try {
111111
codecs.add(
112-
new ShardingIndexedCodec(new ShardingIndexedCodec.Configuration(chunkShape,
113-
new Codec[]{new BytesCodec(new Configuration(Endian.LITTLE))},
114-
new Codec[]{new BytesCodec(new Configuration(Endian.LITTLE)), new Crc32cCodec()})));
112+
new ShardingIndexedCodec(new ShardingIndexedCodec.Configuration(chunkShape,
113+
new Codec[]{new BytesCodec(new Configuration(Endian.LITTLE))},
114+
new Codec[]{new BytesCodec(new Configuration(Endian.LITTLE)), new Crc32cCodec()},
115+
"end")));
115116
} catch (ZarrException e) {
116117
throw new RuntimeException(e);
117118
}
118119
return this;
119120
}
120121

121122
public CodecBuilder withSharding(int[] chunkShape,
122-
Function<CodecBuilder, CodecBuilder> codecBuilder) {
123+
Function<CodecBuilder, CodecBuilder> codecBuilder) {
124+
return withSharding(chunkShape, codecBuilder, "end");
125+
}
126+
127+
public CodecBuilder withSharding(int[] chunkShape,
128+
Function<CodecBuilder, CodecBuilder> codecBuilder, String indexLocation) {
123129
CodecBuilder nestedBuilder = new CodecBuilder(dataType);
124130
try {
125131
codecs.add(new ShardingIndexedCodec(
126-
new ShardingIndexedCodec.Configuration(chunkShape,
127-
codecBuilder.apply(nestedBuilder).build(),
128-
new Codec[]{new BytesCodec(Endian.LITTLE), new Crc32cCodec()})));
132+
new ShardingIndexedCodec.Configuration(chunkShape,
133+
codecBuilder.apply(nestedBuilder).build(),
134+
new Codec[]{new BytesCodec(Endian.LITTLE), new Crc32cCodec()},
135+
indexLocation)));
129136
} catch (ZarrException e) {
130137
throw new RuntimeException(e);
131138
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,21 @@ public static final class Configuration {
257257
public final Codec[] indexCodecs;
258258
@Nonnull
259259
@JsonProperty("index_location")
260-
public final String indexLocation;
260+
public String indexLocation;
261261

262262
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
263263
public Configuration(
264264
@JsonProperty(value = "chunk_shape", required = true) int[] chunkShape,
265265
@Nonnull @JsonProperty("codecs") Codec[] codecs,
266266
@Nonnull @JsonProperty("index_codecs") Codec[] indexCodecs,
267-
@Nonnull @JsonProperty("index_location") String indexLocation
268-
) {
267+
@JsonProperty(value = "index_location", defaultValue = "end") String indexLocation
268+
) throws ZarrException {
269+
if (indexLocation == null) {
270+
indexLocation = "end";
271+
}
272+
if (!indexLocation.equals("start") && !indexLocation.equals("end")) {
273+
throw new ZarrException("Only index_location \"start\" or \"end\" are supported.");
274+
}
269275
this.chunkShape = chunkShape;
270276
this.codecs = codecs;
271277
this.indexCodecs = indexCodecs;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public ByteBuffer encode(ByteBuffer chunkBytes)
5959
zstdStream.close();
6060
return ByteBuffer.wrap(outputStream.toByteArray());
6161
} catch (IOException ex) {
62-
throw new ZarrException("Error in decoding zstd.", ex);
62+
throw new ZarrException("Error in encoding zstd.", ex);
6363
}
6464
}
6565

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,20 +349,21 @@ public void testV3Access() throws IOException, ZarrException {
349349
writeArray.access().withOffset(0, 3073, 3073, 513).write(outArray);
350350
}
351351

352-
@Disabled("uses excessive memory")
353-
@Test
354-
public void testV3ShardingReadWrite() throws IOException, ZarrException {
352+
353+
@ParameterizedTest
354+
@ValueSource(strings = {"start", "end"})
355+
public void testV3ShardingReadWrite(String indexLocation) throws IOException, ZarrException {
355356
Array readArray = Array.open(
356-
new FilesystemStore(TESTDATA).resolve("l4_sample", "color", "8-8-2"));
357+
new FilesystemStore(TESTDATA).resolve("sharding_index_location", indexLocation));
357358
ucar.ma2.Array readArrayContent = readArray.read();
358359
Array writeArray = Array.create(
359-
new FilesystemStore(TESTOUTPUT).resolve("l4_sample_3", "color", "8-8-2"),
360+
new FilesystemStore(TESTOUTPUT).resolve("sharding_index_location", indexLocation),
360361
readArray.metadata
361362
);
362363
writeArray.write(readArrayContent);
363364
ucar.ma2.Array outArray = writeArray.read();
364365

365-
assert MultiArrayUtils.allValuesEqual(outArray, readArrayContent);
366+
assert MultiArrayUtils.allValuesEqual(readArrayContent, outArray);
366367
}
367368

368369
@Test
4.13 KB
Binary file not shown.
4.13 KB
Binary file not shown.
4.13 KB
Binary file not shown.
4.13 KB
Binary file not shown.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"shape": [
3+
16,
4+
16,
5+
16
6+
],
7+
"data_type": "int32",
8+
"chunk_grid": {
9+
"configuration": {
10+
"chunk_shape": [
11+
16,
12+
8,
13+
8
14+
]
15+
},
16+
"name": "regular"
17+
},
18+
"chunk_key_encoding": {
19+
"configuration": {
20+
"separator": "/"
21+
},
22+
"name": "default"
23+
},
24+
"fill_value": 0,
25+
"codecs": [
26+
{
27+
"configuration": {
28+
"chunk_shape": [
29+
8,
30+
4,
31+
8
32+
],
33+
"codecs": [
34+
{
35+
"configuration": {
36+
"order": [2, 1, 0]
37+
},
38+
"name": "transpose"
39+
},
40+
{
41+
"configuration": {
42+
"endian": "little"
43+
},
44+
"name": "bytes"
45+
},
46+
{
47+
"configuration": {
48+
"typesize": 4,
49+
"cname": "lz4",
50+
"clevel": 5,
51+
"shuffle": "noshuffle",
52+
"blocksize": 0
53+
},
54+
"name": "blosc"
55+
}
56+
],
57+
"index_codecs": [
58+
{
59+
"configuration": {
60+
"endian": "little"
61+
},
62+
"name": "bytes"
63+
},
64+
{
65+
"name": "crc32c"
66+
}
67+
],
68+
"index_location": "end"
69+
},
70+
"name": "sharding_indexed"
71+
}
72+
],
73+
"attributes": {},
74+
"zarr_format": 3,
75+
"node_type": "array"
76+
}

0 commit comments

Comments
 (0)