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