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.
This commit is contained in:
Andrew Gaul 2012-07-17 22:18:45 -07:00 committed by Andrew Gaul
parent 6264dee999
commit c30c2c4809
7 changed files with 60 additions and 108 deletions

View File

@ -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<Location> defaultLocation,
@Memoized Supplier<Set<? extends Location>> 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<Boolean> 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);
}

View File

@ -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);

View File

@ -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.");

View File

@ -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<BlobBuilder> blobBuilders;
@Inject
public FileSystemBlobUtilsImpl(FilesystemStorageStrategy storageStrategy, Provider<BlobBuilder> blobBuilders) {
this.storageStrategy = checkNotNull(storageStrategy, "Filesystem Storage Strategy");
public FileSystemBlobUtilsImpl(LocalStorageStrategy storageStrategy, Provider<BlobBuilder> blobBuilders) {
this.storageStrategy = (FilesystemStorageStrategyImpl) checkNotNull(storageStrategy, "Filesystem Storage Strategy");
this.blobBuilders = checkNotNull(blobBuilders, "Filesystem blobBuilders");
}

View File

@ -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<BlobBuilder>() {
@Override
public BlobBuilder get() {

View File

@ -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<String> 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<String> 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
*/

View File

@ -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<String, ConcurrentMap<String, Blob>> containerToBlobs = new ConcurrentHashMap<String, ConcurrentMap<String, Blob>>();
private final ConcurrentMap<String, Location> containerToLocation = new ConcurrentHashMap<String, Location>();
private final Supplier<Location> 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<String, Blob> origValue = containerToBlobs.putIfAbsent(
containerName, new ConcurrentHashMap<String, Blob>());