Skip to content

Commit c20371a

Browse files
committed
Support new method BlobContainer#move()
This has been added in elasticsearch 2.0.0. See #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.
1 parent b91f5da commit c20371a

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

src/main/java/org/elasticsearch/cloud/azure/AzureStorageService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,6 @@ static public final class Fields {
5959
OutputStream getOutputStream(String container, String blob) throws URISyntaxException, StorageException;
6060

6161
ImmutableMap<String,BlobMetaData> listBlobsByPrefix(String container, String keyPath, String prefix) throws URISyntaxException, StorageException, ServiceException;
62+
63+
void moveBlob(String container, String sourceBlob, String targetBlob) throws URISyntaxException, StorageException;
6264
}

src/main/java/org/elasticsearch/cloud/azure/AzureStorageServiceImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,19 @@ public ImmutableMap<String, BlobMetaData> listBlobsByPrefix(String container, St
214214
return blobsBuilder.build();
215215
}
216216

217+
@Override
218+
public void moveBlob(String container, String sourceBlob, String targetBlob) throws URISyntaxException, StorageException {
219+
logger.debug("moveBlob container [{}], sourceBlob [{}], targetBlob [{}]", container, sourceBlob, targetBlob);
220+
CloudBlobContainer blob_container = client.getContainerReference(container);
221+
CloudBlockBlob blobSource = blob_container.getBlockBlobReference(sourceBlob);
222+
if (blobSource.exists()) {
223+
CloudBlockBlob blobTarget = blob_container.getBlockBlobReference(targetBlob);
224+
blobTarget.copyFromBlob(blobSource);
225+
blobSource.delete();
226+
logger.debug("moveBlob container [{}], sourceBlob [{}], targetBlob [{}] -> done", container, sourceBlob, targetBlob);
227+
}
228+
}
229+
217230
@Override
218231
protected void doStart() throws ElasticsearchException {
219232
logger.debug("starting azure storage client instance");

src/main/java/org/elasticsearch/cloud/azure/blobstore/AzureBlobContainer.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ public ImmutableMap<String, BlobMetaData> listBlobsByPrefix(@Nullable String pre
118118
}
119119
}
120120

121+
@Override
122+
public void move(String sourceBlobName, String targetBlobName) throws IOException {
123+
try {
124+
String source = keyPath + sourceBlobName;
125+
String target = keyPath + targetBlobName;
126+
127+
logger.debug("moving blob [{}] to [{}] in container {{}}", source, target, blobStore.container());
128+
129+
blobStore.client().moveBlob(blobStore.container(), source, target);
130+
} catch (URISyntaxException e) {
131+
logger.warn("can not move blob [{}] to [{}] in container {{}}: {}", sourceBlobName, targetBlobName, blobStore.container(), e.getMessage());
132+
throw new IOException(e);
133+
} catch (StorageException e) {
134+
logger.warn("can not move blob [{}] to [{}] in container {{}}: {}", sourceBlobName, targetBlobName, blobStore.container(), e.getMessage());
135+
throw new IOException(e);
136+
}
137+
}
138+
121139
@Override
122140
public ImmutableMap<String, BlobMetaData> listBlobs() throws IOException {
123141
return listBlobsByPrefix(null);

src/test/java/org/elasticsearch/repositories/azure/AzureStorageServiceMock.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ public ImmutableMap<String, BlobMetaData> listBlobsByPrefix(String container, St
9999
return map;
100100
}
101101

102+
@Override
103+
public void moveBlob(String container, String sourceBlob, String targetBlob) throws URISyntaxException, StorageException {
104+
for (String blobName : blobs.keySet()) {
105+
if (endsWithIgnoreCase(blobName, sourceBlob)) {
106+
ByteArrayOutputStream outputStream = blobs.get(blobName);
107+
blobs.put(blobName.replace(sourceBlob, targetBlob), outputStream);
108+
blobs.remove(blobName);
109+
}
110+
}
111+
}
112+
102113
@Override
103114
protected void doStart() throws ElasticsearchException {
104115
}
@@ -133,4 +144,27 @@ public static boolean startsWithIgnoreCase(String str, String prefix) {
133144
String lcPrefix = prefix.toLowerCase(Locale.ROOT);
134145
return lcStr.equals(lcPrefix);
135146
}
147+
148+
/**
149+
* Test if the given String ends with the specified suffix,
150+
* ignoring upper/lower case.
151+
*
152+
* @param str the String to check
153+
* @param suffix the suffix to look for
154+
* @see java.lang.String#startsWith
155+
*/
156+
public static boolean endsWithIgnoreCase(String str, String suffix) {
157+
if (str == null || suffix == null) {
158+
return false;
159+
}
160+
if (str.endsWith(suffix)) {
161+
return true;
162+
}
163+
if (str.length() < suffix.length()) {
164+
return false;
165+
}
166+
String lcStr = str.substring(0, suffix.length()).toLowerCase(Locale.ROOT);
167+
String lcPrefix = suffix.toLowerCase(Locale.ROOT);
168+
return lcStr.equals(lcPrefix);
169+
}
136170
}

0 commit comments

Comments
 (0)