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.classification.InterfaceAudience;
25  import org.apache.hadoop.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
66    public void reset() {
67      this.count = 0;
68    }
69  
70    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
71      Preconditions.checkArgument(filterArguments.size() == 1,
72                                  "Expected 1 but got: %s", filterArguments.size());
73      int limit = ParseFilter.convertByteArrayToInt(filterArguments.get(0));
74      return new ColumnCountGetFilter(limit);
75    }
76  
77    /**
78     * @return The filter serialized using pb
79     */
80    public byte [] toByteArray() {
81      FilterProtos.ColumnCountGetFilter.Builder builder =
82        FilterProtos.ColumnCountGetFilter.newBuilder();
83      builder.setLimit(this.limit);
84      return builder.build().toByteArray();
85    }
86  
87    /**
88     * @param pbBytes A pb serialized {@link ColumnCountGetFilter} instance
89     * @return An instance of {@link ColumnCountGetFilter} made from <code>bytes</code>
90     * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
91     * @see #toByteArray
92     */
93    public static ColumnCountGetFilter parseFrom(final byte [] pbBytes)
94    throws DeserializationException {
95      FilterProtos.ColumnCountGetFilter proto;
96      try {
97        proto = FilterProtos.ColumnCountGetFilter.parseFrom(pbBytes);
98      } catch (InvalidProtocolBufferException e) {
99        throw new DeserializationException(e);
100     }
101     return new ColumnCountGetFilter(proto.getLimit());
102   }
103 
104   /**
105    * @param other
106    * @return true if and only if the fields of the filter that are serialized
107    * are equal to the corresponding fields in other.  Used for testing.
108    */
109   boolean areSerializedFieldsEqual(Filter o) {
110     if (o == this) return true;
111     if (!(o instanceof ColumnCountGetFilter)) return false;
112 
113     ColumnCountGetFilter other = (ColumnCountGetFilter)o;
114     return this.getLimit() == other.getLimit();
115   }
116 
117   @Override
118   public String toString() {
119     return this.getClass().getSimpleName() + " " + this.limit;
120   }
121 }