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