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 binary comparator which lexicographically compares against the specified
32   * byte array using {@link org.apache.hadoop.hbase.util.Bytes#compareTo(byte[], byte[])}.
33   */
34  @InterfaceAudience.Public
35  @InterfaceStability.Stable
36  public class BinaryComparator extends ByteArrayComparable {
37  
38    /**
39     * Constructor
40     * @param value value
41     */
42    public BinaryComparator(byte[] value) {
43      super(value);
44    }
45  
46    @Override
47    public int compareTo(byte [] value, int offset, int length) {
48      return Bytes.compareTo(this.value, 0, this.value.length, value, offset, length);
49    }
50  
51    /**
52     * @return The comparator serialized using pb
53     */
54    public byte [] toByteArray() {
55      ComparatorProtos.BinaryComparator.Builder builder =
56        ComparatorProtos.BinaryComparator.newBuilder();
57      builder.setComparable(super.convert());
58      return builder.build().toByteArray();
59    }
60  
61    /**
62     * @param pbBytes A pb serialized {@link BinaryComparator} instance
63     * @return An instance of {@link BinaryComparator} made from <code>bytes</code>
64     * @throws DeserializationException
65     * @see #toByteArray
66     */
67    public static BinaryComparator parseFrom(final byte [] pbBytes)
68    throws DeserializationException {
69      ComparatorProtos.BinaryComparator proto;
70      try {
71        proto = ComparatorProtos.BinaryComparator.parseFrom(pbBytes);
72      } catch (InvalidProtocolBufferException e) {
73        throw new DeserializationException(e);
74      }
75      return new BinaryComparator(proto.getComparable().getValue().toByteArray());
76    }
77  
78    /**
79     * @param other
80     * @return true if and only if the fields of the comparator that are serialized
81     * are equal to the corresponding fields in other.  Used for testing.
82     */
83    boolean areSerializedFieldsEqual(ByteArrayComparable other) {
84      if (other == this) return true;
85      if (!(other instanceof BinaryComparator)) return false;
86  
87      return super.areSerializedFieldsEqual(other);
88    }
89  }