| 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 com.google.common.base.Preconditions.checkState; |
| 22 | import static org.jclouds.concurrent.FutureIterables.transformParallel; |
| 23 | |
| 24 | import java.util.concurrent.ExecutorService; |
| 25 | import java.util.concurrent.Future; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | import org.jclouds.javax.annotation.concurrent.NotThreadSafe; |
| 29 | import javax.inject.Named; |
| 30 | |
| 31 | import org.jclouds.Constants; |
| 32 | import org.jclouds.blobstore.AsyncBlobStore; |
| 33 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 34 | import org.jclouds.blobstore.domain.PageSet; |
| 35 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 36 | import org.jclouds.blobstore.domain.StorageType; |
| 37 | import org.jclouds.blobstore.domain.internal.PageSetImpl; |
| 38 | import org.jclouds.blobstore.reference.BlobStoreConstants; |
| 39 | import org.jclouds.http.handlers.BackoffLimitedRetryHandler; |
| 40 | import org.jclouds.logging.Logger; |
| 41 | |
| 42 | import com.google.common.base.Function; |
| 43 | import com.google.common.base.Predicate; |
| 44 | import com.google.common.collect.Iterables; |
| 45 | import com.google.inject.Inject; |
| 46 | |
| 47 | /** |
| 48 | * Retrieves all blobmetadata in the list as efficiently as possible |
| 49 | * |
| 50 | * @author Adrian Cole |
| 51 | */ |
| 52 | @NotThreadSafe |
| 53 | public class FetchBlobMetadata implements Function<PageSet<? extends StorageMetadata>, PageSet<? extends StorageMetadata>> { |
| 54 | |
| 55 | protected final BackoffLimitedRetryHandler retryHandler; |
| 56 | protected final AsyncBlobStore ablobstore; |
| 57 | protected final ExecutorService userExecutor; |
| 58 | @Resource |
| 59 | @Named(BlobStoreConstants.BLOBSTORE_LOGGER) |
| 60 | protected Logger logger = Logger.NULL; |
| 61 | |
| 62 | private String container; |
| 63 | /** |
| 64 | * maximum duration of an blob Request |
| 65 | */ |
| 66 | @Inject(optional = true) |
| 67 | @Named(Constants.PROPERTY_REQUEST_TIMEOUT) |
| 68 | protected Long maxTime; |
| 69 | |
| 70 | @Inject |
| 71 | FetchBlobMetadata(@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userExecutor, AsyncBlobStore ablobstore, |
| 72 | BackoffLimitedRetryHandler retryHandler) { |
| 73 | this.userExecutor = userExecutor; |
| 74 | this.ablobstore = ablobstore; |
| 75 | this.retryHandler = retryHandler; |
| 76 | } |
| 77 | |
| 78 | public FetchBlobMetadata setContainerName(String container) { |
| 79 | this.container = container; |
| 80 | return this; |
| 81 | } |
| 82 | |
| 83 | public PageSet<? extends StorageMetadata> apply(PageSet<? extends StorageMetadata> in) { |
| 84 | checkState(container != null, "container name should be initialized"); |
| 85 | |
| 86 | Iterable<BlobMetadata> returnv = transformParallel(Iterables.filter(in, new Predicate<StorageMetadata>() { |
| 87 | |
| 88 | @Override |
| 89 | public boolean apply(StorageMetadata input) { |
| 90 | return input.getType() == StorageType.BLOB; |
| 91 | } |
| 92 | |
| 93 | }), new Function<StorageMetadata, Future<BlobMetadata>>() { |
| 94 | |
| 95 | @Override |
| 96 | public Future<BlobMetadata> apply(StorageMetadata from) { |
| 97 | return ablobstore.blobMetadata(container, from.getName()); |
| 98 | } |
| 99 | |
| 100 | }, userExecutor, maxTime, logger, String.format("getting metadata from containerName: %s", container)); |
| 101 | |
| 102 | return new PageSetImpl<BlobMetadata>(returnv, in.getNextMarker()); |
| 103 | } |
| 104 | } |