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