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 comparator which compares against a specified byte array, but only compares up to the length of
033 * this byte array. For the rest it is similar to {@link BinaryComparator}.
034 */
035@InterfaceAudience.Public
036@SuppressWarnings("ComparableType") // Should this move to Comparator usage?
037public class BinaryPrefixComparator extends ByteArrayComparable {
038
039  /**
040   * Constructor
041   * @param value value
042   */
043  public BinaryPrefixComparator(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,
050      this.value.length <= length ? this.value.length : length);
051  }
052
053  @Override
054  public int compareTo(ByteBuffer value, int offset, int length) {
055    if (this.value.length <= length) {
056      length = this.value.length;
057    }
058    return ByteBufferUtils.compareTo(this.value, 0, this.value.length, value, offset, length);
059  }
060
061  /**
062   * @return The comparator serialized using pb
063   */
064  @Override
065  public byte[] toByteArray() {
066    ComparatorProtos.BinaryPrefixComparator.Builder builder =
067      ComparatorProtos.BinaryPrefixComparator.newBuilder();
068    builder.setComparable(ProtobufUtil.toByteArrayComparable(this.value));
069    return builder.build().toByteArray();
070  }
071
072  /**
073   * @param pbBytes A pb serialized {@link BinaryPrefixComparator} instance
074   * @return An instance of {@link BinaryPrefixComparator} made from <code>bytes</code> n * @see
075   *         #toByteArray
076   */
077  public static BinaryPrefixComparator parseFrom(final byte[] pbBytes)
078    throws DeserializationException {
079    ComparatorProtos.BinaryPrefixComparator proto;
080    try {
081      proto = ComparatorProtos.BinaryPrefixComparator.parseFrom(pbBytes);
082    } catch (InvalidProtocolBufferException e) {
083      throw new DeserializationException(e);
084    }
085    return new BinaryPrefixComparator(proto.getComparable().getValue().toByteArray());
086  }
087
088  /**
089   * n * @return true if and only if the fields of the comparator that are serialized are equal to
090   * 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 BinaryPrefixComparator)) return false;
096
097    return super.areSerializedFieldsEqual(other);
098  }
099}