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