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