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