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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.nio.file.FileAlreadyExistsException;

import static com.google.common.base.Preconditions.checkArgument;
import static io.trino.filesystem.gcs.GcsUtils.getBlob;
import static io.trino.filesystem.gcs.GcsUtils.handleGcsException;
import static java.net.HttpURLConnection.HTTP_PRECON_FAILED;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -65,9 +64,6 @@ public void createExclusive(byte[] data)
throws IOException
{
try {
if (getBlob(storage, location).isPresent()) {
throw new FileAlreadyExistsException("File %s already exists".formatted(location));
}
storage.create(blobInfo(), data, BlobTargetOption.doesNotExist());
}
catch (RuntimeException e) {
Expand All @@ -81,9 +77,6 @@ public OutputStream create(AggregatedMemoryContext memoryContext)
throws IOException
{
try {
if (getBlob(storage, location).isPresent()) {
throw new FileAlreadyExistsException("File %s already exists".formatted(location));
}
WriteChannel writeChannel = storage.writer(blobInfo(), BlobWriteOption.doesNotExist());
return new GcsOutputStream(location, writeChannel, memoryContext, writeBlockSizeBytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
package io.trino.filesystem.gcs;

import com.google.cloud.WriteChannel;
import com.google.cloud.storage.StorageException;
import com.google.common.primitives.Ints;
import io.trino.memory.context.AggregatedMemoryContext;
import io.trino.memory.context.LocalMemoryContext;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.file.FileAlreadyExistsException;

import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.Math.min;
import static java.net.HttpURLConnection.HTTP_PRECON_FAILED;
import static java.util.Objects.requireNonNull;

public class GcsOutputStream
Expand Down Expand Up @@ -134,6 +137,11 @@ public void close()
try {
writeChannel.close();
}
catch (StorageException e) {
if (e.getCode() == HTTP_PRECON_FAILED) {
throw new FileAlreadyExistsException(location.toString());
}
}
catch (IOException e) {
throw new IOException("Error closing file: " + location, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ void testOutputFile()

if (isCreateExclusive()) {
// re-create without overwrite is an error
assertThatThrownBy(outputFile::create)
assertThatThrownBy(() -> outputFile.create().close())
.isInstanceOf(FileAlreadyExistsException.class)
.hasMessageContaining(tempBlob.location().toString());

Expand Down