Skip to content

Commit d34f342

Browse files
committed
feat: method for N5Reader
1 parent b1b51b0 commit d34f342

5 files changed

Lines changed: 54 additions & 26 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ default <T> DataBlock<T> readBlock(
9797
final DatasetAttributes datasetAttributes,
9898
final long... gridPosition) throws N5Exception {
9999

100+
DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
100101
try {
101102
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(pathName),
102-
datasetAttributes);
103-
return datasetAttributes.<T> getDatasetAccess().readBlock(posKva, gridPosition);
103+
convertedDatasetAttributes);
104+
return convertedDatasetAttributes.<T> getDatasetAccess().readBlock(posKva, gridPosition);
104105

105106
} catch (N5Exception.N5NoSuchKeyException e) {
106107
return null;
@@ -113,8 +114,9 @@ default <T> List<DataBlock<T>> readBlocks(
113114
final DatasetAttributes datasetAttributes,
114115
final List<long[]> blockPositions) throws N5Exception {
115116

116-
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(pathName), datasetAttributes);
117-
return datasetAttributes.<T> getDatasetAccess().readBlocks(posKva, blockPositions);
117+
DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
118+
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(pathName), convertedDatasetAttributes);
119+
return convertedDatasetAttributes.<T> getDatasetAccess().readBlocks(posKva, blockPositions);
118120
}
119121

120122
@Override

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,10 @@ default <T> void writeRegion(
220220
final long[] size,
221221
final DataBlockSupplier<T> dataBlocks,
222222
final boolean writeFully) throws N5Exception {
223+
DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
223224
try {
224-
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(datasetPath), datasetAttributes);
225-
datasetAttributes.<T>getDatasetAccess().writeRegion(posKva, min, size, dataBlocks, writeFully);
225+
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(datasetPath), convertedDatasetAttributes);
226+
convertedDatasetAttributes.<T>getDatasetAccess().writeRegion(posKva, min, size, dataBlocks, convertedDatasetAttributes.getDimensions(), writeFully);
226227
} catch (final UncheckedIOException e) {
227228
throw new N5IOException(
228229
"Failed to write blocks into dataset " + datasetPath, e);
@@ -238,9 +239,10 @@ default <T> void writeRegion(
238239
final DataBlockSupplier<T> dataBlocks,
239240
final boolean writeFully,
240241
final ExecutorService exec) throws N5Exception, InterruptedException, ExecutionException {
242+
DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
241243
try {
242-
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(datasetPath), datasetAttributes);
243-
datasetAttributes.<T>getDatasetAccess().writeRegion(posKva, min, size, dataBlocks, writeFully, exec);
244+
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(datasetPath), convertedDatasetAttributes);
245+
convertedDatasetAttributes.<T>getDatasetAccess().writeRegion(posKva, min, size, dataBlocks, convertedDatasetAttributes.getDimensions(), writeFully, exec);
244246
} catch (final UncheckedIOException e) {
245247
throw new N5IOException(
246248
"Failed to write blocks into dataset " + datasetPath, e);
@@ -253,9 +255,10 @@ default <T> void writeBlocks(
253255
final DatasetAttributes datasetAttributes,
254256
final DataBlock<T>... dataBlocks) throws N5Exception {
255257

258+
DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
256259
try {
257-
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(datasetPath), datasetAttributes);
258-
datasetAttributes.<T>getDatasetAccess().writeBlocks(posKva, Arrays.asList(dataBlocks));
260+
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(datasetPath), convertedDatasetAttributes);
261+
convertedDatasetAttributes.<T>getDatasetAccess().writeBlocks(posKva, Arrays.asList(dataBlocks));
259262
} catch (final UncheckedIOException e) {
260263
throw new N5IOException(
261264
"Failed to write blocks into dataset " + datasetPath, e);
@@ -268,9 +271,10 @@ default <T> void writeBlock(
268271
final DatasetAttributes datasetAttributes,
269272
final DataBlock<T> dataBlock) throws N5Exception {
270273

274+
DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
271275
try {
272-
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(path), datasetAttributes);
273-
datasetAttributes.<T> getDatasetAccess().writeBlock(posKva, dataBlock);
276+
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(path), convertedDatasetAttributes);
277+
convertedDatasetAttributes.<T> getDatasetAccess().writeBlock(posKva, dataBlock);
274278
} catch (final UncheckedIOException e) {
275279
throw new N5IOException(
276280
"Failed to write block " + Arrays.toString(dataBlock.getGridPosition()) + " into dataset " + path,

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,22 @@ <T> T getAttribute(
281281
*/
282282
DatasetAttributes getDatasetAttributes(final String pathName) throws N5Exception;
283283

284+
/**
285+
* Some implementations may need to convert arbitrary DatasetAttributes to their specific equivalent variant.
286+
* Ideally, this method would be `protected`, but that's not valid for the interface. The default implementation
287+
* is the identity (returns the input DatasetAttributes unchanged).
288+
* <p>
289+
* The returned DatasetAttributes is not guaranteed to be a unique instance.
290+
*
291+
* @param attributes
292+
* to convert
293+
* @return the converted attributes
294+
*/
295+
default DatasetAttributes getConvertedDatasetAttributes(final DatasetAttributes attributes) {
296+
return attributes;
297+
}
298+
299+
284300
/**
285301
* Reads a {@link DataBlock}.
286302
*
@@ -324,9 +340,10 @@ default <T> List<DataBlock<T>> readBlocks(
324340
final DatasetAttributes datasetAttributes,
325341
final List<long[]> gridPositions) throws N5Exception {
326342

343+
DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
327344
final ArrayList<DataBlock<T>> blocks = new ArrayList<>();
328345
for( final long[] p : gridPositions )
329-
blocks.add(readBlock(pathName, datasetAttributes, p));
346+
blocks.add(readBlock(pathName, convertedDatasetAttributes, p));
330347

331348
return blocks;
332349
}
@@ -376,7 +393,8 @@ default <T> T readSerializedBlock(
376393
final DatasetAttributes attributes,
377394
final long... gridPosition) throws N5Exception, ClassNotFoundException {
378395

379-
final DataBlock<byte[]> block = readBlock(dataset, attributes, gridPosition);
396+
DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(attributes);
397+
final DataBlock<byte[]> block = readBlock(dataset, convertedDatasetAttributes, gridPosition);
380398
if (block == null)
381399
return null;
382400

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ default void setDatasetAttributes(
141141
final String datasetPath,
142142
final DatasetAttributes datasetAttributes) throws N5Exception {
143143

144-
setAttribute(datasetPath, "/", datasetAttributes);
144+
setAttribute(datasetPath, "/", getConvertedDatasetAttributes(datasetAttributes));
145145
}
146146

147147
/**
@@ -212,8 +212,9 @@ default DatasetAttributes createDataset(
212212

213213
final String normalPath = N5URI.normalizeGroupPath(datasetPath);
214214
createGroup(normalPath);
215-
setDatasetAttributes(normalPath, datasetAttributes);
216-
return datasetAttributes;
215+
DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
216+
setDatasetAttributes(normalPath, convertedDatasetAttributes);
217+
return convertedDatasetAttributes;
217218
}
218219

219220
/**
@@ -268,8 +269,10 @@ default <T> void writeBlocks(
268269
final DataBlock<T>... dataBlocks) throws N5Exception {
269270

270271
// default method is naive
271-
for (DataBlock<T> block : dataBlocks)
272-
writeBlock(datasetPath, datasetAttributes, block);
272+
DatasetAttributes convertedAttributes = getConvertedDatasetAttributes(datasetAttributes);
273+
for (DataBlock<T> block : dataBlocks) {
274+
writeBlock(datasetPath, convertedAttributes, block);
275+
}
273276
}
274277

275278
@FunctionalInterface

src/test/java/org/janelia/saalfeldlab/n5/http/HttpReaderFsWriter.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ public <W extends GsonKeyValueN5Writer, R extends GsonKeyValueN5Reader> HttpRead
118118

119119
@Override public DataBlock<?> readBlock(String pathName, DatasetAttributes datasetAttributes, long... gridPosition) throws N5Exception {
120120

121-
return reader.readBlock(pathName, datasetAttributes, gridPosition);
121+
return reader.readBlock(pathName, getConvertedDatasetAttributes(datasetAttributes), gridPosition);
122122
}
123123

124124
@Override public <T> T readSerializedBlock(String dataset, DatasetAttributes attributes, long... gridPosition) throws N5Exception, ClassNotFoundException {
125125

126-
return reader.readSerializedBlock(dataset, attributes, gridPosition);
126+
return reader.readSerializedBlock(dataset, getConvertedDatasetAttributes(attributes), gridPosition);
127127
}
128128

129129
@Override public KeyValueAccess getKeyValueAccess() {
@@ -262,12 +262,13 @@ public <W extends GsonKeyValueN5Writer, R extends GsonKeyValueN5Reader> HttpRead
262262

263263
@Override public DatasetAttributes createDataset(String datasetPath, DatasetAttributes datasetAttributes) throws N5Exception {
264264

265-
writer.createDataset(datasetPath, datasetAttributes);
266-
return datasetAttributes;
265+
DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
266+
writer.createDataset(datasetPath, convertedDatasetAttributes);
267+
return convertedDatasetAttributes;
267268
}
268269

269270
@Override public <T> void writeBlock(String datasetPath, DatasetAttributes datasetAttributes, DataBlock<T> dataBlock) throws N5Exception {
270-
writer.writeBlock(datasetPath, datasetAttributes, dataBlock);
271+
writer.writeBlock(datasetPath, getConvertedDatasetAttributes(datasetAttributes), dataBlock);
271272
}
272273

273274
@Override public boolean deleteBlock(String datasetPath, long... gridPosition) throws N5Exception {
@@ -277,7 +278,7 @@ public <W extends GsonKeyValueN5Writer, R extends GsonKeyValueN5Reader> HttpRead
277278

278279
@Override public void writeSerializedBlock(Serializable object, String datasetPath, DatasetAttributes datasetAttributes, long... gridPosition) throws N5Exception {
279280

280-
writer.writeSerializedBlock(object, datasetPath, datasetAttributes, gridPosition);
281+
writer.writeSerializedBlock(object, datasetPath, getConvertedDatasetAttributes(datasetAttributes), gridPosition);
281282
}
282283

283284
@Override public void setVersion(String path) {
@@ -302,6 +303,6 @@ public <W extends GsonKeyValueN5Writer, R extends GsonKeyValueN5Reader> HttpRead
302303

303304
@Override public <T> void writeBlocks(String datasetPath, DatasetAttributes datasetAttributes, DataBlock<T>... dataBlocks) throws N5Exception {
304305

305-
writer.writeBlocks(datasetPath, datasetAttributes, dataBlocks);
306+
writer.writeBlocks(datasetPath, getConvertedDatasetAttributes(datasetAttributes), dataBlocks);
306307
}
307308
}

0 commit comments

Comments
 (0)