| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.blobstore.strategy.internal; |
| 20 | |
| 21 | import static org.jclouds.blobstore.options.ListContainerOptions.Builder.recursive; |
| 22 | import static org.jclouds.concurrent.FutureIterables.awaitCompletion; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | import java.util.concurrent.ExecutorService; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.Constants; |
| 32 | import org.jclouds.blobstore.AsyncBlobStore; |
| 33 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 34 | import org.jclouds.blobstore.internal.BlobRuntimeException; |
| 35 | import org.jclouds.blobstore.options.ListContainerOptions; |
| 36 | import org.jclouds.blobstore.reference.BlobStoreConstants; |
| 37 | import org.jclouds.blobstore.strategy.ClearContainerStrategy; |
| 38 | import org.jclouds.blobstore.strategy.ClearListStrategy; |
| 39 | import org.jclouds.blobstore.strategy.ListContainerStrategy; |
| 40 | import org.jclouds.http.handlers.BackoffLimitedRetryHandler; |
| 41 | import org.jclouds.logging.Logger; |
| 42 | |
| 43 | import com.google.common.base.Predicate; |
| 44 | import com.google.common.collect.Iterables; |
| 45 | import com.google.common.collect.Maps; |
| 46 | import java.util.concurrent.Future; |
| 47 | import com.google.inject.Inject; |
| 48 | |
| 49 | /** |
| 50 | * Deletes all keys in the container |
| 51 | * |
| 52 | * @author Adrian Cole |
| 53 | */ |
| 54 | @Singleton |
| 55 | public class DeleteAllKeysInList implements ClearListStrategy, ClearContainerStrategy { |
| 56 | @Resource |
| 57 | @Named(BlobStoreConstants.BLOBSTORE_LOGGER) |
| 58 | protected Logger logger = Logger.NULL; |
| 59 | |
| 60 | protected final ListContainerStrategy listContainer; |
| 61 | protected final BackoffLimitedRetryHandler retryHandler; |
| 62 | private final ExecutorService userExecutor; |
| 63 | |
| 64 | protected final AsyncBlobStore connection; |
| 65 | /** |
| 66 | * maximum duration of an blob Request |
| 67 | */ |
| 68 | @Inject(optional = true) |
| 69 | @Named(Constants.PROPERTY_REQUEST_TIMEOUT) |
| 70 | protected Long maxTime; |
| 71 | |
| 72 | @Inject |
| 73 | DeleteAllKeysInList(@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userExecutor, |
| 74 | AsyncBlobStore connection, ListContainerStrategy listContainer, |
| 75 | BackoffLimitedRetryHandler retryHandler) { |
| 76 | |
| 77 | this.userExecutor = userExecutor; |
| 78 | this.connection = connection; |
| 79 | this.listContainer = listContainer; |
| 80 | this.retryHandler = retryHandler; |
| 81 | } |
| 82 | |
| 83 | public void execute(String containerName) { |
| 84 | execute(containerName, recursive()); |
| 85 | } |
| 86 | |
| 87 | public void execute(final String containerName, final ListContainerOptions options) { |
| 88 | String message = options.getDir() != null ? String.format("clearing path %s/%s", |
| 89 | containerName, options.getDir()) : String.format("clearing container %s", |
| 90 | containerName); |
| 91 | if (options.isRecursive()) |
| 92 | message = message + " recursively"; |
| 93 | Map<StorageMetadata, Exception> exceptions = Maps.newHashMap(); |
| 94 | Iterable<? extends StorageMetadata> toDelete = getResourcesToDelete(containerName, options); |
| 95 | for (int i = 0; i < 3; i++) { // TODO parameterize |
| 96 | Map<StorageMetadata, Future<?>> responses = Maps.newHashMap(); |
| 97 | try { |
| 98 | for (final StorageMetadata md : toDelete) { |
| 99 | String fullPath = parentIsFolder(options, md) ? options.getDir() + "/" |
| 100 | + md.getName() : md.getName(); |
| 101 | switch (md.getType()) { |
| 102 | case BLOB: |
| 103 | responses.put(md, connection.removeBlob(containerName, fullPath)); |
| 104 | break; |
| 105 | case FOLDER: |
| 106 | if (options.isRecursive() && !fullPath.equals(options.getDir())) { |
| 107 | execute(containerName, options.clone().inDirectory(fullPath)); |
| 108 | } |
| 109 | responses.put(md, connection.deleteDirectory(containerName, fullPath)); |
| 110 | break; |
| 111 | case RELATIVE_PATH: |
| 112 | if (options.isRecursive() && !fullPath.equals(options.getDir())) { |
| 113 | execute(containerName, options.clone().inDirectory(fullPath)); |
| 114 | } |
| 115 | responses.put(md, connection.deleteDirectory(containerName, md.getName())); |
| 116 | break; |
| 117 | case CONTAINER: |
| 118 | throw new IllegalArgumentException("Container type not supported"); |
| 119 | } |
| 120 | } |
| 121 | } finally { |
| 122 | exceptions = awaitCompletion(responses, userExecutor, maxTime, logger, message); |
| 123 | toDelete = getResourcesToDelete(containerName, options); |
| 124 | if (Iterables.size(toDelete) == 0) { |
| 125 | break; |
| 126 | } |
| 127 | if (exceptions.size() > 0) { |
| 128 | toDelete = Iterables.concat(exceptions.keySet(), toDelete); |
| 129 | retryHandler.imposeBackoffExponentialDelay(i + 1, message); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | if (exceptions.size() > 0) |
| 134 | throw new BlobRuntimeException(String.format("error %s: %s", message, exceptions)); |
| 135 | assert Iterables.size(toDelete) == 0 : String.format("items remaining %s: %s", message, |
| 136 | toDelete); |
| 137 | } |
| 138 | |
| 139 | private boolean parentIsFolder(final ListContainerOptions options, final StorageMetadata md) { |
| 140 | return (options.getDir() != null && md.getName().indexOf('/') == -1); |
| 141 | } |
| 142 | |
| 143 | private Iterable<? extends StorageMetadata> getResourcesToDelete(final String containerName, |
| 144 | final ListContainerOptions options) { |
| 145 | Iterable<? extends StorageMetadata> toDelete = Iterables.filter(listContainer.execute( |
| 146 | containerName, options), new Predicate<StorageMetadata>() { |
| 147 | |
| 148 | @Override |
| 149 | public boolean apply(StorageMetadata input) { |
| 150 | switch (input.getType()) { |
| 151 | case BLOB: |
| 152 | return true; |
| 153 | case FOLDER: |
| 154 | case RELATIVE_PATH: |
| 155 | if (options.isRecursive()) |
| 156 | return true; |
| 157 | break; |
| 158 | } |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | }); |
| 163 | return toDelete; |
| 164 | } |
| 165 | |
| 166 | } |