Skip to content

Commit f9ec90c

Browse files
HDDS-8887. Support --all in ListOptions. (#4931)
1 parent 035c9ea commit f9ec90c

File tree

3 files changed

+68
-12
lines changed

3 files changed

+68
-12
lines changed

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/shell/TestOzoneShellHA.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,49 @@ public void testClientBucketLayoutValidation() {
14041404
}
14051405
}
14061406

1407+
@Test
1408+
public void testListAllKeys()
1409+
throws Exception {
1410+
String volumeName = "vollst";
1411+
// Create volume vollst
1412+
String[] args = new String[] {
1413+
"volume", "create", "o3://" + omServiceId +
1414+
OZONE_URI_DELIMITER + volumeName};
1415+
execute(ozoneShell, args);
1416+
out.reset();
1417+
1418+
// Create bucket bucket1
1419+
args = new String[]{"bucket", "create", "o3://" + omServiceId +
1420+
OZONE_URI_DELIMITER + volumeName + "/bucket1"};
1421+
execute(ozoneShell, args);
1422+
out.reset();
1423+
1424+
// Insert 120 keys into bucket1
1425+
String keyName = OZONE_URI_DELIMITER + volumeName + "/bucket1" +
1426+
OZONE_URI_DELIMITER + "key";
1427+
for (int i = 0; i < 120; i++) {
1428+
args = new String[]{
1429+
"key", "put", "o3://" + omServiceId + keyName + i,
1430+
testFile.getPath()};
1431+
execute(ozoneShell, args);
1432+
}
1433+
1434+
out.reset();
1435+
// Number of keys should return less than 120(100 by default)
1436+
args =
1437+
new String[]{"key", "list", volumeName};
1438+
execute(ozoneShell, args);
1439+
Assert.assertTrue(getNumOfKeys() < 120);
1440+
1441+
out.reset();
1442+
// Use --all option to get all the keys
1443+
args =
1444+
new String[]{"key", "list", "--all", volumeName};
1445+
execute(ozoneShell, args);
1446+
// Number of keys returned should be 120
1447+
Assert.assertEquals(120, getNumOfKeys());
1448+
}
1449+
14071450
@Test
14081451
public void testVolumeListKeys()
14091452
throws Exception {

hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/ListOptions.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,21 @@
2424
*/
2525
public class ListOptions {
2626

27-
@CommandLine.Option(names = {"--length", "-l"},
28-
description = "Maximum number of items to list",
29-
defaultValue = "100",
30-
showDefaultValue = CommandLine.Help.Visibility.ALWAYS)
31-
private int limit;
27+
@CommandLine.ArgGroup(exclusive = true)
28+
private ExclusiveLimit exclusiveLimit = new ExclusiveLimit();
29+
30+
static class ExclusiveLimit {
31+
@CommandLine.Option(names = {"--length", "-l"},
32+
description = "Maximum number of items to list",
33+
defaultValue = "100",
34+
showDefaultValue = CommandLine.Help.Visibility.ALWAYS)
35+
private int limit;
36+
37+
@CommandLine.Option(names = {"--all", "-a"},
38+
description = "List all results",
39+
defaultValue = "false")
40+
private boolean all;
41+
}
3242

3343
@CommandLine.Option(names = {"--start", "-s"},
3444
description = "The item to start the listing from.\n" +
@@ -40,12 +50,19 @@ public class ListOptions {
4050
private String prefix;
4151

4252
public int getLimit() {
43-
if (limit < 1) {
53+
if (exclusiveLimit.all) {
54+
return Integer.MAX_VALUE;
55+
}
56+
if (exclusiveLimit.limit < 1) {
4457
throw new IllegalArgumentException(
4558
"List length should be a positive number");
4659
}
4760

48-
return limit;
61+
return exclusiveLimit.limit;
62+
}
63+
64+
public boolean isAll() {
65+
return exclusiveLimit.all;
4966
}
5067

5168
public String getStartItem() {

hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/volume/ListVolumeHandler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ public class ListVolumeHandler extends Handler {
5656
+ " if list all volumes option is specified.")
5757
private String userName;
5858

59-
@Option(names = {"--all", "-a"},
60-
description = "List all volumes.")
61-
private boolean listAllVolumes;
62-
6359
@Override
6460
protected OzoneAddress getAddress() throws OzoneClientException {
6561
OzoneAddress address = new OzoneAddress(uri);
@@ -76,7 +72,7 @@ protected void execute(OzoneClient client, OzoneAddress address)
7672
}
7773

7874
Iterator<? extends OzoneVolume> volumeIterator;
79-
if (userName != null && !listAllVolumes) {
75+
if (userName != null && !listOptions.isAll()) {
8076
volumeIterator = client.getObjectStore().listVolumesByUser(userName,
8177
listOptions.getPrefix(), listOptions.getStartItem());
8278
} else {

0 commit comments

Comments
 (0)