Skip to content

Commit 5196329

Browse files
committed
Fix deleteByPrefix to get all keys first
1 parent e4fc125 commit 5196329

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

core/src/main/java/com/scalar/db/storage/objectstorage/cloudstorage/CloudStorageWrapper.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.scalar.db.storage.objectstorage.cloudstorage;
22

3-
import com.google.api.gax.paging.Page;
43
import com.google.cloud.WriteChannel;
54
import com.google.cloud.storage.Blob;
65
import com.google.cloud.storage.BlobId;
@@ -18,7 +17,6 @@
1817
import java.io.IOException;
1918
import java.nio.ByteBuffer;
2019
import java.nio.charset.StandardCharsets;
21-
import java.util.ArrayList;
2220
import java.util.List;
2321
import java.util.Optional;
2422
import java.util.Set;
@@ -179,24 +177,22 @@ public void delete(String key, String version) throws ObjectStorageWrapperExcept
179177
@Override
180178
public void deleteByPrefix(String prefix) throws ObjectStorageWrapperException {
181179
try {
182-
Page<Blob> page = storage.list(bucket, Storage.BlobListOption.prefix(prefix));
183-
while (page != null) {
184-
// Collect BlobIds to delete
185-
List<BlobId> blobIds = new ArrayList<>();
186-
for (Blob blob : page.getValues()) {
187-
blobIds.add(BlobId.of(bucket, blob.getName()));
188-
}
189-
// Delete objects in batches
190-
for (int i = 0; i < blobIds.size(); i += BATCH_DELETE_SIZE_LIMIT) {
191-
int endIndex = Math.min(i + BATCH_DELETE_SIZE_LIMIT, blobIds.size());
192-
List<BlobId> batch = blobIds.subList(i, endIndex);
193-
StorageBatch storageBatch = storage.batch();
194-
for (BlobId blobId : batch) {
195-
storageBatch.delete(blobId);
196-
}
197-
storageBatch.submit();
180+
// Collect all blob IDs with the specified prefix
181+
Iterable<Blob> blobs =
182+
storage.list(bucket, Storage.BlobListOption.prefix(prefix)).iterateAll();
183+
List<BlobId> blobIds =
184+
StreamSupport.stream(blobs.spliterator(), false)
185+
.map(blob -> BlobId.of(bucket, blob.getName()))
186+
.collect(Collectors.toList());
187+
// Delete blobs in batches
188+
for (int i = 0; i < blobIds.size(); i += BATCH_DELETE_SIZE_LIMIT) {
189+
int endIndex = Math.min(i + BATCH_DELETE_SIZE_LIMIT, blobIds.size());
190+
List<BlobId> batch = blobIds.subList(i, endIndex);
191+
StorageBatch storageBatch = storage.batch();
192+
for (BlobId blobId : batch) {
193+
storageBatch.delete(blobId);
198194
}
199-
page = page.getNextPage();
195+
storageBatch.submit();
200196
}
201197
} catch (Exception e) {
202198
throw new ObjectStorageWrapperException(

0 commit comments

Comments
 (0)