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 * @param pbBytes A pb serialized {@link QualifierFilter} instance 099 * @return An instance of {@link QualifierFilter} made from <code>bytes</code> 100 * @throws org.apache.hadoop.hbase.exceptions.DeserializationException 101 * @see #toByteArray 102 */ 103 public static QualifierFilter parseFrom(final byte[] pbBytes) throws DeserializationException { 104 FilterProtos.QualifierFilter proto; 105 try { 106 proto = FilterProtos.QualifierFilter.parseFrom(pbBytes); 107 } catch (InvalidProtocolBufferException e) { 108 throw new DeserializationException(e); 109 } 110 final CompareOperator valueCompareOp = 111 CompareOperator.valueOf(proto.getCompareFilter().getCompareOp().name()); 112 ByteArrayComparable valueComparator = null; 113 try { 114 if (proto.getCompareFilter().hasComparator()) { 115 valueComparator = ProtobufUtil.toComparator(proto.getCompareFilter().getComparator()); 116 } 117 } catch (IOException ioe) { 118 throw new DeserializationException(ioe); 119 } 120 return new QualifierFilter(valueCompareOp, valueComparator); 121 } 122 123 /** 124 * @return true if and only if the fields of the filter that are serialized are equal to the 125 * corresponding fields in other. Used for testing. 126 */ 127 @Override 128 boolean areSerializedFieldsEqual(Filter o) { 129 if (o == this) return true; 130 if (!(o instanceof QualifierFilter)) return false; 131 132 return super.areSerializedFieldsEqual(o); 133 } 134 135 @Override 136 public boolean equals(Object obj) { 137 return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj); 138 } 139 140 @Override 141 public int hashCode() { 142 return super.hashCode(); 143 } 144}