Skip to content

Commit

Permalink
Use removeBlobs() for bulk-delete.
Browse files Browse the repository at this point in the history
We should use removeBlobs() for bulk-delete. The change deletes blobs
en-masse. Further, we no longer will perform a HEAD on every blob, as
that's fairly expensive.

Fixes #38
  • Loading branch information
Timur Alperovich committed Jun 13, 2015
1 parent b2c46e6 commit c96cff5
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/main/java/com/bouncestorage/swiftproxy/v1/AccountResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@

package com.bouncestorage.swiftproxy.v1;

import static java.util.Objects.requireNonNull;

import static com.google.common.base.Throwables.propagate;
import static java.util.Objects.requireNonNull;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -151,25 +153,28 @@ public BulkDeleteResult bulkDelete(@NotNull @PathParam("account") String account
}

BulkDeleteResult result = new BulkDeleteResult();
Map<String, List<String>> removeBlobsMap = new HashMap<>();
List<String> deleteContainers = new LinkedList<>();
for (String objectContainer : objects) {
try {
if (objectContainer.startsWith("/")) {
objectContainer = objectContainer.substring(1);
}
int separatorIndex = objectContainer.indexOf('/');
if (separatorIndex < 0) {
blobStore.deleteContainer(objectContainer.substring(1));
result.numberDeleted += 1;
deleteContainers.add(objectContainer.substring(1));
continue;
}
String container = objectContainer.substring(0, separatorIndex);
String object = objectContainer.substring(separatorIndex + 1);

if (!removeBlobsMap.containsKey(container)) {
removeBlobsMap.put(container, new LinkedList<>());
}
if (!blobStore.blobExists(container, object)) {
result.numberNotFound += 1;
} else {
blobStore.removeBlob(container, object);
result.numberDeleted += 1;
removeBlobsMap.get(container).add(object);
}
} catch (ContainerNotFoundException e) {
result.numberNotFound += 1;
Expand All @@ -179,6 +184,29 @@ public BulkDeleteResult bulkDelete(@NotNull @PathParam("account") String account
}
}

removeBlobsMap.forEach((container, blobList) -> {
try {
blobStore.removeBlobs(container, blobList);
result.numberDeleted += blobList.size();
} catch (ContainerNotFoundException e) {
result.numberNotFound += blobList.size();
} catch (Exception e) {
e.printStackTrace();
blobList.forEach(blob -> result.errors.add(container + "/" + blob));
}
});
deleteContainers.forEach(container -> {
try {
blobStore.deleteContainer(container);
result.numberDeleted += 1;
} catch (ContainerNotFoundException e) {
result.numberNotFound += 1;
} catch (Exception e) {
e.printStackTrace();
result.errors.add(container);
}
});

if (result.errors.isEmpty()) {
result.responseStatus = Response.Status.OK.toString();
return result;
Expand Down

0 comments on commit c96cff5

Please sign in to comment.