| 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.atmos.blobstore.strategy; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.concurrent.FutureIterables.awaitCompletion; |
| 23 | |
| 24 | import java.util.Arrays; |
| 25 | import java.util.Map; |
| 26 | import java.util.concurrent.BlockingQueue; |
| 27 | import java.util.concurrent.ExecutionException; |
| 28 | import java.util.concurrent.ExecutorService; |
| 29 | import java.util.concurrent.Future; |
| 30 | import java.util.concurrent.SynchronousQueue; |
| 31 | import java.util.concurrent.TimeUnit; |
| 32 | |
| 33 | import javax.annotation.Resource; |
| 34 | import javax.inject.Named; |
| 35 | import javax.inject.Singleton; |
| 36 | |
| 37 | import org.jclouds.Constants; |
| 38 | import org.jclouds.atmos.AtmosAsyncClient; |
| 39 | import org.jclouds.atmos.domain.AtmosObject; |
| 40 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 41 | import org.jclouds.blobstore.functions.ObjectMD5; |
| 42 | import org.jclouds.blobstore.internal.BlobRuntimeException; |
| 43 | import org.jclouds.blobstore.options.ListContainerOptions; |
| 44 | import org.jclouds.blobstore.strategy.ContainsValueInListStrategy; |
| 45 | import org.jclouds.blobstore.strategy.ListBlobsInContainer; |
| 46 | import org.jclouds.concurrent.Futures; |
| 47 | import org.jclouds.logging.Logger; |
| 48 | |
| 49 | import com.google.common.base.Throwables; |
| 50 | import com.google.common.collect.Maps; |
| 51 | import com.google.common.util.concurrent.ListenableFuture; |
| 52 | import com.google.inject.Inject; |
| 53 | |
| 54 | /** |
| 55 | * Searches Content-MD5 tag for the value associated with the value |
| 56 | * |
| 57 | * @author Adrian Cole |
| 58 | */ |
| 59 | @Singleton |
| 60 | public class FindMD5InUserMetadata implements ContainsValueInListStrategy { |
| 61 | @Resource |
| 62 | protected Logger logger = Logger.NULL; |
| 63 | protected final ObjectMD5 objectMD5; |
| 64 | protected final ListBlobsInContainer getAllBlobMetadata; |
| 65 | private final AtmosAsyncClient client; |
| 66 | private final ExecutorService userExecutor; |
| 67 | /** |
| 68 | * maximum duration of an blob Request |
| 69 | */ |
| 70 | @Inject(optional = true) |
| 71 | @Named(Constants.PROPERTY_REQUEST_TIMEOUT) |
| 72 | protected Long maxTime; |
| 73 | |
| 74 | @Inject |
| 75 | FindMD5InUserMetadata(@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userExecutor, ObjectMD5 objectMD5, |
| 76 | ListBlobsInContainer getAllBlobMetadata, AtmosAsyncClient client) { |
| 77 | this.objectMD5 = objectMD5; |
| 78 | this.getAllBlobMetadata = getAllBlobMetadata; |
| 79 | this.client = client; |
| 80 | this.userExecutor = userExecutor; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean execute(final String containerName, Object value, ListContainerOptions options) { |
| 85 | final byte[] toSearch = objectMD5.apply(value); |
| 86 | final BlockingQueue<Boolean> queue = new SynchronousQueue<Boolean>(); |
| 87 | Map<String, Future<?>> responses = Maps.newHashMap(); |
| 88 | for (BlobMetadata md : getAllBlobMetadata.execute(containerName, options)) { |
| 89 | final ListenableFuture<AtmosObject> future = Futures.makeListenable( |
| 90 | client.headFile(containerName + "/" + md.getName()), userExecutor); |
| 91 | future.addListener(new Runnable() { |
| 92 | public void run() { |
| 93 | try { |
| 94 | AtmosObject object = future.get(); |
| 95 | checkNotNull(object.getSystemMetadata(), object + " has no content metadata"); |
| 96 | if (object.getSystemMetadata().getContentMD5() != null) { |
| 97 | if (Arrays.equals(toSearch, object.getSystemMetadata().getContentMD5())) { |
| 98 | queue.put(true); |
| 99 | } |
| 100 | } else { |
| 101 | logger.debug("object %s has no content md5", object.getSystemMetadata().getObjectID()); |
| 102 | } |
| 103 | } catch (InterruptedException e) { |
| 104 | Throwables.propagate(e); |
| 105 | } catch (ExecutionException e) { |
| 106 | Throwables.propagate(e); |
| 107 | } |
| 108 | } |
| 109 | }, userExecutor); |
| 110 | responses.put(md.getName(), future); |
| 111 | } |
| 112 | Map<String, Exception> exceptions = awaitCompletion(responses, userExecutor, maxTime, logger, |
| 113 | String.format("searching for md5 in container %s", containerName)); |
| 114 | if (exceptions.size() > 0) |
| 115 | throw new BlobRuntimeException(String.format("searching for md5 in container %s: %s", containerName, |
| 116 | exceptions)); |
| 117 | try { |
| 118 | return queue.poll(1, TimeUnit.MICROSECONDS) != null; |
| 119 | } catch (InterruptedException e) { |
| 120 | Throwables.propagate(e); |
| 121 | return false; |
| 122 | } catch (Exception e) { |
| 123 | Throwables.propagateIfPossible(e, BlobRuntimeException.class); |
| 124 | throw new BlobRuntimeException(String.format("Error searching for ETAG of value: [%s] in container:%s", value, |
| 125 | containerName), e); |
| 126 | } |
| 127 | } |
| 128 | } |