|
24 | 24 | import java.io.IOException; |
25 | 25 | import java.io.InputStreamReader; |
26 | 26 | import java.util.ArrayList; |
| 27 | +import java.util.Collection; |
27 | 28 | import java.util.List; |
28 | 29 | import java.util.Objects; |
29 | 30 | import java.util.Optional; |
|
56 | 57 | import com.fasterxml.jackson.annotation.JsonProperty; |
57 | 58 | import com.fasterxml.jackson.core.JsonProcessingException; |
58 | 59 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 60 | +import com.google.common.collect.ArrayListMultimap; |
59 | 61 | import com.google.common.collect.Lists; |
| 62 | +import com.google.common.collect.Multimap; |
60 | 63 |
|
61 | 64 | import org.glassfish.grizzly.http.server.Request; |
62 | 65 | import org.jclouds.blobstore.BlobStore; |
@@ -151,33 +154,45 @@ public BulkDeleteResult bulkDelete(@NotNull @PathParam("account") String account |
151 | 154 | } |
152 | 155 |
|
153 | 156 | BulkDeleteResult result = new BulkDeleteResult(); |
| 157 | + Multimap<String, String> removeBlobsMap = ArrayListMultimap.create(); |
| 158 | + List<String> deleteContainers = new ArrayList<>(); |
154 | 159 | for (String objectContainer : objects) { |
155 | | - try { |
156 | | - if (objectContainer.startsWith("/")) { |
157 | | - objectContainer = objectContainer.substring(1); |
158 | | - } |
159 | | - int separatorIndex = objectContainer.indexOf('/'); |
160 | | - if (separatorIndex < 0) { |
161 | | - blobStore.deleteContainer(objectContainer.substring(1)); |
162 | | - result.numberDeleted += 1; |
163 | | - continue; |
164 | | - } |
165 | | - String container = objectContainer.substring(0, separatorIndex); |
166 | | - String object = objectContainer.substring(separatorIndex + 1); |
| 160 | + if (objectContainer.startsWith("/")) { |
| 161 | + objectContainer = objectContainer.substring(1); |
| 162 | + } |
| 163 | + int separatorIndex = objectContainer.indexOf('/'); |
| 164 | + if (separatorIndex < 0) { |
| 165 | + deleteContainers.add(objectContainer.substring(1)); |
| 166 | + continue; |
| 167 | + } |
| 168 | + String container = objectContainer.substring(0, separatorIndex); |
| 169 | + String object = objectContainer.substring(separatorIndex + 1); |
| 170 | + removeBlobsMap.put(container, object); |
| 171 | + } |
167 | 172 |
|
168 | | - if (!blobStore.blobExists(container, object)) { |
169 | | - result.numberNotFound += 1; |
170 | | - } else { |
171 | | - blobStore.removeBlob(container, object); |
172 | | - result.numberDeleted += 1; |
173 | | - } |
| 173 | + removeBlobsMap.keySet().forEach(container -> { |
| 174 | + Collection<String> blobs = removeBlobsMap.get(container); |
| 175 | + try { |
| 176 | + blobStore.removeBlobs(container, blobs); |
| 177 | + result.numberDeleted += blobs.size(); |
| 178 | + } catch (ContainerNotFoundException e) { |
| 179 | + result.numberNotFound += blobs.size(); |
| 180 | + } catch (Exception e) { |
| 181 | + logger.debug("Failed to remove blobs: " + e.getMessage(), e); |
| 182 | + blobs.forEach(blob -> result.errors.add(container + "/" + blob)); |
| 183 | + } |
| 184 | + }); |
| 185 | + deleteContainers.forEach(container -> { |
| 186 | + try { |
| 187 | + blobStore.deleteContainer(container); |
| 188 | + result.numberDeleted += 1; |
174 | 189 | } catch (ContainerNotFoundException e) { |
175 | 190 | result.numberNotFound += 1; |
176 | 191 | } catch (Exception e) { |
177 | | - e.printStackTrace(); |
178 | | - result.errors.add(objectContainer); |
| 192 | + logger.debug("Failed to delete container " + container + ": " + e.getMessage(), e); |
| 193 | + result.errors.add(container); |
179 | 194 | } |
180 | | - } |
| 195 | + }); |
181 | 196 |
|
182 | 197 | if (result.errors.isEmpty()) { |
183 | 198 | result.responseStatus = Response.Status.OK.toString(); |
|
0 commit comments