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