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  import java.util.Objects;
25  
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.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.classification.InterfaceStability;
31  import org.apache.hadoop.hbase.exceptions.DeserializationException;
32  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
33  import org.apache.hadoop.hbase.util.Bytes;
34  
35  import com.google.common.base.Preconditions;
36  import com.google.protobuf.InvalidProtocolBufferException;
37  
38  /**
39   * A filter that will only return the key component of each KV (the value will
40   * be rewritten as empty).
41   * <p>
42   * This filter can be used to grab all of the keys without having to also grab
43   * the values.
44   */
45  @InterfaceAudience.Public
46  @InterfaceStability.Stable
47  public class KeyOnlyFilter extends FilterBase {
48  
49    boolean lenAsVal;
50    public KeyOnlyFilter() { this(false); }
51    public KeyOnlyFilter(boolean lenAsVal) { this.lenAsVal = lenAsVal; }
52  
53    @Override
54    public Cell transformCell(Cell cell) {
55      return createKeyOnlyCell(cell);
56    }
57  
58    private Cell createKeyOnlyCell(Cell c) {
59      // KV format: <keylen:4><valuelen:4><key:keylen><value:valuelen>
60      // Rebuild as: <keylen:4><0:4><key:keylen>
61      int dataLen = lenAsVal ? Bytes.SIZEOF_INT : 0;
62      int keyOffset = (2 * Bytes.SIZEOF_INT);
63      int keyLen = KeyValueUtil.keyLength(c);
64      byte[] newBuffer = new byte[keyLen + keyOffset + dataLen];
65      Bytes.putInt(newBuffer, 0, keyLen);
66      Bytes.putInt(newBuffer, Bytes.SIZEOF_INT, dataLen);
67      KeyValueUtil.appendKeyTo(c, newBuffer, keyOffset);
68      if (lenAsVal) {
69        Bytes.putInt(newBuffer, newBuffer.length - dataLen, c.getValueLength());
70      }
71      return new KeyValue(newBuffer);
72    }
73  
74    @Override
75    public ReturnCode filterKeyValue(Cell ignored) throws IOException {
76      return ReturnCode.INCLUDE;
77    }
78    
79    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
80      Preconditions.checkArgument((filterArguments.size() == 0 || filterArguments.size() == 1),
81                                  "Expected: 0 or 1 but got: %s", filterArguments.size());
82      KeyOnlyFilter filter = new KeyOnlyFilter();
83      if (filterArguments.size() == 1) {
84        filter.lenAsVal = ParseFilter.convertByteArrayToBoolean(filterArguments.get(0));
85      }
86      return filter;
87    }
88  
89    /**
90     * @return The filter serialized using pb
91     */
92    @Override
93    public byte [] toByteArray() {
94      FilterProtos.KeyOnlyFilter.Builder builder =
95        FilterProtos.KeyOnlyFilter.newBuilder();
96      builder.setLenAsVal(this.lenAsVal);
97      return builder.build().toByteArray();
98    }
99  
100   /**
101    * @param pbBytes A pb serialized {@link KeyOnlyFilter} instance
102    * @return An instance of {@link KeyOnlyFilter} made from <code>bytes</code>
103    * @throws DeserializationException
104    * @see #toByteArray
105    */
106   public static KeyOnlyFilter parseFrom(final byte [] pbBytes)
107   throws DeserializationException {
108     FilterProtos.KeyOnlyFilter proto;
109     try {
110       proto = FilterProtos.KeyOnlyFilter.parseFrom(pbBytes);
111     } catch (InvalidProtocolBufferException e) {
112       throw new DeserializationException(e);
113     }
114     return new KeyOnlyFilter(proto.getLenAsVal());
115   }
116 
117   /**
118    * @param other
119    * @return true if and only if the fields of the filter that are serialized
120    * are equal to the corresponding fields in other.  Used for testing.
121    */
122   @Override
123   boolean areSerializedFieldsEqual(Filter o) {
124     if (o == this) return true;
125     if (!(o instanceof KeyOnlyFilter)) return false;
126 
127     KeyOnlyFilter other = (KeyOnlyFilter)o;
128     return this.lenAsVal == other.lenAsVal;
129   }
130 
131   @Override
132   public boolean equals(Object obj) {
133     return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
134   }
135 
136   @Override
137   public int hashCode() {
138     return Objects.hash(this.lenAsVal);
139   }
140 }