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.hbase.Cell;
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.KeyValueUtil;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.classification.InterfaceStability;
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 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 boolean filterRowKey(Cell cell) throws IOException {
54      // Impl in FilterBase might do unnecessary copy for Off heap backed Cells.
55      return false;
56    }
57  
58    @Override
59    public Cell transformCell(Cell cell) {
60      return createKeyOnlyCell(cell);
61    }
62  
63    private Cell createKeyOnlyCell(Cell c) {
64      // KV format: <keylen:4><valuelen:4><key:keylen><value:valuelen>
65      // Rebuild as: <keylen:4><0:4><key:keylen>
66      int dataLen = lenAsVal ? Bytes.SIZEOF_INT : 0;
67      int keyOffset = (2 * Bytes.SIZEOF_INT);
68      int keyLen = KeyValueUtil.keyLength(c);
69      byte[] newBuffer = new byte[keyLen + keyOffset + dataLen];
70      Bytes.putInt(newBuffer, 0, keyLen);
71      Bytes.putInt(newBuffer, Bytes.SIZEOF_INT, dataLen);
72      KeyValueUtil.appendKeyTo(c, newBuffer, keyOffset);
73      if (lenAsVal) {
74        Bytes.putInt(newBuffer, newBuffer.length - dataLen, c.getValueLength());
75      }
76      return new KeyValue(newBuffer);
77    }
78  
79    @Override
80    public ReturnCode filterKeyValue(Cell ignored) throws IOException {
81      return ReturnCode.INCLUDE;
82    }
83    
84    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
85      Preconditions.checkArgument((filterArguments.size() == 0 || filterArguments.size() == 1),
86                                  "Expected: 0 or 1 but got: %s", filterArguments.size());
87      KeyOnlyFilter filter = new KeyOnlyFilter();
88      if (filterArguments.size() == 1) {
89        filter.lenAsVal = ParseFilter.convertByteArrayToBoolean(filterArguments.get(0));
90      }
91      return filter;
92    }
93  
94    /**
95     * @return The filter serialized using pb
96     */
97    public byte [] toByteArray() {
98      FilterProtos.KeyOnlyFilter.Builder builder =
99        FilterProtos.KeyOnlyFilter.newBuilder();
100     builder.setLenAsVal(this.lenAsVal);
101     return builder.build().toByteArray();
102   }
103 
104   /**
105    * @param pbBytes A pb serialized {@link KeyOnlyFilter} instance
106    * @return An instance of {@link KeyOnlyFilter} made from <code>bytes</code>
107    * @throws DeserializationException
108    * @see #toByteArray
109    */
110   public static KeyOnlyFilter parseFrom(final byte [] pbBytes)
111   throws DeserializationException {
112     FilterProtos.KeyOnlyFilter proto;
113     try {
114       proto = FilterProtos.KeyOnlyFilter.parseFrom(pbBytes);
115     } catch (InvalidProtocolBufferException e) {
116       throw new DeserializationException(e);
117     }
118     return new KeyOnlyFilter(proto.getLenAsVal());
119   }
120 
121   /**
122    * @param other
123    * @return true if and only if the fields of the filter that are serialized
124    * are equal to the corresponding fields in other.  Used for testing.
125    */
126   boolean areSerializedFieldsEqual(Filter o) {
127     if (o == this) return true;
128     if (!(o instanceof KeyOnlyFilter)) return false;
129 
130     KeyOnlyFilter other = (KeyOnlyFilter)o;
131     return this.lenAsVal == other.lenAsVal;
132   }
133 }