Support new method `BlobContainer#move()`

This has been added in elasticsearch 2.0.0. See elasticsearch/elasticsearch#8782

Wondering if this PR should be split in two parts:

* one that clean the code and which is portable to 1.4, 1.x and master
* one that simply add move() method for master branch

@imotov WDYT?

Closes #49.
This commit is contained in:
David Pilato 2015-01-30 18:31:00 +01:00
parent b91f5da2b2
commit c20371aa8f
4 changed files with 67 additions and 0 deletions

View File

@ -59,4 +59,6 @@ public interface AzureStorageService {
OutputStream getOutputStream(String container, String blob) throws URISyntaxException, StorageException;
ImmutableMap<String,BlobMetaData> listBlobsByPrefix(String container, String keyPath, String prefix) throws URISyntaxException, StorageException, ServiceException;
void moveBlob(String container, String sourceBlob, String targetBlob) throws URISyntaxException, StorageException;
}

View File

@ -214,6 +214,19 @@ public class AzureStorageServiceImpl extends AbstractLifecycleComponent<AzureSto
return blobsBuilder.build();
}
@Override
public void moveBlob(String container, String sourceBlob, String targetBlob) throws URISyntaxException, StorageException {
logger.debug("moveBlob container [{}], sourceBlob [{}], targetBlob [{}]", container, sourceBlob, targetBlob);
CloudBlobContainer blob_container = client.getContainerReference(container);
CloudBlockBlob blobSource = blob_container.getBlockBlobReference(sourceBlob);
if (blobSource.exists()) {
CloudBlockBlob blobTarget = blob_container.getBlockBlobReference(targetBlob);
blobTarget.copyFromBlob(blobSource);
blobSource.delete();
logger.debug("moveBlob container [{}], sourceBlob [{}], targetBlob [{}] -> done", container, sourceBlob, targetBlob);
}
}
@Override
protected void doStart() throws ElasticsearchException {
logger.debug("starting azure storage client instance");

View File

@ -118,6 +118,24 @@ public class AzureBlobContainer extends AbstractBlobContainer {
}
}
@Override
public void move(String sourceBlobName, String targetBlobName) throws IOException {
try {
String source = keyPath + sourceBlobName;
String target = keyPath + targetBlobName;
logger.debug("moving blob [{}] to [{}] in container {{}}", source, target, blobStore.container());
blobStore.client().moveBlob(blobStore.container(), source, target);
} catch (URISyntaxException e) {
logger.warn("can not move blob [{}] to [{}] in container {{}}: {}", sourceBlobName, targetBlobName, blobStore.container(), e.getMessage());
throw new IOException(e);
} catch (StorageException e) {
logger.warn("can not move blob [{}] to [{}] in container {{}}: {}", sourceBlobName, targetBlobName, blobStore.container(), e.getMessage());
throw new IOException(e);
}
}
@Override
public ImmutableMap<String, BlobMetaData> listBlobs() throws IOException {
return listBlobsByPrefix(null);

View File

@ -99,6 +99,17 @@ public class AzureStorageServiceMock extends AbstractLifecycleComponent<AzureSto
return map;
}
@Override
public void moveBlob(String container, String sourceBlob, String targetBlob) throws URISyntaxException, StorageException {
for (String blobName : blobs.keySet()) {
if (endsWithIgnoreCase(blobName, sourceBlob)) {
ByteArrayOutputStream outputStream = blobs.get(blobName);
blobs.put(blobName.replace(sourceBlob, targetBlob), outputStream);
blobs.remove(blobName);
}
}
}
@Override
protected void doStart() throws ElasticsearchException {
}
@ -133,4 +144,27 @@ public class AzureStorageServiceMock extends AbstractLifecycleComponent<AzureSto
String lcPrefix = prefix.toLowerCase(Locale.ROOT);
return lcStr.equals(lcPrefix);
}
/**
* Test if the given String ends with the specified suffix,
* ignoring upper/lower case.
*
* @param str the String to check
* @param suffix the suffix to look for
* @see java.lang.String#startsWith
*/
public static boolean endsWithIgnoreCase(String str, String suffix) {
if (str == null || suffix == null) {
return false;
}
if (str.endsWith(suffix)) {
return true;
}
if (str.length() < suffix.length()) {
return false;
}
String lcStr = str.substring(0, suffix.length()).toLowerCase(Locale.ROOT);
String lcPrefix = suffix.toLowerCase(Locale.ROOT);
return lcStr.equals(lcPrefix);
}
}