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 15, 2015
1 parent b2c46e6 commit 52aa43b
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions src/main/java/com/bouncestorage/swiftproxy/v1/AccountResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

import static java.util.Objects.requireNonNull;

import static com.google.common.base.Throwables.getStackTraceAsString;
import static com.google.common.base.Throwables.propagate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
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,34 +154,59 @@ public BulkDeleteResult bulkDelete(@NotNull @PathParam("account") String account
}

BulkDeleteResult result = new BulkDeleteResult();
Map<String, List<String>> removeBlobsMap = new HashMap<>();
List<String> deleteContainers = new ArrayList<>();
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 (!blobStore.blobExists(container, object)) {
result.numberNotFound += 1;
} else {
blobStore.removeBlob(container, object);
result.numberDeleted += 1;
if (!removeBlobsMap.containsKey(container)) {
removeBlobsMap.put(container, new ArrayList<>());
}
removeBlobsMap.get(container).add(object);
} catch (ContainerNotFoundException e) {
result.numberNotFound += 1;
} catch (Exception e) {
e.printStackTrace();
logger.error(e.toString());
logger.debug(getStackTraceAsString(e));
result.errors.add(objectContainer);
}
}

removeBlobsMap.forEach((container, blobList) -> {
try {
blobStore.removeBlobs(container, blobList);
result.numberDeleted += blobList.size();
} catch (ContainerNotFoundException e) {
result.numberNotFound += blobList.size();
} catch (Exception e) {
logger.error(e.toString());
logger.debug(getStackTraceAsString(e));
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) {
logger.error(e.toString());
logger.debug(getStackTraceAsString(e));
result.errors.add(container);
}
});

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

0 comments on commit 52aa43b

Please sign in to comment.