Skip to content

Commit

Permalink
perf: throw N5NoSuchKeyException if no key when reading
Browse files Browse the repository at this point in the history
  • Loading branch information
bogovicj committed Jul 12, 2024
1 parent fe02901 commit cbb6ccb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<!-- NB: Deploy releases to the SciJava Maven repository. -->
<releaseProfiles>sign,deploy-to-scijava</releaseProfiles>

<n5.version>3.2.0</n5.version>
<n5.version>3.2.1-SNAPSHOT</n5.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
package org.janelia.saalfeldlab.n5.googlecloud;

import com.google.api.gax.paging.Page;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobField;
import com.google.cloud.storage.Storage.BlobListOption;
import org.janelia.saalfeldlab.googlecloud.GoogleCloudStorageURI;
import org.janelia.saalfeldlab.googlecloud.GoogleCloudUtils;
import org.janelia.saalfeldlab.n5.KeyValueAccess;
import org.janelia.saalfeldlab.n5.LockedChannel;
import org.janelia.saalfeldlab.n5.N5Exception;
import org.janelia.saalfeldlab.n5.N5URI;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -35,6 +19,23 @@
import java.util.List;
import java.util.stream.Collectors;

import org.janelia.saalfeldlab.googlecloud.GoogleCloudStorageURI;
import org.janelia.saalfeldlab.googlecloud.GoogleCloudUtils;
import org.janelia.saalfeldlab.n5.KeyValueAccess;
import org.janelia.saalfeldlab.n5.LockedChannel;
import org.janelia.saalfeldlab.n5.N5Exception;
import org.janelia.saalfeldlab.n5.N5URI;

import com.google.api.gax.paging.Page;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobField;
import com.google.cloud.storage.Storage.BlobListOption;

public class GoogleCloudStorageKeyValueAccess implements KeyValueAccess {

private final Storage storage;
Expand All @@ -45,7 +46,7 @@ protected static GoogleCloudStorageURI uncheckedContainerLocationStringToGoogleU

try {
return new GoogleCloudStorageURI(uri);
} catch (Exception e) {
} catch (final Exception e) {
throw new N5Exception("Container location " + uri + " is an invalid URI", e);
}
}
Expand Down Expand Up @@ -167,7 +168,7 @@ public String relativize(final String path, final String base) {
* case, since we only care about the relative portion of `path` to `base`, so the result always
* ignores the absolute prefix anyway. */
return GoogleCloudUtils.getGoogleCloudStorageKey(normalize(uri("/" + base).relativize(uri("/" + path)).getPath()));
} catch (URISyntaxException e) {
} catch (final URISyntaxException e) {
throw new N5Exception("Cannot relativize path (" + path + ") with base (" + base + ")", e);
}
}
Expand Down Expand Up @@ -247,6 +248,7 @@ private boolean keyExists(final String key) {

private static boolean blobExists(final Blob blob) {

// TODO document this
return blob != null && blob.exists();
}

Expand Down Expand Up @@ -440,7 +442,8 @@ public InputStream newInputStream() {

final Blob blob = storage.get(BlobId.of(bucketName, path));
if (!blobExists(blob))
return null;
throw new N5Exception.N5NoSuchKeyException("No such key: " + path);

final InputStream in = Channels.newInputStream(blob.reader());
synchronized (resources) {
resources.add(in);
Expand All @@ -453,7 +456,8 @@ public Reader newReader() {

final Blob blob = storage.get(BlobId.of(bucketName, path));
if (!blobExists(blob))
return null;
throw new N5Exception.N5NoSuchKeyException("No such key: " + path);

final Reader in = Channels.newReader(blob.reader(), StandardCharsets.UTF_8.name());
synchronized (resources) {
resources.add(in);
Expand Down

0 comments on commit cbb6ccb

Please sign in to comment.