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 ff7c8504a355..8fee538afc7c 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 @@ -1344,6 +1344,33 @@ public static boolean isAvailable() return theUnsafe != null; } + private static final boolean unaligned=!"sparcv9".equals(System.getProperty("os.arch")); + + private long getLong(Object address, long offset) { + if (unaligned) { + return theUnsafe.getLong(address, offset); + } else { + if (littleEndian) { + return (((long) theUnsafe.getByte(address, offset + 7) ) << 56) | + (((long) theUnsafe.getByte(address, offset + 6) & 0xff) << 48) | + (((long) theUnsafe.getByte(address, offset + 5) & 0xff) << 40) | + (((long) theUnsafe.getByte(address, offset + 4) & 0xff) << 32) | + (((long) theUnsafe.getByte(address, offset + 3) & 0xff) << 24) | + (((long) theUnsafe.getByte(address, offset + 2) & 0xff) << 16) | + (((long) theUnsafe.getByte(address, offset + 1) & 0xff) << 8) | + (((long) theUnsafe.getByte(address, offset ) & 0xff) ); + } else { + return (((long) theUnsafe.getByte(address, offset ) ) << 56) | + (((long) theUnsafe.getByte(address, offset + 1) & 0xff) << 48) | + (((long) theUnsafe.getByte(address, offset + 2) & 0xff) << 40) | + (((long) theUnsafe.getByte(address, offset + 3) & 0xff) << 32) | + (((long) theUnsafe.getByte(address, offset + 4) & 0xff) << 24) | + (((long) theUnsafe.getByte(address, offset + 5) & 0xff) << 16) | + (((long) theUnsafe.getByte(address, offset + 6) & 0xff) << 8) | + (((long) theUnsafe.getByte(address, offset + 7) & 0xff) ); + } + } + } /** * Lexicographically compare two arrays. * @@ -1376,8 +1403,8 @@ public int compareTo(byte[] buffer1, int offset1, int length1, * On the other hand, it is substantially faster on 64-bit. */ for (int i = 0; i < minWords * SIZEOF_LONG; i += SIZEOF_LONG) { - long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i); - long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i); + long lw = getLong(buffer1, offset1Adj + (long) i); + long rw = getLong(buffer2, offset2Adj + (long) i); long diff = lw ^ rw; if(littleEndian){ lw = Long.reverseBytes(lw);