Skip to content
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

Use removeBlobs() for bulk-delete. #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
57 changes: 36 additions & 21 deletions src/main/java/com/bouncestorage/swiftproxy/v1/AccountResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -56,7 +57,9 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;

import org.glassfish.grizzly.http.server.Request;
import org.jclouds.blobstore.BlobStore;
Expand Down Expand Up @@ -151,33 +154,45 @@ public BulkDeleteResult bulkDelete(@NotNull @PathParam("account") String account
}

BulkDeleteResult result = new BulkDeleteResult();
Multimap<String, String> removeBlobsMap = ArrayListMultimap.create();
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;
continue;
}
String container = objectContainer.substring(0, separatorIndex);
String object = objectContainer.substring(separatorIndex + 1);
if (objectContainer.startsWith("/")) {
objectContainer = objectContainer.substring(1);
}
int separatorIndex = objectContainer.indexOf('/');
if (separatorIndex < 0) {
deleteContainers.add(objectContainer.substring(1));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need substring here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right, because we already do substring(1) on line 161. I'm not sure how this works right now for containers that start with "/"!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are probably deleting a non-existent container which does not generate an error. We should enable/add some swift-tests to check for these behaviors.

continue;
}
String container = objectContainer.substring(0, separatorIndex);
String object = objectContainer.substring(separatorIndex + 1);
removeBlobsMap.put(container, object);
}

if (!blobStore.blobExists(container, object)) {
result.numberNotFound += 1;
} else {
blobStore.removeBlob(container, object);
result.numberDeleted += 1;
}
removeBlobsMap.keySet().forEach(container -> {
Collection<String> blobs = removeBlobsMap.get(container);
try {
blobStore.removeBlobs(container, blobs);
result.numberDeleted += blobs.size();
} catch (ContainerNotFoundException e) {
result.numberNotFound += blobs.size();
} catch (Exception e) {
logger.debug("Failed to remove blobs: " + e.getMessage(), e);
blobs.forEach(blob -> result.errors.add(container + "/" + blob));
}
});
deleteContainers.forEach(container -> {
try {
blobStore.deleteContainer(container);
result.numberDeleted += 1;
} catch (ContainerNotFoundException e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not clear whether deleteContainer will actually throw ContainerNotFoundException but this seems like the right thing to do!

result.numberNotFound += 1;
} catch (Exception e) {
e.printStackTrace();
result.errors.add(objectContainer);
logger.debug("Failed to delete container " + container + ": " + e.getMessage(), e);
result.errors.add(container);
}
}
});

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