You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by st...@apache.org on 2011/05/19 23:03:20 UTC

svn commit: r1125099 - in /hbase/branches/0.90: CHANGES.txt src/main/java/org/apache/hadoop/hbase/util/Bytes.java

Author: stack
Date: Thu May 19 21:03:20 2011
New Revision: 1125099

URL: http://svn.apache.org/viewvc?rev=1125099&view=rev
Log:
HBASE-3902 Add Bytes.toBigDecimal and Bytes.toBytes(BigDecimal)

Modified:
    hbase/branches/0.90/CHANGES.txt
    hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/util/Bytes.java

Modified: hbase/branches/0.90/CHANGES.txt
URL: http://svn.apache.org/viewvc/hbase/branches/0.90/CHANGES.txt?rev=1125099&r1=1125098&r2=1125099&view=diff
==============================================================================
--- hbase/branches/0.90/CHANGES.txt (original)
+++ hbase/branches/0.90/CHANGES.txt Thu May 19 21:03:20 2011
@@ -8,12 +8,14 @@ Release 0.90.4 - Unreleased
    HBASE-3895  Fix order of parameters after HBASE-1511
    HBASE-3874  ServerShutdownHandler fails on NPE if a plan has a random
                region assignment
+   HBASE-3902  Add Bytes.toBigDecimal and Bytes.toBytes(BigDecimal)
+               (Vaibhav Puranik)
 
   IMPROVEMENT
    HBASE-3882  hbase-config.sh needs to be updated so it can auto-detects the
                sun jre provided by RHEL6 (Roman Shaposhnik)
 
-Release 0.90.3 - Unreleased
+Release 0.90.3 - May 19th, 2011
   BUG FIXES
    HBASE-3712  HTable.close() doesn't shutdown thread pool
                (Ted Yu via Stack)

Modified: hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/util/Bytes.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/util/Bytes.java?rev=1125099&r1=1125098&r2=1125099&view=diff
==============================================================================
--- hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/util/Bytes.java (original)
+++ hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/util/Bytes.java Thu May 19 21:03:20 2011
@@ -751,6 +751,81 @@ public class Bytes {
   }
 
   /**
+   * Convert a BigDecimal value to a byte array
+   *
+   * @param val
+   * @return the byte array
+   */
+  public static byte[] toBytes(BigDecimal val) {
+    byte[] valueBytes = val.unscaledValue().toByteArray();
+    byte[] result = new byte[valueBytes.length + SIZEOF_INT];
+    int offset = putInt(result, 0, val.scale());
+    putBytes(result, offset, valueBytes, 0, valueBytes.length);
+    return result;
+  }
+
+
+  /**
+   * Converts a byte array to a BigDecimal
+   *
+   * @param bytes
+   * @return the char value
+   */
+  public static BigDecimal toBigDecimal(byte[] bytes) {
+    return toBigDecimal(bytes, 0, bytes.length);
+  }
+
+  /**
+   * Converts a byte array to a BigDecimal value
+   *
+   * @param bytes
+   * @param offset
+   * @return the char value
+   */
+  public static BigDecimal toBigDecimal(byte[] bytes, int offset) {
+    return toBigDecimal(bytes, offset, bytes.length);
+  }
+
+  /**
+   * Converts a byte array to a BigDecimal value
+   *
+   * @param bytes
+   * @param offset
+   * @param length
+   * @return the char value
+   */
+  public static BigDecimal toBigDecimal(byte[] bytes, int offset, final int length) {
+    if (bytes == null || length < SIZEOF_INT + 1 ||
+      (offset + length > bytes.length)) {
+      return null;
+    }
+
+    int scale = toInt(bytes, 0);
+    byte[] tcBytes = new byte[length - SIZEOF_INT];
+    System.arraycopy(bytes, SIZEOF_INT, tcBytes, 0, length - SIZEOF_INT);
+    return new BigDecimal(new BigInteger(tcBytes), scale);
+  }
+
+  /**
+   * Put a BigDecimal value out to the specified byte array position.
+   *
+   * @param bytes  the byte array
+   * @param offset position in the array
+   * @param val    BigDecimal to write out
+   * @return incremented offset
+   */
+  public static int putBigDecimal(byte[] bytes, int offset, BigDecimal val) {
+    if (bytes == null) {
+      return offset;
+    }
+
+    byte[] valueBytes = val.unscaledValue().toByteArray();
+    byte[] result = new byte[valueBytes.length + SIZEOF_INT];
+    offset = putInt(result, offset, val.scale());
+    return putBytes(result, offset, valueBytes, 0, valueBytes.length);
+  }
+  
+  /**
    * @param vint Integer to make a vint of.
    * @return Vint as bytes array.
    */