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 import java.util.ArrayList;
29
30 import com.google.common.base.Preconditions;
31 /**
32 * Implementation of Filter interface that limits results to a specific page
33 * size. It terminates scanning once the number of filter-passed rows is >
34 * the given page size.
35 * <p>
36 * Note that this filter cannot guarantee that the number of results returned
37 * to a client are <= page size. This is because the filter is applied
38 * separately on different region servers. It does however optimize the scan of
39 * individual HRegions by making sure that the page size is never exceeded
40 * locally.
41 */
42 public class PageFilter extends FilterBase {
43 private long pageSize = Long.MAX_VALUE;
44 private int rowsAccepted = 0;
45
46 /**
47 * Default constructor, filters nothing. Required though for RPC
48 * deserialization.
49 */
50 public PageFilter() {
51 super();
52 }
53
54 /**
55 * Constructor that takes a maximum page size.
56 *
57 * @param pageSize Maximum result size.
58 */
59 public PageFilter(final long pageSize) {
60 Preconditions.checkArgument(pageSize >= 0, "must be positive %s", pageSize);
61 this.pageSize = pageSize;
62 }
63
64 public long getPageSize() {
65 return pageSize;
66 }
67
68 public boolean filterAllRemaining() {
69 return this.rowsAccepted >= this.pageSize;
70 }
71
72 public boolean filterRow() {
73 this.rowsAccepted++;
74 return this.rowsAccepted > this.pageSize;
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 long pageSize = ParseFilter.convertByteArrayToLong(filterArguments.get(0));
81 return new PageFilter(pageSize);
82 }
83
84 public void readFields(final DataInput in) throws IOException {
85 this.pageSize = in.readLong();
86 }
87
88 public void write(final DataOutput out) throws IOException {
89 out.writeLong(pageSize);
90 }
91 }