Skip to content

Commit b55f2f0

Browse files
committed
codecbuilder
1 parent 5cbf6ae commit b55f2f0

File tree

5 files changed

+36
-43
lines changed

5 files changed

+36
-43
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Array array = Array.create(
2626
.withDataType(DataType.UINT32)
2727
.withChunkShape(1, 1024, 1024, 1024)
2828
.withFillValue(0)
29-
.withCodecs(c -> c.withSharding(new int[]{1, 32, 32, 32}, c1 -> c1.withBlosc(DataType.UINT32)))
29+
.withCodecs(c -> c.withSharding(new int[]{1, 32, 32, 32}, c1 -> c1.withBlosc()))
3030
.build();
3131
);
3232
array.write(

src/main/java/com/scalableminds/zarrjava/v3/ArrayMetadataBuilder.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.scalableminds.zarrjava.v3;
22

33
import com.scalableminds.zarrjava.ZarrException;
4+
import com.scalableminds.zarrjava.v3.chunkgrid.ChunkGrid;
45
import com.scalableminds.zarrjava.v3.chunkgrid.RegularChunkGrid;
56
import com.scalableminds.zarrjava.v3.chunkkeyencoding.ChunkKeyEncoding;
67
import com.scalableminds.zarrjava.v3.chunkkeyencoding.DefaultChunkKeyEncoding;
@@ -16,7 +17,7 @@ public class ArrayMetadataBuilder {
1617

1718
long[] shape = null;
1819
DataType dataType = null;
19-
RegularChunkGrid chunkGrid = null;
20+
ChunkGrid chunkGrid = null;
2021
ChunkKeyEncoding chunkKeyEncoding =
2122
new DefaultChunkKeyEncoding(new DefaultChunkKeyEncoding.Configuration(Separator.SLASH));
2223

@@ -32,7 +33,7 @@ protected static ArrayMetadataBuilder fromArrayMetadata(ArrayMetadata arrayMetad
3233
ArrayMetadataBuilder builder = new ArrayMetadataBuilder();
3334
builder.shape = arrayMetadata.shape;
3435
builder.dataType = arrayMetadata.dataType;
35-
builder.chunkGrid = (RegularChunkGrid) arrayMetadata.chunkGrid;
36+
builder.chunkGrid = arrayMetadata.chunkGrid;
3637
builder.chunkKeyEncoding = arrayMetadata.chunkKeyEncoding;
3738
builder.fillValue = arrayMetadata.parsedFillValue;
3839
builder.codecs = arrayMetadata.codecs;
@@ -96,7 +97,10 @@ public ArrayMetadataBuilder withCodecs(Codec... codecs) {
9697
}
9798

9899
public ArrayMetadataBuilder withCodecs(Function<CodecBuilder, CodecBuilder> codecBuilder) {
99-
CodecBuilder nestedCodecBuilder = new CodecBuilder();
100+
if (dataType==null) {
101+
throw new IllegalStateException("Please call `withDataType` first.");
102+
}
103+
CodecBuilder nestedCodecBuilder = new CodecBuilder(dataType);
100104
this.codecs = codecBuilder.apply(nestedCodecBuilder)
101105
.build();
102106
return this;
@@ -127,9 +131,10 @@ public ArrayMetadata build() throws ZarrException {
127131
if (chunkGrid == null) {
128132
throw new ZarrException("Chunk grid needs to be provided. Please call `.withChunkShape`.");
129133
}
130-
if (shape.length != chunkGrid.configuration.chunkShape.length) {
134+
if (chunkGrid instanceof RegularChunkGrid
135+
&& shape.length != ((RegularChunkGrid) chunkGrid).configuration.chunkShape.length) {
131136
throw new ZarrException("Shape (ndim=" + shape.length + ") and chunk shape (ndim=" +
132-
chunkGrid.configuration.chunkShape.length +
137+
((RegularChunkGrid) chunkGrid).configuration.chunkShape.length +
133138
") need to have the same number of dimensions.");
134139
}
135140
return new ArrayMetadata(shape, dataType, chunkGrid, chunkKeyEncoding, fillValue, codecs,

src/main/java/com/scalableminds/zarrjava/v3/GroupMetadata.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,7 @@ public GroupMetadata(
4040
this.attributes = attributes;
4141
}
4242

43-
public static Builder builder() {
44-
return new Builder();
45-
}
46-
4743
public static GroupMetadata defaultValue() throws ZarrException {
4844
return new GroupMetadata(ZARR_FORMAT, NODE_TYPE, new HashMap<>());
4945
}
50-
51-
public static final class Builder {
52-
53-
Map<String, Object> attributes = new HashMap<>();
54-
55-
public Builder withAttribute(String key, Object value) {
56-
attributes.put(key, value);
57-
return this;
58-
}
59-
60-
public GroupMetadata build() throws ZarrException {
61-
return new GroupMetadata(ZARR_FORMAT, NODE_TYPE, attributes);
62-
}
63-
}
6446
}

src/main/java/com/scalableminds/zarrjava/v3/codec/CodecBuilder.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
public class CodecBuilder {
1616

17-
List<Codec> codecs;
17+
private DataType dataType;
18+
private List<Codec> codecs;
1819

19-
public CodecBuilder() {
20-
codecs = new ArrayList<>();
20+
public CodecBuilder(DataType dataType) {
21+
this.dataType = dataType;
22+
this.codecs = new ArrayList<>();
2123
}
2224

2325
public CodecBuilder withBlosc(
@@ -33,27 +35,26 @@ public CodecBuilder withBlosc(
3335
return this;
3436
}
3537

36-
public CodecBuilder withBlosc(String cname, String shuffle, int clevel, DataType dataType,
37-
int blockSize) {
38+
public CodecBuilder withBlosc(String cname, String shuffle, int clevel, int blockSize) {
3839
return withBlosc(Blosc.Compressor.fromString(cname), Blosc.Shuffle.fromString(shuffle), clevel,
3940
dataType.getByteCount(), blockSize
4041
);
4142
}
4243

43-
public CodecBuilder withBlosc(String cname, String shuffle, int clevel, DataType dataType) {
44-
return withBlosc(cname, shuffle, clevel, dataType, 0);
44+
public CodecBuilder withBlosc(String cname, String shuffle, int clevel) {
45+
return withBlosc(cname, shuffle, clevel, 0);
4546
}
4647

47-
public CodecBuilder withBlosc(String cname, int clevel, DataType dataType) {
48-
return withBlosc(cname, "noshuffle", clevel, dataType);
48+
public CodecBuilder withBlosc(String cname, int clevel) {
49+
return withBlosc(cname, "noshuffle", clevel);
4950
}
5051

51-
public CodecBuilder withBlosc(String cname, DataType dataType) {
52-
return withBlosc(cname, 5, dataType);
52+
public CodecBuilder withBlosc(String cname) {
53+
return withBlosc(cname, 5);
5354
}
5455

55-
public CodecBuilder withBlosc(DataType dataType) {
56-
return withBlosc("zstd", dataType);
56+
public CodecBuilder withBlosc() {
57+
return withBlosc("zstd");
5758
}
5859

5960
public CodecBuilder withTranspose(String order) {
@@ -99,7 +100,7 @@ public CodecBuilder withSharding(int[] chunkShape) {
99100

100101
public CodecBuilder withSharding(int[] chunkShape,
101102
Function<CodecBuilder, CodecBuilder> codecBuilder) {
102-
CodecBuilder nestedBuilder = new CodecBuilder();
103+
CodecBuilder nestedBuilder = new CodecBuilder(dataType);
103104
try {
104105
codecs.add(new ShardingIndexedCodec(
105106
new ShardingIndexedCodec.Configuration(chunkShape, codecBuilder.apply(nestedBuilder)

src/test/java/com/scalableminds/zarrjava/ZarrTest.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,14 @@ public void testV3ShardingReadWrite() throws IOException, ZarrException {
107107

108108
@Test
109109
public void testV3ArrayMetadataBuilder() throws ZarrException {
110-
Array.metadataBuilder().withShape(1, 4096, 4096, 1536).withDataType(DataType.UINT32)
111-
.withChunkShape(
112-
1, 1024, 1024, 1024).withFillValue(0).withCodecs(
113-
c -> c.withSharding(new int[]{1, 32, 32, 32}, c1 -> c1.withBlosc(DataType.UINT32))).build();
110+
Array.metadataBuilder()
111+
.withShape(1, 4096, 4096, 1536)
112+
.withDataType(DataType.UINT32)
113+
.withChunkShape(1, 1024, 1024, 1024)
114+
.withFillValue(0)
115+
.withCodecs(
116+
c -> c.withSharding(new int[]{1, 32, 32, 32}, c1 -> c1.withBlosc()))
117+
.build();
114118
}
115119

116120
@Test
@@ -134,7 +138,8 @@ public void testV3Group() throws IOException, ZarrException {
134138
.withShape(10, 10)
135139
.withDataType(DataType.UINT8)
136140
.withChunkShape(5, 5)
137-
.build());
141+
.build()
142+
);
138143
array.write(new long[]{2, 2}, ucar.ma2.Array.factory(ucar.ma2.DataType.UBYTE, new int[]{8, 8}));
139144

140145
assertArrayEquals(((Array) ((Group) group.list()[0]).list()[0]).metadata.chunkShape(),

0 commit comments

Comments
 (0)