View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.filter;
22  
23  import org.apache.hadoop.hbase.KeyValue;
24  
25  import java.io.DataInput;
26  import java.io.DataOutput;
27  import java.io.IOException;
28  
29  /**
30   * Simple filter that returns first N columns on row only.
31   * This filter was written to test filters in Get and as soon as it gets
32   * its quota of columns, {@link #filterAllRemaining()} returns true.  This
33   * makes this filter unsuitable as a Scan filter.
34   */
35  public class ColumnCountGetFilter extends FilterBase {
36    private int limit = 0;
37    private int count = 0;
38  
39    /**
40     * Used during serialization.
41     * Do not use.
42     */
43    public ColumnCountGetFilter() {
44      super();
45    }
46  
47    public ColumnCountGetFilter(final int n) {
48      this.limit = n;
49    }
50  
51    public int getLimit() {
52      return limit;
53    }
54  
55    @Override
56    public boolean filterAllRemaining() {
57      return this.count > this.limit;
58    }
59  
60    @Override
61    public ReturnCode filterKeyValue(KeyValue v) {
62      this.count++;
63      return filterAllRemaining() ? ReturnCode.SKIP: ReturnCode.INCLUDE;
64    }
65  
66    @Override
67    public void reset() {
68      this.count = 0;
69    }
70  
71    @Override
72    public void readFields(DataInput in) throws IOException {
73      this.limit = in.readInt();
74    }
75  
76    @Override
77    public void write(DataOutput out) throws IOException {
78      out.writeInt(this.limit);
79    }
80  }