diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueTestUtil.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueTestUtil.java index 7467d67..ee6477c 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueTestUtil.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueTestUtil.java @@ -22,10 +22,9 @@ import java.nio.ByteBuffer; import java.util.Collection; import java.util.List; -import org.apache.yetus.audience.InterfaceAudience; import org.apache.hadoop.hbase.util.Bytes; -import org.apache.hadoop.hbase.util.IterableUtils; import org.apache.hadoop.hbase.util.Strings; +import org.apache.yetus.audience.InterfaceAudience; import org.apache.hadoop.hbase.shaded.com.google.common.collect.Lists; @@ -64,10 +63,12 @@ public class KeyValueTestUtil { boolean includeMemstoreTS) { int totalBytes = KeyValueUtil.totalLengthWithMvccVersion(kvs, includeMemstoreTS); ByteBuffer bb = ByteBuffer.allocate(totalBytes); - for (KeyValue kv : IterableUtils.nullSafe(kvs)) { - KeyValueUtil.appendToByteBuffer(bb, kv, includeMemstoreTS); + if (totalBytes > 0) { + for (KeyValue kv : kvs) { + KeyValueUtil.appendToByteBuffer(bb, kv, includeMemstoreTS); + } + bb.rewind(); } - bb.rewind(); return bb; } diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueUtil.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueUtil.java index 6fd37c0..8b44edd 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueUtil.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueUtil.java @@ -29,13 +29,12 @@ import java.util.ArrayList; import java.util.List; import org.apache.hadoop.hbase.KeyValue.Type; -import org.apache.yetus.audience.InterfaceAudience; import org.apache.hadoop.hbase.io.util.StreamUtils; import org.apache.hadoop.hbase.util.ByteBufferUtils; import org.apache.hadoop.hbase.util.Bytes; -import org.apache.hadoop.hbase.util.IterableUtils; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.io.WritableUtils; +import org.apache.yetus.audience.InterfaceAudience; import org.apache.hadoop.hbase.shaded.com.google.common.base.Function; import org.apache.hadoop.hbase.shaded.com.google.common.collect.Lists; @@ -89,8 +88,11 @@ public class KeyValueUtil { public static int totalLengthWithMvccVersion(final Iterable kvs, final boolean includeMvccVersion) { + if (kvs == null) { + return 0; + } int length = 0; - for (KeyValue kv : IterableUtils.nullSafe(kvs)) { + for (KeyValue kv : kvs) { length += lengthWithMvccVersion(kv, includeMvccVersion); } return length; 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 e36b1bb..79225ad 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 @@ -30,27 +30,29 @@ import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.security.SecureRandom; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; +import com.google.protobuf.ByteString; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellComparator; import org.apache.hadoop.hbase.KeyValue; -import org.apache.yetus.audience.InterfaceAudience; import org.apache.hadoop.io.RawComparator; import org.apache.hadoop.io.WritableComparator; import org.apache.hadoop.io.WritableUtils; - -import sun.misc.Unsafe; +import org.apache.yetus.audience.InterfaceAudience; import org.apache.hadoop.hbase.shaded.com.google.common.annotations.VisibleForTesting; -import org.apache.hadoop.hbase.shaded.com.google.common.collect.Lists; -import com.google.protobuf.ByteString; + +import sun.misc.Unsafe; /** * Utility class that handles byte arrays, conversions to/from other types, @@ -2337,21 +2339,24 @@ public class Bytes implements Comparable { } public static boolean isSorted(Collection arrays) { - byte[] previous = new byte[0]; - for (byte[] array : IterableUtils.nullSafe(arrays)) { - if (Bytes.compareTo(previous, array) > 0) { - return false; + if (CollectionUtils.isNotEmpty(arrays)) { + byte[] previous = new byte[0]; + for (byte[] array : arrays) { + if (Bytes.compareTo(previous, array) > 0) { + return false; + } + previous = array; } - previous = array; } return true; } public static List getUtf8ByteArrays(List strings) { - List byteArrays = Lists.newArrayListWithCapacity(CollectionUtils.nullSafeSize(strings)); - for (String s : IterableUtils.nullSafe(strings)) { - byteArrays.add(Bytes.toBytes(s)); + if (CollectionUtils.isEmpty(strings)) { + return Collections.emptyList(); } + List byteArrays = new ArrayList<>(strings.size()); + strings.forEach(string -> byteArrays.add(Bytes.toBytes(string))); return byteArrays; } diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/IterableUtils.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/IterableUtils.java deleted file mode 100644 index e0bca84..0000000 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/IterableUtils.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import org.apache.yetus.audience.InterfaceAudience; - -/** - * Utility methods for Iterable including null-safe handlers. - */ -@InterfaceAudience.Private -public class IterableUtils { - - private static final List EMPTY_LIST = Collections - .unmodifiableList(new ArrayList<>(0)); - - @SuppressWarnings("unchecked") - public static Iterable nullSafe(Iterable in) { - if (in == null) { - return (List) EMPTY_LIST; - } - return in; - } - -}