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