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