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  package org.apache.hadoop.hbase.filter;
20  
21  import java.nio.ByteBuffer;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.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.ByteBufferUtils;
28  import org.apache.hadoop.hbase.util.ByteStringer;
29  import org.apache.hadoop.hbase.util.Bytes;
30  
31  
32  /** Base class for byte array comparators */
33  @InterfaceAudience.Public
34  @InterfaceStability.Stable
35  // TODO Now we are deviating a lot from the actual Comparable<byte[]> what this implements, by
36  // adding special compareTo methods. We have to clean it. Deprecate this class and replace it
37  // with a more generic one which says it compares bytes (not necessary a byte array only)
38  // BytesComparable implements Comparable<Byte> will work?
39  public abstract class ByteArrayComparable implements Comparable<byte[]> {
40  
41    byte[] value;
42  
43    /**
44     * Constructor.
45     * @param value the value to compare against
46     */
47    public ByteArrayComparable(byte [] value) {
48      this.value = value;
49    }
50  
51    public byte[] getValue() {
52      return value;
53    }
54  
55    /**
56     * @return The comparator serialized using pb
57     */
58    public abstract byte [] toByteArray();
59  
60    ComparatorProtos.ByteArrayComparable convert() {
61      ComparatorProtos.ByteArrayComparable.Builder builder =
62        ComparatorProtos.ByteArrayComparable.newBuilder();
63      if (value != null) builder.setValue(ByteStringer.wrap(value));
64      return builder.build();
65    }
66  
67    /**
68     * @param pbBytes A pb serialized {@link ByteArrayComparable} instance
69     * @return An instance of {@link ByteArrayComparable} made from <code>bytes</code>
70     * @throws DeserializationException
71     * @see #toByteArray
72     */
73    public static ByteArrayComparable parseFrom(final byte [] pbBytes)
74    throws DeserializationException {
75      throw new DeserializationException(
76        "parseFrom called on base ByteArrayComparable, but should be called on derived type");
77    }
78  
79    /**
80     * @param other
81     * @return true if and only if the fields of the comparator that are serialized
82     * are equal to the corresponding fields in other.  Used for testing.
83     */
84    boolean areSerializedFieldsEqual(ByteArrayComparable other) {
85      if (other == this) return true;
86  
87      return Bytes.equals(this.getValue(), other.getValue());
88    }
89  
90    @Override
91    public int compareTo(byte [] value) {
92      return compareTo(value, 0, value.length);
93    }
94  
95    /**
96     * Special compareTo method for subclasses, to avoid
97     * copying byte[] unnecessarily.
98     * @param value byte[] to compare
99     * @param offset offset into value
100    * @param length number of bytes to compare
101    * @return a negative integer, zero, or a positive integer as this object
102    *         is less than, equal to, or greater than the specified object.
103    */
104   public abstract int compareTo(byte [] value, int offset, int length);
105 
106   /**
107    * Special compareTo method for subclasses, to avoid copying bytes unnecessarily.
108    * @param value bytes to compare within a ByteBuffer
109    * @param offset offset into value
110    * @param length number of bytes to compare
111    * @return a negative integer, zero, or a positive integer as this object
112    *         is less than, equal to, or greater than the specified object.
113    */
114   public int compareTo(ByteBuffer value, int offset, int length) {
115     // For BC, providing a default implementation here which is doing a bytes copy to a temp byte[]
116     // and calling compareTo(byte[]). Make sure to override this method in subclasses to avoid
117     // copying bytes unnecessarily.
118     byte[] temp = new byte[length];
119     ByteBufferUtils.copyFromBufferToArray(temp, value, offset, 0, length);
120     return compareTo(temp);
121   }
122 }