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   * This filter is used to filter based on the column qualifier. It takes an
36   * operator (equal, greater, not equal, etc) and a byte [] comparator for the
37   * column qualifier portion of a key.
38   * <p>
39   * This filter can be wrapped with {@link WhileMatchFilter} and {@link SkipFilter}
40   * to add more control.
41   * <p>
42   * Multiple filters can be combined using {@link FilterList}.
43   * <p>
44   * If an already known column qualifier is looked for, 
45   * use {@link org.apache.hadoop.hbase.client.Get#addColumn}
46   * directly rather than a filter.
47   */
48  @InterfaceAudience.Public
49  @InterfaceStability.Stable
50  public class QualifierFilter extends CompareFilter {
51  
52    /**
53     * Constructor.
54     * @param op the compare op for column qualifier matching
55     * @param qualifierComparator the comparator for column qualifier matching
56     */
57    public QualifierFilter(final CompareOp op,
58        final ByteArrayComparable qualifierComparator) {
59      super(op, qualifierComparator);
60    }
61  
62    @Override
63    public ReturnCode filterKeyValue(Cell v) {
64      int qualifierLength = v.getQualifierLength();
65      if (qualifierLength > 0) {
66        if (compareQualifier(this.compareOp, this.comparator, v)) {
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 QualifierFilter(compareOp, comparator);
78    }
79  
80    /**
81     * @return The filter serialized using pb
82     */
83    public byte [] toByteArray() {
84      FilterProtos.QualifierFilter.Builder builder =
85        FilterProtos.QualifierFilter.newBuilder();
86      builder.setCompareFilter(super.convert());
87      return builder.build().toByteArray();
88    }
89  
90    /**
91     * @param pbBytes A pb serialized {@link QualifierFilter} instance
92     * @return An instance of {@link QualifierFilter} made from <code>bytes</code>
93     * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
94     * @see #toByteArray
95     */
96    public static QualifierFilter parseFrom(final byte [] pbBytes)
97    throws DeserializationException {
98      FilterProtos.QualifierFilter proto;
99      try {
100       proto = FilterProtos.QualifierFilter.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 QualifierFilter(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 QualifierFilter)) return false;
125 
126     return super.areSerializedFieldsEqual(o);
127   }
128 }