Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@

<!-- NB: Deploy releases to the SciJava Maven repository. -->
<releaseProfiles>sign,deploy-to-scijava</releaseProfiles>
<n5.version>4.0.0-alpha-9</n5.version>
<n5.version>4.0.0-alpha-12-SNAPSHOT</n5.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,9 @@ public DatasetAttributes getDatasetAttributes(String pathName) {
new RawCompression());
}

@SuppressWarnings("unchecked")
@Override
public DataBlock<?> readBlock(
public <T> DataBlock<T> readBlock(
String pathName,
final DatasetAttributes datasetAttributes,
final long... gridPosition) throws N5Exception {
Expand All @@ -713,7 +714,7 @@ public DataBlock<?> readBlock(
if (datasetAttributes.getDataType() == DataType.STRING) {
final int[] intHdf5CroppedBlockSize = Arrays.stream(hdf5CroppedBlockSize).mapToInt(i -> (int)i).toArray();
MDArray<String> data = reader.string().readMDArrayBlockWithOffset(normalizedPathName, intHdf5CroppedBlockSize, hdf5Offset);
return new StringDataBlock(croppedBlockSize, gridPosition, data.getAsFlatArray());
return (DataBlock<T>)new StringDataBlock(croppedBlockSize, gridPosition, data.getAsFlatArray());
}

final DataType dataType = datasetAttributes.getDataType();
Expand All @@ -733,7 +734,7 @@ public DataBlock<?> readBlock(
H5Sclose(fileSpaceId);
H5Sclose(memorySpaceId);
}
return block;
return (DataBlock<T>)block;
}

@Override
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/org/janelia/saalfeldlab/n5/hdf5/N5HDF5Writer.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
import org.janelia.saalfeldlab.n5.N5Writer;
import org.janelia.saalfeldlab.n5.RawCompression;
import org.janelia.saalfeldlab.n5.hdf5.N5HDF5Util.OpenDataSetCache.OpenDataSet;
import org.janelia.saalfeldlab.n5.shard.Region;
import org.janelia.saalfeldlab.n5.shard.Nesting.NestedGrid;
import org.janelia.saalfeldlab.n5.shard.Nesting.NestedPosition;

import java.io.File;
import java.io.IOException;
Expand All @@ -55,6 +58,8 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

import static hdf.hdf5lib.H5.H5Dget_space;
import static hdf.hdf5lib.H5.H5Dwrite;
Expand Down Expand Up @@ -564,6 +569,50 @@ public <T> void writeShard(
writeBlock(pathName, datasetAttributes, dataBlock);
}

@Override
public <T> void writeRegion(
String datasetPath,
DatasetAttributes datasetAttributes,
long[] min,
long[] size,
DataBlockSupplier<T> dataBlocks,
boolean writeFully) throws N5Exception {

final NestedGrid grid = datasetAttributes.getNestedBlockGrid();
final Region region = new Region(min, size, grid);
for (long[] key : Region.gridPositions(region.minPos().key(), region.maxPos().key())) {
final NestedPosition pos = grid.nestedPosition(key, 0); // HDF5 is never nested, get level 0
final long[] gridPosition = pos.absolute(0);
final DataBlock<T> existingDataBlock = writeFully || region.fullyContains(pos)
? null
: readBlock(datasetPath, datasetAttributes, gridPosition);
final DataBlock<T> dataBlock = dataBlocks.get(gridPosition, existingDataBlock);
// null blocks may be provided when they contain only the fill value
// and only non-empty blocks should be written, for example
if (dataBlock == null) {
deleteBlock(datasetPath, datasetAttributes, gridPosition);
} else {
writeBlock(datasetPath, datasetAttributes, dataBlock);
}
}

}

public <T> void writeRegion(
String datasetPath,
DatasetAttributes datasetAttributes,
long[] min,
long[] size,
DataBlockSupplier<T> dataBlocks,
boolean writeFully,
ExecutorService exec) throws N5Exception, InterruptedException, ExecutionException {

// block until the write is complete
exec.submit(() -> {
writeRegion(datasetPath, datasetAttributes, min, size, dataBlocks, writeFully);
}).get();
}

@Override
public boolean deleteBlock(String pathName, final long... gridPosition) throws N5Exception {

Expand Down
Loading