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 com.google.protobuf.InvalidProtocolBufferException;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.KeyValue;
26  import org.apache.hadoop.hbase.exceptions.DeserializationException;
27  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
28  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
29  
30  import java.io.IOException;
31  import java.util.ArrayList;
32  
33  /**
34   * This filter is used to filter based on the column family. It takes an
35   * operator (equal, greater, not equal, etc) and a byte [] comparator for the
36   * column family portion of a key.
37   * <p/>
38   * This filter can be wrapped with {@link org.apache.hadoop.hbase.filter.WhileMatchFilter} and {@link org.apache.hadoop.hbase.filter.SkipFilter}
39   * to add more control.
40   * <p/>
41   * Multiple filters can be combined using {@link org.apache.hadoop.hbase.filter.FilterList}.
42   * <p/>
43   * If an already known column family is looked for, use {@link org.apache.hadoop.hbase.client.Get#addFamily(byte[])}
44   * directly rather than a filter.
45   */
46  @InterfaceAudience.Public
47  @InterfaceStability.Stable
48  public class FamilyFilter extends CompareFilter {
49  
50    /**
51     * Constructor.
52     *
53     * @param familyCompareOp  the compare op for column family matching
54     * @param familyComparator the comparator for column family matching
55     */
56    public FamilyFilter(final CompareOp familyCompareOp,
57                        final ByteArrayComparable familyComparator) {
58        super(familyCompareOp, familyComparator);
59    }
60  
61    @Override
62    public ReturnCode filterKeyValue(KeyValue v) {
63      int familyLength = v.getFamilyLength();
64      if (familyLength > 0) {
65        if (doCompare(this.compareOp, this.comparator, v.getBuffer(),
66            v.getFamilyOffset(), familyLength)) {
67          return ReturnCode.SKIP;
68        }
69      }
70      return ReturnCode.INCLUDE;
71    }
72  
73    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
74      ArrayList arguments = CompareFilter.extractArguments(filterArguments);
75      CompareOp compareOp = (CompareOp)arguments.get(0);
76      ByteArrayComparable comparator = (ByteArrayComparable)arguments.get(1);
77      return new FamilyFilter(compareOp, comparator);
78    }
79  
80    /**
81     * @return The filter serialized using pb
82     */
83    public byte [] toByteArray() {
84      FilterProtos.FamilyFilter.Builder builder =
85        FilterProtos.FamilyFilter.newBuilder();
86      builder.setCompareFilter(super.convert());
87      return builder.build().toByteArray();
88    }
89  
90    /**
91     * @param pbBytes A pb serialized {@link FamilyFilter} instance
92     * @return An instance of {@link FamilyFilter} made from <code>bytes</code>
93     * @throws DeserializationException
94     * @see #toByteArray
95     */
96    public static FamilyFilter parseFrom(final byte [] pbBytes)
97    throws DeserializationException {
98      FilterProtos.FamilyFilter proto;
99      try {
100       proto = FilterProtos.FamilyFilter.parseFrom(pbBytes);
101     } catch (InvalidProtocolBufferException e) {
102       throw new DeserializationException(e);
103     }
104     final CompareOp valueCompareOp =
105       CompareOp.valueOf(proto.getCompareFilter().getCompareOp().name());
106     ByteArrayComparable valueComparator = null;
107     try {
108       if (proto.getCompareFilter().hasComparator()) {
109         valueComparator = ProtobufUtil.toComparator(proto.getCompareFilter().getComparator());
110       }
111     } catch (IOException ioe) {
112       throw new DeserializationException(ioe);
113     }
114     return new FamilyFilter(valueCompareOp,valueComparator);
115   }
116 
117   /**
118    * @param other
119    * @return true if and only if the fields of the filter that are serialized
120    * are equal to the corresponding fields in other.  Used for testing.
121    */
122   boolean areSerializedFieldsEqual(Filter o) {
123     if (o == this) return true;
124     if (!(o instanceof FamilyFilter)) return false;
125 
126     FamilyFilter other = (FamilyFilter)o;
127     return super.areSerializedFieldsEqual(other);
128  }
129 }