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 column qualifier. It takes an operator (equal, 034 * greater, not equal, etc) and a byte [] comparator for the column qualifier portion of a key. 035 * <p> 036 * This filter can be wrapped with {@link WhileMatchFilter} and {@link SkipFilter} to add more 037 * control. 038 * <p> 039 * Multiple filters can be combined using {@link FilterList}. 040 * <p> 041 * If an already known column qualifier is looked for, use 042 * {@link org.apache.hadoop.hbase.client.Get#addColumn} directly rather than a filter. 043 */ 044@InterfaceAudience.Public 045public class QualifierFilter extends CompareFilter { 046 047 /** 048 * Constructor. 049 * @param op the compare op for column qualifier matching 050 * @param qualifierComparator the comparator for column qualifier matching 051 * @deprecated Since 2.0.0. Will be removed in 3.0.0. Use 052 * {@link #QualifierFilter(CompareOperator, ByteArrayComparable)} instead. 053 */ 054 @Deprecated 055 public QualifierFilter(final CompareOp op, final ByteArrayComparable qualifierComparator) { 056 super(op, qualifierComparator); 057 } 058 059 /** 060 * Constructor. 061 * @param op the compare op for column qualifier matching 062 * @param qualifierComparator the comparator for column qualifier matching 063 */ 064 public QualifierFilter(final CompareOperator op, final ByteArrayComparable qualifierComparator) { 065 super(op, qualifierComparator); 066 } 067 068 @Deprecated 069 @Override 070 public ReturnCode filterKeyValue(final Cell c) { 071 return filterCell(c); 072 } 073 074 @Override 075 public ReturnCode filterCell(final Cell c) { 076 if (compareQualifier(getCompareOperator(), this.comparator, c)) { 077 return ReturnCode.SKIP; 078 } 079 return ReturnCode.INCLUDE; 080 } 081 082 public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) { 083 ArrayList<?> arguments = CompareFilter.extractArguments(filterArguments); 084 CompareOperator compareOp = (CompareOperator) arguments.get(0); 085 ByteArrayComparable comparator = (ByteArrayComparable) arguments.get(1); 086 return new QualifierFilter(compareOp, comparator); 087 } 088 089 /** Returns The filter serialized using pb */ 090 @Override 091 public byte[] toByteArray() { 092 FilterProtos.QualifierFilter.Builder builder = FilterProtos.QualifierFilter.newBuilder(); 093 builder.setCompareFilter(super.convert()); 094 return builder.build().toByteArray(); 095 } 096 097 /** 098 * Parse a serialized representation of {@link QualifierFilter} 099 * @param pbBytes A pb serialized {@link QualifierFilter} instance 100 * @return An instance of {@link QualifierFilter} made from <code>bytes</code> 101 * @throws DeserializationException if an error occurred 102 * @see #toByteArray 103 */ 104 public static QualifierFilter parseFrom(final byte[] pbBytes) throws DeserializationException { 105 FilterProtos.QualifierFilter proto; 106 try { 107 proto = FilterProtos.QualifierFilter.parseFrom(pbBytes); 108 } catch (InvalidProtocolBufferException e) { 109 throw new DeserializationException(e); 110 } 111 final CompareOperator valueCompareOp = 112 CompareOperator.valueOf(proto.getCompareFilter().getCompareOp().name()); 113 ByteArrayComparable valueComparator = null; 114 try { 115 if (proto.getCompareFilter().hasComparator()) { 116 valueComparator = ProtobufUtil.toComparator(proto.getCompareFilter().getComparator()); 117 } 118 } catch (IOException ioe) { 119 throw new DeserializationException(ioe); 120 } 121 return new QualifierFilter(valueCompareOp, valueComparator); 122 } 123 124 /** 125 * Returns true if and only if the fields of the filter that are serialized are equal to the 126 * corresponding fields in other. Used for testing. 127 */ 128 @Override 129 boolean areSerializedFieldsEqual(Filter o) { 130 if (o == this) { 131 return true; 132 } 133 if (!(o instanceof QualifierFilter)) { 134 return false; 135 } 136 return super.areSerializedFieldsEqual(o); 137 } 138 139 @Override 140 public boolean equals(Object obj) { 141 return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj); 142 } 143 144 @Override 145 public int hashCode() { 146 return super.hashCode(); 147 } 148}