Harmonize filesystem and transient putBlob

This commit is contained in:
Andrew Gaul 2012-07-16 22:00:11 -07:00 committed by Andrew Gaul
parent 3154371433
commit 02203afb07
6 changed files with 47 additions and 26 deletions

View File

@ -457,17 +457,22 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
String blobKey = blob.getMetadata().getName(); String blobKey = blob.getMetadata().getName();
logger.debug("Put blob with key [%s] to container [%s]", blobKey, containerName); logger.debug("Put blob with key [%s] to container [%s]", blobKey, containerName);
String eTag = getEtag(blob); if (!storageStrategy.containerExists(containerName)) {
return Futures.immediateFailedFuture(new IllegalStateException("containerName not found: " + containerName));
}
try { try {
// TODO // TODO
// must override existing file? // must override existing file?
storageStrategy.writePayloadOnFile(containerName, blobKey, blob.getPayload()); storageStrategy.putBlob(containerName, blob);
} catch (IOException e) { } catch (IOException e) {
logger.error(e, "An error occurred storing the new blob with name [%s] to container [%s].", blobKey, logger.error(e, "An error occurred storing the new blob with name [%s] to container [%s].", blobKey,
containerName); containerName);
Throwables.propagate(e); Throwables.propagate(e);
} }
String eTag = getEtag(blob);
return immediateFuture(eTag); return immediateFuture(eTag);
} }
@ -591,13 +596,6 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
} }
} }
private Blob copyBlob(Blob blob) {
Blob returnVal = blobFactory.create(copy(blob.getMetadata()));
returnVal.setPayload(blob.getPayload());
copyPayloadHeadersToBlob(blob.getPayload(), returnVal);
return returnVal;
}
/** /**
* Calculates the object MD5 and returns it as eTag * Calculates the object MD5 and returns it as eTag
* *
@ -616,6 +614,13 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
return eTag; return eTag;
} }
private Blob copyBlob(Blob blob) {
Blob returnVal = blobFactory.create(copy(blob.getMetadata()));
returnVal.setPayload(blob.getPayload());
copyPayloadHeadersToBlob(blob.getPayload(), returnVal);
return returnVal;
}
@Override @Override
protected boolean deleteAndVerifyContainerGone(final String container) { protected boolean deleteAndVerifyContainerGone(final String container) {
storageStrategy.deleteContainer(container); storageStrategy.deleteContainer(container);

View File

@ -169,11 +169,11 @@ public interface FilesystemStorageStrategy {
void removeBlob(String container, String key); void removeBlob(String container, String key);
/** /**
* Write a {@link Blob} {@link Payload} into a file * Write a {@link Blob} into a file
* @param fileName * @param container
* @param payload * @param blob
* @throws IOException * @throws IOException
*/ */
void writePayloadOnFile(String container, String blobKey, Payload payload) throws IOException; void putBlob(String containerName, Blob blob) throws IOException;
} }

View File

@ -204,19 +204,13 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
return blobFile; return blobFile;
} }
/**
* Write a {@link Blob} {@link Payload} into a file
*
* @param container
* @param blobKey
* @param payload
* @throws IOException
*/
@Override @Override
public void writePayloadOnFile(String container, String blobKey, Payload payload) throws IOException { public void putBlob(final String containerName, final Blob blob) throws IOException {
filesystemContainerNameValidator.validate(container); String blobKey = blob.getMetadata().getName();
Payload payload = blob.getPayload();
filesystemContainerNameValidator.validate(containerName);
filesystemBlobKeyValidator.validate(blobKey); filesystemBlobKeyValidator.validate(blobKey);
File outputFile = getFileForBlobKey(container, blobKey); File outputFile = getFileForBlobKey(containerName, blobKey);
FileOutputStream output = null; FileOutputStream output = null;
try { try {
Files.createParentDirs(outputFile); Files.createParentDirs(outputFile);

View File

@ -740,6 +740,7 @@ public class FilesystemAsyncBlobStoreTest {
* can't be deleted. See http://code.google.com/p/jclouds/issues/detail?id=737 * can't be deleted. See http://code.google.com/p/jclouds/issues/detail?id=737
*/ */
final String containerName = "containerWithRanges"; final String containerName = "containerWithRanges";
blobStore.createContainerInLocation(null, containerName);
String payload = "abcdefgh"; String payload = "abcdefgh";
InputStream is; InputStream is;
Blob blob = blobStore.blobBuilder("test").payload(new StringPayload(payload)).build(); Blob blob = blobStore.blobBuilder("test").payload(new StringPayload(payload)).build();

View File

@ -371,8 +371,10 @@ public class FilesystemStorageStrategyImplTest {
blobKey = TestUtils.createRandomBlobKey("writePayload-", ".img"); blobKey = TestUtils.createRandomBlobKey("writePayload-", ".img");
sourceFile = TestUtils.getImageForBlobPayload(); sourceFile = TestUtils.getImageForBlobPayload();
filePayload = new FilePayload(sourceFile); filePayload = new FilePayload(sourceFile);
Blob blob = storageStrategy.newBlob(blobKey);
blob.setPayload(filePayload);
// write files // write files
storageStrategy.writePayloadOnFile(CONTAINER_NAME, blobKey, filePayload); storageStrategy.putBlob(CONTAINER_NAME, blob);
// verify that the files is equal // verify that the files is equal
File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey); File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey);
InputSupplier<FileInputStream> expectedInput = InputSupplier<FileInputStream> expectedInput =

View File

@ -472,7 +472,8 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore {
storageStrategy.putBlob(containerName, blob); storageStrategy.putBlob(containerName, blob);
return immediateFuture(Iterables.getOnlyElement(blob.getAllHeaders().get(HttpHeaders.ETAG))); String eTag = getEtag(blob);
return immediateFuture(eTag);
} }
private Blob createUpdatedCopyOfBlobInContainer(String containerName, Blob in) { private Blob createUpdatedCopyOfBlobInContainer(String containerName, Blob in) {
@ -635,6 +636,24 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore {
} }
} }
/**
* Calculates the object MD5 and returns it as eTag
*
* @param object
* @return
*/
private String getEtag(Blob object) {
try {
Payloads.calculateMD5(object, crypto.md5());
} catch (IOException ex) {
logger.error(ex, "An error occurred calculating MD5 for object with name %s.", object.getMetadata().getName());
Throwables.propagate(ex);
}
String eTag = CryptoStreams.hex(object.getPayload().getContentMetadata().getContentMD5());
return eTag;
}
private Blob copyBlob(Blob blob) { private Blob copyBlob(Blob blob) {
Blob returnVal = blobFactory.create(copy(blob.getMetadata())); Blob returnVal = blobFactory.create(copy(blob.getMetadata()));
returnVal.setPayload(blob.getPayload()); returnVal.setPayload(blob.getPayload());