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   * Parse a serialized representation of {@link BinaryComparator}
068   * @param pbBytes A pb serialized {@link BinaryComparator} instance
069   * @return An instance of {@link BinaryComparator} made from <code>bytes</code>
070   * @throws DeserializationException if an error occurred
071   * @see #toByteArray
072   */
073  public static BinaryComparator parseFrom(final byte[] pbBytes) throws DeserializationException {
074    ComparatorProtos.BinaryComparator proto;
075    try {
076      proto = ComparatorProtos.BinaryComparator.parseFrom(pbBytes);
077    } catch (InvalidProtocolBufferException e) {
078      throw new DeserializationException(e);
079    }
080    return new BinaryComparator(proto.getComparable().getValue().toByteArray());
081  }
082
083  /**
084   * Returns true if and only if the fields of the comparator that are serialized are equal to the
085   * corresponding fields in other. Used for testing.
086   */
087  @Override
088  boolean areSerializedFieldsEqual(ByteArrayComparable other) {
089    if (other == this) {
090      return true;
091    }
092    if (!(other instanceof BinaryComparator)) {
093      return false;
094    }
095    return super.areSerializedFieldsEqual(other);
096  }
097}