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  import java.util.ArrayList;
29  
30  import com.google.common.base.Preconditions;
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  public class ColumnCountGetFilter extends FilterBase {
39    private int limit = 0;
40    private int count = 0;
41  
42    /**
43     * Used during serialization.
44     * Do not use.
45     */
46    public ColumnCountGetFilter() {
47      super();
48    }
49  
50    public ColumnCountGetFilter(final int n) {
51      Preconditions.checkArgument(n >= 0, "limit be positive %s", n);
52      this.limit = n;
53    }
54  
55    public int getLimit() {
56      return limit;
57    }
58  
59    @Override
60    public boolean filterAllRemaining() {
61      return this.count > this.limit;
62    }
63  
64    @Override
65    public ReturnCode filterKeyValue(KeyValue v) {
66      this.count++;
67      return filterAllRemaining() ? ReturnCode.NEXT_COL:
68                                    ReturnCode.INCLUDE_AND_NEXT_COL;
69    }
70  
71    @Override
72    public void reset() {
73      this.count = 0;
74    }
75  
76    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
77      Preconditions.checkArgument(filterArguments.size() == 1,
78                                  "Expected 1 but got: %s", filterArguments.size());
79      int limit = ParseFilter.convertByteArrayToInt(filterArguments.get(0));
80      return new ColumnCountGetFilter(limit);
81    }
82  
83    @Override
84    public void readFields(DataInput in) throws IOException {
85      this.limit = in.readInt();
86    }
87  
88    @Override
89    public void write(DataOutput out) throws IOException {
90      out.writeInt(this.limit);
91    }
92  
93    @Override
94    public String toString() {
95      return this.getClass().getSimpleName() + " " + this.limit;
96    }
97  }