View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.filter;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.exceptions.DeserializationException;
29  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
30  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
31  
32  import com.google.protobuf.InvalidProtocolBufferException;
33  
34  /**
35   * <p>
36   * This filter is used to filter based on the column family. It takes an
37   * operator (equal, greater, not equal, etc) and a byte [] comparator for the
38   * column family portion of a key.
39   * </p><p>
40   * This filter can be wrapped with {@link org.apache.hadoop.hbase.filter.WhileMatchFilter} and {@link org.apache.hadoop.hbase.filter.SkipFilter}
41   * to add more control.
42   * </p><p>
43   * Multiple filters can be combined using {@link org.apache.hadoop.hbase.filter.FilterList}.
44   * </p>
45   * If an already known column family is looked for, use {@link org.apache.hadoop.hbase.client.Get#addFamily(byte[])}
46   * directly rather than a filter.
47   */
48  @InterfaceAudience.Public
49  @InterfaceStability.Stable
50  public class FamilyFilter extends CompareFilter {
51  
52    /**
53     * Constructor.
54     *
55     * @param familyCompareOp  the compare op for column family matching
56     * @param familyComparator the comparator for column family matching
57     */
58    public FamilyFilter(final CompareOp familyCompareOp,
59                        final ByteArrayComparable familyComparator) {
60        super(familyCompareOp, familyComparator);
61    }
62  
63    @Override
64    public ReturnCode filterKeyValue(Cell v) {
65      int familyLength = v.getFamilyLength();
66      if (familyLength > 0) {
67        if (doCompare(this.compareOp, this.comparator, v.getFamilyArray(),
68            v.getFamilyOffset(), familyLength)) {
69          return ReturnCode.NEXT_ROW;
70        }
71      }
72      return ReturnCode.INCLUDE;
73    }
74  
75    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
76      ArrayList<?> arguments = CompareFilter.extractArguments(filterArguments);
77      CompareOp compareOp = (CompareOp)arguments.get(0);
78      ByteArrayComparable comparator = (ByteArrayComparable)arguments.get(1);
79      return new FamilyFilter(compareOp, comparator);
80    }
81  
82    /**
83     * @return The filter serialized using pb
84     */
85    public byte [] toByteArray() {
86      FilterProtos.FamilyFilter.Builder builder =
87        FilterProtos.FamilyFilter.newBuilder();
88      builder.setCompareFilter(super.convert());
89      return builder.build().toByteArray();
90    }
91  
92    /**
93     * @param pbBytes A pb serialized {@link FamilyFilter} instance
94     * @return An instance of {@link FamilyFilter} made from <code>bytes</code>
95     * @throws DeserializationException
96     * @see #toByteArray
97     */
98    public static FamilyFilter parseFrom(final byte [] pbBytes)
99    throws DeserializationException {
100     FilterProtos.FamilyFilter proto;
101     try {
102       proto = FilterProtos.FamilyFilter.parseFrom(pbBytes);
103     } catch (InvalidProtocolBufferException e) {
104       throw new DeserializationException(e);
105     }
106     final CompareOp valueCompareOp =
107       CompareOp.valueOf(proto.getCompareFilter().getCompareOp().name());
108     ByteArrayComparable valueComparator = null;
109     try {
110       if (proto.getCompareFilter().hasComparator()) {
111         valueComparator = ProtobufUtil.toComparator(proto.getCompareFilter().getComparator());
112       }
113     } catch (IOException ioe) {
114       throw new DeserializationException(ioe);
115     }
116     return new FamilyFilter(valueCompareOp,valueComparator);
117   }
118 
119   /**
120    * @param other
121    * @return true if and only if the fields of the filter that are serialized
122    * are equal to the corresponding fields in other.  Used for testing.
123    */
124   boolean areSerializedFieldsEqual(Filter o) {
125     if (o == this) return true;
126     if (!(o instanceof FamilyFilter)) return false;
127 
128     FamilyFilter other = (FamilyFilter)o;
129     return super.areSerializedFieldsEqual(other);
130  }
131 }