Skip to content

Commit

Permalink
Merge pull request #124 from saalfeldlab/perf/skipIsFile
Browse files Browse the repository at this point in the history
perf skip isFile check during readBlock
  • Loading branch information
bogovicj authored Jul 15, 2024
2 parents 6b6d4d2 + 6fe8f29 commit 1630a27
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
*/
package org.janelia.saalfeldlab.n5;

import java.io.IOException;
import java.lang.reflect.Type;

import com.google.gson.JsonSyntaxException;
Expand Down Expand Up @@ -233,20 +232,6 @@ default String[] listFromContainer(final String normalPathName) {
return GsonKeyValueN5Reader.super.list(normalPathName);
}

@Override
default String absoluteDataBlockPath(
final String normalPath,
final long... gridPosition) {

final String[] components = new String[gridPosition.length + 1];
components[0] = normalPath;
int i = 0;
for (final long p : gridPosition)
components[++i] = Long.toString(p);

return getKeyValueAccess().compose(getURI(), components);
}

/**
* Check for attributes that are required for a group to be a dataset.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ public FileSystemKeyValueAccess(final FileSystem fileSystem) {
@Override
public LockedFileChannel lockForReading(final String normalPath) throws IOException {

return new LockedFileChannel(normalPath, true);
try {
return new LockedFileChannel(normalPath, true);
} catch (NoSuchFileException e) {
throw new N5Exception.N5NoSuchKeyException("No such file", e);
}
}

@Override
Expand All @@ -171,7 +175,11 @@ public LockedFileChannel lockForWriting(final String normalPath) throws IOExcept

public LockedFileChannel lockForReading(final Path path) throws IOException {

return new LockedFileChannel(path, true);
try {
return new LockedFileChannel(path, true);
} catch (NoSuchFileException e) {
throw new N5Exception.N5NoSuchKeyException("No such file", e);
}
}

public LockedFileChannel lockForWriting(final Path path) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ default DataBlock<?> readBlock(
final long... gridPosition) throws N5Exception {

final String path = absoluteDataBlockPath(N5URI.normalizeGroupPath(pathName), gridPosition);
if (!getKeyValueAccess().isFile(path))
return null;

try (final LockedChannel lockedChannel = getKeyValueAccess().lockForReading(path)) {
return DefaultBlockReader.readBlock(lockedChannel.newInputStream(), datasetAttributes, gridPosition);
} catch (final N5Exception.N5NoSuchKeyException e) {
return null;
} catch (final IOException | UncheckedIOException e) {
throw new N5IOException(
"Failed to read block " + Arrays.toString(gridPosition) + " from dataset " + path,
Expand Down Expand Up @@ -137,14 +137,13 @@ default String absoluteDataBlockPath(
final String normalPath,
final long... gridPosition) {

final String[] components = new String[gridPosition.length + 2];
components[0] = getURI().getPath();
components[1] = normalPath;
int i = 1;
final String[] components = new String[gridPosition.length + 1];
components[0] = normalPath;
int i = 0;
for (final long p : gridPosition)
components[++i] = Long.toString(p);

return getKeyValueAccess().compose(components);
return getKeyValueAccess().compose(getURI(), components);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/janelia/saalfeldlab/n5/N5Exception.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,31 @@ protected N5ClassCastException(
super(message, cause, enableSuppression, writableStackTrace);
}
}

public static class N5NoSuchKeyException extends N5IOException {

public N5NoSuchKeyException(final String message) {

super(message);
}

public N5NoSuchKeyException(final String message, final Throwable cause) {

super(message, cause);
}

public N5NoSuchKeyException(final Throwable cause) {

super(cause);
}

protected N5NoSuchKeyException(
final String message,
final Throwable cause,
final boolean enableSuppression,
final boolean writableStackTrace) {

super(message, cause, enableSuppression, writableStackTrace);
}
}
}

0 comments on commit 1630a27

Please sign in to comment.