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 java.nio.ByteBuffer;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.classification.InterfaceStability;
26  import org.apache.hadoop.hbase.exceptions.DeserializationException;
27  import org.apache.hadoop.hbase.protobuf.generated.ComparatorProtos;
28  import org.apache.hadoop.hbase.util.ByteBufferUtils;
29  import org.apache.hadoop.hbase.util.Bytes;
30  
31  import com.google.protobuf.InvalidProtocolBufferException;
32  
33  /**
34   * A comparator which compares against a specified byte array, but only compares
35   * up to the length of this byte array. For the rest it is similar to
36   * {@link BinaryComparator}.
37   */
38  @InterfaceAudience.Public
39  @InterfaceStability.Stable
40  public class BinaryPrefixComparator extends ByteArrayComparable {
41  
42    /**
43     * Constructor
44     * @param value value
45     */
46    public BinaryPrefixComparator(byte[] value) {
47      super(value);
48    }
49  
50    @Override
51    public int compareTo(byte [] value, int offset, int length) {
52      return Bytes.compareTo(this.value, 0, this.value.length, value, offset,
53          this.value.length <= length ? this.value.length : length);
54    }
55  
56    @Override
57    public int compareTo(ByteBuffer value, int offset, int length) {
58      if (this.value.length <= length) {
59        length = this.value.length;
60      }
61      return -(ByteBufferUtils.compareTo(value, offset, length, this.value, 0, this.value.length));
62    }
63  
64    /**
65     * @return The comparator serialized using pb
66     */
67    public byte [] toByteArray() {
68      ComparatorProtos.BinaryPrefixComparator.Builder builder =
69        ComparatorProtos.BinaryPrefixComparator.newBuilder();
70      builder.setComparable(super.convert());
71      return builder.build().toByteArray();
72    }
73  
74    /**
75     * @param pbBytes A pb serialized {@link BinaryPrefixComparator} instance
76     * @return An instance of {@link BinaryPrefixComparator} made from <code>bytes</code>
77     * @throws DeserializationException
78     * @see #toByteArray
79     */
80    public static BinaryPrefixComparator parseFrom(final byte [] pbBytes)
81    throws DeserializationException {
82      ComparatorProtos.BinaryPrefixComparator proto;
83      try {
84        proto = ComparatorProtos.BinaryPrefixComparator.parseFrom(pbBytes);
85      } catch (InvalidProtocolBufferException e) {
86        throw new DeserializationException(e);
87      }
88      return new BinaryPrefixComparator(proto.getComparable().getValue().toByteArray());
89    }
90  
91    /**
92     * @param other
93     * @return true if and only if the fields of the comparator that are serialized
94     * are equal to the corresponding fields in other.  Used for testing.
95     */
96    boolean areSerializedFieldsEqual(ByteArrayComparable other) {
97      if (other == this) return true;
98      if (!(other instanceof BinaryPrefixComparator)) return false;
99  
100     return super.areSerializedFieldsEqual(other);
101   }
102 }