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