You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by la...@apache.org on 2011/10/26 20:04:56 UTC
svn commit: r1189350 - in /hbase/branches/0.92: CHANGES.txt
src/main/java/org/apache/hadoop/hbase/util/Bytes.java
src/test/java/org/apache/hadoop/hbase/util/TestBytes.java
Author: larsh
Date: Wed Oct 26 18:04:55 2011
New Revision: 1189350
URL: http://svn.apache.org/viewvc?rev=1189350&view=rev
Log:
HBASE-4648 Bytes.toBigDecimal() doesn't use offset
Modified:
hbase/branches/0.92/CHANGES.txt
hbase/branches/0.92/src/main/java/org/apache/hadoop/hbase/util/Bytes.java
hbase/branches/0.92/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java
Modified: hbase/branches/0.92/CHANGES.txt
URL: http://svn.apache.org/viewvc/hbase/branches/0.92/CHANGES.txt?rev=1189350&r1=1189349&r2=1189350&view=diff
==============================================================================
--- hbase/branches/0.92/CHANGES.txt (original)
+++ hbase/branches/0.92/CHANGES.txt Wed Oct 26 18:04:55 2011
@@ -24,6 +24,7 @@ Release 0.92.0 - Unreleased
(Akash Ashok)
HBASE-4503 Purge deprecated HBaseClusterTestCase
HBASE-4374 Up default regions size from 256M to 1G
+ HBASE-4648 Bytes.toBigDecimal() doesn't use offset (Brian Keller via Lars H)
BUG FIXES
HBASE-3280 YouAreDeadException being swallowed in HRS getMaster
Modified: hbase/branches/0.92/src/main/java/org/apache/hadoop/hbase/util/Bytes.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.92/src/main/java/org/apache/hadoop/hbase/util/Bytes.java?rev=1189350&r1=1189349&r2=1189350&view=diff
==============================================================================
--- hbase/branches/0.92/src/main/java/org/apache/hadoop/hbase/util/Bytes.java (original)
+++ hbase/branches/0.92/src/main/java/org/apache/hadoop/hbase/util/Bytes.java Wed Oct 26 18:04:55 2011
@@ -819,17 +819,6 @@ public class Bytes {
*
* @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
*/
@@ -839,9 +828,9 @@ public class Bytes {
return null;
}
- int scale = toInt(bytes, 0);
+ int scale = toInt(bytes, offset);
byte[] tcBytes = new byte[length - SIZEOF_INT];
- System.arraycopy(bytes, SIZEOF_INT, tcBytes, 0, length - SIZEOF_INT);
+ System.arraycopy(bytes, offset + SIZEOF_INT, tcBytes, 0, length - SIZEOF_INT);
return new BigDecimal(new BigInteger(tcBytes), scale);
}
Modified: hbase/branches/0.92/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.92/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java?rev=1189350&r1=1189349&r2=1189350&view=diff
==============================================================================
--- hbase/branches/0.92/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java (original)
+++ hbase/branches/0.92/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java Wed Oct 26 18:04:55 2011
@@ -24,6 +24,7 @@ import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
+import java.math.BigDecimal;
import java.util.Arrays;
import junit.framework.TestCase;
@@ -107,11 +108,25 @@ public class TestBytes extends TestCase
}
}
+ public void testToInt() throws Exception {
+ int [] ints = {-1, 123, Integer.MIN_VALUE, Integer.MAX_VALUE};
+ for (int i = 0; i < ints.length; i++) {
+ byte [] b = Bytes.toBytes(ints[i]);
+ assertEquals(ints[i], Bytes.toInt(b));
+ byte [] b2 = bytesWithOffset(b);
+ assertEquals(ints[i], Bytes.toInt(b2, 1));
+ assertEquals(ints[i], Bytes.toInt(b2, 1, Bytes.SIZEOF_INT));
+ }
+ }
+
public void testToLong() throws Exception {
- long [] longs = {-1l, 123l, 122232323232l};
+ long [] longs = {-1l, 123l, Long.MIN_VALUE, Long.MAX_VALUE};
for (int i = 0; i < longs.length; i++) {
byte [] b = Bytes.toBytes(longs[i]);
assertEquals(longs[i], Bytes.toLong(b));
+ byte [] b2 = bytesWithOffset(b);
+ assertEquals(longs[i], Bytes.toLong(b2, 1));
+ assertEquals(longs[i], Bytes.toLong(b2, 1, Bytes.SIZEOF_LONG));
}
}
@@ -120,6 +135,8 @@ public class TestBytes extends TestCase
for (int i = 0; i < floats.length; i++) {
byte [] b = Bytes.toBytes(floats[i]);
assertEquals(floats[i], Bytes.toFloat(b));
+ byte [] b2 = bytesWithOffset(b);
+ assertEquals(floats[i], Bytes.toFloat(b2, 1));
}
}
@@ -128,9 +145,30 @@ public class TestBytes extends TestCase
for (int i = 0; i < doubles.length; i++) {
byte [] b = Bytes.toBytes(doubles[i]);
assertEquals(doubles[i], Bytes.toDouble(b));
+ byte [] b2 = bytesWithOffset(b);
+ assertEquals(doubles[i], Bytes.toDouble(b2, 1));
}
}
+ public void testToBigDecimal() throws Exception {
+ BigDecimal [] decimals = {new BigDecimal("-1"), new BigDecimal("123.123"),
+ new BigDecimal("123123123123")};
+ for (int i = 0; i < decimals.length; i++) {
+ byte [] b = Bytes.toBytes(decimals[i]);
+ assertEquals(decimals[i], Bytes.toBigDecimal(b));
+ byte [] b2 = bytesWithOffset(b);
+ assertEquals(decimals[i], Bytes.toBigDecimal(b2, 1, b.length));
+ }
+ }
+
+ private byte [] bytesWithOffset(byte [] src) {
+ // add one byte in front to test offset
+ byte [] result = new byte[src.length + 1];
+ result[0] = (byte) 0xAA;
+ System.arraycopy(src, 0, result, 1, src.length);
+ return result;
+ }
+
public void testBinarySearch() throws Exception {
byte [][] arr = {
{1},