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    @Override
88    public byte [] toByteArray() {
89      FilterProtos.ColumnCountGetFilter.Builder builder =
90        FilterProtos.ColumnCountGetFilter.newBuilder();
91      builder.setLimit(this.limit);
92      return builder.build().toByteArray();
93    }
94  
95    /**
96     * @param pbBytes A pb serialized {@link ColumnCountGetFilter} instance
97     * @return An instance of {@link ColumnCountGetFilter} made from <code>bytes</code>
98     * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
99     * @see #toByteArray
100    */
101   public static ColumnCountGetFilter parseFrom(final byte [] pbBytes)
102   throws DeserializationException {
103     FilterProtos.ColumnCountGetFilter proto;
104     try {
105       proto = FilterProtos.ColumnCountGetFilter.parseFrom(pbBytes);
106     } catch (InvalidProtocolBufferException e) {
107       throw new DeserializationException(e);
108     }
109     return new ColumnCountGetFilter(proto.getLimit());
110   }
111 
112   /**
113    * @param other
114    * @return true if and only if the fields of the filter that are serialized
115    * are equal to the corresponding fields in other.  Used for testing.
116    */
117   @Override
118   boolean areSerializedFieldsEqual(Filter o) {
119     if (o == this) return true;
120     if (!(o instanceof ColumnCountGetFilter)) return false;
121 
122     ColumnCountGetFilter other = (ColumnCountGetFilter)o;
123     return this.getLimit() == other.getLimit();
124   }
125 
126   @Override
127   public String toString() {
128     return this.getClass().getSimpleName() + " " + this.limit;
129   }
130 }