Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ private int getAbsolutePos(int pos) {
public int length() {
return this.length;
}

@Override
public int getIntLE(int pos) {
return LittleEndianBytes.toInt(t, getAbsolutePos(pos));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ public HashKey(T t) {

/** Returns The number of bytes in this HashKey */
public abstract int length();

/**
* Returns the little-endian 32-bit int value starting at the given position in this
* {@code HashKey}.
* @param pos the starting offset of the 4-byte little-endian int
* @return the 32-bit value decoded in little-endian order
*/
public abstract int getIntLE(int pos);
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,9 @@ public <T> int hash(HashKey<T> hashKey, int initval) {
a = b = c = 0xdeadbeef + length + initval;
int offset = 0;
for (; length > 12; offset += 12, length -= 12) {
a += (hashKey.get(offset) & BYTE_MASK);
a += ((hashKey.get(offset + 1) & BYTE_MASK) << 8);
a += ((hashKey.get(offset + 2) & BYTE_MASK) << 16);
a += ((hashKey.get(offset + 3) & BYTE_MASK) << 24);
b += (hashKey.get(offset + 4) & BYTE_MASK);
b += ((hashKey.get(offset + 5) & BYTE_MASK) << 8);
b += ((hashKey.get(offset + 6) & BYTE_MASK) << 16);
b += ((hashKey.get(offset + 7) & BYTE_MASK) << 24);
c += (hashKey.get(offset + 8) & BYTE_MASK);
c += ((hashKey.get(offset + 9) & BYTE_MASK) << 8);
c += ((hashKey.get(offset + 10) & BYTE_MASK) << 16);
c += ((hashKey.get(offset + 11) & BYTE_MASK) << 24);
a += hashKey.getIntLE(offset);
b += hashKey.getIntLE(offset + 4);
c += hashKey.getIntLE(offset + 8);

/*
* mix -- mix 3 32-bit values reversibly. This is reversible, so any information in (a,b,c)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.util;

import java.nio.ByteBuffer;
import org.apache.hadoop.hbase.ByteBufferExtendedCell;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.unsafe.HBasePlatformDependent;
import org.apache.yetus.audience.InterfaceAudience;

/**
* Utility methods for reading and writing little-endian integers from byte[] and ByteBuffer. Used
* by hashing components to perform fast, low-level LE conversions with optional Unsafe
* acceleration.
*/
@InterfaceAudience.Private
public final class LittleEndianBytes {
final static boolean UNSAFE_UNALIGNED = HBasePlatformDependent.unaligned();

static abstract class Converter {
abstract int toInt(byte[] bytes, int offset);

abstract int toInt(ByteBuffer buffer, int offset);

abstract int putInt(byte[] bytes, int offset, int val);
}

static class ConverterHolder {
static final String UNSAFE_CONVERTER_NAME =
ConverterHolder.class.getName() + "$UnsafeConverter";
static final Converter BEST_CONVERTER = getBestConverter();

static Converter getBestConverter() {
try {
Class<? extends Converter> theClass =
Class.forName(UNSAFE_CONVERTER_NAME).asSubclass(Converter.class);
return theClass.getConstructor().newInstance();
} catch (Throwable t) {
return PureJavaConverter.INSTANCE;
}
}

static final class PureJavaConverter extends Converter {
static final PureJavaConverter INSTANCE = new PureJavaConverter();

private PureJavaConverter() {
}

@Override
int toInt(byte[] bytes, int offset) {
int n = 0;
for (int i = offset + 3; i >= offset; i--) {
n <<= 8;
n ^= (bytes[i] & 0xFF);
}
return n;
}

@Override
int toInt(ByteBuffer buffer, int offset) {
return Integer.reverseBytes(buffer.getInt(offset));
}

@Override
int putInt(byte[] bytes, int offset, int val) {
for (int i = offset; i < offset + 3; i++) {
bytes[i] = (byte) val;
val >>>= 8;
}
bytes[offset + 3] = (byte) val;
return offset + Bytes.SIZEOF_INT;
}
}

static final class UnsafeConverter extends Converter {
static final UnsafeConverter INSTANCE = new UnsafeConverter();

public UnsafeConverter() {
}

static {
if (!UNSAFE_UNALIGNED) {
throw new Error();
}
}

@Override
int toInt(byte[] bytes, int offset) {
return UnsafeAccess.toIntLE(bytes, offset);
}

@Override
int toInt(ByteBuffer buffer, int offset) {
return UnsafeAccess.toIntLE(buffer, offset);
}

@Override
int putInt(byte[] bytes, int offset, int val) {
return UnsafeAccess.putIntLE(bytes, offset, val);
}
}
}

/*
* Writes an int in little-endian order. Caller must ensure bounds; no checks are performed.
*/
public static void putInt(byte[] bytes, int offset, int val) {
assert offset >= 0 && bytes.length - offset >= Bytes.SIZEOF_INT;
ConverterHolder.BEST_CONVERTER.putInt(bytes, offset, val);
}

/*
* Reads an int in little-endian order. Caller must ensure bounds; no checks are performed.
*/
public static int toInt(byte[] bytes, int offset) {
assert offset >= 0 && bytes.length - offset >= Bytes.SIZEOF_INT;
return ConverterHolder.BEST_CONVERTER.toInt(bytes, offset);
}

/*
* Reads an int in little-endian order from ByteBuffer. Caller must ensure bounds; no checks are
* performed.
*/
public static int toInt(ByteBuffer buffer, int offset) {
assert offset >= 0 && buffer.capacity() - offset >= Bytes.SIZEOF_INT;
return ConverterHolder.BEST_CONVERTER.toInt(buffer, offset);
}

/*
* Reads an int in little-endian order from the row portion of the Cell, at the given offset.
*/
public static int getRowAsInt(Cell cell, int offset) {
if (cell instanceof ByteBufferExtendedCell) {
ByteBufferExtendedCell bbCell = (ByteBufferExtendedCell) cell;
return toInt(bbCell.getRowByteBuffer(), bbCell.getRowPosition() + offset);
}
return toInt(cell.getRowArray(), cell.getRowOffset() + offset);
}

/*
* Reads an int in little-endian order from the qualifier portion of the Cell, at the given
* offset.
*/
public static int getQualifierAsInt(Cell cell, int offset) {
if (cell instanceof ByteBufferExtendedCell) {
ByteBufferExtendedCell bbCell = (ByteBufferExtendedCell) cell;
return toInt(bbCell.getQualifierByteBuffer(), bbCell.getQualifierPosition() + offset);
}
return toInt(cell.getQualifierArray(), cell.getQualifierOffset() + offset);
}

private LittleEndianBytes() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ public <T> int hash(HashKey<T> hashKey, int seed) {
int len_4 = length >> 2;

for (int i = 0; i < len_4; i++) {
int i_4 = (i << 2);
int k = hashKey.get(i_4 + 3);
k = k << 8;
k = k | (hashKey.get(i_4 + 2) & 0xff);
k = k << 8;
k = k | (hashKey.get(i_4 + 1) & 0xff);
k = k << 8;
// noinspection PointlessArithmeticExpression
k = k | (hashKey.get(i_4 + 0) & 0xff);
int k = hashKey.getIntLE(i << 2);
k *= m;
k ^= k >>> r;
k *= m;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public <T> int hash(HashKey<T> hashKey, int initval) {
int roundedEnd = (length & 0xfffffffc); // round down to 4 byte block

for (int i = 0; i < roundedEnd; i += 4) {
// little endian load order
int k1 = (hashKey.get(i) & 0xff) | ((hashKey.get(i + 1) & 0xff) << 8)
| ((hashKey.get(i + 2) & 0xff) << 16) | (hashKey.get(i + 3) << 24);
int k1 = hashKey.getIntLE(i);
k1 *= c1;
k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15);
k1 *= c2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public byte get(int offset) {
public int length() {
return this.t.getRowLength();
}

@Override
public int getIntLE(int offset) {
return LittleEndianBytes.getRowAsInt(t, offset);
}
}
Loading