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 */ 019 020package org.apache.hadoop.hbase.filter; 021 022import static org.apache.hadoop.hbase.util.Bytes.len; 023 024import java.io.IOException; 025import java.util.ArrayList; 026import java.util.Objects; 027 028import org.apache.hadoop.hbase.Cell; 029import org.apache.hadoop.hbase.CellUtil; 030import org.apache.hadoop.hbase.PrivateCellUtil; 031import org.apache.yetus.audience.InterfaceAudience; 032import org.apache.hadoop.hbase.exceptions.DeserializationException; 033import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException; 034import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations; 035import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos; 036import org.apache.hadoop.hbase.util.Bytes; 037 038import org.apache.hbase.thirdparty.com.google.common.base.Preconditions; 039 040/** 041 * This filter is used for selecting only those keys with columns that are 042 * between minColumn to maxColumn. For example, if minColumn is 'an', and 043 * maxColumn is 'be', it will pass keys with columns like 'ana', 'bad', but not 044 * keys with columns like 'bed', 'eye' 045 * 046 * If minColumn is null, there is no lower bound. If maxColumn is null, there is 047 * no upper bound. 048 * 049 * minColumnInclusive and maxColumnInclusive specify if the ranges are inclusive 050 * or not. 051 */ 052@InterfaceAudience.Public 053public class ColumnRangeFilter extends FilterBase { 054 protected byte[] minColumn = null; 055 protected boolean minColumnInclusive = true; 056 protected byte[] maxColumn = null; 057 protected boolean maxColumnInclusive = false; 058 059 /** 060 * Create a filter to select those keys with columns that are between minColumn 061 * and maxColumn. 062 * @param minColumn minimum value for the column range. If if it's null, 063 * there is no lower bound. 064 * @param minColumnInclusive if true, include minColumn in the range. 065 * @param maxColumn maximum value for the column range. If it's null, 066 * @param maxColumnInclusive if true, include maxColumn in the range. 067 * there is no upper bound. 068 */ 069 public ColumnRangeFilter(final byte[] minColumn, boolean minColumnInclusive, 070 final byte[] maxColumn, boolean maxColumnInclusive) { 071 this.minColumn = minColumn; 072 this.minColumnInclusive = minColumnInclusive; 073 this.maxColumn = maxColumn; 074 this.maxColumnInclusive = maxColumnInclusive; 075 } 076 077 /** 078 * @return if min column range is inclusive. 079 */ 080 public boolean isMinColumnInclusive() { 081 return minColumnInclusive; 082 } 083 084 /** 085 * @return if max column range is inclusive. 086 */ 087 public boolean isMaxColumnInclusive() { 088 return maxColumnInclusive; 089 } 090 091 /** 092 * @return the min column range for the filter 093 */ 094 public byte[] getMinColumn() { 095 return this.minColumn; 096 } 097 098 /** 099 * @return true if min column is inclusive, false otherwise 100 */ 101 public boolean getMinColumnInclusive() { 102 return this.minColumnInclusive; 103 } 104 105 /** 106 * @return the max column range for the filter 107 */ 108 public byte[] getMaxColumn() { 109 return this.maxColumn; 110 } 111 112 /** 113 * @return true if max column is inclusive, false otherwise 114 */ 115 public boolean getMaxColumnInclusive() { 116 return this.maxColumnInclusive; 117 } 118 119 @Override 120 public boolean filterRowKey(Cell cell) throws IOException { 121 // Impl in FilterBase might do unnecessary copy for Off heap backed Cells. 122 return false; 123 } 124 125 @Override 126 @Deprecated 127 public ReturnCode filterKeyValue(final Cell c) { 128 return filterCell(c); 129 } 130 131 @Override 132 public ReturnCode filterCell(final Cell c) { 133 int cmpMin = 1; 134 135 if (this.minColumn != null) { 136 cmpMin = CellUtil.compareQualifiers(c, this.minColumn, 0, this.minColumn.length); 137 } 138 139 if (cmpMin < 0) { 140 return ReturnCode.SEEK_NEXT_USING_HINT; 141 } 142 143 if (!this.minColumnInclusive && cmpMin == 0) { 144 return ReturnCode.NEXT_COL; 145 } 146 147 if (this.maxColumn == null) { 148 return ReturnCode.INCLUDE; 149 } 150 151 int cmpMax = CellUtil.compareQualifiers(c, this.maxColumn, 0, this.maxColumn.length); 152 153 if ((this.maxColumnInclusive && cmpMax <= 0) || (!this.maxColumnInclusive && cmpMax < 0)) { 154 return ReturnCode.INCLUDE; 155 } 156 157 return ReturnCode.NEXT_ROW; 158 } 159 160 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) { 161 Preconditions.checkArgument(filterArguments.size() == 4, 162 "Expected 4 but got: %s", filterArguments.size()); 163 byte [] minColumn = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0)); 164 boolean minColumnInclusive = ParseFilter.convertByteArrayToBoolean(filterArguments.get(1)); 165 byte [] maxColumn = ParseFilter.removeQuotesFromByteArray(filterArguments.get(2)); 166 boolean maxColumnInclusive = ParseFilter.convertByteArrayToBoolean(filterArguments.get(3)); 167 168 if (minColumn.length == 0) 169 minColumn = null; 170 if (maxColumn.length == 0) 171 maxColumn = null; 172 return new ColumnRangeFilter(minColumn, minColumnInclusive, 173 maxColumn, maxColumnInclusive); 174 } 175 176 /** 177 * @return The filter serialized using pb 178 */ 179 @Override 180 public byte [] toByteArray() { 181 FilterProtos.ColumnRangeFilter.Builder builder = 182 FilterProtos.ColumnRangeFilter.newBuilder(); 183 if (this.minColumn != null) builder.setMinColumn( 184 UnsafeByteOperations.unsafeWrap(this.minColumn)); 185 builder.setMinColumnInclusive(this.minColumnInclusive); 186 if (this.maxColumn != null) builder.setMaxColumn( 187 UnsafeByteOperations.unsafeWrap(this.maxColumn)); 188 builder.setMaxColumnInclusive(this.maxColumnInclusive); 189 return builder.build().toByteArray(); 190 } 191 192 /** 193 * @param pbBytes A pb serialized {@link ColumnRangeFilter} instance 194 * @return An instance of {@link ColumnRangeFilter} made from <code>bytes</code> 195 * @throws DeserializationException 196 * @see #toByteArray 197 */ 198 public static ColumnRangeFilter parseFrom(final byte [] pbBytes) 199 throws DeserializationException { 200 FilterProtos.ColumnRangeFilter proto; 201 try { 202 proto = FilterProtos.ColumnRangeFilter.parseFrom(pbBytes); 203 } catch (InvalidProtocolBufferException e) { 204 throw new DeserializationException(e); 205 } 206 return new ColumnRangeFilter(proto.hasMinColumn()?proto.getMinColumn().toByteArray():null, 207 proto.getMinColumnInclusive(),proto.hasMaxColumn()?proto.getMaxColumn().toByteArray():null, 208 proto.getMaxColumnInclusive()); 209 } 210 211 /** 212 * @param o filter to serialize. 213 * @return true if and only if the fields of the filter that are serialized are equal to the 214 * corresponding fields in other. Used for testing. 215 */ 216 @Override 217 boolean areSerializedFieldsEqual(Filter o) { 218 if (o == this) { 219 return true; 220 } 221 if (!(o instanceof ColumnRangeFilter)) { 222 return false; 223 } 224 ColumnRangeFilter other = (ColumnRangeFilter) o; 225 return Bytes.equals(this.getMinColumn(), other.getMinColumn()) 226 && this.getMinColumnInclusive() == other.getMinColumnInclusive() 227 && Bytes.equals(this.getMaxColumn(), other.getMaxColumn()) 228 && this.getMaxColumnInclusive() == other.getMaxColumnInclusive(); 229 } 230 231 @Override 232 public Cell getNextCellHint(Cell cell) { 233 return PrivateCellUtil.createFirstOnRowCol(cell, this.minColumn, 0, len(this.minColumn)); 234 } 235 236 @Override 237 public String toString() { 238 return this.getClass().getSimpleName() + " " 239 + (this.minColumnInclusive ? "[" : "(") + Bytes.toStringBinary(this.minColumn) 240 + ", " + Bytes.toStringBinary(this.maxColumn) 241 + (this.maxColumnInclusive ? "]" : ")"); 242 } 243 244 @Override 245 public boolean equals(Object obj) { 246 return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj); 247 } 248 249 @Override 250 public int hashCode() { 251 return Objects.hash(Bytes.hashCode(getMinColumn()), getMinColumnInclusive(), 252 Bytes.hashCode(getMaxColumn()), getMaxColumnInclusive()); 253 } 254}