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