View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.filter;
21  
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.classification.InterfaceStability;
24  import org.apache.hadoop.hbase.exceptions.DeserializationException;
25  import org.apache.hadoop.hbase.protobuf.generated.ComparatorProtos;
26  import org.apache.hadoop.hbase.util.Bytes;
27  
28  import com.google.protobuf.InvalidProtocolBufferException;
29  
30  /**
31   * A comparator which compares against a specified byte array, but only compares
32   * up to the length of this byte array. For the rest it is similar to
33   * {@link BinaryComparator}.
34   */
35  @InterfaceAudience.Public
36  @InterfaceStability.Stable
37  public class BinaryPrefixComparator extends ByteArrayComparable {
38  
39    /**
40     * Constructor
41     * @param value value
42     */
43    public BinaryPrefixComparator(byte[] value) {
44      super(value);
45    }
46  
47    @Override
48    public int compareTo(byte [] value, int offset, int length) {
49      return Bytes.compareTo(this.value, 0, this.value.length, value, offset,
50          this.value.length <= length ? this.value.length : length);
51    }
52  
53    /**
54     * @return The comparator serialized using pb
55     */
56    public byte [] toByteArray() {
57      ComparatorProtos.BinaryPrefixComparator.Builder builder =
58        ComparatorProtos.BinaryPrefixComparator.newBuilder();
59      builder.setComparable(super.convert());
60      return builder.build().toByteArray();
61    }
62  
63    /**
64     * @param pbBytes A pb serialized {@link BinaryPrefixComparator} instance
65     * @return An instance of {@link BinaryPrefixComparator} made from <code>bytes</code>
66     * @throws DeserializationException
67     * @see #toByteArray
68     */
69    public static BinaryPrefixComparator parseFrom(final byte [] pbBytes)
70    throws DeserializationException {
71      ComparatorProtos.BinaryPrefixComparator proto;
72      try {
73        proto = ComparatorProtos.BinaryPrefixComparator.parseFrom(pbBytes);
74      } catch (InvalidProtocolBufferException e) {
75        throw new DeserializationException(e);
76      }
77      return new BinaryPrefixComparator(proto.getComparable().getValue().toByteArray());
78    }
79  
80    /**
81     * @param other
82     * @return true if and only if the fields of the comparator that are serialized
83     * are equal to the corresponding fields in other.  Used for testing.
84     */
85    boolean areSerializedFieldsEqual(ByteArrayComparable other) {
86      if (other == this) return true;
87      if (!(other instanceof BinaryPrefixComparator)) return false;
88  
89      return super.areSerializedFieldsEqual(other);
90    }
91  }