001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.filter;
019
020import java.nio.ByteBuffer;
021import org.apache.hadoop.hbase.exceptions.DeserializationException;
022import org.apache.hadoop.hbase.util.ByteBufferUtils;
023import org.apache.hadoop.hbase.util.Bytes;
024import org.apache.yetus.audience.InterfaceAudience;
025
026import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
027
028import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
029import org.apache.hadoop.hbase.shaded.protobuf.generated.ComparatorProtos;
030
031/**
032 * A binary comparator which lexicographically compares against the specified byte array using
033 * {@link org.apache.hadoop.hbase.util.Bytes#compareTo(byte[], byte[])}.
034 * @since 2.0.0
035 */
036@InterfaceAudience.Public
037@SuppressWarnings("ComparableType") // Should this move to Comparator usage?
038public class BinaryComparator extends org.apache.hadoop.hbase.filter.ByteArrayComparable {
039  /**
040   * Constructor
041   * @param value value
042   */
043  public BinaryComparator(byte[] value) {
044    super(value);
045  }
046
047  @Override
048  public int compareTo(byte[] value, int offset, int length) {
049    return Bytes.compareTo(this.value, 0, this.value.length, value, offset, length);
050  }
051
052  @Override
053  public int compareTo(ByteBuffer value, int offset, int length) {
054    return ByteBufferUtils.compareTo(this.value, 0, this.value.length, value, offset, length);
055  }
056
057  /** Returns The comparator serialized using pb */
058  @Override
059  public byte[] toByteArray() {
060    ComparatorProtos.BinaryComparator.Builder builder =
061      ComparatorProtos.BinaryComparator.newBuilder();
062    builder.setComparable(ProtobufUtil.toByteArrayComparable(this.value));
063    return builder.build().toByteArray();
064  }
065
066  /**
067   * @param pbBytes A pb serialized {@link BinaryComparator} instance
068   * @return An instance of {@link BinaryComparator} made from <code>bytes</code>
069   * @see #toByteArray
070   */
071  public static BinaryComparator parseFrom(final byte[] pbBytes) throws DeserializationException {
072    ComparatorProtos.BinaryComparator proto;
073    try {
074      proto = ComparatorProtos.BinaryComparator.parseFrom(pbBytes);
075    } catch (InvalidProtocolBufferException e) {
076      throw new DeserializationException(e);
077    }
078    return new BinaryComparator(proto.getComparable().getValue().toByteArray());
079  }
080
081  /**
082   * @return true if and only if the fields of the comparator that are serialized are equal to the
083   *         corresponding fields in other. Used for testing.
084   */
085  @Override
086  boolean areSerializedFieldsEqual(ByteArrayComparable other) {
087    if (other == this) return true;
088    if (!(other instanceof BinaryComparator)) return false;
089
090    return super.areSerializedFieldsEqual(other);
091  }
092}