From c30c2c48092f9675230b2cd604245741085a8c18 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Tue, 17 Jul 2012 22:18:45 -0700 Subject: [PATCH] Introduce LocalStorageStrategy This commit will allow eventual unification of the filesystem and transient blobstores. No functional changes -- rename FilesystemStorageStrategy to LocalStorageStrategy and implement it in FilesystemStorageStrategyImpl and TransientStorageStrategy. --- .../filesystem/FilesystemAsyncBlobStore.java | 8 +- .../FilesystemBlobStoreContextModule.java | 4 +- .../FilesystemStorageStrategyImpl.java | 18 +-- .../internal/FileSystemBlobUtilsImpl.java | 11 +- .../FilesystemStorageStrategyImplTest.java | 5 +- .../blobstore/LocalStorageStrategy.java | 113 +++++------------- .../blobstore/TransientStorageStrategy.java | 9 +- 7 files changed, 60 insertions(+), 108 deletions(-) rename apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/FilesystemStorageStrategy.java => blobstore/src/main/java/org/jclouds/blobstore/LocalStorageStrategy.java (51%) diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java index 813feb70e5..a533e03632 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java @@ -58,6 +58,7 @@ import org.jclouds.Constants; import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.ContainerNotFoundException; import org.jclouds.blobstore.KeyNotFoundException; +import org.jclouds.blobstore.LocalStorageStrategy; import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob.Factory; import org.jclouds.blobstore.domain.BlobMetadata; @@ -82,7 +83,6 @@ import org.jclouds.crypto.CryptoStreams; import org.jclouds.date.DateService; import org.jclouds.domain.Location; import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator; -import org.jclouds.filesystem.strategy.FilesystemStorageStrategy; import org.jclouds.http.HttpCommand; import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; @@ -121,7 +121,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { protected final ContentMetadataCodec contentMetadataCodec; protected final IfDirectoryReturnNameStrategy ifDirectoryReturnName; protected final Factory blobFactory; - protected final FilesystemStorageStrategy storageStrategy; + protected final LocalStorageStrategy storageStrategy; @Inject protected FilesystemAsyncBlobStore(BlobStoreContext context, @@ -133,7 +133,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { @Named(Constants.PROPERTY_USER_THREADS) ExecutorService service, Supplier defaultLocation, @Memoized Supplier> locations, - Factory blobFactory, FilesystemStorageStrategy storageStrategy) { + Factory blobFactory, LocalStorageStrategy storageStrategy) { super(context, blobUtils, service, defaultLocation, locations); this.blobFactory = blobFactory; this.dateService = dateService; @@ -340,7 +340,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { @Override public ListenableFuture createContainerInLocation(final Location location, @PathParam("container") @ParamValidators({ FilesystemContainerNameValidator.class }) String name) { - boolean result = storageStrategy.createContainer(name); + boolean result = storageStrategy.createContainerInLocation(name, null); return immediateFuture(result); } diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/config/FilesystemBlobStoreContextModule.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/config/FilesystemBlobStoreContextModule.java index 4560bd5156..26f39d70af 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/config/FilesystemBlobStoreContextModule.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/config/FilesystemBlobStoreContextModule.java @@ -21,6 +21,7 @@ package org.jclouds.filesystem.config; import org.jclouds.blobstore.AsyncBlobStore; import org.jclouds.blobstore.BlobRequestSigner; import org.jclouds.blobstore.BlobStore; +import org.jclouds.blobstore.LocalStorageStrategy; import org.jclouds.blobstore.TransientBlobRequestSigner; import org.jclouds.blobstore.attr.ConsistencyModel; import org.jclouds.blobstore.config.BlobStoreMapModule; @@ -32,7 +33,6 @@ import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator; import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator; import org.jclouds.filesystem.predicates.validators.internal.FilesystemBlobKeyValidatorImpl; import org.jclouds.filesystem.predicates.validators.internal.FilesystemContainerNameValidatorImpl; -import org.jclouds.filesystem.strategy.FilesystemStorageStrategy; import org.jclouds.filesystem.strategy.internal.FilesystemStorageStrategyImpl; import org.jclouds.filesystem.util.internal.FileSystemBlobUtilsImpl; import org.jclouds.rest.config.BinderUtils; @@ -56,7 +56,7 @@ public class FilesystemBlobStoreContextModule extends AbstractModule { install(new BlobStoreObjectModule()); install(new BlobStoreMapModule()); bind(ConsistencyModel.class).toInstance(ConsistencyModel.STRICT); - bind(FilesystemStorageStrategy.class).to(FilesystemStorageStrategyImpl.class); + bind(LocalStorageStrategy.class).to(FilesystemStorageStrategyImpl.class); bind(BlobUtils.class).to(FileSystemBlobUtilsImpl.class); bind(FilesystemBlobKeyValidator.class).to(FilesystemBlobKeyValidatorImpl.class); bind(FilesystemContainerNameValidator.class).to(FilesystemContainerNameValidatorImpl.class); diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java index 918f9d21c2..5e1814faf0 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java @@ -35,14 +35,15 @@ import javax.inject.Provider; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.DirectoryFileFilter; +import org.jclouds.blobstore.LocalStorageStrategy; import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.BlobBuilder; import org.jclouds.blobstore.options.ListContainerOptions; import org.jclouds.crypto.CryptoStreams; +import org.jclouds.domain.Location; import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator; import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator; import org.jclouds.filesystem.reference.FilesystemConstants; -import org.jclouds.filesystem.strategy.FilesystemStorageStrategy; import org.jclouds.io.Payload; import org.jclouds.logging.Logger; import org.jclouds.rest.annotations.ParamValidators; @@ -55,7 +56,7 @@ import com.google.common.io.Files; * * @author Alfredo "Rainbowbreeze" Morresi */ -public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy { +public class FilesystemStorageStrategyImpl implements LocalStorageStrategy { private static final String BACK_SLASH = "\\"; @@ -109,8 +110,13 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy return blob; } - @Override public boolean createContainer(String container) { + return createContainerInLocation(container, null); + } + + @Override + public boolean createContainerInLocation(String container, Location location) { + // TODO: implement location logger.debug("Creating container %s", container); filesystemContainerNameValidator.validate(container); return createDirectoryWithResult(container, null); @@ -151,7 +157,6 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy } } - @Override public Blob newBlob(@ParamValidators({ FilesystemBlobKeyValidator.class }) String name) { filesystemBlobKeyValidator.validate(name); return blobBuilders.get().name(name).build(); @@ -195,7 +200,6 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy * @param blobKey * @return */ - @Override public File getFileForBlobKey(String container, String blobKey) { filesystemContainerNameValidator.validate(container); filesystemBlobKeyValidator.validate(blobKey); @@ -262,17 +266,14 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy return blobNames; } - @Override public boolean directoryExists(String container, String directory) { return buildPathAndChecksIfDirectoryExists(container, directory); } - @Override public void createDirectory(String container, String directory) { createDirectoryWithResult(container, directory); } - @Override public void deleteDirectory(String container, String directory) { // create complete dir path String fullDirPath = buildPathStartingFromBaseDir(container, directory); @@ -284,7 +285,6 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy } } - @Override public long countBlobs(String container, ListContainerOptions options) { // TODO throw new UnsupportedOperationException("Not supported yet."); diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/util/internal/FileSystemBlobUtilsImpl.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/util/internal/FileSystemBlobUtilsImpl.java index b24a54db53..87ff1cdfdb 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/util/internal/FileSystemBlobUtilsImpl.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/util/internal/FileSystemBlobUtilsImpl.java @@ -23,27 +23,28 @@ import static com.google.common.base.Preconditions.checkNotNull; import javax.inject.Provider; import org.jclouds.blobstore.AsyncBlobStore; +import org.jclouds.blobstore.LocalStorageStrategy; import org.jclouds.blobstore.domain.BlobBuilder; import org.jclouds.blobstore.options.ListContainerOptions; import org.jclouds.blobstore.util.BlobUtils; -import org.jclouds.filesystem.strategy.FilesystemStorageStrategy; +import org.jclouds.filesystem.strategy.internal.FilesystemStorageStrategyImpl; import com.google.inject.Inject; /** * Implements the {@link BlobUtils} interfaced and act as a bridge to - * {@link FilesystemStorageStrategy} when used inside {@link AsyncBlobStore} + * {@link LocalStorageStrategy} when used inside {@link AsyncBlobStore} * * @author Alfredo "Rainbowbreeze" Morresi */ public class FileSystemBlobUtilsImpl implements BlobUtils { - protected final FilesystemStorageStrategy storageStrategy; + protected final FilesystemStorageStrategyImpl storageStrategy; protected final Provider blobBuilders; @Inject - public FileSystemBlobUtilsImpl(FilesystemStorageStrategy storageStrategy, Provider blobBuilders) { - this.storageStrategy = checkNotNull(storageStrategy, "Filesystem Storage Strategy"); + public FileSystemBlobUtilsImpl(LocalStorageStrategy storageStrategy, Provider blobBuilders) { + this.storageStrategy = (FilesystemStorageStrategyImpl) checkNotNull(storageStrategy, "Filesystem Storage Strategy"); this.blobBuilders = checkNotNull(blobBuilders, "Filesystem blobBuilders"); } diff --git a/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java b/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java index 69c6ce1a05..e2bc7d91a9 100644 --- a/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java +++ b/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java @@ -42,7 +42,6 @@ import org.jclouds.blobstore.options.ListContainerOptions; import org.jclouds.encryption.internal.JCECrypto; import org.jclouds.filesystem.predicates.validators.internal.FilesystemBlobKeyValidatorImpl; import org.jclouds.filesystem.predicates.validators.internal.FilesystemContainerNameValidatorImpl; -import org.jclouds.filesystem.strategy.FilesystemStorageStrategy; import org.jclouds.filesystem.utils.TestUtils; import org.jclouds.io.payloads.FilePayload; import org.testng.annotations.AfterMethod; @@ -73,7 +72,7 @@ public class FilesystemStorageStrategyImplTest { System.setProperty(LOGGING_CONFIG_KEY, LOGGING_CONFIG_VALUE); } - private FilesystemStorageStrategy storageStrategy; + private FilesystemStorageStrategyImpl storageStrategy; @BeforeMethod protected void setUp() throws Exception { @@ -414,7 +413,7 @@ public class FilesystemStorageStrategyImplTest { String absoluteContainerPath = absoluteBasePath + CONTAINER_NAME + FS; // create storageStrategy with an absolute path - FilesystemStorageStrategy storageStrategyAbsolute = new FilesystemStorageStrategyImpl( + FilesystemStorageStrategyImpl storageStrategyAbsolute = new FilesystemStorageStrategyImpl( new Provider() { @Override public BlobBuilder get() { diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/FilesystemStorageStrategy.java b/blobstore/src/main/java/org/jclouds/blobstore/LocalStorageStrategy.java similarity index 51% rename from apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/FilesystemStorageStrategy.java rename to blobstore/src/main/java/org/jclouds/blobstore/LocalStorageStrategy.java index fc648f1092..1d830afdd0 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/FilesystemStorageStrategy.java +++ b/blobstore/src/main/java/org/jclouds/blobstore/LocalStorageStrategy.java @@ -16,21 +16,20 @@ * specific language governing permissions and limitations * under the License. */ -package org.jclouds.filesystem.strategy; +package org.jclouds.blobstore; -import java.io.File; import java.io.IOException; import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.options.ListContainerOptions; -import org.jclouds.io.Payload; +import org.jclouds.domain.Location; /** - * Strategy for filesystem operations related to container and blob + * Strategy for local operations related to container and blob * * @author Alfredo "Rainbowbreeze" Morresi */ -public interface FilesystemStorageStrategy { +public interface LocalStorageStrategy { /** * Creates a new container @@ -38,7 +37,7 @@ public interface FilesystemStorageStrategy { * @param container * @return */ - boolean createContainer(String container); + boolean createContainerInLocation(String container, Location location); /** * Deletes a container and all its content @@ -58,17 +57,17 @@ public interface FilesystemStorageStrategy { * delete the container itself * @param container */ - void clearContainer(final String container); + void clearContainer(String container); /** - * Like {@link #clearContainer(String)} except you can use options to do things like recursive - * deletes, or clear at a different path than root. - * - * @param container - * what to clear - * @param options - * recursion and path to clear - */ + * Like {@link #clearContainer(String)} except you can use options to do things like recursive + * deletes, or clear at a different path than root. + * + * @param container + * what to clear + * @param options + * recursion and path to clear + */ void clearContainer(String container, ListContainerOptions options); /** @@ -78,64 +77,26 @@ public interface FilesystemStorageStrategy { Iterable getAllContainerNames(); /** - * Determines if a directory exists - * - * @param container - * container where the directory resides - * @param directory - * full path to the directory - */ - boolean directoryExists(String container, String directory); - - /** - * Creates a folder or a directory marker depending on the service - * - * @param container - * container to create the directory in - * @param directory - * full path to the directory - */ - void createDirectory(String container, String directory); - - /** - * Deletes a folder or a directory marker depending on the service - * - * @param container - * container to delete the directory from - * @param directory - * full path to the directory to delete - */ - void deleteDirectory(String container, String directory); - - - /** - * Creates a new blob - * @param name - * @return - */ - Blob newBlob(String name); - - /** - * + * Return true if a blob named by key exists * @param container * @param key * @return */ boolean blobExists(String container, String key); - /** - * Load the blob with the given key belonging to the container with the given - * name. There must exist a resource on the file system whose complete name - * is given concatenating the container name and the key - * - * @param container - * it's the name of the container the blob belongs to - * @param key - * it's the key of the blob - * - * @return the blob belonging to the given container with the given key - */ - Blob getBlob(final String containerName, final String blobName); + /** + * Load the blob with the given key belonging to the container with the given + * name. There must exist a resource on the file system whose complete name + * is given concatenating the container name and the key + * + * @param container + * it's the name of the container the blob belongs to + * @param key + * it's the key of the blob + * + * @return the blob belonging to the given container with the given key + */ + Blob getBlob(String containerName, String blobName); /** * Returns all the blobs key inside a container @@ -146,23 +107,7 @@ public interface FilesystemStorageStrategy { Iterable getBlobKeysInsideContainer(String container) throws IOException; /** - * Counts number of blobs inside a container - * @param container - * @param options - * @return - */ - long countBlobs(String container, ListContainerOptions options); - - /** - * Returns a {@link File} object that links to the blob - * @param container - * @param key - * @return - */ - File getFileForBlobKey(String container, String key); - - /** - * + * Remove blob named by the given key * @param container * @param key */ diff --git a/blobstore/src/main/java/org/jclouds/blobstore/TransientStorageStrategy.java b/blobstore/src/main/java/org/jclouds/blobstore/TransientStorageStrategy.java index 611e93ba25..a9030c1f42 100644 --- a/blobstore/src/main/java/org/jclouds/blobstore/TransientStorageStrategy.java +++ b/blobstore/src/main/java/org/jclouds/blobstore/TransientStorageStrategy.java @@ -26,9 +26,10 @@ import com.google.common.base.Preconditions; import com.google.common.base.Supplier; import org.jclouds.blobstore.domain.Blob; +import org.jclouds.blobstore.options.ListContainerOptions; import org.jclouds.domain.Location; -public class TransientStorageStrategy { +public class TransientStorageStrategy implements LocalStorageStrategy { private final ConcurrentMap> containerToBlobs = new ConcurrentHashMap>(); private final ConcurrentMap containerToLocation = new ConcurrentHashMap(); private final Supplier defaultLocation; @@ -49,6 +50,12 @@ public class TransientStorageStrategy { containerToBlobs.get(containerName).clear(); } + @Override + public void clearContainer(String container, ListContainerOptions options) { + // TODO implement options + clearContainer(container); + } + public boolean createContainerInLocation(final String containerName, final Location location) { ConcurrentMap origValue = containerToBlobs.putIfAbsent( containerName, new ConcurrentHashMap());