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