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  import java.util.Objects;
24  
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.Cell;
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 filterAllRemaining() {
57      return this.count > this.limit;
58    }
59  
60    @Override
61    public ReturnCode filterKeyValue(Cell v) {
62      this.count++;
63      return filterAllRemaining() ? ReturnCode.NEXT_COL : ReturnCode.INCLUDE_AND_NEXT_COL;
64    }
65  
66    // Override here explicitly as the method in super class FilterBase might do a KeyValue recreate.
67    // See HBASE-12068
68    @Override
69    public Cell transformCell(Cell v) {
70      return v;
71    }
72  
73    @Override
74    public void reset() {
75      this.count = 0;
76    }
77  
78    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
79      Preconditions.checkArgument(filterArguments.size() == 1,
80                                  "Expected 1 but got: %s", filterArguments.size());
81      int limit = ParseFilter.convertByteArrayToInt(filterArguments.get(0));
82      return new ColumnCountGetFilter(limit);
83    }
84  
85    /**
86     * @return The filter serialized using pb
87     */
88    @Override
89    public byte [] toByteArray() {
90      FilterProtos.ColumnCountGetFilter.Builder builder =
91        FilterProtos.ColumnCountGetFilter.newBuilder();
92      builder.setLimit(this.limit);
93      return builder.build().toByteArray();
94    }
95  
96    /**
97     * @param pbBytes A pb serialized {@link ColumnCountGetFilter} instance
98     * @return An instance of {@link ColumnCountGetFilter} made from <code>bytes</code>
99     * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
100    * @see #toByteArray
101    */
102   public static ColumnCountGetFilter parseFrom(final byte [] pbBytes)
103   throws DeserializationException {
104     FilterProtos.ColumnCountGetFilter proto;
105     try {
106       proto = FilterProtos.ColumnCountGetFilter.parseFrom(pbBytes);
107     } catch (InvalidProtocolBufferException e) {
108       throw new DeserializationException(e);
109     }
110     return new ColumnCountGetFilter(proto.getLimit());
111   }
112 
113   /**
114    * @param other
115    * @return true if and only if the fields of the filter that are serialized
116    * are equal to the corresponding fields in other.  Used for testing.
117    */
118   @Override
119   boolean areSerializedFieldsEqual(Filter o) {
120     if (o == this) return true;
121     if (!(o instanceof ColumnCountGetFilter)) return false;
122 
123     ColumnCountGetFilter other = (ColumnCountGetFilter)o;
124     return this.getLimit() == other.getLimit();
125   }
126 
127   @Override
128   public String toString() {
129     return this.getClass().getSimpleName() + " " + this.limit;
130   }
131 
132   @Override
133   public boolean equals(Object obj) {
134     return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
135   }
136 
137   @Override
138   public int hashCode() {
139     return Objects.hash(this.limit);
140   }
141 }