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