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.io.IOException; 021import java.util.ArrayList; 022import org.apache.hadoop.hbase.Cell; 023import org.apache.hadoop.hbase.CompareOperator; 024import org.apache.hadoop.hbase.exceptions.DeserializationException; 025import org.apache.yetus.audience.InterfaceAudience; 026 027import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException; 028 029import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 030import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos; 031 032/** 033 * This filter is used to filter based on the key. It takes an operator (equal, greater, not equal, 034 * etc) and a byte [] comparator for the row, and column qualifier portions of a key. 035 * <p> 036 * This filter can be wrapped with {@link WhileMatchFilter} to add more control. 037 * <p> 038 * Multiple filters can be combined using {@link FilterList}. 039 * <p> 040 * If an already known row range needs to be scanned, use 041 * {@link org.apache.hadoop.hbase.CellScanner} start and stop rows directly rather than a filter. 042 */ 043@InterfaceAudience.Public 044public class RowFilter extends CompareFilter { 045 046 private boolean filterOutRow = false; 047 048 /** 049 * Constructor. 050 * @param rowCompareOp the compare op for row matching 051 * @param rowComparator the comparator for row matching 052 * @deprecated Since 2.0.0. Will remove in 3.0.0. Use 053 * {@link #RowFilter(CompareOperator, ByteArrayComparable)}} instead. 054 */ 055 @Deprecated 056 public RowFilter(final CompareOp rowCompareOp, final ByteArrayComparable rowComparator) { 057 super(rowCompareOp, rowComparator); 058 } 059 060 /** 061 * Constructor. 062 * @param op the compare op for row matching 063 * @param rowComparator the comparator for row matching 064 */ 065 public RowFilter(final CompareOperator op, final ByteArrayComparable rowComparator) { 066 super(op, rowComparator); 067 } 068 069 @Override 070 public void reset() { 071 this.filterOutRow = false; 072 } 073 074 @Deprecated 075 @Override 076 public ReturnCode filterKeyValue(final Cell c) { 077 return filterCell(c); 078 } 079 080 @Override 081 public ReturnCode filterCell(final Cell v) { 082 if (this.filterOutRow) { 083 return ReturnCode.NEXT_ROW; 084 } 085 return ReturnCode.INCLUDE; 086 } 087 088 @Override 089 public boolean filterRowKey(Cell firstRowCell) { 090 if (compareRow(getCompareOperator(), this.comparator, firstRowCell)) { 091 this.filterOutRow = true; 092 } 093 return this.filterOutRow; 094 } 095 096 @Override 097 public boolean filterRow() { 098 return this.filterOutRow; 099 } 100 101 public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) { 102 @SuppressWarnings("rawtypes") // for arguments 103 ArrayList arguments = CompareFilter.extractArguments(filterArguments); 104 CompareOperator compareOp = (CompareOperator) arguments.get(0); 105 ByteArrayComparable comparator = (ByteArrayComparable) arguments.get(1); 106 return new RowFilter(compareOp, comparator); 107 } 108 109 /** Returns The filter serialized using pb */ 110 @Override 111 public byte[] toByteArray() { 112 FilterProtos.RowFilter.Builder builder = FilterProtos.RowFilter.newBuilder(); 113 builder.setCompareFilter(super.convert()); 114 return builder.build().toByteArray(); 115 } 116 117 /** 118 * Parse a serialized representation of {@link RowFilter} 119 * @param pbBytes A pb serialized {@link RowFilter} instance 120 * @return An instance of {@link RowFilter} made from <code>bytes</code> 121 * @throws DeserializationException if an error occurred 122 * @see #toByteArray 123 */ 124 public static RowFilter parseFrom(final byte[] pbBytes) throws DeserializationException { 125 FilterProtos.RowFilter proto; 126 try { 127 proto = FilterProtos.RowFilter.parseFrom(pbBytes); 128 } catch (InvalidProtocolBufferException e) { 129 throw new DeserializationException(e); 130 } 131 final CompareOperator valueCompareOp = 132 CompareOperator.valueOf(proto.getCompareFilter().getCompareOp().name()); 133 ByteArrayComparable valueComparator = null; 134 try { 135 if (proto.getCompareFilter().hasComparator()) { 136 valueComparator = ProtobufUtil.toComparator(proto.getCompareFilter().getComparator()); 137 } 138 } catch (IOException ioe) { 139 throw new DeserializationException(ioe); 140 } 141 return new RowFilter(valueCompareOp, valueComparator); 142 } 143 144 /** 145 * Returns true if and only if the fields of the filter that are serialized are equal to the 146 * corresponding fields in other. Used for testing. 147 */ 148 @Override 149 boolean areSerializedFieldsEqual(Filter o) { 150 if (o == this) { 151 return true; 152 } 153 if (!(o instanceof RowFilter)) { 154 return false; 155 } 156 return super.areSerializedFieldsEqual(o); 157 } 158 159 @Override 160 public boolean equals(Object obj) { 161 return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj); 162 } 163 164 @Override 165 public int hashCode() { 166 return super.hashCode(); 167 } 168}