mirror of https://github.com/apache/jclouds.git
Shuffle strategy methods into consistent order
Group by container, blob, and miscellaneous methods. Within these groups, sort by by exists, getter, setter, and remove. Code movement only; no functional changes.
This commit is contained in:
parent
3e2e24493e
commit
f5548f3a94
|
@ -33,6 +33,10 @@ import javax.inject.Inject;
|
|||
import javax.inject.Named;
|
||||
import javax.inject.Provider;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.io.Closeables;
|
||||
import com.google.common.io.Files;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.filefilter.DirectoryFileFilter;
|
||||
import org.jclouds.blobstore.LocalStorageStrategy;
|
||||
|
@ -50,10 +54,6 @@ import org.jclouds.io.Payloads;
|
|||
import org.jclouds.logging.Logger;
|
||||
import org.jclouds.rest.annotations.ParamValidators;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.io.Closeables;
|
||||
import com.google.common.io.Files;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alfredo "Rainbowbreeze" Morresi
|
||||
|
@ -92,32 +92,15 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean blobExists(String container, String key) {
|
||||
filesystemContainerNameValidator.validate(container);
|
||||
filesystemBlobKeyValidator.validate(key);
|
||||
return buildPathAndChecksIfFileExists(container, key);
|
||||
}
|
||||
|
||||
public Iterable<String> getAllContainerNames() {
|
||||
Iterable<String> containers = new Iterable<String>() {
|
||||
@Override
|
||||
public Blob getBlob(final String container, final String key) {
|
||||
BlobBuilder builder = blobBuilders.get();
|
||||
builder.name(key);
|
||||
File file = getFileForBlobKey(container, key);
|
||||
try {
|
||||
builder.payload(file).calculateMD5();
|
||||
} catch (IOException e) {
|
||||
logger.error("An error occurred calculating MD5 for blob %s from container ", key, container);
|
||||
Throwables.propagateIfPossible(e);
|
||||
}
|
||||
Blob blob = builder.build();
|
||||
if (blob.getPayload().getContentMetadata().getContentMD5() != null)
|
||||
blob.getMetadata().setETag(CryptoStreams.hex(blob.getPayload().getContentMetadata().getContentMD5()));
|
||||
return blob;
|
||||
public Iterator<String> iterator() {
|
||||
return new FileIterator(buildPathStartingFromBaseDir(), DirectoryFileFilter.INSTANCE);
|
||||
}
|
||||
};
|
||||
|
||||
public boolean createContainer(String container) {
|
||||
filesystemContainerNameValidator.validate(container);
|
||||
return createContainerInLocation(container, null);
|
||||
return containers;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -134,22 +117,15 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
|
|||
deleteDirectory(container, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty the directory of its content (files and subdirectories)
|
||||
*
|
||||
* @param container
|
||||
*/
|
||||
@Override
|
||||
public void clearContainer(final String container) {
|
||||
filesystemContainerNameValidator.validate(container);
|
||||
clearContainer(container, ListContainerOptions.Builder.recursive());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearContainer(String container, ListContainerOptions options) {
|
||||
filesystemContainerNameValidator.validate(container);
|
||||
// TODO
|
||||
// now all is deleted, check it based on options
|
||||
// TODO implement options
|
||||
try {
|
||||
File containerFile = openFolder(container);
|
||||
File[] children = containerFile.listFiles();
|
||||
|
@ -163,85 +139,11 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
|
|||
}
|
||||
}
|
||||
|
||||
public Blob newBlob(@ParamValidators({ FilesystemBlobKeyValidator.class }) String name) {
|
||||
filesystemBlobKeyValidator.validate(name);
|
||||
return blobBuilders.get().name(name).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeBlob(final String container, final String blobKey) {
|
||||
public boolean blobExists(String container, String key) {
|
||||
filesystemContainerNameValidator.validate(container);
|
||||
filesystemBlobKeyValidator.validate(blobKey);
|
||||
String fileName = buildPathStartingFromBaseDir(container, blobKey);
|
||||
logger.debug("Deleting blob %s", fileName);
|
||||
File fileToBeDeleted = new File(fileName);
|
||||
fileToBeDeleted.delete();
|
||||
|
||||
// now examins if the key of the blob is a complex key (with a directory structure)
|
||||
// and eventually remove empty directory
|
||||
removeDirectoriesTreeOfBlobKey(container, blobKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an iterator that reports all the containers under base path
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Iterable<String> getAllContainerNames() {
|
||||
Iterable<String> containers = new Iterable<String>() {
|
||||
@Override
|
||||
public Iterator<String> iterator() {
|
||||
return new FileIterator(buildPathStartingFromBaseDir(), DirectoryFileFilter.INSTANCE);
|
||||
}
|
||||
};
|
||||
|
||||
return containers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link File} object that links to the blob
|
||||
*
|
||||
* @param container
|
||||
* @param blobKey
|
||||
* @return
|
||||
*/
|
||||
public File getFileForBlobKey(String container, String blobKey) {
|
||||
filesystemContainerNameValidator.validate(container);
|
||||
filesystemBlobKeyValidator.validate(blobKey);
|
||||
String fileName = buildPathStartingFromBaseDir(container, blobKey);
|
||||
File blobFile = new File(fileName);
|
||||
return blobFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String putBlob(final String containerName, final Blob blob) throws IOException {
|
||||
String blobKey = blob.getMetadata().getName();
|
||||
Payload payload = blob.getPayload();
|
||||
filesystemContainerNameValidator.validate(containerName);
|
||||
filesystemBlobKeyValidator.validate(blobKey);
|
||||
File outputFile = getFileForBlobKey(containerName, blobKey);
|
||||
FileOutputStream output = null;
|
||||
try {
|
||||
Files.createParentDirs(outputFile);
|
||||
if (payload.getRawContent() instanceof File)
|
||||
Files.copy((File) payload.getRawContent(), outputFile);
|
||||
else {
|
||||
output = new FileOutputStream(outputFile);
|
||||
payload.writeTo(output);
|
||||
}
|
||||
Payloads.calculateMD5(payload, crypto.md5());
|
||||
String eTag = CryptoStreams.hex(payload.getContentMetadata().getContentMD5());
|
||||
return eTag;
|
||||
} catch (IOException ex) {
|
||||
if (outputFile != null) {
|
||||
outputFile.delete();
|
||||
}
|
||||
throw ex;
|
||||
} finally {
|
||||
Closeables.closeQuietly(output);
|
||||
payload.release();
|
||||
}
|
||||
filesystemBlobKeyValidator.validate(key);
|
||||
return buildPathAndChecksIfFileExists(container, key);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -275,6 +177,67 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
|
|||
return blobNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Blob getBlob(final String container, final String key) {
|
||||
BlobBuilder builder = blobBuilders.get();
|
||||
builder.name(key);
|
||||
File file = getFileForBlobKey(container, key);
|
||||
try {
|
||||
builder.payload(file).calculateMD5();
|
||||
} catch (IOException e) {
|
||||
logger.error("An error occurred calculating MD5 for blob %s from container ", key, container);
|
||||
Throwables.propagateIfPossible(e);
|
||||
}
|
||||
Blob blob = builder.build();
|
||||
if (blob.getPayload().getContentMetadata().getContentMD5() != null)
|
||||
blob.getMetadata().setETag(CryptoStreams.hex(blob.getPayload().getContentMetadata().getContentMD5()));
|
||||
return blob;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String putBlob(final String containerName, final Blob blob) throws IOException {
|
||||
String blobKey = blob.getMetadata().getName();
|
||||
Payload payload = blob.getPayload();
|
||||
filesystemContainerNameValidator.validate(containerName);
|
||||
filesystemBlobKeyValidator.validate(blobKey);
|
||||
File outputFile = getFileForBlobKey(containerName, blobKey);
|
||||
FileOutputStream output = null;
|
||||
try {
|
||||
Files.createParentDirs(outputFile);
|
||||
if (payload.getRawContent() instanceof File)
|
||||
Files.copy((File) payload.getRawContent(), outputFile);
|
||||
else {
|
||||
output = new FileOutputStream(outputFile);
|
||||
payload.writeTo(output);
|
||||
}
|
||||
Payloads.calculateMD5(payload, crypto.md5());
|
||||
String eTag = CryptoStreams.hex(payload.getContentMetadata().getContentMD5());
|
||||
return eTag;
|
||||
} catch (IOException ex) {
|
||||
if (outputFile != null) {
|
||||
outputFile.delete();
|
||||
}
|
||||
throw ex;
|
||||
} finally {
|
||||
Closeables.closeQuietly(output);
|
||||
payload.release();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeBlob(final String container, final String blobKey) {
|
||||
filesystemContainerNameValidator.validate(container);
|
||||
filesystemBlobKeyValidator.validate(blobKey);
|
||||
String fileName = buildPathStartingFromBaseDir(container, blobKey);
|
||||
logger.debug("Deleting blob %s", fileName);
|
||||
File fileToBeDeleted = new File(fileName);
|
||||
fileToBeDeleted.delete();
|
||||
|
||||
// now examins if the key of the blob is a complex key (with a directory structure)
|
||||
// and eventually remove empty directory
|
||||
removeDirectoriesTreeOfBlobKey(container, blobKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getLocation(final String containerName) {
|
||||
return null;
|
||||
|
@ -285,6 +248,31 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
|
|||
return File.separator;
|
||||
}
|
||||
|
||||
public boolean createContainer(String container) {
|
||||
filesystemContainerNameValidator.validate(container);
|
||||
return createContainerInLocation(container, null);
|
||||
}
|
||||
|
||||
public Blob newBlob(@ParamValidators({ FilesystemBlobKeyValidator.class }) String name) {
|
||||
filesystemBlobKeyValidator.validate(name);
|
||||
return blobBuilders.get().name(name).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link File} object that links to the blob
|
||||
*
|
||||
* @param container
|
||||
* @param blobKey
|
||||
* @return
|
||||
*/
|
||||
public File getFileForBlobKey(String container, String blobKey) {
|
||||
filesystemContainerNameValidator.validate(container);
|
||||
filesystemBlobKeyValidator.validate(blobKey);
|
||||
String fileName = buildPathStartingFromBaseDir(container, blobKey);
|
||||
File blobFile = new File(fileName);
|
||||
return blobFile;
|
||||
}
|
||||
|
||||
public boolean directoryExists(String container, String directory) {
|
||||
return buildPathAndChecksIfDirectoryExists(container, directory);
|
||||
}
|
||||
|
|
|
@ -31,6 +31,19 @@ import org.jclouds.domain.Location;
|
|||
*/
|
||||
public interface LocalStorageStrategy {
|
||||
|
||||
/**
|
||||
* Checks if a container exists
|
||||
* @param container
|
||||
* @return
|
||||
*/
|
||||
boolean containerExists(String container);
|
||||
|
||||
/**
|
||||
* Return an iterator that reports all the containers under base path
|
||||
* @return
|
||||
*/
|
||||
Iterable<String> getAllContainerNames();
|
||||
|
||||
/**
|
||||
* Creates a new container
|
||||
*
|
||||
|
@ -45,13 +58,6 @@ public interface LocalStorageStrategy {
|
|||
*/
|
||||
void deleteContainer(String container);
|
||||
|
||||
/**
|
||||
* Checks if a container exists
|
||||
* @param container
|
||||
* @return
|
||||
*/
|
||||
boolean containerExists(String container);
|
||||
|
||||
/**
|
||||
* Empty the container of its content (files and subdirectories), but doesn't
|
||||
* delete the container itself
|
||||
|
@ -70,12 +76,6 @@ public interface LocalStorageStrategy {
|
|||
*/
|
||||
void clearContainer(String container, ListContainerOptions options);
|
||||
|
||||
/**
|
||||
* Return an iterator that reports all the containers under base path
|
||||
* @return
|
||||
*/
|
||||
Iterable<String> getAllContainerNames();
|
||||
|
||||
/**
|
||||
* Return true if a blob named by key exists
|
||||
* @param container
|
||||
|
@ -84,6 +84,14 @@ public interface LocalStorageStrategy {
|
|||
*/
|
||||
boolean blobExists(String container, String key);
|
||||
|
||||
/**
|
||||
* Returns all the blobs key inside a container
|
||||
* @param container
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
Iterable<String> getBlobKeysInsideContainer(String container) throws IOException;
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
@ -98,21 +106,6 @@ public interface LocalStorageStrategy {
|
|||
*/
|
||||
Blob getBlob(String containerName, String blobName);
|
||||
|
||||
/**
|
||||
* Returns all the blobs key inside a container
|
||||
* @param container
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
Iterable<String> getBlobKeysInsideContainer(String container) throws IOException;
|
||||
|
||||
/**
|
||||
* Remove blob named by the given key
|
||||
* @param container
|
||||
* @param key
|
||||
*/
|
||||
void removeBlob(String container, String key);
|
||||
|
||||
/**
|
||||
* Write a {@link Blob} into a file
|
||||
* @param container
|
||||
|
@ -122,6 +115,13 @@ public interface LocalStorageStrategy {
|
|||
*/
|
||||
String putBlob(String containerName, Blob blob) throws IOException;
|
||||
|
||||
/**
|
||||
* Remove blob named by the given key
|
||||
* @param container
|
||||
* @param key
|
||||
*/
|
||||
void removeBlob(String container, String key);
|
||||
|
||||
/**
|
||||
* @param containerName name of container
|
||||
* @return Location of container or null
|
||||
|
|
|
@ -77,25 +77,14 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
|
|||
this.uriBuilders = uriBuilders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<String> getAllContainerNames() {
|
||||
return containerToBlobs.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containerExists(final String containerName) {
|
||||
return containerToBlobs.containsKey(containerName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearContainer(final String containerName) {
|
||||
containerToBlobs.get(containerName).clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearContainer(String container, ListContainerOptions options) {
|
||||
// TODO implement options
|
||||
clearContainer(container);
|
||||
public Iterable<String> getAllContainerNames() {
|
||||
return containerToBlobs.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -114,12 +103,28 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
|
|||
containerToBlobs.remove(containerName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearContainer(final String containerName) {
|
||||
clearContainer(containerName, ListContainerOptions.Builder.recursive());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearContainer(String containerName, ListContainerOptions options) {
|
||||
// TODO implement options
|
||||
containerToBlobs.get(containerName).clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean blobExists(final String containerName, final String blobName) {
|
||||
Map<String, Blob> map = containerToBlobs.get(containerName);
|
||||
return map != null && map.containsKey(blobName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<String> getBlobKeysInsideContainer(final String containerName) {
|
||||
return containerToBlobs.get(containerName).keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Blob getBlob(final String containerName, final String blobName) {
|
||||
Map<String, Blob> map = containerToBlobs.get(containerName);
|
||||
|
@ -143,11 +148,6 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
|
|||
map.remove(blobName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<String> getBlobKeysInsideContainer(final String containerName) {
|
||||
return containerToBlobs.get(containerName).keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getLocation(final String containerName) {
|
||||
return containerToLocation.get(containerName);
|
||||
|
|
Loading…
Reference in New Issue