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