From e252c30645d416697bbb22f1ca20846b3256a92a Mon Sep 17 00:00:00 2001 From: "Apekshit(Appy) Sharma" Date: Fri, 3 Apr 2015 17:23:27 -0700 Subject: [PATCH] HBASE-6919 Remove unnecessary throws IOException from Bytes.readVLong. Added readAsVLong() to deprecate readVLong() which was throwing IOException. Added test for readAsVLong(). Signed-off-by: Sean Busbey --- .../org/apache/hadoop/hbase/util/Bytes.java | 16 +++++++++++++-- .../apache/hadoop/hbase/util/TestBytes.java | 14 +++++++++++++ .../hbase/io/hfile/HFileReaderImpl.java | 20 ++++++------------- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java index bec35ee7680..8096178a920 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java @@ -1364,14 +1364,26 @@ public class Bytes implements Comparable { } /** - * Reads a zero-compressed encoded long from input stream and returns it. + * Reads a zero-compressed encoded long from input buffer and returns it. * @param buffer Binary array * @param offset Offset into array at which vint begins. * @throws java.io.IOException e - * @return deserialized long from stream. + * @return deserialized long from buffer. + * @deprecated Use {@link #readAsVLong()} instead. */ + @Deprecated public static long readVLong(final byte [] buffer, final int offset) throws IOException { + return readAsVLong(buffer, offset); + } + + /** + * Reads a zero-compressed encoded long from input buffer and returns it. + * @param buffer Binary array + * @param offset Offset into array at which vint begins. + * @return deserialized long from buffer. + */ + public static long readAsVLong(final byte [] buffer, final int offset) { byte firstByte = buffer[offset]; int len = WritableUtils.decodeVIntSize(firstByte); if (len == 1) { diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java index d948a2bce61..eb5e4537838 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java @@ -31,6 +31,7 @@ import junit.framework.TestCase; import org.apache.hadoop.hbase.testclassification.MiscTests; import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.apache.hadoop.io.WritableUtils; import org.junit.Assert; import org.junit.experimental.categories.Category; @@ -213,6 +214,19 @@ public class TestBytes extends TestCase { assertEquals(7, target.limit()); } + public void testReadAsVLong() throws Exception { + long [] longs = {-1l, 123l, Long.MIN_VALUE, Long.MAX_VALUE}; + for (int i = 0; i < longs.length; i++) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream output = new DataOutputStream(baos); + WritableUtils.writeVLong(output, longs[i]); + byte[] long_bytes_no_offset = baos.toByteArray(); + assertEquals(longs[i], Bytes.readAsVLong(long_bytes_no_offset, 0)); + byte[] long_bytes_with_offset = bytesWithOffset(long_bytes_no_offset); + assertEquals(longs[i], Bytes.readAsVLong(long_bytes_with_offset, 1)); + } + } + public void testToStringBinaryForBytes() { byte[] array = { '0', '9', 'a', 'z', 'A', 'Z', '@', 1 }; String actual = Bytes.toStringBinary(array); diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java index a007f37b416..933ad22cbee 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java @@ -585,13 +585,9 @@ public class HFileReaderImpl implements HFile.Reader, Configurable { } if (this.reader.shouldIncludeMemstoreTS()) { if (this.reader.isDecodeMemstoreTS()) { - try { - memstoreTS = Bytes.readVLong(blockBuffer.array(), blockBuffer.arrayOffset() - + blockBuffer.position()); - memstoreTSLen = WritableUtils.getVIntSize(memstoreTS); - } catch (Exception e) { - throw new RuntimeException("Error reading memstore timestamp", e); - } + memstoreTS = Bytes.readAsVLong(blockBuffer.array(), blockBuffer.arrayOffset() + + blockBuffer.position()); + memstoreTSLen = WritableUtils.getVIntSize(memstoreTS); } else { memstoreTS = 0; memstoreTSLen = 1; @@ -973,13 +969,9 @@ public class HFileReaderImpl implements HFile.Reader, Configurable { protected void readMvccVersion() { if (this.reader.shouldIncludeMemstoreTS()) { if (this.reader.isDecodeMemstoreTS()) { - try { - currMemstoreTS = Bytes.readVLong(blockBuffer.array(), blockBuffer.arrayOffset() - + blockBuffer.position()); - currMemstoreTSLen = WritableUtils.getVIntSize(currMemstoreTS); - } catch (Exception e) { - throw new RuntimeException("Error reading memstore timestamp", e); - } + currMemstoreTS = Bytes.readAsVLong(blockBuffer.array(), blockBuffer.arrayOffset() + + blockBuffer.position()); + currMemstoreTSLen = WritableUtils.getVIntSize(currMemstoreTS); } else { currMemstoreTS = 0; currMemstoreTSLen = 1;