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