Repository: hbase Updated Branches: refs/heads/hbase-12439 1c4d9c896 -> ecdfb8232
HBASE-17859 ByteBufferUtils#compareTo is wrong Project: http://git-wip-us.apache.org/repos/asf/hbase/repo Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/73e1bcd3 Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/73e1bcd3 Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/73e1bcd3 Branch: refs/heads/hbase-12439 Commit: 73e1bcd33515061be2dc2e51e6ad19d9798a8ef6 Parents: 9facfa5 Author: CHIA-PING TSAI <chia7...@gmail.com> Authored: Fri Mar 31 19:45:10 2017 +0800 Committer: Chia-Ping Tsai <chia7...@gmail.com> Committed: Sat Apr 1 13:42:36 2017 +0800 ---------------------------------------------------------------------- .../hadoop/hbase/util/ByteBufferUtils.java | 9 +++-- .../hadoop/hbase/util/TestByteBufferUtils.java | 39 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/73e1bcd3/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteBufferUtils.java ---------------------------------------------------------------------- diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteBufferUtils.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteBufferUtils.java index 760afd4..4bed97c 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteBufferUtils.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteBufferUtils.java @@ -16,6 +16,7 @@ */ package org.apache.hadoop.hbase.util; +import com.google.common.annotations.VisibleForTesting; import java.io.ByteArrayOutputStream; import java.io.DataInput; import java.io.DataInputStream; @@ -49,8 +50,10 @@ public final class ByteBufferUtils { public final static int VALUE_MASK = 0x7f; public final static int NEXT_BIT_SHIFT = 7; public final static int NEXT_BIT_MASK = 1 << 7; - private static final boolean UNSAFE_AVAIL = UnsafeAvailChecker.isAvailable(); - private static final boolean UNSAFE_UNALIGNED = UnsafeAvailChecker.unaligned(); + @VisibleForTesting + static boolean UNSAFE_AVAIL = UnsafeAvailChecker.isAvailable(); + @VisibleForTesting + static boolean UNSAFE_UNALIGNED = UnsafeAvailChecker.unaligned(); private ByteBufferUtils() { } @@ -668,7 +671,7 @@ public final class ByteBufferUtils { int end2 = o2 + l2; for (int i = o1, j = o2; i < end1 && j < end2; i++, j++) { int a = buf1[i] & 0xFF; - int b = buf2.get(i) & 0xFF; + int b = buf2.get(j) & 0xFF; if (a != b) { return a - b; } http://git-wip-us.apache.org/repos/asf/hbase/blob/73e1bcd3/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteBufferUtils.java ---------------------------------------------------------------------- diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteBufferUtils.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteBufferUtils.java index b78574a..053fb24 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteBufferUtils.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteBufferUtils.java @@ -28,8 +28,10 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.nio.ByteBuffer; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; @@ -38,15 +40,44 @@ import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.testclassification.MiscTests; import org.apache.hadoop.hbase.testclassification.SmallTests; import org.apache.hadoop.io.WritableUtils; +import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; @Category({MiscTests.class, SmallTests.class}) +@RunWith(Parameterized.class) public class TestByteBufferUtils { private byte[] array; + @AfterClass + public static void afterClass() throws Exception { + ByteBufferUtils.UNSAFE_AVAIL = UnsafeAvailChecker.isAvailable(); + ByteBufferUtils.UNSAFE_UNALIGNED = UnsafeAvailChecker.unaligned(); + } + + @Parameterized.Parameters + public static Collection<Object[]> parameters() { + List<Object[]> paramList = new ArrayList<>(2); + { + paramList.add(new Object[] { false }); + paramList.add(new Object[] { true }); + } + return paramList; + } + + public TestByteBufferUtils(boolean useUnsafeIfPossible) { + if (useUnsafeIfPossible) { + ByteBufferUtils.UNSAFE_AVAIL = UnsafeAvailChecker.isAvailable(); + ByteBufferUtils.UNSAFE_UNALIGNED = UnsafeAvailChecker.unaligned(); + } else { + ByteBufferUtils.UNSAFE_AVAIL = false; + ByteBufferUtils.UNSAFE_UNALIGNED = false; + } + } /** * Create an array with sample data. */ @@ -412,6 +443,14 @@ public class TestByteBufferUtils { assertTrue(result > 0); result = ByteBufferUtils.compareTo(bb3, 0, bb3.remaining(), b3, 0, b3.length); assertTrue(result < 0); + + byte[] b4 = Bytes.toBytes("123"); + ByteBuffer bb4 = ByteBuffer.allocate(10 + b4.length); + for (int i = 10; i < (bb4.capacity()); ++i) { + bb4.put(i, b4[i - 10]); + } + result = ByteBufferUtils.compareTo(b4, 0, b4.length, bb4, 10, b4.length); + assertEquals(0, result); } @Test