EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.blobstore.strategy.internal]

COVERAGE SUMMARY FOR SOURCE FILE [DeleteAllKeysInList.java]

nameclass, %method, %block, %line, %
DeleteAllKeysInList.java33%  (1/3)22%  (2/9)6%   (24/387)15%  (7.8/52)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DeleteAllKeysInList$10%   (0/1)0%   (0/2)0%   (0/25)0%   (0/6)
DeleteAllKeysInList$1 (DeleteAllKeysInList, ListContainerOptions): void 0%   (0/1)0%   (0/9)0%   (0/1)
apply (StorageMetadata): boolean 0%   (0/1)0%   (0/16)0%   (0/5)
     
class DeleteAllKeysInList$20%   (0/1)0%   (0/1)0%   (0/33)0%   (0/1)
<static initializer> 0%   (0/1)0%   (0/33)0%   (0/1)
     
class DeleteAllKeysInList100% (1/1)33%  (2/6)7%   (24/329)17%  (7.8/46)
execute (String): void 0%   (0/1)0%   (0/5)0%   (0/2)
execute (String, ListContainerOptions): void 0%   (0/1)0%   (0/271)0%   (0/33)
getResourcesToDelete (String, ListContainerOptions): Iterable 0%   (0/1)0%   (0/14)0%   (0/2)
parentIsFolder (ListContainerOptions, StorageMetadata): boolean 0%   (0/1)0%   (0/13)0%   (0/1)
<static initializer> 100% (1/1)75%  (6/8)75%  (0.8/1)
DeleteAllKeysInList (ExecutorService, AsyncBlobStore, ListContainerStrategy, ... 100% (1/1)100% (18/18)100% (7/7)

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 */
19package org.jclouds.blobstore.strategy.internal;
20 
21import static org.jclouds.blobstore.options.ListContainerOptions.Builder.recursive;
22import static org.jclouds.concurrent.FutureIterables.awaitCompletion;
23 
24import java.util.Map;
25import java.util.concurrent.ExecutorService;
26 
27import javax.annotation.Resource;
28import javax.inject.Named;
29import javax.inject.Singleton;
30 
31import org.jclouds.Constants;
32import org.jclouds.blobstore.AsyncBlobStore;
33import org.jclouds.blobstore.domain.StorageMetadata;
34import org.jclouds.blobstore.internal.BlobRuntimeException;
35import org.jclouds.blobstore.options.ListContainerOptions;
36import org.jclouds.blobstore.reference.BlobStoreConstants;
37import org.jclouds.blobstore.strategy.ClearContainerStrategy;
38import org.jclouds.blobstore.strategy.ClearListStrategy;
39import org.jclouds.blobstore.strategy.ListContainerStrategy;
40import org.jclouds.http.handlers.BackoffLimitedRetryHandler;
41import org.jclouds.logging.Logger;
42 
43import com.google.common.base.Predicate;
44import com.google.common.collect.Iterables;
45import com.google.common.collect.Maps;
46import java.util.concurrent.Future;
47import com.google.inject.Inject;
48 
49/**
50 * Deletes all keys in the container
51 * 
52 * @author Adrian Cole
53 */
54@Singleton
55public 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}

[all classes][org.jclouds.blobstore.strategy.internal]
EMMA 2.0.5312 (C) Vladimir Roubtsov