accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ctubb...@apache.org
Subject [1/4] git commit: ACCUMULO-748 applying [~treardon]'s patch, added unit test
Date Thu, 14 Nov 2013 01:19:49 GMT
Updated Branches:
  refs/heads/master b71bc4d13 -> 209cc076b


ACCUMULO-748 applying [~treardon]'s patch, added unit test


Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo
Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/9c16ef01
Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/9c16ef01
Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/9c16ef01

Branch: refs/heads/master
Commit: 9c16ef0153b9f47cfcf3b54b19563b713af2cad3
Parents: fce4ee7
Author: Eric Newton <eric.newton@gmail.com>
Authored: Wed Nov 13 14:20:03 2013 -0500
Committer: Eric Newton <eric.newton@gmail.com>
Committed: Wed Nov 13 14:20:03 2013 -0500

----------------------------------------------------------------------
 .../core/util/shell/commands/TablesCommand.java | 34 ++++++++++++++++----
 .../org/apache/accumulo/test/ShellServerIT.java | 11 +++++++
 2 files changed, 38 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/accumulo/blob/9c16ef01/core/src/main/java/org/apache/accumulo/core/util/shell/commands/TablesCommand.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/accumulo/core/util/shell/commands/TablesCommand.java
b/core/src/main/java/org/apache/accumulo/core/util/shell/commands/TablesCommand.java
index 19b49e9..e1fb4b3 100644
--- a/core/src/main/java/org/apache/accumulo/core/util/shell/commands/TablesCommand.java
+++ b/core/src/main/java/org/apache/accumulo/core/util/shell/commands/TablesCommand.java
@@ -29,37 +29,55 @@ import org.apache.accumulo.core.util.shell.Shell.Command;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
+import org.apache.commons.collections.MapUtils;
 import org.apache.commons.collections.iterators.AbstractIteratorDecorator;
 
 public class TablesCommand extends Command {
+  private static final String NAME_AND_ID_FORMAT = "%-15s => %10s%n";
+  
   private Option tableIdOption;
+  private Option sortByTableIdOption;
   private Option disablePaginationOpt;
   
   @SuppressWarnings("unchecked")
   @Override
   public int execute(final String fullCommand, final CommandLine cl, final Shell shellState)
throws AccumuloException, AccumuloSecurityException, IOException {
+    Iterator<String> it = null;
     if (cl.hasOption(tableIdOption.getOpt())) {
-      final Map<String,String> tableIds = new TreeMap<String,String>(shellState.getConnector().tableOperations().tableIdMap());
-      shellState.printLines(new TableIdIterator(tableIds.entrySet().iterator()), !cl.hasOption(disablePaginationOpt.getOpt()));
+      it = new TableIdIterator(shellState.getConnector().tableOperations().tableIdMap(),
cl.hasOption(sortByTableIdOption.getOpt()));
     } else {
-      shellState.printLines(shellState.getConnector().tableOperations().list().iterator(),
!cl.hasOption(disablePaginationOpt.getOpt()));
+      it = shellState.getConnector().tableOperations().list().iterator();
     }
+    
+    shellState.printLines(it, !cl.hasOption(disablePaginationOpt.getOpt()));
     return 0;
   }
   
   /**
-   * Decorator that formats table id and name for display.
+   * Decorator that formats table name and id for display.
    */
   private static final class TableIdIterator extends AbstractIteratorDecorator {
-    public TableIdIterator(Iterator<Entry<String,String>> iterator) {
-      super(iterator);
+    private final boolean sortByTableId;
+    
+    /**
+     * @param tableIdMap tableName -> tableId
+     * @param sortByTableId
+     */
+    @SuppressWarnings("unchecked")
+    public TableIdIterator(Map<String,String> tableIdMap, boolean sortByTableId) {
+      super(new TreeMap<String,String>((sortByTableId ? MapUtils.invertMap(tableIdMap)
: tableIdMap)).entrySet().iterator());
+      this.sortByTableId = sortByTableId;
     }
     
     @SuppressWarnings("rawtypes")
     @Override
     public Object next() {
       Entry entry = (Entry) super.next();
-      return String.format("%-15s => %10s%n", entry.getKey(), entry.getValue());
+      if (sortByTableId) {
+        return String.format(NAME_AND_ID_FORMAT, entry.getValue(), entry.getKey());
+      } else {
+        return String.format(NAME_AND_ID_FORMAT, entry.getKey(), entry.getValue());
+      }
     }
   }
   
@@ -73,6 +91,8 @@ public class TablesCommand extends Command {
     final Options o = new Options();
     tableIdOption = new Option("l", "list-ids", false, "display internal table ids along
with the table name");
     o.addOption(tableIdOption);
+    sortByTableIdOption = new Option("s", "sort-ids", false, "with -l: sort output by table
ids");
+    o.addOption(sortByTableIdOption);
     disablePaginationOpt = new Option("np", "no-pagination", false, "disable pagination of
output");
     o.addOption(disablePaginationOpt);
     return o;

http://git-wip-us.apache.org/repos/asf/accumulo/blob/9c16ef01/test/src/test/java/org/apache/accumulo/test/ShellServerIT.java
----------------------------------------------------------------------
diff --git a/test/src/test/java/org/apache/accumulo/test/ShellServerIT.java b/test/src/test/java/org/apache/accumulo/test/ShellServerIT.java
index 31867f3..dbf5f4c 100644
--- a/test/src/test/java/org/apache/accumulo/test/ShellServerIT.java
+++ b/test/src/test/java/org/apache/accumulo/test/ShellServerIT.java
@@ -718,6 +718,17 @@ public class ShellServerIT extends SimpleMacIT {
     exec("scan -t xyzzy", true, "value", true);
     exec("deletetable -f xyzzy", true);
   }
+  
+  @Test(timeout = 30 * 1000)
+  public void tables() throws Exception {
+    exec("createtable zzzz");
+    exec("createtable aaaa");
+    exec("notable");
+    String lst = exec("tables -l");
+    assertTrue(lst.indexOf("aaaa") < lst.indexOf("zzzz"));
+    lst = exec("tables -l -s");
+    assertTrue(lst.indexOf("zzzz") < lst.indexOf("aaaa"));
+  }
 
   @Test(timeout = 30 * 1000)
   public void systempermission() throws Exception {


Mime
View raw message