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