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  package org.apache.hadoop.hbase.filter;
20  
21  
22  import com.google.common.base.Preconditions;
23  import com.google.protobuf.InvalidProtocolBufferException;
24  import org.apache.hadoop.classification.InterfaceAudience;
25  import org.apache.hadoop.classification.InterfaceStability;
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.exceptions.DeserializationException;
28  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
29  
30  import java.util.ArrayList;
31  
32  /**
33   * A filter that will only return the key component of each KV (the value will
34   * be rewritten as empty).
35   * <p>
36   * This filter can be used to grab all of the keys without having to also grab
37   * the values.
38   */
39  @InterfaceAudience.Public
40  @InterfaceStability.Stable
41  public class KeyOnlyFilter extends FilterBase {
42  
43    boolean lenAsVal;
44    public KeyOnlyFilter() { this(false); }
45    public KeyOnlyFilter(boolean lenAsVal) { this.lenAsVal = lenAsVal; }
46  
47    @Override
48    public KeyValue transform(KeyValue kv) {
49      return kv.createKeyOnly(this.lenAsVal);
50    }
51  
52    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
53      Preconditions.checkArgument((filterArguments.size() == 0 || filterArguments.size() == 1),
54                                  "Expected: 0 or 1 but got: %s", filterArguments.size());
55      KeyOnlyFilter filter = new KeyOnlyFilter();
56      if (filterArguments.size() == 1) {
57        filter.lenAsVal = ParseFilter.convertByteArrayToBoolean(filterArguments.get(0));
58      }
59      return filter;
60    }
61  
62    /**
63     * @return The filter serialized using pb
64     */
65    public byte [] toByteArray() {
66      FilterProtos.KeyOnlyFilter.Builder builder =
67        FilterProtos.KeyOnlyFilter.newBuilder();
68      builder.setLenAsVal(this.lenAsVal);
69      return builder.build().toByteArray();
70    }
71  
72    /**
73     * @param pbBytes A pb serialized {@link KeyOnlyFilter} instance
74     * @return An instance of {@link KeyOnlyFilter} made from <code>bytes</code>
75     * @throws DeserializationException
76     * @see #toByteArray
77     */
78    public static KeyOnlyFilter parseFrom(final byte [] pbBytes)
79    throws DeserializationException {
80      FilterProtos.KeyOnlyFilter proto;
81      try {
82        proto = FilterProtos.KeyOnlyFilter.parseFrom(pbBytes);
83      } catch (InvalidProtocolBufferException e) {
84        throw new DeserializationException(e);
85      }
86      return new KeyOnlyFilter(proto.getLenAsVal());
87    }
88  
89    /**
90     * @param other
91     * @return true if and only if the fields of the filter that are serialized
92     * are equal to the corresponding fields in other.  Used for testing.
93     */
94    boolean areSerializedFieldsEqual(Filter o) {
95      if (o == this) return true;
96      if (!(o instanceof KeyOnlyFilter)) return false;
97  
98      KeyOnlyFilter other = (KeyOnlyFilter)o;
99      return this.lenAsVal == other.lenAsVal;
100   }
101 }