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