Skip to content

Commit 516699c

Browse files
zhuxiaolong37huiguangjun
authored andcommitted
Optimize bucketStat code
1 parent 9e49383 commit 516699c

File tree

3 files changed

+123
-30
lines changed

3 files changed

+123
-30
lines changed

src/main/java/com/aliyun/oss/internal/ResponseParsers.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ public final class ResponseParsers {
227227
public static final GetSymbolicLinkResponseParser getSymbolicLinkResponseParser = new GetSymbolicLinkResponseParser();
228228

229229
public static final DeleteDirectoryResponseParser deleteDirectoryResponseParser = new DeleteDirectoryResponseParser();
230+
231+
public static Long parseLongWithDefault(String defaultValue){
232+
if(defaultValue == null || "".equals(defaultValue)){
233+
return 0L;
234+
}
235+
return Long.parseLong(defaultValue);
236+
}
237+
230238
public static final class EmptyResponseParser implements ResponseParser<ResponseMessage> {
231239

232240
@Override
@@ -2804,19 +2812,19 @@ public static BucketStat parseGetBucketStat(InputStream responseBody) throws Res
28042812
Long storage = Long.parseLong(root.getChildText("Storage"));
28052813
Long objectCount = Long.parseLong(root.getChildText("ObjectCount"));
28062814
Long multipartUploadCount = Long.parseLong(root.getChildText("MultipartUploadCount"));
2807-
Long liveChannelCount = Long.parseLong(root.getChildText("LiveChannelCount"));
2808-
Long lastModifiedTime = Long.parseLong(root.getChildText("LastModifiedTime"));
2809-
Long standardStorage = Long.parseLong(root.getChildText("StandardStorage"));
2810-
Long standardObjectCount = Long.parseLong(root.getChildText("StandardObjectCount"));
2811-
Long infrequentAccessStorage = Long.parseLong(root.getChildText("InfrequentAccessStorage"));
2812-
Long infrequentAccessRealStorage = Long.parseLong(root.getChildText("InfrequentAccessRealStorage"));
2813-
Long infrequentAccessObjectCount = Long.parseLong(root.getChildText("InfrequentAccessObjectCount"));
2814-
Long archiveStorage = Long.parseLong(root.getChildText("ArchiveStorage"));
2815-
Long archiveRealStorage = Long.parseLong(root.getChildText("ArchiveRealStorage"));
2816-
Long archiveObjectCount = Long.parseLong(root.getChildText("ArchiveObjectCount"));
2817-
Long coldArchiveStorage = Long.parseLong(root.getChildText("ColdArchiveStorage"));
2818-
Long coldArchiveRealStorage = Long.parseLong(root.getChildText("ColdArchiveRealStorage"));
2819-
Long coldArchiveObjectCount = Long.parseLong(root.getChildText("ColdArchiveObjectCount"));
2815+
Long liveChannelCount = parseLongWithDefault(root.getChildText("LiveChannelCount"));
2816+
Long lastModifiedTime = parseLongWithDefault(root.getChildText("LastModifiedTime"));
2817+
Long standardStorage = parseLongWithDefault(root.getChildText("StandardStorage"));
2818+
Long standardObjectCount = parseLongWithDefault(root.getChildText("StandardObjectCount"));
2819+
Long infrequentAccessStorage = parseLongWithDefault(root.getChildText("InfrequentAccessStorage"));
2820+
Long infrequentAccessRealStorage = parseLongWithDefault(root.getChildText("InfrequentAccessRealStorage"));
2821+
Long infrequentAccessObjectCount = parseLongWithDefault(root.getChildText("InfrequentAccessObjectCount"));
2822+
Long archiveStorage = parseLongWithDefault(root.getChildText("ArchiveStorage"));
2823+
Long archiveRealStorage = parseLongWithDefault(root.getChildText("ArchiveRealStorage"));
2824+
Long archiveObjectCount = parseLongWithDefault(root.getChildText("ArchiveObjectCount"));
2825+
Long coldArchiveStorage = parseLongWithDefault(root.getChildText("ColdArchiveStorage"));
2826+
Long coldArchiveRealStorage = parseLongWithDefault(root.getChildText("ColdArchiveRealStorage"));
2827+
Long coldArchiveObjectCount = parseLongWithDefault(root.getChildText("ColdArchiveObjectCount"));
28202828
BucketStat bucketStat = new BucketStat()
28212829
.withStorageSize(storage)
28222830
.withObjectCount(objectCount)

src/test/java/com/aliyun/oss/common/parser/ResponseParsersTest.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,8 +3126,13 @@ public void testparseGetBucketStat() {
31263126
} catch (Exception e) {
31273127
Assert.assertTrue(false);
31283128
}
3129+
}
3130+
3131+
@Test
3132+
public void testParseGetBucketStat() {
3133+
InputStream instream = null;
31293134

3130-
respBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
3135+
String respBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
31313136
"<BucketStat>\n" +
31323137
" <Storage>1600</Storage>\n" +
31333138
" <ObjectCount>230</ObjectCount>\n" +
@@ -3166,8 +3171,42 @@ public void testparseGetBucketStat() {
31663171
Assert.assertEquals(Long.valueOf(2359296), result.getColdArchiveStorage());
31673172
Assert.assertEquals(Long.valueOf(360), result.getColdArchiveRealStorage());
31683173
Assert.assertEquals(Long.valueOf(36), result.getColdArchiveObjectCount());
3169-
} catch (ResponseParseException e) {
3170-
Assert.assertTrue(true);
3174+
} catch (Exception e) {
3175+
Assert.assertTrue(false);
3176+
}
3177+
}
3178+
3179+
@Test
3180+
public void testParseGetBucketStatReturnEmpty() {
3181+
InputStream instream = null;
3182+
3183+
String respBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
3184+
"<BucketStat>\n" +
3185+
" <Storage>1600</Storage>\n" +
3186+
" <ObjectCount>230</ObjectCount>\n" +
3187+
" <MultipartUploadCount>40</MultipartUploadCount>\n" +
3188+
" <LiveChannelCount></LiveChannelCount>\n" +
3189+
"</BucketStat>";
3190+
3191+
try {
3192+
instream = new ByteArrayInputStream(respBody.getBytes("utf-8"));
3193+
BucketStat result = ResponseParsers.parseGetBucketStat(instream);
3194+
Assert.assertEquals(Long.valueOf(1600), result.getStorageSize());
3195+
Assert.assertEquals(Long.valueOf(230), result.getObjectCount());
3196+
Assert.assertEquals(Long.valueOf(40), result.getMultipartUploadCount());
3197+
Assert.assertEquals(Long.valueOf(0), result.getLiveChannelCount());
3198+
Assert.assertEquals(Long.valueOf(0), result.getLastModifiedTime());
3199+
Assert.assertEquals(Long.valueOf(0), result.getStandardStorage());
3200+
Assert.assertEquals(Long.valueOf(0), result.getStandardObjectCount());
3201+
Assert.assertEquals(Long.valueOf(0), result.getInfrequentAccessStorage());
3202+
Assert.assertEquals(Long.valueOf(0), result.getInfrequentAccessRealStorage());
3203+
Assert.assertEquals(Long.valueOf(0), result.getInfrequentAccessObjectCount());
3204+
Assert.assertEquals(Long.valueOf(0), result.getArchiveStorage());
3205+
Assert.assertEquals(Long.valueOf(0), result.getArchiveRealStorage());
3206+
Assert.assertEquals(Long.valueOf(0), result.getArchiveObjectCount());
3207+
Assert.assertEquals(Long.valueOf(0), result.getColdArchiveStorage());
3208+
Assert.assertEquals(Long.valueOf(0), result.getColdArchiveRealStorage());
3209+
Assert.assertEquals(Long.valueOf(0), result.getColdArchiveObjectCount());
31713210
} catch (Exception e) {
31723211
Assert.assertTrue(false);
31733212
}

src/test/java/com/aliyun/oss/integrationtests/BucketStatTest.java

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,6 @@ public void testGetBucketStat() {
6767
Assert.assertTrue(stat.getStorageSize() >= 1024 * 300);
6868
Assert.assertTrue(stat.getObjectCount() >= 1);
6969
Assert.assertTrue(stat.getMultipartUploadCount() >= 1);
70-
Assert.assertTrue(stat.getStandardStorage() >= 1024 * 300);
71-
Assert.assertTrue(stat.getStandardObjectCount() >= 1);
72-
Assert.assertTrue(stat.getLiveChannelCount() >= 0);
73-
Assert.assertTrue(stat.getLastModifiedTime() >= 0);
74-
Assert.assertTrue(stat.getInfrequentAccessStorage() >= 0);
75-
Assert.assertTrue(stat.getInfrequentAccessRealStorage() >= 0);
76-
Assert.assertTrue(stat.getInfrequentAccessObjectCount() >= 0);
77-
Assert.assertTrue(stat.getArchiveStorage() >= 0);
78-
Assert.assertTrue(stat.getArchiveRealStorage() >= 0);
79-
Assert.assertTrue(stat.getArchiveObjectCount() >= 0);
80-
Assert.assertTrue(stat.getColdArchiveStorage() >= 0);
81-
Assert.assertTrue(stat.getColdArchiveRealStorage() >= 0);
82-
Assert.assertTrue(stat.getColdArchiveObjectCount() >= 0);
8370
Assert.assertEquals(stat.getRequestId().length(), REQUEST_ID_LEN);
8471
} catch (Exception e) {
8572
e.printStackTrace();
@@ -109,5 +96,64 @@ public void testUnormalGetBucketStat() {
10996
}
11097

11198
}
112-
99+
100+
@Test
101+
public void testGetBucketStatForStorageInfo() {
102+
String key = "obj-upload-file-storage-stat.txt";
103+
String uploadId = null;
104+
105+
try {
106+
107+
File file = createSampleFile(key, 1024 * 500);
108+
109+
// upload a file
110+
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
111+
uploadFileRequest.setUploadFile(file.getAbsolutePath());
112+
uploadFileRequest.setTaskNum(10);
113+
114+
UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest);
115+
Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName);
116+
Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key);
117+
118+
// init upload
119+
InitiateMultipartUploadRequest initiateMultipartUploadRequest =
120+
new InitiateMultipartUploadRequest(bucketName, key);
121+
InitiateMultipartUploadResult initiateMultipartUploadResult =
122+
ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
123+
Assert.assertEquals(initiateMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
124+
uploadId = initiateMultipartUploadResult.getUploadId();
125+
126+
BucketStat stat = ossClient.getBucketStat(bucketName);
127+
System.out.println(stat.getStorageSize() + "," + stat.getObjectCount() + "," + stat.getMultipartUploadCount());
128+
Assert.assertTrue(stat.getStorageSize() >= 1024 * 300);
129+
Assert.assertTrue(stat.getObjectCount() >= 1);
130+
Assert.assertTrue(stat.getMultipartUploadCount() >= 1);
131+
Assert.assertTrue(stat.getStandardStorage() >= 1024 * 300);
132+
Assert.assertTrue(stat.getStandardObjectCount() >= 1);
133+
Assert.assertTrue(stat.getLiveChannelCount() >= 0);
134+
Assert.assertTrue(stat.getLastModifiedTime() >= 0);
135+
Assert.assertTrue(stat.getInfrequentAccessStorage() >= 0);
136+
Assert.assertTrue(stat.getInfrequentAccessRealStorage() >= 0);
137+
Assert.assertTrue(stat.getInfrequentAccessObjectCount() >= 0);
138+
Assert.assertTrue(stat.getArchiveStorage() >= 0);
139+
Assert.assertTrue(stat.getArchiveRealStorage() >= 0);
140+
Assert.assertTrue(stat.getArchiveObjectCount() >= 0);
141+
Assert.assertTrue(stat.getColdArchiveStorage() >= 0);
142+
Assert.assertTrue(stat.getColdArchiveRealStorage() >= 0);
143+
Assert.assertTrue(stat.getColdArchiveObjectCount() >= 0);
144+
Assert.assertEquals(stat.getRequestId().length(), REQUEST_ID_LEN);
145+
} catch (Exception e) {
146+
e.printStackTrace();
147+
Assert.fail(e.getMessage());
148+
} catch (Throwable e) {
149+
e.printStackTrace();
150+
Assert.fail(e.getMessage());
151+
} finally {
152+
if (uploadId != null) {
153+
AbortMultipartUploadRequest AbortMultipartUploadRequest =
154+
new AbortMultipartUploadRequest(bucketName, key, uploadId);
155+
ossClient.abortMultipartUpload(AbortMultipartUploadRequest);
156+
}
157+
}
158+
}
113159
}

0 commit comments

Comments
 (0)