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.HBaseZeroCopyByteString;
23  import com.google.protobuf.InvalidProtocolBufferException;
24  
25  import org.apache.hadoop.classification.InterfaceAudience;
26  import org.apache.hadoop.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.Cell;
28  import org.apache.hadoop.hbase.KeyValue;
29  import org.apache.hadoop.hbase.KeyValueUtil;
30  import org.apache.hadoop.hbase.exceptions.DeserializationException;
31  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
32  import org.apache.hadoop.hbase.util.Bytes;
33  
34  import java.util.Set;
35  import java.util.TreeSet;
36  
37  /**
38   * The filter looks for the given columns in KeyValue. Once there is a match for
39   * any one of the columns, it returns ReturnCode.NEXT_ROW for remaining
40   * KeyValues in the row.
41   * <p>
42   * Note : It may emit KVs which do not have the given columns in them, if
43   * these KVs happen to occur before a KV which does have a match. Given this
44   * caveat, this filter is only useful for special cases
45   * like {@link org.apache.hadoop.hbase.mapreduce.RowCounter}.
46   * <p>
47   */
48  @InterfaceAudience.Public
49  @InterfaceStability.Stable
50  public class FirstKeyValueMatchingQualifiersFilter extends FirstKeyOnlyFilter {
51  
52    private Set<byte []> qualifiers;
53  
54    /**
55     * Constructor which takes a set of columns. As soon as first KeyValue
56     * matching any of these columns is found, filter moves to next row.
57     * 
58     * @param qualifiers the set of columns to me matched.
59     */
60    public FirstKeyValueMatchingQualifiersFilter(Set<byte []> qualifiers) {
61      this.qualifiers = qualifiers;
62    }
63  
64    @Override
65    public ReturnCode filterKeyValue(Cell v) {
66      if (hasFoundKV()) {
67        return ReturnCode.NEXT_ROW;
68      } else if (hasOneMatchingQualifier(v)) {
69        setFoundKV(true);
70      }
71      return ReturnCode.INCLUDE;
72    }
73  
74    private boolean hasOneMatchingQualifier(Cell v) {
75      for (byte[] q : qualifiers) {
76        // TODO get rid of this by adding matching qualifier to interface.
77        KeyValue kv = KeyValueUtil.ensureKeyValue(v);
78        if (kv.matchingQualifier(q)) {
79          return true;
80        }
81      }
82      return false;
83    }
84  
85    /**
86     * @return The filter serialized using pb
87     */
88    public byte [] toByteArray() {
89      FilterProtos.FirstKeyValueMatchingQualifiersFilter.Builder builder =
90        FilterProtos.FirstKeyValueMatchingQualifiersFilter.newBuilder();
91      for (byte[] qualifier : qualifiers) {
92        if (qualifier != null) builder.addQualifiers(HBaseZeroCopyByteString.wrap(qualifier));
93      }
94      return builder.build().toByteArray();
95    }
96  
97    /**
98     * @param pbBytes A pb serialized {@link FirstKeyValueMatchingQualifiersFilter} instance
99     * @return An instance of {@link FirstKeyValueMatchingQualifiersFilter} made from <code>bytes</code>
100    * @throws DeserializationException
101    * @see #toByteArray
102    */
103   public static FirstKeyValueMatchingQualifiersFilter parseFrom(final byte [] pbBytes)
104   throws DeserializationException {
105     FilterProtos.FirstKeyValueMatchingQualifiersFilter proto;
106     try {
107       proto = FilterProtos.FirstKeyValueMatchingQualifiersFilter.parseFrom(pbBytes);
108     } catch (InvalidProtocolBufferException e) {
109       throw new DeserializationException(e);
110     }
111 
112     TreeSet<byte []> qualifiers = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
113     for (ByteString qualifier : proto.getQualifiersList()) {
114       qualifiers.add(qualifier.toByteArray());
115     }
116     return new FirstKeyValueMatchingQualifiersFilter(qualifiers);
117   }
118 
119   /**
120    * @param other
121    * @return true if and only if the fields of the filter that are serialized
122    * are equal to the corresponding fields in other.  Used for testing.
123    */
124   boolean areSerializedFieldsEqual(Filter o) {
125     if (o == this) return true;
126     if (!(o instanceof FirstKeyValueMatchingQualifiersFilter)) return false;
127 
128     FirstKeyValueMatchingQualifiersFilter other = (FirstKeyValueMatchingQualifiersFilter)o;
129     return this.qualifiers.equals(other.qualifiers);
130   }
131 }