Miscellaneous local blobstore cleanups

There are no more functional differences between the filesystem and
transient blobstores.  This is the last commit before introducing a
unified LocalAsyncBlobStore class.
This commit is contained in:
Andrew Gaul 2012-07-19 10:36:18 -07:00 committed by Andrew Gaul
parent b890765e9e
commit 756e46333f
5 changed files with 49 additions and 11 deletions

View File

@ -33,7 +33,6 @@ import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
import static com.google.common.util.concurrent.Futures.immediateFuture;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Set;
@ -43,8 +42,6 @@ import java.util.concurrent.ExecutorService;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import org.jclouds.Constants;
import org.jclouds.blobstore.BlobStoreContext;
@ -71,7 +68,6 @@ import org.jclouds.blobstore.util.BlobStoreUtils;
import org.jclouds.blobstore.util.BlobUtils;
import org.jclouds.collect.Memoized;
import org.jclouds.domain.Location;
import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator;
import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
@ -81,7 +77,6 @@ import org.jclouds.io.ContentMetadata;
import org.jclouds.io.ContentMetadataCodec;
import org.jclouds.io.Payload;
import org.jclouds.logging.Logger;
import org.jclouds.rest.annotations.ParamValidators;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
@ -190,7 +185,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
}
}
final String delimiter = options.isRecursive() ? null : File.separator;
final String delimiter = options.isRecursive() ? null : storageStrategy.getSeparator();
if (delimiter != null) {
SortedSet<String> commonPrefixes = newTreeSet(
transform(contents, new CommonPrefixes(prefix, delimiter)));
@ -237,6 +232,15 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
return immediateFuture(null);
}
/**
* {@inheritDoc}
*/
@Override
public ListenableFuture<Void> clearContainer(final String container) {
storageStrategy.clearContainer(container);
return immediateFuture(null);
}
/**
* Override parent method because it uses strange futures and listenables
* that creates problem in the test if more than one test that deletes the
@ -251,6 +255,22 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
return immediateFuture(null);
}
public ListenableFuture<Boolean> deleteContainerIfEmpty(final String container) {
Boolean returnVal = true;
if (storageStrategy.containerExists(container)) {
try {
if (Iterables.isEmpty(storageStrategy.getBlobKeysInsideContainer(container)))
storageStrategy.deleteContainer(container);
else
returnVal = false;
} catch (IOException e) {
logger.error(e, "An error occurred loading blobs contained into container %s", container);
Throwables.propagate(e);
}
}
return immediateFuture(returnVal);
}
/**
* {@inheritDoc}
*/
@ -285,11 +305,10 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
/**
* {@inheritDoc}
*/
@Path("{container}")
@Override
public ListenableFuture<Boolean> createContainerInLocation(final Location location,
@PathParam("container") @ParamValidators({ FilesystemContainerNameValidator.class }) String name) {
boolean result = storageStrategy.createContainerInLocation(name, null);
final String name) {
boolean result = storageStrategy.createContainerInLocation(name, location);
return immediateFuture(result);
}

View File

@ -116,6 +116,7 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
}
public boolean createContainer(String container) {
filesystemContainerNameValidator.validate(container);
return createContainerInLocation(container, null);
}
@ -279,6 +280,11 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
return null;
}
@Override
public String getSeparator() {
return File.separator;
}
public boolean directoryExists(String container, String directory) {
return buildPathAndChecksIfDirectoryExists(container, directory);
}

View File

@ -127,4 +127,7 @@ public interface LocalStorageStrategy {
* @return Location of container or null
*/
Location getLocation(String containerName);
/** @return path separator, either / or \ */
String getSeparator();
}

View File

@ -181,7 +181,7 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore {
}
}
final String delimiter = options.isRecursive() ? null : "/";
final String delimiter = options.isRecursive() ? null : storageStrategy.getSeparator();
if (delimiter != null) {
SortedSet<String> commonPrefixes = newTreeSet(
transform(contents, new CommonPrefixes(prefix, delimiter)));
@ -238,7 +238,12 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore {
}
/**
* {@inheritDoc}
* Override parent method because it uses strange futures and listenables
* that creates problem in the test if more than one test that deletes the
* container is executed
*
* @param container
* @return
*/
@Override
public ListenableFuture<Void> deleteContainer(final String container) {

View File

@ -143,6 +143,11 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
return containerToLocation.get(containerName);
}
@Override
public String getSeparator() {
return "/";
}
private Blob createUpdatedCopyOfBlobInContainer(String containerName, Blob in) {
checkNotNull(in, "blob");
checkNotNull(in.getPayload(), "blob.payload");