Skip to content

Commit 1128077

Browse files
committed
Merge pull request #155 from simon-liubin/pro/source_batch
文件管理批量操作
2 parents cb9e9bc + 4730168 commit 1128077

File tree

3 files changed

+83
-51
lines changed

3 files changed

+83
-51
lines changed

src/main/java/com/qiniu/storage/BucketManager.java

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -259,64 +259,51 @@ private Response post(String url, byte[] body) throws QiniuException {
259259
public static class Batch {
260260
private ArrayList<String> ops;
261261

262-
private Batch(ArrayList<String> ops) {
263-
this.ops = ops;
262+
public Batch() {
263+
this.ops = new ArrayList<String>();
264264
}
265265

266-
public static Batch copy(String source_bucket, StringMap key_pairs, String target_bucket) {
267-
return twoKey("copy", source_bucket, key_pairs, target_bucket);
268-
}
269-
270-
public static Batch rename(String bucket, StringMap key_pairs) {
271-
return move(bucket, key_pairs, bucket);
266+
public Batch copy(String from_bucket, String from_key, String to_bucket, String to_key) {
267+
String from = entry(from_bucket, from_key);
268+
String to = entry(to_bucket, to_key);
269+
ops.add("copy" + "/" + from + "/" + to);
270+
return this;
272271
}
273272

274-
public static Batch move(String source_bucket, StringMap key_pairs, String target_bucket) {
275-
return twoKey("move", source_bucket, key_pairs, target_bucket);
273+
public Batch rename(String from_bucket, String from_key, String to_key) {
274+
return move(from_bucket, from_key, from_bucket, to_key);
276275
}
277276

278-
public static Batch delete(String bucket, String[] keys) {
279-
return oneKey("delete", bucket, keys);
277+
public Batch move(String from_bucket, String from_key, String to_bucket, String to_key) {
278+
String from = entry(from_bucket, from_key);
279+
String to = entry(to_bucket, to_key);
280+
ops.add("move" + "/" + from + "/" + to);
281+
return this;
280282
}
281283

282-
public static Batch stat(String bucket, String[] keys) {
283-
return oneKey("stat", bucket, keys);
284+
public Batch delete(String bucket, String... keys) {
285+
for (String key : keys) {
286+
ops.add("delete" + "/" + entry(bucket, key));
287+
}
288+
return this;
284289
}
285290

286-
public static Batch oneKey(String operation, String bucket, String[] keys) {
287-
ArrayList<String> ops = new ArrayList<String>(keys.length);
291+
public Batch stat(String bucket, String... keys) {
288292
for (String key : keys) {
289-
ops.add(operation + "/" + entry(bucket, key));
293+
ops.add("stat" + "/" + entry(bucket, key));
290294
}
291-
return new Batch(ops);
295+
return this;
292296
}
293297

294-
public static Batch twoKey(final String operation, final String source_bucket,
295-
StringMap key_pairs, String target_bucket) {
296-
297-
final String t_bucket = target_bucket == null ? source_bucket : target_bucket;
298-
final ArrayList<String> ops = new ArrayList<String>(key_pairs.size());
299-
key_pairs.forEach(new StringMap.Consumer() {
300-
@Override
301-
public void accept(String key, Object value) {
302-
String from = entry(source_bucket, key);
303-
String to = entry(t_bucket, (String) value);
304-
ops.add(operation + "/" + from + "/" + to);
305-
}
306-
});
307-
308-
return new Batch(ops);
298+
public byte[] toBody() {
299+
String body = StringUtils.join(ops, "&op=", "op=");
300+
return StringUtils.utf8Bytes(body);
309301
}
310302

311303
public Batch merge(Batch batch) {
312304
this.ops.addAll(batch.ops);
313305
return this;
314306
}
315-
316-
public byte[] toBody() {
317-
String body = StringUtils.join(ops, "&op=", "op=");
318-
return StringUtils.utf8Bytes(body);
319-
}
320307
}
321308

322309
/**

src/main/java/com/qiniu/util/StringUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,14 @@ public static String join(Collection<String> list, String sep, String prefix) {
104104
if (prefix != null) {
105105
buf.append(prefix);
106106
}
107+
int count = 0;
107108
for (String it : list) {
109+
count++;
108110
if (it != null) {
109111
buf.append(it);
110-
buf.append(sep);
112+
if (count < arraySize) {
113+
buf.append(sep);
114+
}
111115
}
112116
}
113117
return buf.toString();

src/test/java/com/qiniu/storage/BucketTest.java

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,16 @@ public void testList() {
4949
@Test
5050
public void testListIterator() {
5151
BucketManager.FileListIterator it = bucketManager.createFileListIterator(TestConfig.bucket, null, 2, null);
52+
53+
assertTrue(it.hasNext());
54+
FileInfo[] items0 = it.next();
55+
assertNotNull(items0[0]);
56+
5257
while (it.hasNext()) {
5358
FileInfo[] items = it.next();
54-
assertNotNull(items[0]);
59+
if (items.length > 1) {
60+
assertNotNull(items[0]);
61+
}
5562
}
5663
}
5764

@@ -147,11 +154,12 @@ public void testFetch() {
147154
}
148155
}
149156

157+
150158
@Test
151159
public void testBatchCopy() {
152160
String key = "copyTo" + Math.random();
153-
StringMap x = new StringMap().put(TestConfig.key, key);
154-
BucketManager.Batch ops = BucketManager.Batch.copy(TestConfig.bucket, x, TestConfig.bucket);
161+
BucketManager.Batch ops = new BucketManager.Batch().
162+
copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, key);
155163
try {
156164
Response r = bucketManager.batch(ops);
157165
BatchStatus[] bs = r.jsonToObject(BatchStatus[].class);
@@ -160,8 +168,7 @@ public void testBatchCopy() {
160168
e.printStackTrace();
161169
fail();
162170
}
163-
String[] array = {key};
164-
ops = BucketManager.Batch.delete(TestConfig.bucket, array);
171+
ops = new BucketManager.Batch().delete(TestConfig.bucket, key);
165172
try {
166173
Response r = bucketManager.batch(ops);
167174
BatchStatus[] bs = r.jsonToObject(BatchStatus[].class);
@@ -182,10 +189,8 @@ public void testBatchMove() {
182189
}
183190
String key2 = key + "to";
184191
StringMap x = new StringMap().put(key, key2);
185-
BucketManager.Batch ops = BucketManager.Batch.move(TestConfig.bucket,
186-
x,
187-
TestConfig.bucket
188-
);
192+
BucketManager.Batch ops = new BucketManager.Batch().move(TestConfig.bucket,
193+
key, TestConfig.bucket, key2);
189194
try {
190195
Response r = bucketManager.batch(ops);
191196
BatchStatus[] bs = r.jsonToObject(BatchStatus[].class);
@@ -212,8 +217,7 @@ public void testBatchRename() {
212217
fail();
213218
}
214219
String key2 = key + "to";
215-
StringMap x = new StringMap().put(key, key2);
216-
BucketManager.Batch ops = BucketManager.Batch.rename(TestConfig.bucket, x);
220+
BucketManager.Batch ops = new BucketManager.Batch().rename(TestConfig.bucket, key, key2);
217221
try {
218222
Response r = bucketManager.batch(ops);
219223
BatchStatus[] bs = r.jsonToObject(BatchStatus[].class);
@@ -233,7 +237,7 @@ public void testBatchRename() {
233237
@Test
234238
public void testBatchStat() {
235239
String[] array = {"java-sdk.html"};
236-
BucketManager.Batch ops = BucketManager.Batch.stat(TestConfig.bucket, array);
240+
BucketManager.Batch ops = new BucketManager.Batch().stat(TestConfig.bucket, array);
237241
try {
238242
Response r = bucketManager.batch(ops);
239243
BatchStatus[] bs = r.jsonToObject(BatchStatus[].class);
@@ -243,4 +247,41 @@ public void testBatchStat() {
243247
fail();
244248
}
245249
}
250+
251+
@Test
252+
public void testBatch() {
253+
String[] array = {"java-sdk.html"};
254+
String key = "copyFrom" + Math.random();
255+
256+
String key1 = "moveFrom" + Math.random();
257+
String key2 = "moveTo" + Math.random();
258+
259+
String key3 = "moveFrom" + Math.random();
260+
String key4 = "moveTo" + Math.random();
261+
262+
try {
263+
bucketManager.copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, key1);
264+
bucketManager.copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, key3);
265+
} catch (QiniuException e) {
266+
e.printStackTrace();
267+
fail();
268+
}
269+
270+
BucketManager.Batch ops = new BucketManager.Batch()
271+
.copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, key)
272+
.move(TestConfig.bucket, key1, TestConfig.bucket, key2)
273+
.rename(TestConfig.bucket, key3, key4)
274+
.stat(TestConfig.bucket, array)
275+
.stat(TestConfig.bucket, array[0]);
276+
try {
277+
Response r = bucketManager.batch(ops);
278+
BatchStatus[] bs = r.jsonToObject(BatchStatus[].class);
279+
for (BatchStatus b : bs) {
280+
assertEquals(200, b.code);
281+
}
282+
} catch (QiniuException e) {
283+
e.printStackTrace();
284+
fail();
285+
}
286+
}
246287
}

0 commit comments

Comments
 (0)