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
36 public class ColumnPaginationFilter extends FilterBase
37 {
38 private int limit = 0;
39 private int offset = 0;
40 private int count = 0;
41
42
43
44
45 public ColumnPaginationFilter()
46 {
47 super();
48 }
49
50 public ColumnPaginationFilter(final int limit, final int offset)
51 {
52 Preconditions.checkArgument(limit >= 0, "limit must be positive %s", limit);
53 Preconditions.checkArgument(offset >= 0, "offset must be positive %s", offset);
54 this.limit = limit;
55 this.offset = offset;
56 }
57
58
59
60
61 public int getLimit() {
62 return limit;
63 }
64
65
66
67
68 public int getOffset() {
69 return offset;
70 }
71
72 @Override
73 public ReturnCode filterKeyValue(KeyValue v)
74 {
75 if(count >= offset + limit)
76 {
77 return ReturnCode.NEXT_ROW;
78 }
79
80 ReturnCode code = count < offset ? ReturnCode.NEXT_COL :
81 ReturnCode.INCLUDE_AND_NEXT_COL;
82 count++;
83 return code;
84 }
85
86 @Override
87 public void reset()
88 {
89 this.count = 0;
90 }
91
92 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
93 Preconditions.checkArgument(filterArguments.size() == 2,
94 "Expected 2 but got: %s", filterArguments.size());
95 int limit = ParseFilter.convertByteArrayToInt(filterArguments.get(0));
96 int offset = ParseFilter.convertByteArrayToInt(filterArguments.get(1));
97 return new ColumnPaginationFilter(limit, offset);
98 }
99
100 public void readFields(DataInput in) throws IOException
101 {
102 this.limit = in.readInt();
103 this.offset = in.readInt();
104 }
105
106 public void write(DataOutput out) throws IOException
107 {
108 out.writeInt(this.limit);
109 out.writeInt(this.offset);
110 }
111
112 @Override
113 public String toString() {
114 return String.format("%s (%d, %d)", this.getClass().getSimpleName(),
115 this.limit, this.offset);
116 }
117 }