accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e..@apache.org
Subject svn commit: r1478484 - in /accumulo/trunk: ./ assemble/ core/ core/src/main/java/org/apache/accumulo/core/util/ core/src/test/java/org/apache/accumulo/core/util/ core/src/test/java/org/apache/accumulo/core/util/shell/ examples/ fate/src/main/java/org/a...
Date Thu, 02 May 2013 18:51:15 GMT
Author: ecn
Date: Thu May  2 18:51:13 2013
New Revision: 1478484

URL: http://svn.apache.org/r1478484
Log:
ACCUMULO-1334 patching in Corey Nolet's updates to unify large number formatting for humans

Added:
    accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/NumUtil.java   (with props)
    accumulo/trunk/core/src/test/java/org/apache/accumulo/core/util/NumUtilTest.java   (with
props)
Removed:
    accumulo/trunk/core/src/test/java/org/apache/accumulo/core/util/shell/TableDiskUsageTest.java
Modified:
    accumulo/trunk/   (props changed)
    accumulo/trunk/assemble/   (props changed)
    accumulo/trunk/core/   (props changed)
    accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/TableDiskUsage.java
    accumulo/trunk/examples/   (props changed)
    accumulo/trunk/fate/src/main/java/org/apache/accumulo/fate/ZooStore.java   (props changed)
    accumulo/trunk/fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooSession.java 
 (props changed)
    accumulo/trunk/pom.xml   (props changed)
    accumulo/trunk/server/   (props changed)
    accumulo/trunk/server/src/main/java/org/apache/accumulo/server/monitor/servlets/DefaultServlet.java
    accumulo/trunk/server/src/main/java/org/apache/accumulo/server/monitor/util/celltypes/NumberType.java
    accumulo/trunk/src/   (props changed)

Propchange: accumulo/trunk/
------------------------------------------------------------------------------
  Merged /accumulo/branches/1.5:r1478483

Propchange: accumulo/trunk/assemble/
------------------------------------------------------------------------------
  Merged /accumulo/branches/1.5/assemble:r1478483

Propchange: accumulo/trunk/core/
------------------------------------------------------------------------------
  Merged /accumulo/branches/1.5/core:r1478483

Added: accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/NumUtil.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/NumUtil.java?rev=1478484&view=auto
==============================================================================
--- accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/NumUtil.java (added)
+++ accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/NumUtil.java Thu May 
2 18:51:13 2013
@@ -0,0 +1,54 @@
+/*
+ * 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.core.util;
+
+import java.text.DecimalFormat;
+
+public class NumUtil {
+
+  private static final String QUANTITY_SUFFIX[] = {"", "K", "M", "B", "T", "e15", "e18",
"e21"};
+  private static final String SIZE_SUFFIX[]     = {"", "K", "M", "G", "T", "P", "E", "Z"};
+
+  private static DecimalFormat df = new DecimalFormat("#,###,##0");
+  private static DecimalFormat df_mantissa = new DecimalFormat("#,###,##0.00");
+
+  public static String bigNumberForSize(long big) {
+    return bigNumber(big, SIZE_SUFFIX, 1024);
+  }
+
+  public static String bigNumberForQuantity(long big) {
+    return bigNumber(big, QUANTITY_SUFFIX, 1000);
+  }
+
+  public static String bigNumberForQuantity(double big) {
+    return bigNumber(big, QUANTITY_SUFFIX, 1000);
+  }
+
+  private static String bigNumber(long big, String[] SUFFIXES, long base) {
+    if (big < base) return df.format(big) + SUFFIXES[0];
+    int exp = (int) (Math.log(big) / Math.log(base));
+    double val = big / Math.pow(base, exp);
+    return df_mantissa.format(val) +  SUFFIXES[exp];
+  }
+
+  private static String bigNumber(double big, String[] SUFFIXES, long base) {
+    if (big < base) return df.format(big) + SUFFIXES[0];
+    int exp = (int) (Math.log(big) / Math.log(base));
+    double val = big / Math.pow(base, exp);
+    return df_mantissa.format(val) + SUFFIXES[exp];
+  }
+}

Propchange: accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/NumUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/TableDiskUsage.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/TableDiskUsage.java?rev=1478484&r1=1478483&r2=1478484&view=diff
==============================================================================
--- accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/TableDiskUsage.java (original)
+++ accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/TableDiskUsage.java Thu
May  2 18:51:13 2013
@@ -237,20 +237,11 @@ public class TableDiskUsage {
       throws TableNotFoundException, IOException {
     
     Map<TreeSet<String>,Long> usage = getDiskUsage(acuConf, tables, fs, conn,
humanReadable);
-    
+
+    String valueFormat = humanReadable ? "%9s" : "%,24d";
     for (Entry<TreeSet<String>,Long> entry : usage.entrySet()) {
-      String valueFormat = humanReadable ? "%s" : "%,24d";
-      Object value = humanReadable ? humanReadableBytes(entry.getValue()) : entry.getValue();
+      Object value = humanReadable ? NumUtil.bigNumberForSize(entry.getValue()) : entry.getValue();
       printer.print(String.format(valueFormat + " %s", value, entry.getKey()));
     }
   }
-  
-  public static String humanReadableBytes(long bytes) {
-    if (bytes < 1024) return String.format("%4dB", bytes);
-    int exp = (int) (Math.log(bytes) / Math.log(1024));
-    String pre = "KMGTPE".charAt(exp-1) + "";
-    double val = bytes / Math.pow(1024, exp);
-    return String.format(val >= 1000 ? "%4.0f%s" : " %3.1f%s", val, pre);
-  }
-
 }

Added: accumulo/trunk/core/src/test/java/org/apache/accumulo/core/util/NumUtilTest.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/test/java/org/apache/accumulo/core/util/NumUtilTest.java?rev=1478484&view=auto
==============================================================================
--- accumulo/trunk/core/src/test/java/org/apache/accumulo/core/util/NumUtilTest.java (added)
+++ accumulo/trunk/core/src/test/java/org/apache/accumulo/core/util/NumUtilTest.java Thu May
 2 18:51:13 2013
@@ -0,0 +1,53 @@
+/*
+ * 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.core.util;
+
+import static org.apache.accumulo.core.util.NumUtil.bigNumberForQuantity;
+import static org.apache.accumulo.core.util.NumUtil.bigNumberForSize;
+import static org.junit.Assert.assertEquals;
+
+import org.apache.accumulo.core.util.NumUtil;
+import org.junit.Test;
+
+public class NumUtilTest {
+
+  @Test
+  public void testBigNumberForSize() {
+    assertEquals("1,000", bigNumberForSize(1000));
+    assertEquals("1.00K", bigNumberForSize(1024));
+    assertEquals("1.50K", bigNumberForSize(1024 + (1024 / 2)));
+    assertEquals("1,024.00K", bigNumberForSize(1024 * 1024 - 1));
+    assertEquals("1.00M", bigNumberForSize(1024 * 1024));
+    assertEquals("1.50M", bigNumberForSize(1024 * 1024 + (1024 * 1024 / 2)));
+    assertEquals("1,024.00M", bigNumberForSize(1073741823));
+    assertEquals("1.00G", bigNumberForSize(1073741824));
+    assertEquals("1,024.00G", bigNumberForSize(1099511627775l));
+    assertEquals("1.00T", bigNumberForSize(1099511627776l));
+  }
+
+  @Test
+  public void testBigNumberForQuantity() {
+    assertEquals("999", bigNumberForQuantity(999));
+    assertEquals("1.00K", bigNumberForQuantity(1000));
+    assertEquals("1.02K", bigNumberForQuantity(1024));
+    assertEquals("5.00K", bigNumberForQuantity(5000));
+    assertEquals("50.00K", bigNumberForQuantity(50000));
+    assertEquals("5.00M", bigNumberForQuantity(5000000));
+    assertEquals("5.00B", bigNumberForQuantity(5000000000l));
+    assertEquals("5.00T", bigNumberForQuantity(5000000000000l));
+  }
+}

Propchange: accumulo/trunk/core/src/test/java/org/apache/accumulo/core/util/NumUtilTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: accumulo/trunk/examples/
------------------------------------------------------------------------------
  Merged /accumulo/branches/1.5/examples:r1478483

Propchange: accumulo/trunk/fate/src/main/java/org/apache/accumulo/fate/ZooStore.java
------------------------------------------------------------------------------
  Merged /accumulo/branches/1.5/fate/src/main/java/org/apache/accumulo/fate/ZooStore.java:r1478483

Propchange: accumulo/trunk/fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooSession.java
------------------------------------------------------------------------------
  Merged /accumulo/branches/1.5/fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooSession.java:r1478483

Propchange: accumulo/trunk/pom.xml
------------------------------------------------------------------------------
  Merged /accumulo/branches/1.5/pom.xml:r1478483

Propchange: accumulo/trunk/server/
------------------------------------------------------------------------------
  Merged /accumulo/branches/1.5/server:r1478483

Modified: accumulo/trunk/server/src/main/java/org/apache/accumulo/server/monitor/servlets/DefaultServlet.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/server/src/main/java/org/apache/accumulo/server/monitor/servlets/DefaultServlet.java?rev=1478484&r1=1478483&r2=1478484&view=diff
==============================================================================
--- accumulo/trunk/server/src/main/java/org/apache/accumulo/server/monitor/servlets/DefaultServlet.java
(original)
+++ accumulo/trunk/server/src/main/java/org/apache/accumulo/server/monitor/servlets/DefaultServlet.java
Thu May  2 18:51:13 2013
@@ -44,6 +44,7 @@ import org.apache.accumulo.core.file.Fil
 import org.apache.accumulo.core.master.thrift.MasterMonitorInfo;
 import org.apache.accumulo.core.util.CachedConfiguration;
 import org.apache.accumulo.core.util.Duration;
+import org.apache.accumulo.core.util.NumUtil;
 import org.apache.accumulo.core.util.Pair;
 import org.apache.accumulo.server.conf.ServerConfiguration;
 import org.apache.accumulo.server.monitor.Monitor;
@@ -357,10 +358,8 @@ public class DefaultServlet extends Basi
     sb.append("</table>\n");
   }
   
-  private static final String BYTES[] = {"", "K", "M", "G", "T", "P", "E", "Z"};
-  
   private static String bytes(long big) {
-    return NumberType.bigNumber(big, BYTES, 1024);
+    return NumUtil.bigNumberForSize(big);
   }
   
   public static void tableRow(StringBuilder sb, boolean highlight, Object... cells) {

Modified: accumulo/trunk/server/src/main/java/org/apache/accumulo/server/monitor/util/celltypes/NumberType.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/server/src/main/java/org/apache/accumulo/server/monitor/util/celltypes/NumberType.java?rev=1478484&r1=1478483&r2=1478484&view=diff
==============================================================================
--- accumulo/trunk/server/src/main/java/org/apache/accumulo/server/monitor/util/celltypes/NumberType.java
(original)
+++ accumulo/trunk/server/src/main/java/org/apache/accumulo/server/monitor/util/celltypes/NumberType.java
Thu May  2 18:51:13 2013
@@ -16,6 +16,8 @@
  */
 package org.apache.accumulo.server.monitor.util.celltypes;
 
+import static org.apache.accumulo.core.util.NumUtil.bigNumberForQuantity;
+
 public class NumberType<T extends Number> extends CellType<T> {
   
   private T warnMin, warnMax, errMin, errMax;
@@ -74,77 +76,43 @@ public class NumberType<T extends Number
   }
   
   public static String commas(long i) {
-    return bigNumber(i);
+    return bigNumberForQuantity(i);
   }
   
   public static String commas(long i, long errMin, long errMax) {
     if (i < errMin || i > errMax)
-      return String.format("<span class='error'>%s</span>", bigNumber(i));
-    return bigNumber(i);
+      return String.format("<span class='error'>%s</span>", bigNumberForQuantity(i));
+    return bigNumberForQuantity(i);
   }
   
   public static String commas(double i) {
-    return bigNumber((long) i);
+    return bigNumberForQuantity((long) i);
   }
   
   public static String commas(double d, double errMin, double errMax) {
     if (d < errMin || d > errMax)
-      return String.format("<span class='error'>%s</span>", bigNumber(d));
-    return bigNumber(d);
+      return String.format("<span class='error'>%s</span>", bigNumberForQuantity(d));
+    return bigNumberForQuantity(d);
   }
   
   public static String commas(long i, long warnMin, long warnMax, long errMin, long errMax)
{
     if (i < errMin || i > errMax)
-      return String.format("<span class='error'>%s</span>", bigNumber(i));
+      return String.format("<span class='error'>%s</span>", bigNumberForQuantity(i));
     if (i < warnMin || i > warnMax)
-      return String.format("<span class='warning'>%s</span>", bigNumber(i));
-    return bigNumber(i);
+      return String.format("<span class='warning'>%s</span>", bigNumberForQuantity(i));
+    return bigNumberForQuantity(i);
   }
   
   public static String commas(double d, double warnMin, double warnMax, double errMin, double
errMax) {
     if (d < errMin || d > errMax)
-      return String.format("<span class='error'>%s</span>", bigNumber(d));
+      return String.format("<span class='error'>%s</span>", bigNumberForQuantity(d));
     if (d < warnMin || d > warnMax)
-      return String.format("<span class='warning'>%s</span>", bigNumber(d));
-    return bigNumber(d);
-  }
-  
-  private static final String COUNTS[] = {"", "K", "M", "B", "T", "e15", "e18", "e21"};
-  
-  public static String bigNumber(long big) {
-    return bigNumber(big, COUNTS, 1000);
-  }
-  
-  public static String bigNumber(double big) {
-    return bigNumber(big, COUNTS, 1000);
-  }
-  
-  public static String bigNumber(long big, String[] SUFFIXES, long base) {
-    float divisor = 1;
-    for (int i = 0; i < SUFFIXES.length; i++) {
-      if (big / divisor < 1024) {
-        if (i == 0)
-          return String.format("%,d", big);
-        return String.format("%,.2f%s", big / divisor, SUFFIXES[i]);
-      }
-      divisor *= base;
-    }
-    return String.format("%,.2f%s", big / divisor, SUFFIXES[SUFFIXES.length - 1]);
-  }
-  
-  public static String bigNumber(double big, String[] SUFFIXES, long base) {
-    float divisor = 1;
-    for (int i = 0; i < SUFFIXES.length; i++) {
-      if (big / divisor < 1024)
-        return String.format("%,.2f%s", big / divisor, SUFFIXES[i]);
-      divisor *= base;
-    }
-    return String.format("%,.2f%s", big / divisor, SUFFIXES[SUFFIXES.length - 1]);
+      return String.format("<span class='warning'>%s</span>", bigNumberForQuantity(d));
+    return bigNumberForQuantity(d);
   }
   
   @Override
   public String alignment() {
     return "right";
   }
-  
 }

Propchange: accumulo/trunk/src/
------------------------------------------------------------------------------
  Merged /accumulo/branches/1.5/src:r1478483



Mime
View raw message