001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.filter; 019 020import java.nio.ByteBuffer; 021import org.apache.hadoop.hbase.exceptions.DeserializationException; 022import org.apache.hadoop.hbase.util.ByteBufferUtils; 023import org.apache.hadoop.hbase.util.Bytes; 024import org.apache.yetus.audience.InterfaceAudience; 025 026/** Base class for byte array comparators */ 027@InterfaceAudience.Public 028// TODO Now we are deviating a lot from the actual Comparable<byte[]> that this implements, by 029// adding special compareTo methods. We have to clean it. Deprecate this class and replace it 030// with a more generic one which says it compares bytes (not necessary a byte array only) 031// BytesComparable implements Comparable<Byte> will work? 032@SuppressWarnings("ComparableType") // Should this move to Comparator usage? 033public abstract class ByteArrayComparable implements Comparable<byte[]> { 034 035 byte[] value; 036 037 /** 038 * Constructor. 039 * @param value the value to compare against 040 */ 041 public ByteArrayComparable(byte[] value) { 042 this.value = value; 043 } 044 045 public byte[] getValue() { 046 return value; 047 } 048 049 /** Returns The comparator serialized using pb */ 050 public abstract byte[] toByteArray(); 051 052 /** 053 * @param pbBytes A pb serialized {@link ByteArrayComparable} instance 054 * @return An instance of {@link ByteArrayComparable} made from <code>bytes</code> 055 * @see #toByteArray 056 */ 057 public static ByteArrayComparable parseFrom(final byte[] pbBytes) 058 throws DeserializationException { 059 throw new DeserializationException( 060 "parseFrom called on base ByteArrayComparable, but should be called on derived type"); 061 } 062 063 /** 064 * @return true if and only if the fields of the comparator that are serialized are equal to the 065 * corresponding fields in other. Used for testing. 066 */ 067 boolean areSerializedFieldsEqual(ByteArrayComparable other) { 068 if (other == this) return true; 069 070 return Bytes.equals(this.getValue(), other.getValue()); 071 } 072 073 @Override 074 public int compareTo(byte[] value) { 075 return compareTo(value, 0, value.length); 076 } 077 078 /** 079 * Special compareTo method for subclasses, to avoid copying byte[] unnecessarily. 080 * @param value byte[] to compare 081 * @param offset offset into value 082 * @param length number of bytes to compare 083 * @return a negative integer, zero, or a positive integer as this object is less than, equal to, 084 * or greater than the specified object. 085 */ 086 public abstract int compareTo(byte[] value, int offset, int length); 087 088 /** 089 * Special compareTo method for subclasses, to avoid copying bytes unnecessarily. 090 * @param value bytes to compare within a ByteBuffer 091 * @param offset offset into value 092 * @param length number of bytes to compare 093 * @return a negative integer, zero, or a positive integer as this object is less than, equal to, 094 * or greater than the specified object. 095 */ 096 public int compareTo(ByteBuffer value, int offset, int length) { 097 // For BC, providing a default implementation here which is doing a bytes copy to a temp byte[] 098 // and calling compareTo(byte[]). Make sure to override this method in subclasses to avoid 099 // copying bytes unnecessarily. 100 byte[] temp = new byte[length]; 101 ByteBufferUtils.copyFromBufferToArray(temp, value, offset, 0, length); 102 return compareTo(temp); 103 } 104}