Skip to content

Commit

Permalink
Remove extra check for object existence in repository-gcs read object
Browse files Browse the repository at this point in the history
  • Loading branch information
tlrx committed Jun 29, 2018
1 parent 7a76e3a commit 077b42f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
Expand Up @@ -54,6 +54,7 @@
import java.util.Map;
import java.util.stream.Collectors;

import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_PRECON_FAILED;

class GoogleCloudStorageBlobStore extends AbstractComponent implements BlobStore {
Expand Down Expand Up @@ -163,16 +164,19 @@ boolean blobExists(String blobName) throws IOException {
*/
InputStream readBlob(String blobName) throws IOException {
final BlobId blobId = BlobId.of(bucketName, blobName);
final Blob blob = SocketAccess.doPrivilegedIOException(() -> client().get(blobId));
if (blob == null) {
throw new NoSuchFileException("Blob [" + blobName + "] does not exit");
}
final ReadChannel readChannel = SocketAccess.doPrivilegedIOException(blob::reader);
final ReadChannel readChannel = SocketAccess.doPrivilegedIOException(() -> client().reader(blobId));
return Channels.newInputStream(new ReadableByteChannel() {
@SuppressForbidden(reason = "Channel is based of a socket not a file")
@Override
public int read(ByteBuffer dst) throws IOException {
return SocketAccess.doPrivilegedIOException(() -> readChannel.read(dst));
try {
return SocketAccess.doPrivilegedIOException(() -> readChannel.read(dst));
} catch (StorageException e) {
if (e.getCode() == HTTP_NOT_FOUND) {
throw new NoSuchFileException("Blob [" + blobName + "] does not exist");
}
throw e;
}
}

@Override
Expand Down
Expand Up @@ -167,7 +167,27 @@ public Iterable<Blob> getValues() {
public ReadChannel reader(BlobId blob, BlobSourceOption... options) {
if (bucketName.equals(blob.getBucket())) {
final byte[] bytes = blobs.get(blob.getName());
final ReadableByteChannel readableByteChannel = Channels.newChannel(new ByteArrayInputStream(bytes));

final ReadableByteChannel readableByteChannel;
if (bytes != null) {
readableByteChannel = Channels.newChannel(new ByteArrayInputStream(bytes));
} else {
readableByteChannel = new ReadableByteChannel() {
@Override
public int read(ByteBuffer dst) throws IOException {
throw new StorageException(404, "Object not found");
}

@Override
public boolean isOpen() {
return false;
}

@Override
public void close() throws IOException {
}
};
}
return new ReadChannel() {
@Override
public void close() {
Expand Down
Expand Up @@ -49,7 +49,11 @@ public abstract class ESBlobStoreContainerTestCase extends ESTestCase {
public void testReadNonExistingPath() throws IOException {
try(BlobStore store = newBlobStore()) {
final BlobContainer container = store.blobContainer(new BlobPath());
expectThrows(NoSuchFileException.class, () -> container.readBlob("non-existing"));
expectThrows(NoSuchFileException.class, () -> {
try (InputStream is = container.readBlob("non-existing")) {
is.read();
}
});
}
}

Expand Down

0 comments on commit 077b42f

Please sign in to comment.