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  package org.apache.hadoop.hbase.filter;
20  
21  import com.google.common.base.Preconditions;
22  import com.google.protobuf.InvalidProtocolBufferException;
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.exceptions.DeserializationException;
27  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
28  
29  import java.io.IOException;
30  import java.util.ArrayList;
31  
32  /**
33   * Implementation of Filter interface that limits results to a specific page
34   * size. It terminates scanning once the number of filter-passed rows is >
35   * the given page size.
36   * <p>
37   * Note that this filter cannot guarantee that the number of results returned
38   * to a client are &lt;= page size. This is because the filter is applied
39   * separately on different region servers. It does however optimize the scan of
40   * individual HRegions by making sure that the page size is never exceeded
41   * locally.
42   */
43  @InterfaceAudience.Public
44  @InterfaceStability.Stable
45  public class PageFilter extends FilterBase {
46    private long pageSize = Long.MAX_VALUE;
47    private int rowsAccepted = 0;
48  
49    /**
50     * Constructor that takes a maximum page size.
51     *
52     * @param pageSize Maximum result size.
53     */
54    public PageFilter(final long pageSize) {
55      Preconditions.checkArgument(pageSize >= 0, "must be positive %s", pageSize);
56      this.pageSize = pageSize;
57    }
58  
59    public long getPageSize() {
60      return pageSize;
61    }
62  
63    @Override
64    public ReturnCode filterKeyValue(Cell ignored) throws IOException {
65      return ReturnCode.INCLUDE;
66    }
67  
68    // Override here explicitly as the method in super class FilterBase might do a KeyValue recreate.
69    // See HBASE-12068
70    @Override
71    public Cell transformCell(Cell v) {
72      return v;
73    }
74  
75    @Override
76    public boolean filterAllRemaining() {
77      return this.rowsAccepted >= this.pageSize;
78    }
79  
80    @Override
81    public boolean filterRow() {
82      this.rowsAccepted++;
83      return this.rowsAccepted > this.pageSize;
84    }
85    
86    @Override
87    public boolean hasFilterRow() {
88      return true;
89    }
90  
91    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
92      Preconditions.checkArgument(filterArguments.size() == 1,
93                                  "Expected 1 but got: %s", filterArguments.size());
94      long pageSize = ParseFilter.convertByteArrayToLong(filterArguments.get(0));
95      return new PageFilter(pageSize);
96    }
97  
98    /**
99     * @return The filter serialized using pb
100    */
101   @Override
102   public byte [] toByteArray() {
103     FilterProtos.PageFilter.Builder builder =
104       FilterProtos.PageFilter.newBuilder();
105     builder.setPageSize(this.pageSize);
106     return builder.build().toByteArray();
107   }
108 
109   /**
110    * @param pbBytes A pb serialized {@link PageFilter} instance
111    * @return An instance of {@link PageFilter} made from <code>bytes</code>
112    * @throws DeserializationException
113    * @see #toByteArray
114    */
115   public static PageFilter parseFrom(final byte [] pbBytes)
116   throws DeserializationException {
117     FilterProtos.PageFilter proto;
118     try {
119       proto = FilterProtos.PageFilter.parseFrom(pbBytes);
120     } catch (InvalidProtocolBufferException e) {
121       throw new DeserializationException(e);
122     }
123     return new PageFilter(proto.getPageSize());
124   }
125 
126   /**
127    * @param other
128    * @return true if and only if the fields of the filter that are serialized
129    * are equal to the corresponding fields in other.  Used for testing.
130    */
131   @Override
132   boolean areSerializedFieldsEqual(Filter o) {
133     if (o == this) return true;
134     if (!(o instanceof PageFilter)) return false;
135 
136     PageFilter other = (PageFilter)o;
137     return this.getPageSize() == other.getPageSize();
138   }
139 
140   @Override
141   public String toString() {
142     return this.getClass().getSimpleName() + " " + this.pageSize;
143   }
144 }