accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e..@apache.org
Subject svn commit: r1478068 - in /accumulo/branches/1.5: core/src/main/java/org/apache/accumulo/core/util/ core/src/main/java/org/apache/accumulo/core/util/shell/commands/ server/src/main/java/org/apache/accumulo/server/util/ test/src/test/java/org/apache/acc...
Date Wed, 01 May 2013 16:07:55 GMT
Author: ecn
Date: Wed May  1 16:07:55 2013
New Revision: 1478068

URL: http://svn.apache.org/r1478068
Log:
ACCUMULO-1334 add disk usage to the API

Modified:
    accumulo/branches/1.5/core/src/main/java/org/apache/accumulo/core/util/TableDiskUsage.java
    accumulo/branches/1.5/core/src/main/java/org/apache/accumulo/core/util/shell/commands/DUCommand.java
    accumulo/branches/1.5/server/src/main/java/org/apache/accumulo/server/util/TableDiskUsage.java
    accumulo/branches/1.5/test/src/test/java/org/apache/accumulo/test/ShellServerTest.java

Modified: accumulo/branches/1.5/core/src/main/java/org/apache/accumulo/core/util/TableDiskUsage.java
URL: http://svn.apache.org/viewvc/accumulo/branches/1.5/core/src/main/java/org/apache/accumulo/core/util/TableDiskUsage.java?rev=1478068&r1=1478067&r2=1478068&view=diff
==============================================================================
--- accumulo/branches/1.5/core/src/main/java/org/apache/accumulo/core/util/TableDiskUsage.java
(original)
+++ accumulo/branches/1.5/core/src/main/java/org/apache/accumulo/core/util/TableDiskUsage.java
Wed May  1 16:07:55 2013
@@ -116,18 +116,19 @@ public class TableDiskUsage {
     void print(String line);
   }
   
-  public static void printDiskUsage(AccumuloConfiguration acuConf, Collection<String>
tables, FileSystem fs, Connector conn) throws TableNotFoundException,
-  IOException {
+  public static void printDiskUsage(AccumuloConfiguration acuConf, Collection<String>
tables, FileSystem fs, Connector conn, boolean humanReadable)
+      throws TableNotFoundException, IOException {
     printDiskUsage(acuConf, tables, fs, conn, new Printer() {
       @Override
       public void print(String line) {
         System.out.println(line);
       }
-    });
+    }, humanReadable);
   }
-  public static void printDiskUsage(AccumuloConfiguration acuConf, Collection<String>
tables, FileSystem fs, Connector conn, Printer printer) throws TableNotFoundException,
-  IOException {
   
+  public static void printDiskUsage(AccumuloConfiguration acuConf, Collection<String>
tables, FileSystem fs, Connector conn, Printer printer,
+      boolean humanReadable) throws TableNotFoundException, IOException {
+    
     TableDiskUsage tdu = new TableDiskUsage();
     
     HashSet<String> tableIds = new HashSet<String>();
@@ -220,7 +221,7 @@ public class TableDiskUsage {
       
       usage.put(tableNames, entry.getValue());
     }
-
+    
     if(!emptyTableIds.isEmpty()) {
       TreeSet<String> emptyTables = new TreeSet<String>();
       for (String tableId : emptyTableIds) {
@@ -229,9 +230,20 @@ public class TableDiskUsage {
       usage.put(emptyTables, 0L);
     }
     
-    for (Entry<TreeSet<String>,Long> entry : usage.entrySet())
-      printer.print(String.format("%,24d %s", entry.getValue(), entry.getKey()));
-    
+    for (Entry<TreeSet<String>,Long> entry : usage.entrySet()) {
+      String valueFormat = humanReadable ? "%s" : "%,24d";
+      Object value = humanReadable ? humanReadableBytes(entry.getValue()) : entry.getValue();
+      printer.print(String.format(valueFormat + " %s", value, entry.getKey()));
+    }
   }
   
+  static final String[] SUFFIXES = {"K", "M", "G", "T", "P", "E", "Z"};
+  
+  public static String humanReadableBytes(long bytes) {
+    if (bytes < 1024) return String.format("%4dB", bytes);
+    int exp = (int) (Math.log(bytes) / Math.log(1024));
+    String suffix = SUFFIXES[exp-1];
+    double val = bytes / Math.pow(1024, exp);
+    return String.format(val >= 1000 ? "%4.0f%s" : " %3.1f%s", val, suffix);
+  }
 }

Modified: accumulo/branches/1.5/core/src/main/java/org/apache/accumulo/core/util/shell/commands/DUCommand.java
URL: http://svn.apache.org/viewvc/accumulo/branches/1.5/core/src/main/java/org/apache/accumulo/core/util/shell/commands/DUCommand.java?rev=1478068&r1=1478067&r2=1478068&view=diff
==============================================================================
--- accumulo/branches/1.5/core/src/main/java/org/apache/accumulo/core/util/shell/commands/DUCommand.java
(original)
+++ accumulo/branches/1.5/core/src/main/java/org/apache/accumulo/core/util/shell/commands/DUCommand.java
Wed May  1 16:07:55 2013
@@ -36,11 +36,14 @@ import org.apache.hadoop.fs.FileSystem;
 
 public class DUCommand extends Command {
   
-  private Option optTablePattern;
+  private Option optTablePattern, optHumanReadble;
   
   public int execute(final String fullCommand, final CommandLine cl, final Shell shellState)
throws IOException, TableNotFoundException {
     
     final SortedSet<String> tablesToFlush = new TreeSet<String>(Arrays.asList(cl.getArgs()));
+
+    boolean prettyPrint = cl.hasOption(optHumanReadble.getOpt()) ? true : false;
+
     if (cl.hasOption(optTablePattern.getOpt())) {
       for (String table : shellState.getConnector().tableOperations().list()) {
         if (table.matches(cl.getOptionValue(optTablePattern.getOpt()))) {
@@ -63,7 +66,7 @@ public class DUCommand extends Command {
           }
         }
         
-      });
+      }, prettyPrint);
     } catch (Exception ex) {
       throw new RuntimeException(ex);
     }
@@ -82,7 +85,11 @@ public class DUCommand extends Command {
     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");
+
     o.addOption(optTablePattern);
+    o.addOption(optHumanReadble);
     
     return o;
   }

Modified: accumulo/branches/1.5/server/src/main/java/org/apache/accumulo/server/util/TableDiskUsage.java
URL: http://svn.apache.org/viewvc/accumulo/branches/1.5/server/src/main/java/org/apache/accumulo/server/util/TableDiskUsage.java?rev=1478068&r1=1478067&r2=1478068&view=diff
==============================================================================
--- accumulo/branches/1.5/server/src/main/java/org/apache/accumulo/server/util/TableDiskUsage.java
(original)
+++ accumulo/branches/1.5/server/src/main/java/org/apache/accumulo/server/util/TableDiskUsage.java
Wed May  1 16:07:55 2013
@@ -42,7 +42,7 @@ public class TableDiskUsage {
     Opts opts = new Opts();
     opts.parseArgs(TableDiskUsage.class.getName(), args);
     Connector conn = opts.getConnector();
-    org.apache.accumulo.core.util.TableDiskUsage.printDiskUsage(DefaultConfiguration.getInstance(),
opts.tables, fs, conn);
+    org.apache.accumulo.core.util.TableDiskUsage.printDiskUsage(DefaultConfiguration.getInstance(),
opts.tables, fs, conn, false);
   }
   
 }

Modified: accumulo/branches/1.5/test/src/test/java/org/apache/accumulo/test/ShellServerTest.java
URL: http://svn.apache.org/viewvc/accumulo/branches/1.5/test/src/test/java/org/apache/accumulo/test/ShellServerTest.java?rev=1478068&r1=1478067&r2=1478068&view=diff
==============================================================================
--- accumulo/branches/1.5/test/src/test/java/org/apache/accumulo/test/ShellServerTest.java
(original)
+++ accumulo/branches/1.5/test/src/test/java/org/apache/accumulo/test/ShellServerTest.java
Wed May  1 16:07:55 2013
@@ -111,7 +111,8 @@ public class ShellServerTest {
   
   static void assertGoodExit(String s, boolean stringPresent) {
     Shell.log.debug(output.get());
-    assertEquals(shell.getExitCode(), 0);
+    assertEquals(0, shell.getExitCode());
+
     if (s.length() > 0)
       assertEquals(s + " present in " + output.get() + " was not " + stringPresent, stringPresent,
output.get().contains(s));
   }
@@ -174,7 +175,6 @@ public class ShellServerTest {
     exec("deletetable -f t", true);
     exec("deletetable -f t2", true);
   }
-  
   private DistCp newDistCp() {
     try {
       @SuppressWarnings("unchecked")
@@ -239,6 +239,11 @@ public class ShellServerTest {
     make10();
     exec("flush -t t -w");
     exec("du t", true, " [t]", true);
+    output.clear();
+    shell.execCommand("du -h", false, false);
+    String o = output.get();
+    System.out.println("XX" + o + "XX");
+    assertTrue(o.matches(".*\\s26[4-6]B\\s\\[t\\]\\n"));  // for some reason, there's 1-2
bytes of fluctuation
     exec("deletetable -f t");
   }
   



Mime
View raw message