junegunn commented on code in PR #7934:
URL: https://github.com/apache/hbase/pull/7934#discussion_r3392887244


##########
hbase-common/src/main/java/org/apache/hadoop/hbase/util/LittleEndianBytes.java:
##########
@@ -0,0 +1,170 @@
+/*
+ * 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.
+ */
[email protected]
+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;
+      }

Review Comment:
   How about making this implementation similar to the `toInt(ByteBuffer)` 
version? It should be a bit easier to understand.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to