1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.ArrayList;
26
27 import org.apache.hadoop.hbase.KeyValue;
28 import com.google.common.base.Preconditions;
29
30
31
32
33
34
35 public class ColumnPaginationFilter extends FilterBase
36 {
37 private int limit = 0;
38 private int offset = 0;
39 private int count = 0;
40
41
42
43
44 public ColumnPaginationFilter()
45 {
46 super();
47 }
48
49 public ColumnPaginationFilter(final int limit, final int offset)
50 {
51 Preconditions.checkArgument(limit >= 0, "limit must be positive %s", limit);
52 Preconditions.checkArgument(offset >= 0, "offset must be positive %s", offset);
53 this.limit = limit;
54 this.offset = offset;
55 }
56
57
58
59
60 public int getLimit() {
61 return limit;
62 }
63
64
65
66
67 public int getOffset() {
68 return offset;
69 }
70
71 @Override
72 public ReturnCode filterKeyValue(KeyValue v)
73 {
74 if(count >= offset + limit)
75 {
76 return ReturnCode.NEXT_ROW;
77 }
78
79 ReturnCode code = count < offset ? ReturnCode.SKIP : ReturnCode.INCLUDE;
80 count++;
81 return code;
82 }
83
84 @Override
85 public void reset()
86 {
87 this.count = 0;
88 }
89
90 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
91 Preconditions.checkArgument(filterArguments.size() == 2,
92 "Expected 2 but got: %s", filterArguments.size());
93 int limit = ParseFilter.convertByteArrayToInt(filterArguments.get(0));
94 int offset = ParseFilter.convertByteArrayToInt(filterArguments.get(1));
95 return new ColumnPaginationFilter(limit, offset);
96 }
97
98 public void readFields(DataInput in) throws IOException
99 {
100 this.limit = in.readInt();
101 this.offset = in.readInt();
102 }
103
104 public void write(DataOutput out) throws IOException
105 {
106 out.writeInt(this.limit);
107 out.writeInt(this.offset);
108 }
109 }