View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.filter;
20  
21  import com.google.protobuf.ByteString;
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.generated.FilterProtos;
28  import org.apache.hadoop.hbase.util.Bytes;
29  
30  import java.util.Set;
31  import java.util.TreeSet;
32  
33  /**
34   * The filter looks for the given columns in KeyValue. Once there is a match for
35   * any one of the columns, it returns ReturnCode.NEXT_ROW for remaining
36   * KeyValues in the row.
37   * <p>
38   * Note : It may emit KVs which do not have the given columns in them, if
39   * these KVs happen to occur before a KV which does have a match. Given this
40   * caveat, this filter is only useful for special cases
41   * like {@link org.apache.hadoop.hbase.mapreduce.RowCounter}.
42   * <p>
43   */
44  @InterfaceAudience.Public
45  @InterfaceStability.Stable
46  public class FirstKeyValueMatchingQualifiersFilter extends FirstKeyOnlyFilter {
47  
48    private Set<byte []> qualifiers;
49  
50    /**
51     * Constructor which takes a set of columns. As soon as first KeyValue
52     * matching any of these columns is found, filter moves to next row.
53     * 
54     * @param qualifiers the set of columns to me matched.
55     */
56    public FirstKeyValueMatchingQualifiersFilter(Set<byte []> qualifiers) {
57      this.qualifiers = qualifiers;
58    }
59  
60    public ReturnCode filterKeyValue(KeyValue v) {
61      if (hasFoundKV()) {
62        return ReturnCode.NEXT_ROW;
63      } else if (hasOneMatchingQualifier(v)) {
64        setFoundKV(true);
65      }
66      return ReturnCode.INCLUDE;
67    }
68  
69    private boolean hasOneMatchingQualifier(KeyValue v) {
70      for (byte[] q : qualifiers) {
71        if (v.matchingQualifier(q)) {
72          return true;
73        }
74      }
75      return false;
76    }
77  
78    /**
79     * @return The filter serialized using pb
80     */
81    public byte [] toByteArray() {
82      FilterProtos.FirstKeyValueMatchingQualifiersFilter.Builder builder =
83        FilterProtos.FirstKeyValueMatchingQualifiersFilter.newBuilder();
84      for (byte[] qualifier : qualifiers) {
85        if (qualifier != null) builder.addQualifiers(ByteString.copyFrom(qualifier));
86      }
87      return builder.build().toByteArray();
88    }
89  
90    /**
91     * @param pbBytes A pb serialized {@link FirstKeyValueMatchingQualifiersFilter} instance
92     * @return An instance of {@link FirstKeyValueMatchingQualifiersFilter} made from <code>bytes</code>
93     * @throws DeserializationException
94     * @see #toByteArray
95     */
96    public static FirstKeyValueMatchingQualifiersFilter parseFrom(final byte [] pbBytes)
97    throws DeserializationException {
98      FilterProtos.FirstKeyValueMatchingQualifiersFilter proto;
99      try {
100       proto = FilterProtos.FirstKeyValueMatchingQualifiersFilter.parseFrom(pbBytes);
101     } catch (InvalidProtocolBufferException e) {
102       throw new DeserializationException(e);
103     }
104 
105     TreeSet<byte []> qualifiers = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
106     for (ByteString qualifier : proto.getQualifiersList()) {
107       qualifiers.add(qualifier.toByteArray());
108     }
109     return new FirstKeyValueMatchingQualifiersFilter(qualifiers);
110   }
111 
112   /**
113    * @param other
114    * @return true if and only if the fields of the filter that are serialized
115    * are equal to the corresponding fields in other.  Used for testing.
116    */
117   boolean areSerializedFieldsEqual(Filter o) {
118     if (o == this) return true;
119     if (!(o instanceof FirstKeyValueMatchingQualifiersFilter)) return false;
120 
121     FirstKeyValueMatchingQualifiersFilter other = (FirstKeyValueMatchingQualifiersFilter)o;
122     return this.qualifiers.equals(other.qualifiers);
123   }
124 }