Remove extra check for object existence in repository-gcs read object (#31661)

This commit is contained in:
Tanguy Leroux 2018-06-29 13:52:31 +02:00 committed by GitHub
parent 117e9066db
commit d8b3f332ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 8 deletions

View File

@ -54,6 +54,7 @@ import java.util.List;
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 {
@ -163,16 +164,19 @@ class GoogleCloudStorageBlobStore extends AbstractComponent implements BlobStore
*/
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

View File

@ -167,7 +167,27 @@ class MockStorage implements Storage {
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() {

View File

@ -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();
}
});
}
}