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  
20  package org.apache.hadoop.hbase.filter;
21  
22  import java.util.ArrayList;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.classification.InterfaceStability;
26  import org.apache.hadoop.hbase.Cell;
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   * Simple filter that returns first N columns on row only.
35   * This filter was written to test filters in Get and as soon as it gets
36   * its quota of columns, {@link #filterAllRemaining()} returns true.  This
37   * makes this filter unsuitable as a Scan filter.
38   */
39  @InterfaceAudience.Public
40  @InterfaceStability.Stable
41  public class ColumnCountGetFilter extends FilterBase {
42    private int limit = 0;
43    private int count = 0;
44  
45    public ColumnCountGetFilter(final int n) {
46      Preconditions.checkArgument(n >= 0, "limit be positive %s", n);
47      this.limit = n;
48    }
49  
50    public int getLimit() {
51      return limit;
52    }
53  
54    @Override
55    public boolean filterAllRemaining() {
56      return this.count > this.limit;
57    }
58  
59    @Override
60    public ReturnCode filterKeyValue(Cell v) {
61      this.count++;
62      return filterAllRemaining() ? ReturnCode.NEXT_COL : ReturnCode.INCLUDE_AND_NEXT_COL;
63    }
64  
65    // Override here explicitly as the method in super class FilterBase might do a KeyValue recreate.
66    // See HBASE-12068
67    @Override
68    public Cell transformCell(Cell v) {
69      return v;
70    }
71  
72    @Override
73    public void reset() {
74      this.count = 0;
75    }
76  
77    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
78      Preconditions.checkArgument(filterArguments.size() == 1,
79                                  "Expected 1 but got: %s", filterArguments.size());
80      int limit = ParseFilter.convertByteArrayToInt(filterArguments.get(0));
81      return new ColumnCountGetFilter(limit);
82    }
83  
84    /**
85     * @return The filter serialized using pb
86     */
87    public byte [] toByteArray() {
88      FilterProtos.ColumnCountGetFilter.Builder builder =
89        FilterProtos.ColumnCountGetFilter.newBuilder();
90      builder.setLimit(this.limit);
91      return builder.build().toByteArray();
92    }
93  
94    /**
95     * @param pbBytes A pb serialized {@link ColumnCountGetFilter} instance
96     * @return An instance of {@link ColumnCountGetFilter} made from <code>bytes</code>
97     * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
98     * @see #toByteArray
99     */
100   public static ColumnCountGetFilter parseFrom(final byte [] pbBytes)
101   throws DeserializationException {
102     FilterProtos.ColumnCountGetFilter proto;
103     try {
104       proto = FilterProtos.ColumnCountGetFilter.parseFrom(pbBytes);
105     } catch (InvalidProtocolBufferException e) {
106       throw new DeserializationException(e);
107     }
108     return new ColumnCountGetFilter(proto.getLimit());
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 ColumnCountGetFilter)) return false;
119 
120     ColumnCountGetFilter other = (ColumnCountGetFilter)o;
121     return this.getLimit() == other.getLimit();
122   }
123 
124   @Override
125   public String toString() {
126     return this.getClass().getSimpleName() + " " + this.limit;
127   }
128 }