Skip to content

Commit

Permalink
HBASE-6919 Remove unnecessary throws IOException from Bytes.readVLong.
Browse files Browse the repository at this point in the history
Added readAsVLong() to deprecate readVLong() which was throwing IOException. Added test for readAsVLong().

Signed-off-by: Sean Busbey <busbey@apache.org>
  • Loading branch information
Apekshit(Appy) Sharma authored and busbey committed Apr 4, 2015
1 parent 6c22333 commit e252c30
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
16 changes: 14 additions & 2 deletions hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1364,14 +1364,26 @@ public static long bytesToVint(final byte [] buffer) {
}

/**
* 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

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;

Expand Down Expand Up @@ -213,6 +214,19 @@ public void testGetBytesForByteBuffer() {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,9 @@ protected int blockSeek(Cell key, boolean seekBefore) {
}
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;
Expand Down Expand Up @@ -973,13 +969,9 @@ protected void updateCurrBlock(HFileBlock newBlock) {
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;
Expand Down

0 comments on commit e252c30

Please sign in to comment.