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