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