Skip to content

Commit 4acd90e

Browse files
cmhulberttpietzsch
authored andcommitted
Refactor for compatibility with upcoming Codecs Release (#2)
* refactor: don't pass `decodedLength` to ReadData * refactor: move DataBlockFactory/DataBlockCodecFactory to N5Codecs * feat: Add StringDataCodec, ObjectDataCodec, StringDataBlockCodec, ObjectDataBlockCodec DataCodecs now creat the access object and return it during `deserialize` * refactor: add AbstractDataBlock to extract shared logic between Default/String/Object blocks * refactor: DatasetAttributes responsible for DataBlockCodec creation N5BlockCodec uses dataType (and potentially other DatasetAttributes to wrap the desired DataBlockCodec * revert: add back createDataBlock logic in DataType * doc: retain javadoc from before refactor * revert: keep protected constructor with DataBlockCodec parameter refactor: inline createDataBlockCodec from constructor params * refactor: remove currently unused N5BlockCodec. Something like this may be needed when multiple codecs are supported * refactor: dont expose N5Codecs internals * refactor: rename encodeBlockHeader -> createBlockHeader * feat: add ZarrStringDataCodec support
1 parent a89457a commit 4acd90e

File tree

11 files changed

+352
-171
lines changed

11 files changed

+352
-171
lines changed

src/main/java/org/janelia/saalfeldlab/n5/Bzip2Compression.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public boolean equals(final Object other) {
6060
}
6161

6262
@Override
63-
public ReadData decode(final ReadData readData, final int decodedLength) throws IOException {
64-
final InputStream inflater = new BZip2CompressorInputStream(readData.inputStream());
65-
return ReadData.from(inflater, decodedLength);
63+
public ReadData decode(final ReadData readData) throws IOException {
64+
65+
return ReadData.from(new BZip2CompressorInputStream(readData.inputStream()));
6666
}
6767

6868
@Override

src/main/java/org/janelia/saalfeldlab/n5/Compression.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,13 @@ default String getType() {
8585
*
8686
* @param readData
8787
* data to decode
88-
* @param decodedLength
89-
* length of the decoded data (-1 if unknown)
9088
*
9189
* @return decoded ReadData
9290
*
9391
* @throws IOException
9492
* if any I/O error occurs
9593
*/
96-
ReadData decode(ReadData readData, int decodedLength) throws IOException;
94+
ReadData decode(ReadData readData) throws IOException;
9795

9896
/**
9997
* Encode the given {@code readData}.

src/main/java/org/janelia/saalfeldlab/n5/DataBlock.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ static int getNumElements(final int[] size) {
9595
*/
9696
interface DataBlockFactory<T> {
9797

98+
99+
/**
100+
* Factory for {@link DataBlock DataBlocks}.
101+
*
102+
* @param blockSize
103+
* the block size
104+
* @param gridPosition
105+
* the grid position
106+
* @param data
107+
* the number of elements (not necessarily one element per block
108+
* element)
109+
* @return the data block
110+
*/
98111
DataBlock<T> createDataBlock(int[] blockSize, long[] gridPosition, T data);
112+
99113
}
100114
}

src/main/java/org/janelia/saalfeldlab/n5/DataType.java

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
* modification, are permitted provided that the following conditions are met:
77
*
88
* 1. Redistributions of source code must retain the above copyright notice,
9-
* this list of conditions and the following disclaimer.
9+
* this list of conditions and the following disclaimer.
1010
* 2. Redistributions in binary form must reproduce the above copyright notice,
11-
* this list of conditions and the following disclaimer in the documentation
12-
* and/or other materials provided with the distribution.
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
1313
*
1414
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1515
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@@ -34,10 +34,6 @@
3434
import com.google.gson.JsonPrimitive;
3535
import com.google.gson.JsonSerializationContext;
3636
import com.google.gson.JsonSerializer;
37-
import org.janelia.saalfeldlab.n5.codec.N5Codecs;
38-
import org.janelia.saalfeldlab.n5.codec.DataBlockCodec;
39-
import org.janelia.saalfeldlab.n5.codec.N5Codecs.DataBlockCodecFactory;
40-
4137
/**
4238
* Enumerates available data types.
4339
*
@@ -47,101 +43,90 @@ public enum DataType {
4743

4844
UINT8(
4945
"uint8",
50-
(blockSize, gridPosition, numElements) -> new ByteArrayDataBlock(
51-
blockSize,
52-
gridPosition,
53-
new byte[numElements]),
54-
N5Codecs.BYTE),
46+
(blockSize, gridPosition, numElements) -> {
47+
ByteArrayDataBlock dataBlock = new ByteArrayDataBlock(blockSize, gridPosition, new byte[numElements]);
48+
49+
50+
return new ByteArrayDataBlock(
51+
blockSize,
52+
gridPosition,
53+
new byte[numElements]);
54+
}),
5555
UINT16(
5656
"uint16",
5757
(blockSize, gridPosition, numElements) -> new ShortArrayDataBlock(
5858
blockSize,
5959
gridPosition,
60-
new short[numElements]),
61-
N5Codecs.SHORT),
60+
new short[numElements])),
6261
UINT32(
6362
"uint32",
6463
(blockSize, gridPosition, numElements) -> new IntArrayDataBlock(
6564
blockSize,
6665
gridPosition,
67-
new int[numElements]),
68-
N5Codecs.INT),
66+
new int[numElements])),
6967
UINT64(
7068
"uint64",
7169
(blockSize, gridPosition, numElements) -> new LongArrayDataBlock(
7270
blockSize,
7371
gridPosition,
74-
new long[numElements]),
75-
N5Codecs.LONG),
72+
new long[numElements])),
7673
INT8(
7774
"int8",
7875
(blockSize, gridPosition, numElements) -> new ByteArrayDataBlock(
7976
blockSize,
8077
gridPosition,
81-
new byte[numElements]),
82-
N5Codecs.BYTE),
78+
new byte[numElements])),
8379
INT16(
8480
"int16",
8581
(blockSize, gridPosition, numElements) -> new ShortArrayDataBlock(
8682
blockSize,
8783
gridPosition,
88-
new short[numElements]),
89-
N5Codecs.SHORT),
84+
new short[numElements])),
9085
INT32(
9186
"int32",
9287
(blockSize, gridPosition, numElements) -> new IntArrayDataBlock(
9388
blockSize,
9489
gridPosition,
95-
new int[numElements]),
96-
N5Codecs.INT),
90+
new int[numElements])),
9791
INT64(
9892
"int64",
9993
(blockSize, gridPosition, numElements) -> new LongArrayDataBlock(
10094
blockSize,
10195
gridPosition,
102-
new long[numElements]),
103-
N5Codecs.LONG),
96+
new long[numElements])),
10497
FLOAT32(
10598
"float32",
10699
(blockSize, gridPosition, numElements) -> new FloatArrayDataBlock(
107100
blockSize,
108101
gridPosition,
109-
new float[numElements]),
110-
N5Codecs.FLOAT),
102+
new float[numElements])),
111103
FLOAT64(
112104
"float64",
113105
(blockSize, gridPosition, numElements) -> new DoubleArrayDataBlock(
114106
blockSize,
115107
gridPosition,
116-
new double[numElements]),
117-
N5Codecs.DOUBLE),
108+
new double[numElements])),
118109
STRING(
119110
"string",
120111
(blockSize, gridPosition, numElements) -> new StringDataBlock(
121112
blockSize,
122113
gridPosition,
123-
new String[numElements]),
124-
N5Codecs.STRING),
114+
new String[numElements])),
125115
OBJECT(
126116
"object",
127117
(blockSize, gridPosition, numElements) -> new ByteArrayDataBlock(
128118
blockSize,
129119
gridPosition,
130-
new byte[numElements]),
131-
N5Codecs.OBJECT);
132-
120+
new byte[numElements]));
133121

134122
private final String label;
135123

136124
private final DataBlockFactory dataBlockFactory;
137125

138-
private final DataBlockCodecFactory<?> dataBlockCodecFactory;
139-
140-
DataType(final String label, final DataBlockFactory dataBlockFactory, final DataBlockCodecFactory<?> dataBlockCodecFactory) {
126+
DataType(final String label, final DataBlockFactory dataBlockFactory) {
141127

142128
this.label = label;
143129
this.dataBlockFactory = dataBlockFactory;
144-
this.dataBlockCodecFactory = dataBlockCodecFactory;
145130
}
146131

147132
@Override
@@ -190,19 +175,6 @@ public DataBlock<?> createDataBlock(final int[] blockSize, final long[] gridPosi
190175
return dataBlockFactory.createDataBlock(blockSize, gridPosition, DataBlock.getNumElements(blockSize));
191176
}
192177

193-
/**
194-
* Get the default {@link DataBlockCodec}, with the specified {@code
195-
* compression}, for {@link DataBlock DataBlocks} of this {@code DataType}.
196-
* The default codec is used for de/serializing blocks to N5 format.
197-
*
198-
* @param compression
199-
*
200-
* @return the default {@code DataBlockCodec}
201-
*/
202-
public DataBlockCodec<?> createDataBlockCodec(final Compression compression) {
203-
return dataBlockCodecFactory.createDataBlockCodec(compression);
204-
}
205-
206178
private interface DataBlockFactory {
207179

208180
DataBlock<?> createDataBlock(final int[] blockSize, final long[] gridPosition, final int numElements);

src/main/java/org/janelia/saalfeldlab/n5/DatasetAttributes.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import java.io.Serializable;
2929
import java.util.Arrays;
3030
import java.util.HashMap;
31+
3132
import org.janelia.saalfeldlab.n5.codec.DataBlockCodec;
33+
import org.janelia.saalfeldlab.n5.codec.N5Codecs;
3234

3335
/**
3436
* Mandatory dataset attributes:
@@ -58,16 +60,16 @@ public class DatasetAttributes implements Serializable {
5860
private final long[] dimensions;
5961
private final int[] blockSize;
6062
private final DataType dataType;
61-
private final Compression compression;
6263
private final DataBlockCodec<?> dataBlockCodec;
64+
private final Compression compression;
6365

6466
public DatasetAttributes(
6567
final long[] dimensions,
6668
final int[] blockSize,
6769
final DataType dataType,
6870
final Compression compression) {
6971

70-
this(dimensions, blockSize, dataType, compression, dataType.createDataBlockCodec(compression));
72+
this(dimensions, blockSize, dataType, compression, N5Codecs.createDataBlockCodec(dataType, compression));
7173
}
7274

7375
protected DatasetAttributes(
@@ -118,7 +120,7 @@ public DataType getDataType() {
118120
* @return the {@code DataBlockCodec} for this dataset
119121
*/
120122
public <T> DataBlockCodec<T> getDataBlockCodec() {
121-
return (DataBlockCodec<T>) dataBlockCodec;
123+
return (DataBlockCodec<T>)dataBlockCodec;
122124
}
123125

124126
public HashMap<String, Object> asMap() {

src/main/java/org/janelia/saalfeldlab/n5/GzipCompression.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ private InputStream decode(final InputStream in) throws IOException {
8585
}
8686

8787
@Override
88-
public ReadData decode(final ReadData readData, final int decodedLength) throws IOException {
89-
final InputStream inflater = decode(readData.inputStream());
90-
return ReadData.from(inflater, decodedLength);
88+
public ReadData decode(final ReadData readData) throws IOException {
89+
90+
return ReadData.from(decode(readData.inputStream()));
9191
}
9292

9393
@Override

src/main/java/org/janelia/saalfeldlab/n5/Lz4Compression.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public boolean equals(final Object other) {
6060
}
6161

6262
@Override
63-
public ReadData decode(final ReadData readData, final int decodedLength) throws IOException {
64-
final InputStream inflater = new LZ4BlockInputStream(readData.inputStream());
65-
return ReadData.from(inflater, decodedLength);
63+
public ReadData decode(final ReadData readData) throws IOException {
64+
65+
return ReadData.from(new LZ4BlockInputStream(readData.inputStream()));
6666
}
6767

6868
@Override

src/main/java/org/janelia/saalfeldlab/n5/RawCompression.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ public class RawCompression implements Compression {
3535

3636
@Override
3737
public boolean equals(final Object other) {
38-
if (other == null || other.getClass() != RawCompression.class)
39-
return false;
40-
else
41-
return true;
38+
return other != null && other.getClass() == RawCompression.class;
4239
}
4340

4441
@Override
@@ -47,7 +44,7 @@ public ReadData encode(final ReadData readData) {
4744
}
4845

4946
@Override
50-
public ReadData decode(final ReadData readData, int decodedLength) {
47+
public ReadData decode(final ReadData readData) {
5148
return readData;
5249
}
5350
}

src/main/java/org/janelia/saalfeldlab/n5/XzCompression.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public boolean equals(final Object other) {
6060
}
6161

6262
@Override
63-
public ReadData decode(final ReadData readData, final int decodedLength) throws IOException {
64-
final InputStream inflater = new XZCompressorInputStream(readData.inputStream());
65-
return ReadData.from(inflater, decodedLength);
63+
public ReadData decode(final ReadData readData) throws IOException {
64+
65+
return ReadData.from(new XZCompressorInputStream(readData.inputStream()));
6666
}
6767

6868
@Override

0 commit comments

Comments
 (0)