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 org.apache.hadoop.hbase.exceptions.DeserializationException;
021import org.apache.hadoop.hbase.util.Bytes;
022import org.apache.yetus.audience.InterfaceAudience;
023
024import org.apache.hbase.thirdparty.com.google.protobuf.ByteString;
025import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
026
027import org.apache.hadoop.hbase.shaded.protobuf.generated.ComparatorProtos;
028
029/**
030 * A comparator which compares against a specified byte array, but only compares specific portion of
031 * the byte array. For the rest it is similar to {@link BinaryComparator}.
032 */
033@InterfaceAudience.Public
034@SuppressWarnings("ComparableType")
035public class BinaryComponentComparator extends ByteArrayComparable {
036  private int offset; // offset of component from beginning.
037
038  /**
039   * Constructor
040   * @param value  value of the component
041   * @param offset offset of the component from begining
042   */
043  public BinaryComponentComparator(byte[] value, int offset) {
044    super(value);
045    this.offset = offset;
046  }
047
048  @Override
049  public int compareTo(byte[] value) {
050    return compareTo(value, 0, value.length);
051  }
052
053  @Override
054  public int compareTo(byte[] value, int offset, int length) {
055    return Bytes.compareTo(this.value, 0, this.value.length, value, offset + this.offset,
056      this.value.length);
057  }
058
059  @Override
060  public boolean equals(Object other) {
061    if (other == this) {
062      return true;
063    }
064    if (!(other instanceof BinaryComponentComparator)) {
065      return false;
066    }
067    BinaryComponentComparator bcc = (BinaryComponentComparator) other;
068    return offset == bcc.offset && (compareTo(bcc.value) == 0);
069  }
070
071  @Override
072  public int hashCode() {
073    int result = super.hashCode();
074    result = 31 * result + offset;
075    return result;
076  }
077
078  /** Returns The comparator serialized using pb */
079  @Override
080  public byte[] toByteArray() {
081    ComparatorProtos.BinaryComponentComparator.Builder builder =
082      ComparatorProtos.BinaryComponentComparator.newBuilder();
083    builder.setValue(ByteString.copyFrom(this.value));
084    builder.setOffset(this.offset);
085    return builder.build().toByteArray();
086  }
087
088  /**
089   * Parse a serialized representation of {@link BinaryComponentComparator}
090   * @param pbBytes A pb serialized {@link BinaryComponentComparator} instance
091   * @return An instance of {@link BinaryComponentComparator} made from <code>bytes</code>
092   * @throws DeserializationException if an error occurred
093   * @see #toByteArray
094   */
095  public static BinaryComponentComparator parseFrom(final byte[] pbBytes)
096    throws DeserializationException {
097    ComparatorProtos.BinaryComponentComparator proto;
098    try {
099      proto = ComparatorProtos.BinaryComponentComparator.parseFrom(pbBytes);
100    } catch (InvalidProtocolBufferException e) {
101      throw new DeserializationException(e);
102    }
103    return new BinaryComponentComparator(proto.getValue().toByteArray(), proto.getOffset());
104  }
105
106  /**
107   * Returns true if and only if the fields of the comparator that are serialized are equal to the
108   * corresponding fields in other. Used for testing.
109   */
110  @Override
111  boolean areSerializedFieldsEqual(ByteArrayComparable other) {
112    if (other == this) {
113      return true;
114    }
115    if (!(other instanceof BinaryComponentComparator)) {
116      return false;
117    }
118    return super.areSerializedFieldsEqual(other);
119  }
120}