Merge branch '1.6' into 1.7 Conflicts: shell/src/main/java/org/apache/accumulo/shell/commands/DUCommand.java
Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/0bbc6759 Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/0bbc6759 Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/0bbc6759 Branch: refs/heads/master Commit: 0bbc67594b25954ed2cb3a2f04e45789a1c7691c Parents: 12366d4 2b168f8 Author: Josh Elser <els...@apache.org> Authored: Mon May 4 14:09:31 2015 -0400 Committer: Josh Elser <els...@apache.org> Committed: Mon May 4 14:09:31 2015 -0400 ---------------------------------------------------------------------- .../org/apache/accumulo/shell/commands/DUCommand.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/0bbc6759/shell/src/main/java/org/apache/accumulo/shell/commands/DUCommand.java ---------------------------------------------------------------------- diff --cc shell/src/main/java/org/apache/accumulo/shell/commands/DUCommand.java index 3e851d4,0000000..2adcc81 mode 100644,000000..100644 --- a/shell/src/main/java/org/apache/accumulo/shell/commands/DUCommand.java +++ b/shell/src/main/java/org/apache/accumulo/shell/commands/DUCommand.java @@@ -1,126 -1,0 +1,129 @@@ +/* + * 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.accumulo.shell.commands; + +import java.io.IOException; +import java.util.Arrays; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.apache.accumulo.core.client.Instance; +import org.apache.accumulo.core.client.NamespaceNotFoundException; +import org.apache.accumulo.core.client.TableNotFoundException; +import org.apache.accumulo.core.client.admin.DiskUsage; +import org.apache.accumulo.core.client.impl.Namespaces; +import org.apache.accumulo.core.util.NumUtil; +import org.apache.accumulo.shell.Shell; +import org.apache.accumulo.shell.Shell.Command; +import org.apache.accumulo.shell.ShellOptions; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; + +public class DUCommand extends Command { + + private Option optTablePattern, optHumanReadble, optNamespace; + + @Override + public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws IOException, TableNotFoundException, + NamespaceNotFoundException { + + final SortedSet<String> tables = new TreeSet<String>(Arrays.asList(cl.getArgs())); + + if (cl.hasOption(ShellOptions.tableOption)) { - String tableName = cl.getOptionValue(ShellOptions.tableOption); - if (!shellState.getConnector().tableOperations().exists(tableName)) { - throw new TableNotFoundException(tableName, tableName, "specified table that doesn't exist"); - } - tables.add(tableName); ++ tables.add(cl.getOptionValue(ShellOptions.tableOption)); + } + + if (cl.hasOption(optNamespace.getOpt())) { + Instance instance = shellState.getInstance(); + String namespaceId = Namespaces.getNamespaceId(instance, cl.getOptionValue(optNamespace.getOpt())); + tables.addAll(Namespaces.getTableNames(instance, namespaceId)); + } + + boolean prettyPrint = cl.hasOption(optHumanReadble.getOpt()) ? true : false; + + // Add any patterns + if (cl.hasOption(optTablePattern.getOpt())) { + for (String table : shellState.getConnector().tableOperations().list()) { + if (table.matches(cl.getOptionValue(optTablePattern.getOpt()))) { + tables.add(table); + } + } + } + + // If we didn't get any tables, and we have a table selected, add the current table + if (tables.isEmpty() && !shellState.getTableName().isEmpty()) { + tables.add(shellState.getTableName()); + } + ++ // sanity check...make sure the user-specified tables exist ++ for (String tableName : tables) { ++ if (!shellState.getConnector().tableOperations().exists(tableName)) { ++ throw new TableNotFoundException(tableName, tableName, "specified table that doesn't exist"); ++ } ++ } ++ + try { + String valueFormat = prettyPrint ? "%9s" : "%,24d"; + for (DiskUsage usage : shellState.getConnector().tableOperations().getDiskUsage(tables)) { + Object value = prettyPrint ? NumUtil.bigNumberForSize(usage.getUsage()) : usage.getUsage(); + shellState.getReader().println(String.format(valueFormat + " %s", value, usage.getTables())); + } + } catch (Exception ex) { + throw new RuntimeException(ex); + } + return 0; + } + + @Override + public String description() { + return "prints how much space, in bytes, is used by files referenced by a table. " + + "When multiple tables are specified it prints how much space, in bytes, is used by files shared between tables, if any."; + } + + @Override + public Options getOptions() { + final Options o = new Options(); + + optTablePattern = new Option("p", "pattern", true, "regex pattern of table names"); + optTablePattern.setArgName("pattern"); + + optHumanReadble = new Option("h", "human-readable", false, "format large sizes to human readable units"); + optHumanReadble.setArgName("human readable output"); + + optNamespace = new Option(ShellOptions.namespaceOption, "namespace", true, "name of a namespace"); + optNamespace.setArgName("namespace"); + + o.addOption(OptUtil.tableOpt("table to examine")); + + o.addOption(optTablePattern); + o.addOption(optHumanReadble); + o.addOption(optNamespace); + + return o; + } + + @Override + public String usage() { + return getName() + " <table>{ <table>}"; + } + + @Override + public int numArgs() { + return Shell.NO_FIXED_ARG_LENGTH_CHECK; + } +}