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  import com.google.common.base.Preconditions;
22  import com.google.protobuf.InvalidProtocolBufferException;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.KeyValue;
26  import org.apache.hadoop.hbase.exceptions.DeserializationException;
27  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
28  
29  import java.util.ArrayList;
30  
31  /**
32   * A filter that will only return the first KV from each row.
33   * <p>
34   * This filter can be used to more efficiently perform row count operations.
35   */
36  @InterfaceAudience.Public
37  @InterfaceStability.Stable
38  public class FirstKeyOnlyFilter extends FilterBase {
39    private boolean foundKV = false;
40  
41    public FirstKeyOnlyFilter() {
42    }
43  
44    public void reset() {
45      foundKV = false;
46    }
47  
48    public ReturnCode filterKeyValue(KeyValue v) {
49      if(foundKV) return ReturnCode.NEXT_ROW;
50      foundKV = true;
51      return ReturnCode.INCLUDE;
52    }
53  
54    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
55      Preconditions.checkArgument(filterArguments.size() == 0,
56                                  "Expected 0 but got: %s", filterArguments.size());
57      return new FirstKeyOnlyFilter();
58    }
59  
60    /**
61     * @return true if first KV has been found.
62     */
63    protected boolean hasFoundKV() {
64      return this.foundKV;
65    }
66  
67    /**
68     *
69     * @param value update {@link #foundKV} flag with value.
70     */
71    protected void setFoundKV(boolean value) {
72      this.foundKV = value;
73    }
74  
75    /**
76     * @return The filter serialized using pb
77     */
78    public byte [] toByteArray() {
79      FilterProtos.FirstKeyOnlyFilter.Builder builder =
80        FilterProtos.FirstKeyOnlyFilter.newBuilder();
81      return builder.build().toByteArray();
82    }
83  
84    /**
85     * @param pbBytes A pb serialized {@link FirstKeyOnlyFilter} instance
86     * @return An instance of {@link FirstKeyOnlyFilter} made from <code>bytes</code>
87     * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
88     * @see #toByteArray
89     */
90    public static FirstKeyOnlyFilter parseFrom(final byte [] pbBytes)
91    throws DeserializationException {
92      // There is nothing to deserialize.  Why do this at all?
93      try {
94        FilterProtos.FirstKeyOnlyFilter.parseFrom(pbBytes);
95      } catch (InvalidProtocolBufferException e) {
96        throw new DeserializationException(e);
97      }
98      // Just return a new instance.
99      return new FirstKeyOnlyFilter();
100   }
101 
102   /**
103    * @param other
104    * @return true if and only if the fields of the filter that are serialized
105    * are equal to the corresponding fields in other.  Used for testing.
106    */
107   boolean areSerializedFieldsEqual(Filter o) {
108     if (o == this) return true;
109     if (!(o instanceof FirstKeyOnlyFilter)) return false;
110 
111     return true;
112   }
113 }