-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Use Stream#gather API instead of Guava code #27523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,15 +57,16 @@ | |
| import static com.google.cloud.storage.Storage.SignUrlOption.withExtHeaders; | ||
| import static com.google.cloud.storage.Storage.SignUrlOption.withV4Signature; | ||
| import static com.google.common.base.Preconditions.checkState; | ||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
| import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
| import static com.google.common.collect.Iterables.partition; | ||
| import static io.airlift.concurrent.MoreFutures.getFutureValue; | ||
| import static io.trino.filesystem.gcs.GcsUtils.encodedKey; | ||
| import static io.trino.filesystem.gcs.GcsUtils.getBlob; | ||
| import static io.trino.filesystem.gcs.GcsUtils.handleGcsException; | ||
| import static io.trino.filesystem.gcs.GcsUtils.keySha256Checksum; | ||
| import static java.util.Objects.requireNonNull; | ||
| import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
| import static java.util.stream.Gatherers.windowFixed; | ||
|
|
||
| public class GcsFileSystem | ||
| implements TrinoFileSystem | ||
|
|
@@ -164,16 +165,19 @@ public void deleteFile(Location location) | |
| public void deleteFiles(Collection<Location> locations) | ||
| throws IOException | ||
| { | ||
| List<ListenableFuture<?>> batchFutures = new ArrayList<>(); | ||
| List<ListenableFuture<?>> batchFutures = locations.stream() | ||
| .gather(windowFixed(batchSize)) | ||
| .map(locationBatch -> { | ||
| StorageBatch batch = storage.batch(); | ||
| for (Location location : locationBatch) { | ||
| GcsLocation gcsLocation = new GcsLocation(location); | ||
| batch.delete(BlobId.of(gcsLocation.bucket(), gcsLocation.path())); | ||
| } | ||
| return executorService.submit(batch::submit); | ||
| }) | ||
| .collect(toImmutableList()); | ||
|
|
||
| try { | ||
| for (List<Location> locationBatch : partition(locations, batchSize)) { | ||
| StorageBatch batch = storage.batch(); | ||
| for (Location location : locationBatch) { | ||
| GcsLocation gcsLocation = new GcsLocation(location); | ||
| batch.delete(BlobId.of(gcsLocation.bucket(), gcsLocation.path())); | ||
| } | ||
| batchFutures.add(executorService.submit(batch::submit)); | ||
| } | ||
| getFutureValue(Futures.allAsList(batchFutures)); | ||
| } | ||
| catch (RuntimeException e) { | ||
|
|
@@ -186,16 +190,19 @@ public void deleteDirectory(Location location) | |
| throws IOException | ||
| { | ||
| GcsLocation gcsLocation = new GcsLocation(normalizeToDirectory(location)); | ||
|
|
||
| List<ListenableFuture<?>> batchFutures = getPage(gcsLocation).streamAll() | ||
| .gather(windowFixed(batchSize)) | ||
| .map(blobBatch -> { | ||
| StorageBatch batch = storage.batch(); | ||
| for (Blob blob : blobBatch) { | ||
| batch.delete(blob.getBlobId()); | ||
| } | ||
| return executorService.submit(batch::submit); | ||
| }) | ||
| .collect(toImmutableList()); | ||
|
|
||
| try { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here -- code moved outside of try |
||
| List<ListenableFuture<?>> batchFutures = new ArrayList<>(); | ||
|
|
||
| for (List<Blob> blobBatch : partition(getPage(gcsLocation).iterateAll(), batchSize)) { | ||
| StorageBatch batch = storage.batch(); | ||
| for (Blob blob : blobBatch) { | ||
| batch.delete(blob.getBlobId()); | ||
| } | ||
| batchFutures.add(executorService.submit(batch::submit)); | ||
| } | ||
| getFutureValue(Futures.allAsList(batchFutures)); | ||
| } | ||
| catch (RuntimeException e) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,7 +81,6 @@ | |
| import com.google.common.base.Splitter; | ||
| import com.google.common.collect.AbstractSequentialIterator; | ||
| import com.google.common.collect.ImmutableSet; | ||
| import com.google.common.collect.Iterables; | ||
| import com.google.common.collect.Iterators; | ||
| import com.google.common.io.Closer; | ||
| import com.google.common.net.MediaType; | ||
|
|
@@ -185,6 +184,7 @@ | |
| import static java.util.concurrent.TimeUnit.SECONDS; | ||
| import static java.util.stream.Collectors.joining; | ||
| import static java.util.stream.Collectors.toList; | ||
| import static java.util.stream.Gatherers.windowFixed; | ||
| import static org.apache.hadoop.fs.FSExceptionMessages.CANNOT_SEEK_PAST_EOF; | ||
| import static org.apache.hadoop.fs.FSExceptionMessages.NEGATIVE_SEEK; | ||
| import static org.apache.hadoop.fs.FSExceptionMessages.STREAM_IS_CLOSED; | ||
|
|
@@ -775,10 +775,9 @@ public void deleteFiles(Collection<Path> paths) | |
| throws IOException | ||
| { | ||
| try { | ||
| Iterable<List<Path>> partitions = Iterables.partition(paths, DELETE_BATCH_SIZE); | ||
| for (List<Path> currentBatch : partitions) { | ||
| deletePaths(currentBatch); | ||
| } | ||
| paths.stream() | ||
| .gather(windowFixed(DELETE_BATCH_SIZE)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. windowFixed involves unnecessary data copying which it's also not superior on readability aspect. we should use |
||
| .forEach(this::deletePaths); | ||
| } | ||
| catch (MultiObjectDeleteException e) { | ||
| String errors = e.getErrors().stream() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did it flow outside of the try catch block?