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 * Constructor. 048 * @param op the compare op for column qualifier matching 049 * @param qualifierComparator the comparator for column qualifier matching 050 */ 051 public QualifierFilter(final CompareOperator op, final ByteArrayComparable qualifierComparator) { 052 super(op, qualifierComparator); 053 } 054 055 @Override 056 public ReturnCode filterCell(final Cell c) { 057 if (compareQualifier(getCompareOperator(), this.comparator, c)) { 058 return ReturnCode.SKIP; 059 } 060 return ReturnCode.INCLUDE; 061 } 062 063 public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) { 064 ArrayList<?> arguments = CompareFilter.extractArguments(filterArguments); 065 CompareOperator compareOp = (CompareOperator) arguments.get(0); 066 ByteArrayComparable comparator = (ByteArrayComparable) arguments.get(1); 067 return new QualifierFilter(compareOp, comparator); 068 } 069 070 /** Returns The filter serialized using pb */ 071 @Override 072 public byte[] toByteArray() { 073 FilterProtos.QualifierFilter.Builder builder = FilterProtos.QualifierFilter.newBuilder(); 074 builder.setCompareFilter(super.convert()); 075 return builder.build().toByteArray(); 076 } 077 078 /** 079 * Parse a serialized representation of {@link QualifierFilter} 080 * @param pbBytes A pb serialized {@link QualifierFilter} instance 081 * @return An instance of {@link QualifierFilter} made from <code>bytes</code> 082 * @throws DeserializationException if an error occurred 083 * @see #toByteArray 084 */ 085 public static QualifierFilter parseFrom(final byte[] pbBytes) throws DeserializationException { 086 FilterProtos.QualifierFilter proto; 087 try { 088 proto = FilterProtos.QualifierFilter.parseFrom(pbBytes); 089 } catch (InvalidProtocolBufferException e) { 090 throw new DeserializationException(e); 091 } 092 final CompareOperator valueCompareOp = 093 CompareOperator.valueOf(proto.getCompareFilter().getCompareOp().name()); 094 ByteArrayComparable valueComparator = null; 095 try { 096 if (proto.getCompareFilter().hasComparator()) { 097 valueComparator = ProtobufUtil.toComparator(proto.getCompareFilter().getComparator()); 098 } 099 } catch (IOException ioe) { 100 throw new DeserializationException(ioe); 101 } 102 return new QualifierFilter(valueCompareOp, valueComparator); 103 } 104 105 /** 106 * Returns true if and only if the fields of the filter that are serialized are equal to the 107 * corresponding fields in other. Used for testing. 108 */ 109 @Override 110 boolean areSerializedFieldsEqual(Filter o) { 111 if (o == this) { 112 return true; 113 } 114 if (!(o instanceof QualifierFilter)) { 115 return false; 116 } 117 return super.areSerializedFieldsEqual(o); 118 } 119 120 @Override 121 public boolean equals(Object obj) { 122 return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj); 123 } 124 125 @Override 126 public int hashCode() { 127 return super.hashCode(); 128 } 129}