Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
import org.apache.commons.io.FileUtils;
import org.apache.hadoop.hdds.cli.HddsVersionProvider;
import org.apache.hadoop.hdds.server.JsonUtils;
import org.apache.hadoop.ozone.shell.ListOptions;
import org.apache.hadoop.ozone.shell.ListLimitOptions;
import org.apache.hadoop.ozone.shell.PrefixFilterOption;
import picocli.CommandLine;

/**
Expand Down Expand Up @@ -67,7 +68,10 @@ public class DiskUsageSubCommand implements Callable {
private boolean noHeader;

@CommandLine.Mixin
private ListOptions listOptions;
private ListLimitOptions listOptions;

@CommandLine.Mixin
private PrefixFilterOption prefixFilter;

private static final String ENDPOINT = "/api/v1/namespace/du";

Expand Down Expand Up @@ -147,7 +151,7 @@ public Void call() throws Exception {
printWithUnderline("DU", true);
printDUHeader();
int limit = listOptions.getLimit();
String seekStr = listOptions.getPrefix();
String seekStr = prefixFilter.getPrefix();
if (seekStr == null) {
seekStr = "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,29 @@

import picocli.CommandLine;

/**
* Common options for 'list' commands.
*/
public class ListOptions {

@CommandLine.ArgGroup(exclusive = true)
private ExclusiveLimit exclusiveLimit = new ExclusiveLimit();
/** Options for limiting the size of lists. Use with {@link CommandLine.Mixin}. */
public class ListLimitOptions {

@CommandLine.Option(names = {"--start", "-s"},
description = "The item to start the listing from.\n" +
"This will be excluded from the result.")
private String startItem;
@CommandLine.ArgGroup
private ExclusiveGroup group = new ExclusiveGroup();

@CommandLine.Option(names = {"--prefix", "-p"},
description = "Prefix to filter the items")
private String prefix;
public boolean isAll() {
return group.all;
}

public int getLimit() {
if (exclusiveLimit.all) {
if (group.all) {
return Integer.MAX_VALUE;
}
if (exclusiveLimit.limit < 1) {
if (group.limit < 1) {
throw new IllegalArgumentException(
"List length should be a positive number");
}

return exclusiveLimit.limit;
}

public boolean isAll() {
return exclusiveLimit.all;
}

public String getStartItem() {
return startItem;
}

public String getPrefix() {
return prefix;
return group.limit;
}

static class ExclusiveLimit {
static class ExclusiveGroup {
@CommandLine.Option(names = {"--length", "-l"},
description = "Maximum number of items to list",
defaultValue = "100",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.ozone.shell;

import picocli.CommandLine;

/** Options to provide pagination of lists. Use with {@link CommandLine.Mixin}. */
public class ListPaginationOptions {

@CommandLine.Mixin
private ListLimitOptions limitOptions;

@CommandLine.Option(names = {"--start", "-s"},
description = "The item to start the listing from.\n" +
"This will be excluded from the result.")
private String startItem;

public int getLimit() {
return limitOptions.getLimit();
}

public boolean isAll() {
return limitOptions.isAll();
}

public String getStartItem() {
return startItem;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.ozone.shell;

import picocli.CommandLine;

/** Option for filtering lists by prefix. Use with {@link CommandLine.Mixin}. */
public class PrefixFilterOption {

@CommandLine.Option(names = {"--prefix", "-p"},
description = "Prefix to filter the items")
private String prefix;

public String getPrefix() {
return prefix;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import org.apache.hadoop.ozone.client.OzoneBucket;
import org.apache.hadoop.ozone.client.OzoneClient;
import org.apache.hadoop.ozone.client.OzoneVolume;
import org.apache.hadoop.ozone.shell.ListOptions;
import org.apache.hadoop.ozone.shell.ListPaginationOptions;
import org.apache.hadoop.ozone.shell.OzoneAddress;
import org.apache.hadoop.ozone.shell.PrefixFilterOption;
import org.apache.hadoop.ozone.shell.volume.VolumeHandler;
import picocli.CommandLine;
import picocli.CommandLine.Command;
Expand All @@ -40,7 +41,10 @@
public class ListBucketHandler extends VolumeHandler {

@CommandLine.Mixin
private ListOptions listOptions;
private ListPaginationOptions listOptions;

@CommandLine.Mixin
private PrefixFilterOption prefixFilter;

@CommandLine.Option(names = {"--has-snapshot"},
description = "Only show buckets that have at least one active snapshot.")
Expand All @@ -54,7 +58,7 @@ protected void execute(OzoneClient client, OzoneAddress address)
ObjectStore objectStore = client.getObjectStore();
OzoneVolume vol = objectStore.getVolume(volumeName);
Iterator<? extends OzoneBucket> bucketIterator =
vol.listBuckets(listOptions.getPrefix(),
vol.listBuckets(prefixFilter.getPrefix(),
listOptions.getStartItem(), filterByHasSnapshot);
List<Object> bucketList = new ArrayList<>();
int counter = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
import org.apache.hadoop.ozone.client.OzoneClient;
import org.apache.hadoop.ozone.client.OzoneKey;
import org.apache.hadoop.ozone.client.OzoneVolume;
import org.apache.hadoop.ozone.shell.ListOptions;
import org.apache.hadoop.ozone.shell.ListPaginationOptions;
import org.apache.hadoop.ozone.shell.OzoneAddress;
import org.apache.hadoop.ozone.shell.PrefixFilterOption;
import org.apache.hadoop.ozone.shell.common.VolumeBucketHandler;
import picocli.CommandLine;
import picocli.CommandLine.Command;
Expand All @@ -39,7 +40,10 @@
public class ListKeyHandler extends VolumeBucketHandler {

@CommandLine.Mixin
private ListOptions listOptions;
private ListPaginationOptions listOptions;

@CommandLine.Mixin
private PrefixFilterOption prefixFilter;

@Override
protected void execute(OzoneClient client, OzoneAddress address)
Expand All @@ -62,13 +66,13 @@ private void listKeysInsideBucket(OzoneClient client, OzoneAddress address)
if (!Strings.isNullOrEmpty(snapshotNameWithIndicator)) {
keyPrefix += snapshotNameWithIndicator;

if (!Strings.isNullOrEmpty(listOptions.getPrefix())) {
if (!Strings.isNullOrEmpty(prefixFilter.getPrefix())) {
keyPrefix += "/";
}
}

if (!Strings.isNullOrEmpty(listOptions.getPrefix())) {
keyPrefix += listOptions.getPrefix();
if (!Strings.isNullOrEmpty(prefixFilter.getPrefix())) {
keyPrefix += prefixFilter.getPrefix();
}

OzoneVolume vol = client.getObjectStore().getVolume(volumeName);
Expand Down Expand Up @@ -111,8 +115,8 @@ private void listKeysInsideVolume(OzoneClient client, OzoneAddress address)
vol.listBuckets(null);
int maxKeyLimit = listOptions.getLimit();
String keyPrefix = "";
if (!Strings.isNullOrEmpty(listOptions.getPrefix())) {
keyPrefix += listOptions.getPrefix();
if (!Strings.isNullOrEmpty(prefixFilter.getPrefix())) {
keyPrefix += prefixFilter.getPrefix();
}

int totalKeys = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.hadoop.ozone.client.OzoneClient;
import org.apache.hadoop.ozone.client.OzoneSnapshotDiff;
import org.apache.hadoop.ozone.shell.Handler;
import org.apache.hadoop.ozone.shell.ListOptions;
import org.apache.hadoop.ozone.shell.ListPaginationOptions;
import org.apache.hadoop.ozone.shell.OzoneAddress;
import org.apache.hadoop.ozone.shell.bucket.BucketUri;
import picocli.CommandLine;
Expand Down Expand Up @@ -50,7 +50,7 @@ public class ListSnapshotDiffHandler extends Handler {
private boolean listAllStatus;

@CommandLine.Mixin
private ListOptions listOptions;
private ListPaginationOptions listOptions;

@Override
protected OzoneAddress getAddress() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import org.apache.hadoop.ozone.client.OzoneClient;
import org.apache.hadoop.ozone.client.OzoneSnapshot;
import org.apache.hadoop.ozone.shell.Handler;
import org.apache.hadoop.ozone.shell.ListOptions;
import org.apache.hadoop.ozone.shell.ListPaginationOptions;
import org.apache.hadoop.ozone.shell.OzoneAddress;
import org.apache.hadoop.ozone.shell.PrefixFilterOption;
import org.apache.hadoop.ozone.shell.bucket.BucketUri;
import picocli.CommandLine;

Expand All @@ -40,7 +41,10 @@ public class ListSnapshotHandler extends Handler {
private BucketUri snapshotPath;

@CommandLine.Mixin
private ListOptions listOptions;
private ListPaginationOptions listOptions;

@CommandLine.Mixin
private PrefixFilterOption prefixFilter;

@Override
protected OzoneAddress getAddress() {
Expand All @@ -54,7 +58,7 @@ protected void execute(OzoneClient client, OzoneAddress address)
String bucketName = snapshotPath.getValue().getBucketName();

Iterator<OzoneSnapshot> snapshotInfos = client.getObjectStore()
.listSnapshot(volumeName, bucketName, listOptions.getPrefix(),
.listSnapshot(volumeName, bucketName, prefixFilter.getPrefix(),
listOptions.getStartItem());
int counter = printAsJsonArray(snapshotInfos, listOptions.getLimit());
if (isVerbose()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import org.apache.hadoop.ozone.client.OzoneClientException;
import org.apache.hadoop.ozone.client.OzoneVolume;
import org.apache.hadoop.ozone.shell.Handler;
import org.apache.hadoop.ozone.shell.ListOptions;
import org.apache.hadoop.ozone.shell.ListPaginationOptions;
import org.apache.hadoop.ozone.shell.OzoneAddress;
import org.apache.hadoop.ozone.shell.PrefixFilterOption;
import org.apache.hadoop.ozone.shell.Shell;
import org.apache.hadoop.security.UserGroupInformation;
import picocli.CommandLine;
Expand All @@ -46,7 +47,10 @@ public class ListVolumeHandler extends Handler {
private String uri;

@CommandLine.Mixin
private ListOptions listOptions;
private ListPaginationOptions listOptions;

@CommandLine.Mixin
private PrefixFilterOption prefixFilter;

@Option(names = {"--user", "-u"},
description = "List accessible volumes of the user. This will be ignored"
Expand All @@ -71,10 +75,10 @@ protected void execute(OzoneClient client, OzoneAddress address)
Iterator<? extends OzoneVolume> volumeIterator;
if (userName != null && !listOptions.isAll()) {
volumeIterator = client.getObjectStore().listVolumesByUser(userName,
listOptions.getPrefix(), listOptions.getStartItem());
prefixFilter.getPrefix(), listOptions.getStartItem());
} else {
volumeIterator = client.getObjectStore().listVolumes(
listOptions.getPrefix(), listOptions.getStartItem());
prefixFilter.getPrefix(), listOptions.getStartItem());
}

int counter = printAsJsonArray(volumeIterator, listOptions.getLimit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.hadoop.hdds.cli.AbstractSubcommand;
import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
import org.apache.hadoop.ozone.debug.logs.container.utils.ContainerDatanodeDatabase;
import org.apache.hadoop.ozone.shell.ListOptions;
import org.apache.hadoop.ozone.shell.ListLimitOptions;
import picocli.CommandLine;


Expand All @@ -42,7 +42,7 @@ public class ListContainers extends AbstractSubcommand implements Callable<Void>
private HddsProtos.LifeCycleState state;

@CommandLine.Mixin
private ListOptions listOptions;
private ListLimitOptions listOptions;

@CommandLine.ParentCommand
private ContainerLogController parent;
Expand Down