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