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