| 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.openstack.swift.blobstore; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.blobstore.util.BlobStoreUtils.createParentIfNeededAsync; |
| 23 | |
| 24 | import java.util.Set; |
| 25 | import java.util.concurrent.ExecutorService; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Provider; |
| 30 | import javax.inject.Singleton; |
| 31 | |
| 32 | import org.jclouds.Constants; |
| 33 | import org.jclouds.blobstore.BlobStoreContext; |
| 34 | import org.jclouds.blobstore.domain.Blob; |
| 35 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 36 | import org.jclouds.blobstore.domain.PageSet; |
| 37 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 38 | import org.jclouds.blobstore.domain.internal.PageSetImpl; |
| 39 | import org.jclouds.blobstore.functions.BlobToHttpGetOptions; |
| 40 | import org.jclouds.blobstore.internal.BaseAsyncBlobStore; |
| 41 | import org.jclouds.blobstore.options.CreateContainerOptions; |
| 42 | import org.jclouds.blobstore.options.ListContainerOptions; |
| 43 | import org.jclouds.blobstore.options.PutOptions; |
| 44 | import org.jclouds.blobstore.strategy.internal.FetchBlobMetadata; |
| 45 | import org.jclouds.blobstore.util.BlobUtils; |
| 46 | import org.jclouds.collect.Memoized; |
| 47 | import org.jclouds.concurrent.Futures; |
| 48 | import org.jclouds.domain.Location; |
| 49 | import org.jclouds.http.options.GetOptions; |
| 50 | import org.jclouds.openstack.swift.CommonSwiftAsyncClient; |
| 51 | import org.jclouds.openstack.swift.CommonSwiftClient; |
| 52 | import org.jclouds.openstack.swift.blobstore.functions.BlobStoreListContainerOptionsToListContainerOptions; |
| 53 | import org.jclouds.openstack.swift.blobstore.functions.BlobToObject; |
| 54 | import org.jclouds.openstack.swift.blobstore.functions.ContainerToResourceList; |
| 55 | import org.jclouds.openstack.swift.blobstore.functions.ContainerToResourceMetadata; |
| 56 | import org.jclouds.openstack.swift.blobstore.functions.ObjectToBlob; |
| 57 | import org.jclouds.openstack.swift.blobstore.functions.ObjectToBlobMetadata; |
| 58 | import org.jclouds.openstack.swift.domain.ContainerMetadata; |
| 59 | import org.jclouds.openstack.swift.domain.MutableObjectInfoWithMetadata; |
| 60 | import org.jclouds.openstack.swift.domain.ObjectInfo; |
| 61 | import org.jclouds.openstack.swift.domain.SwiftObject; |
| 62 | |
| 63 | import com.google.common.base.Function; |
| 64 | import com.google.common.base.Supplier; |
| 65 | import com.google.common.collect.Iterables; |
| 66 | import com.google.common.util.concurrent.ListenableFuture; |
| 67 | |
| 68 | /** |
| 69 | * |
| 70 | * @author Adrian Cole |
| 71 | */ |
| 72 | @Singleton |
| 73 | public class SwiftAsyncBlobStore extends BaseAsyncBlobStore { |
| 74 | private final CommonSwiftClient sync; |
| 75 | private final CommonSwiftAsyncClient async; |
| 76 | private final ContainerToResourceMetadata container2ResourceMd; |
| 77 | private final BlobStoreListContainerOptionsToListContainerOptions container2ContainerListOptions; |
| 78 | private final ContainerToResourceList container2ResourceList; |
| 79 | private final ObjectToBlob object2Blob; |
| 80 | private final BlobToObject blob2Object; |
| 81 | private final ObjectToBlobMetadata object2BlobMd; |
| 82 | private final BlobToHttpGetOptions blob2ObjectGetOptions; |
| 83 | private final Provider<FetchBlobMetadata> fetchBlobMetadataProvider; |
| 84 | |
| 85 | @Inject |
| 86 | protected SwiftAsyncBlobStore(BlobStoreContext context, BlobUtils blobUtils, |
| 87 | @Named(Constants.PROPERTY_USER_THREADS) ExecutorService service, Supplier<Location> defaultLocation, |
| 88 | @Memoized Supplier<Set<? extends Location>> locations, CommonSwiftClient sync, |
| 89 | CommonSwiftAsyncClient async, ContainerToResourceMetadata container2ResourceMd, |
| 90 | BlobStoreListContainerOptionsToListContainerOptions container2ContainerListOptions, |
| 91 | ContainerToResourceList container2ResourceList, ObjectToBlob object2Blob, BlobToObject blob2Object, |
| 92 | ObjectToBlobMetadata object2BlobMd, BlobToHttpGetOptions blob2ObjectGetOptions, |
| 93 | Provider<FetchBlobMetadata> fetchBlobMetadataProvider) { |
| 94 | super(context, blobUtils, service, defaultLocation, locations); |
| 95 | this.sync = sync; |
| 96 | this.async = async; |
| 97 | this.container2ResourceMd = container2ResourceMd; |
| 98 | this.container2ContainerListOptions = container2ContainerListOptions; |
| 99 | this.container2ResourceList = container2ResourceList; |
| 100 | this.object2Blob = object2Blob; |
| 101 | this.blob2Object = blob2Object; |
| 102 | this.object2BlobMd = object2BlobMd; |
| 103 | this.blob2ObjectGetOptions = blob2ObjectGetOptions; |
| 104 | this.fetchBlobMetadataProvider = checkNotNull(fetchBlobMetadataProvider, "fetchBlobMetadataProvider"); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * This implementation invokes {@link CommonSwiftAsyncClient#listContainers} |
| 109 | */ |
| 110 | @Override |
| 111 | public ListenableFuture<PageSet<? extends StorageMetadata>> list() { |
| 112 | return Futures.compose(async.listContainers(), |
| 113 | new Function<Set<ContainerMetadata>, org.jclouds.blobstore.domain.PageSet<? extends StorageMetadata>>() { |
| 114 | public org.jclouds.blobstore.domain.PageSet<? extends StorageMetadata> apply( |
| 115 | Set<ContainerMetadata> from) { |
| 116 | return new PageSetImpl<StorageMetadata>(Iterables.transform(from, container2ResourceMd), null); |
| 117 | } |
| 118 | }, service); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * This implementation invokes {@link CommonSwiftAsyncClient#containerExists} |
| 123 | * |
| 124 | * @param container |
| 125 | * container name |
| 126 | */ |
| 127 | @Override |
| 128 | public ListenableFuture<Boolean> containerExists(String container) { |
| 129 | return async.containerExists(container); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Note that location is currently ignored. |
| 134 | */ |
| 135 | @Override |
| 136 | public ListenableFuture<Boolean> createContainerInLocation(Location location, String container) { |
| 137 | return async.createContainer(container); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * This implementation invokes {@link CommonSwiftAsyncClient#listBucket} |
| 142 | * |
| 143 | * @param container |
| 144 | * container name |
| 145 | */ |
| 146 | @Override |
| 147 | public ListenableFuture<PageSet<? extends StorageMetadata>> list(String container, ListContainerOptions options) { |
| 148 | org.jclouds.openstack.swift.options.ListContainerOptions httpOptions = container2ContainerListOptions |
| 149 | .apply(options); |
| 150 | ListenableFuture<PageSet<ObjectInfo>> returnVal = async.listObjects(container, httpOptions); |
| 151 | ListenableFuture<PageSet<? extends StorageMetadata>> list = Futures.compose(returnVal, container2ResourceList, |
| 152 | service); |
| 153 | return options.isDetailed() ? Futures.compose(list, fetchBlobMetadataProvider.get().setContainerName(container), |
| 154 | service) : list; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * This implementation invokes {@link CommonSwiftAsyncClient#objectExists} |
| 159 | * |
| 160 | * @param container |
| 161 | * container name |
| 162 | * @param key |
| 163 | * object key |
| 164 | */ |
| 165 | @Override |
| 166 | public ListenableFuture<Boolean> blobExists(String container, String key) { |
| 167 | return async.objectExists(container, key); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * This implementation invokes {@link CommonSwiftAsyncClient#headObject} |
| 172 | * |
| 173 | * @param container |
| 174 | * container name |
| 175 | * @param key |
| 176 | * object key |
| 177 | */ |
| 178 | @Override |
| 179 | public ListenableFuture<BlobMetadata> blobMetadata(String container, String key) { |
| 180 | return Futures.compose(async.getObjectInfo(container, key), |
| 181 | new Function<MutableObjectInfoWithMetadata, BlobMetadata>() { |
| 182 | |
| 183 | @Override |
| 184 | public BlobMetadata apply(MutableObjectInfoWithMetadata from) { |
| 185 | return object2BlobMd.apply(from); |
| 186 | } |
| 187 | |
| 188 | }, service); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * This implementation invokes {@link CommonSwiftAsyncClient#getObject} |
| 193 | * |
| 194 | * @param container |
| 195 | * container name |
| 196 | * @param key |
| 197 | * object key |
| 198 | */ |
| 199 | @Override |
| 200 | public ListenableFuture<Blob> getBlob(String container, String key, org.jclouds.blobstore.options.GetOptions options) { |
| 201 | GetOptions httpOptions = blob2ObjectGetOptions.apply(options); |
| 202 | ListenableFuture<SwiftObject> returnVal = async.getObject(container, key, httpOptions); |
| 203 | return Futures.compose(returnVal, object2Blob, service); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * This implementation invokes {@link CommonSwiftAsyncClient#putObject} |
| 208 | * |
| 209 | * @param container |
| 210 | * container name |
| 211 | * @param blob |
| 212 | * object |
| 213 | */ |
| 214 | @Override |
| 215 | public ListenableFuture<String> putBlob(String container, Blob blob) { |
| 216 | createParentIfNeededAsync(this, container, blob); |
| 217 | return async.putObject(container, blob2Object.apply(blob)); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * This implementation invokes {@link CommonSwiftAsyncClient#removeObject} |
| 222 | * |
| 223 | * @param container |
| 224 | * container name |
| 225 | * @param key |
| 226 | * object key |
| 227 | */ |
| 228 | @Override |
| 229 | public ListenableFuture<Void> removeBlob(String container, String key) { |
| 230 | return async.removeObject(container, key); |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | protected boolean deleteAndVerifyContainerGone(String container) { |
| 235 | sync.deleteContainerIfEmpty(container); |
| 236 | return !sync.containerExists(container); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public ListenableFuture<String> putBlob(String container, Blob blob, PutOptions options) { |
| 241 | // TODO implement options |
| 242 | return putBlob(container, blob); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public ListenableFuture<Boolean> createContainerInLocation(Location location, String container, |
| 247 | CreateContainerOptions options) { |
| 248 | if (options.isPublicRead()) |
| 249 | throw new UnsupportedOperationException("publicRead"); |
| 250 | return createContainerInLocation(location, container); |
| 251 | } |
| 252 | } |