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 package org.apache.hadoop.hbase.filter;
21
22 import org.apache.hadoop.hbase.KeyValue;
23
24 import java.io.DataInput;
25 import java.io.DataOutput;
26 import java.io.IOException;
27 import java.util.List;
28
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 public class PageFilter extends FilterBase {
41 private long pageSize = Long.MAX_VALUE;
42 private int rowsAccepted = 0;
43
44 /**
45 * Default constructor, filters nothing. Required though for RPC
46 * deserialization.
47 */
48 public PageFilter() {
49 super();
50 }
51
52 /**
53 * Constructor that takes a maximum page size.
54 *
55 * @param pageSize Maximum result size.
56 */
57 public PageFilter(final long pageSize) {
58 this.pageSize = pageSize;
59 }
60
61 public long getPageSize() {
62 return pageSize;
63 }
64
65 public boolean filterAllRemaining() {
66 return this.rowsAccepted >= this.pageSize;
67 }
68
69 public boolean filterRow() {
70 this.rowsAccepted++;
71 return this.rowsAccepted > this.pageSize;
72 }
73
74 public void readFields(final DataInput in) throws IOException {
75 this.pageSize = in.readLong();
76 }
77
78 public void write(final DataOutput out) throws IOException {
79 out.writeLong(pageSize);
80 }
81 }