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 java.io.DataInput;
23 import java.io.DataOutput;
24 import java.io.IOException;
25 import java.util.List;
26
27 import org.apache.hadoop.hbase.KeyValue;
28
29 /**
30 * A filter, based on the ColumnCountGetFilter, takes two arguments: limit and offset.
31 * This filter can be used for row-based indexing, where references to other tables are stored across many columns,
32 * in order to efficient lookups and paginated results for end users.
33 */
34 public class ColumnPaginationFilter extends FilterBase
35 {
36 private int limit = 0;
37 private int offset = 0;
38 private int count = 0;
39
40 /**
41 * Used during serialization. Do not use.
42 */
43 public ColumnPaginationFilter()
44 {
45 super();
46 }
47
48 public ColumnPaginationFilter(final int limit, final int offset)
49 {
50 this.limit = limit;
51 this.offset = offset;
52 }
53
54 @Override
55 public ReturnCode filterKeyValue(KeyValue v)
56 {
57 if(count >= offset + limit)
58 {
59 return ReturnCode.NEXT_ROW;
60 }
61
62 ReturnCode code = count < offset ? ReturnCode.SKIP : ReturnCode.INCLUDE;
63 count++;
64 return code;
65 }
66
67 @Override
68 public void reset()
69 {
70 this.count = 0;
71 }
72
73 public void readFields(DataInput in) throws IOException
74 {
75 this.limit = in.readInt();
76 this.offset = in.readInt();
77 }
78
79 public void write(DataOutput out) throws IOException
80 {
81 out.writeInt(this.limit);
82 out.writeInt(this.offset);
83 }
84 }