Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/main/java/org/janelia/saalfeldlab/n5/DataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
* Enumerates available data types.
*
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/org/janelia/saalfeldlab/n5/DatasetAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public class DatasetAttributes implements Serializable {

private final long[] dimensions;

// number of samples per chunk per dimension
private final int[] chunkSize;

// number of samples per block per dimension
// identical to chunkSize for non-sharded datasets
private final int[] blockSize;

// TODO add a getter?
// the shard size
private final int[] outerBlockSize;

private final DataType dataType;

private final JsonElement defaultValue;
Expand All @@ -108,7 +108,7 @@ public class DatasetAttributes implements Serializable {

public DatasetAttributes(
final long[] dimensions,
final int[] outerBlockSize,
final int[] blockSize,
final DataType dataType,
final JsonElement defaultValue,
final BlockCodecInfo blockCodecInfo,
Expand All @@ -117,7 +117,7 @@ public DatasetAttributes(

this.dimensions = dimensions;
this.dataType = dataType;
this.outerBlockSize = outerBlockSize;
this.blockSize = blockSize;
this.defaultValue = defaultValue == null ? JsonNull.INSTANCE : defaultValue;

this.blockCodecInfo = blockCodecInfo == null ? defaultBlockCodecInfo() : blockCodecInfo;
Expand All @@ -131,7 +131,7 @@ public DatasetAttributes(
.toArray(DataCodecInfo[]::new);

access = createDatasetAccess();
blockSize = access.getGrid().getBlockSize(0);
chunkSize = access.getGrid().getBlockSize(0);
}

public DatasetAttributes(
Expand Down Expand Up @@ -189,7 +189,7 @@ public DatasetAttributes(
this(dimensions, blockSize, dataType, new DataCodecInfo[0]);
}

protected DatasetAccess<?> createDatasetAccess() {
private DatasetAccess<?> createDatasetAccess() {

final int m = nestingDepth(blockCodecInfo);

Expand All @@ -200,7 +200,7 @@ protected DatasetAccess<?> createDatasetAccess() {
// NestedGrid validates block sizes, so instantiate it before creating the blockCodecs
// blockCodecInfo.create below could fail unexpecedly with invalid
// blockSizes so validate first
blockSizes[m - 1] = outerBlockSize;
blockSizes[m - 1] = blockSize;
BlockCodecInfo tmpInfo = blockCodecInfo;
for (int l = m - 1; l > 0; --l) {
final ShardCodecInfo info = (ShardCodecInfo)tmpInfo;
Expand All @@ -210,7 +210,7 @@ protected DatasetAccess<?> createDatasetAccess() {

BlockCodecInfo currentBlockCodecInfo = blockCodecInfo;
DataCodecInfo[] currentDataCodecInfos = dataCodecInfos;

DatasetCodecInfo[] datasetCodecInfos = this.datasetCodecInfos;

final NestedGrid grid = new NestedGrid(blockSizes, dimensions);
Expand Down Expand Up @@ -274,6 +274,11 @@ public int getNumDimensions() {
return dimensions.length;
}

public int[] getChunkSize() {

return chunkSize;
}

public int[] getBlockSize() {

return blockSize;
Expand Down Expand Up @@ -318,9 +323,9 @@ public DataType getDataType() {
*
* @return the {@code DatasetAccess} for this dataset
*/
<T> DatasetAccess<T> getDatasetAccess() {
protected <T> DatasetAccess<T> getDatasetAccess() {

return (DatasetAccess<T>)access;
return (DatasetAccess<T>) access;
}

/**
Expand All @@ -334,7 +339,6 @@ public NestedGrid getNestedBlockGrid() {
return getDatasetAccess().getGrid();
}


public BlockCodecInfo getBlockCodecInfo() {

return blockCodecInfo;
Expand All @@ -359,7 +363,7 @@ public HashMap<String, Object> asMap() {

final HashMap<String, Object> map = new HashMap<>();
map.put(DIMENSIONS_KEY, dimensions);
map.put(BLOCK_SIZE_KEY, blockSize);
map.put(BLOCK_SIZE_KEY, chunkSize);
map.put(DATA_TYPE_KEY, dataType);
map.put(COMPRESSION_KEY, getCompression());
return map;
Expand Down Expand Up @@ -425,7 +429,7 @@ public static class DatasetAttributesAdapter implements JsonSerializer<DatasetAt

final JsonObject obj = new JsonObject();
obj.add(DIMENSIONS_KEY, context.serialize(src.dimensions));
obj.add(BLOCK_SIZE_KEY, context.serialize(src.blockSize));
obj.add(BLOCK_SIZE_KEY, context.serialize(src.chunkSize));
obj.add(DATA_TYPE_KEY, context.serialize(src.dataType));

final DataCodecInfo[] codecs = src.dataCodecInfos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ default JsonElement getAttributes(final String pathName) throws N5Exception {
}

@Override
default <T> DataBlock<T> readBlock(
default <T> DataBlock<T> readChunk(
final String pathName,
final DatasetAttributes datasetAttributes,
final long... gridPosition) throws N5Exception {
Expand All @@ -103,26 +103,26 @@ default <T> DataBlock<T> readBlock(
try {
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(pathName),
convertedDatasetAttributes);
return convertedDatasetAttributes.<T> getDatasetAccess().readBlock(posKva, gridPosition);
return convertedDatasetAttributes.<T> getDatasetAccess().readChunk(posKva, gridPosition);

} catch (N5Exception.N5NoSuchKeyException e) {
return null;
}
}

@Override
default <T> List<DataBlock<T>> readBlocks(
default <T> List<DataBlock<T>> readChunks(
final String pathName,
final DatasetAttributes datasetAttributes,
final List<long[]> blockPositions) throws N5Exception {

DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(pathName), convertedDatasetAttributes);
return convertedDatasetAttributes.<T> getDatasetAccess().readBlocks(posKva, blockPositions);
return convertedDatasetAttributes.<T> getDatasetAccess().readChunks(posKva, blockPositions);
}

@Override
default <T> DataBlock<T> readShard(
default <T> DataBlock<T> readBlock(
final String pathName,
final DatasetAttributes datasetAttributes,
final long... gridPosition) throws N5Exception {
Expand All @@ -132,7 +132,7 @@ default <T> DataBlock<T> readShard(
try {
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(pathName),
convertedDatasetAttributes);
return convertedDatasetAttributes.<T> getDatasetAccess().readShard(posKva, gridPosition, shardLevel);
return convertedDatasetAttributes.<T> getDatasetAccess().readBlock(posKva, gridPosition, shardLevel);

} catch (N5Exception.N5NoSuchKeyException e) {
return null;
Expand Down Expand Up @@ -172,7 +172,7 @@ default String absoluteAttributesPath(final String normalPath) {
}

@Override
default boolean shardExists(
default boolean blockExists(
final String pathName,
final DatasetAttributes datasetAttributes,
final long... gridPosition) throws N5Exception {
Expand Down
50 changes: 30 additions & 20 deletions src/main/java/org/janelia/saalfeldlab/n5/GsonKeyValueN5Writer.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ default void createGroup(final String path) throws N5Exception {

/**
* Helper method that writes an attributes tree into the store
*
* <p>
* TODO This method is not part of the public API and should be protected
* in Java versions greater than 8
*
Expand Down Expand Up @@ -225,12 +225,12 @@ default <T> void writeRegion(
final DatasetAttributes datasetAttributes,
final long[] min,
final long[] size,
final DataBlockSupplier<T> dataBlocks,
final DataBlockSupplier<T> chunkSupplier,
final boolean writeFully) throws N5Exception {
DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
try {
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(datasetPath), convertedDatasetAttributes);
convertedDatasetAttributes.<T>getDatasetAccess().writeRegion(posKva, min, size, dataBlocks, writeFully);
convertedDatasetAttributes.<T>getDatasetAccess().writeRegion(posKva, min, size, chunkSupplier, writeFully);
} catch (final UncheckedIOException e) {
throw new N5IOException(
"Failed to write blocks into dataset " + datasetPath, e);
Expand All @@ -243,66 +243,66 @@ default <T> void writeRegion(
final DatasetAttributes datasetAttributes,
final long[] min,
final long[] size,
final DataBlockSupplier<T> dataBlocks,
final DataBlockSupplier<T> chunkSupplier,
final boolean writeFully,
final ExecutorService exec) throws N5Exception, InterruptedException, ExecutionException {
DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
try {
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(datasetPath), convertedDatasetAttributes);
convertedDatasetAttributes.<T>getDatasetAccess().writeRegion(posKva, min, size, dataBlocks, writeFully, exec);
convertedDatasetAttributes.<T>getDatasetAccess().writeRegion(posKva, min, size, chunkSupplier, writeFully, exec);
} catch (final UncheckedIOException e) {
throw new N5IOException(
"Failed to write blocks into dataset " + datasetPath, e);
}
}

@Override
default <T> void writeBlocks(
default <T> void writeChunks(
final String datasetPath,
final DatasetAttributes datasetAttributes,
final DataBlock<T>... dataBlocks) throws N5Exception {
final DataBlock<T>... chunks) throws N5Exception {

DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
try {
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(datasetPath), convertedDatasetAttributes);
convertedDatasetAttributes.<T>getDatasetAccess().writeBlocks(posKva, Arrays.asList(dataBlocks));
convertedDatasetAttributes.<T>getDatasetAccess().writeChunks(posKva, Arrays.asList(chunks));
} catch (final UncheckedIOException e) {
throw new N5IOException(
"Failed to write blocks into dataset " + datasetPath, e);
"Failed to write chunks into dataset " + datasetPath, e);
}
}

@Override
default <T> void writeBlock(
default <T> void writeChunk(
final String path,
final DatasetAttributes datasetAttributes,
final DataBlock<T> dataBlock) throws N5Exception {
final DataBlock<T> chunk) throws N5Exception {

DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
try {
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(path), convertedDatasetAttributes);
convertedDatasetAttributes.<T> getDatasetAccess().writeBlock(posKva, dataBlock);
convertedDatasetAttributes.<T> getDatasetAccess().writeChunk(posKva, chunk);
} catch (final UncheckedIOException e) {
throw new N5IOException(
"Failed to write block " + Arrays.toString(dataBlock.getGridPosition()) + " into dataset " + path,
"Failed to write chunk " + Arrays.toString(chunk.getGridPosition()) + " into dataset " + path,
e);
}
}

@Override
default <T> void writeShard(
default <T> void writeBlock(
final String path,
final DatasetAttributes datasetAttributes,
final DataBlock<T> shard) throws N5Exception {
final DataBlock<T> dataBlock) throws N5Exception {

final DatasetAttributes convertedDatasetAttributes = getConvertedDatasetAttributes(datasetAttributes);
final int shardLevel = convertedDatasetAttributes.getNestedBlockGrid().numLevels() - 1;
try {
final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(path), convertedDatasetAttributes);
convertedDatasetAttributes.<T> getDatasetAccess().writeShard(posKva, shard, shardLevel);
convertedDatasetAttributes.<T> getDatasetAccess().writeBlock(posKva, dataBlock, shardLevel);
} catch (final UncheckedIOException e) {
throw new N5IOException(
"Failed to write block " + Arrays.toString(shard.getGridPosition()) + " into dataset " + path,
"Failed to write block " + Arrays.toString(dataBlock.getGridPosition()) + " into dataset " + path,
e);
}
}
Expand All @@ -326,16 +326,26 @@ default boolean deleteBlock(
final long... gridPosition) throws N5Exception {

final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(path), datasetAttributes);
return datasetAttributes.getDatasetAccess().deleteBlock(posKva, gridPosition);
return posKva.remove(gridPosition);
}

@Override
default boolean deleteChunk(
final String path,
final DatasetAttributes datasetAttributes,
final long... gridPosition) throws N5Exception {

final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(path), datasetAttributes);
return datasetAttributes.getDatasetAccess().deleteChunk(posKva, gridPosition);
}

@Override
default boolean deleteBlocks(
default boolean deleteChunks(
final String path,
final DatasetAttributes datasetAttributes,
final List<long[]> gridPositions) throws N5Exception {

final PositionValueAccess posKva = PositionValueAccess.fromKva(getKeyValueAccess(), getURI(), N5URI.normalizeGroupPath(path), datasetAttributes);
return datasetAttributes.getDatasetAccess().deleteBlocks(posKva, gridPositions);
return datasetAttributes.getDatasetAccess().deleteChunks(posKva, gridPositions);
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/janelia/saalfeldlab/n5/GsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static <T> T readAttribute(
* @param gson
* used to deserialize {@code attribute}
* @param type
* to desrialize {@code attribute} as
* to deserialize {@code attribute} as
* @param <T>
* return type represented by {@link Type type}
* @return the deserialized attribute object, or {@code null} if
Expand Down Expand Up @@ -548,7 +548,7 @@ static <T> void writeAttribute(
}

/**
* Writes the attributes JsonElemnt to a given {@link Writer}.
* Writes the attributes JsonElement to a given {@link Writer}.
* This will overwrite any existing attributes.
*
* @param writer
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/org/janelia/saalfeldlab/n5/N5Exception.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
*/
package org.janelia.saalfeldlab.n5;

import java.io.IOException;
import java.io.UncheckedIOException;

public class N5Exception extends RuntimeException {

public N5Exception() {
Expand Down
Loading
Loading