1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.hbql.filter;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HBaseConfiguration;
27 import org.apache.hadoop.hbase.KeyValue;
28 import org.apache.hadoop.hbase.filter.Filter;
29 import org.apache.hadoop.hbase.io.HbaseObjectWritable;
30 import org.apache.hadoop.io.Writable;
31
32 import java.io.DataInput;
33 import java.io.DataOutput;
34 import java.io.IOException;
35
36
37
38
39
40
41
42
43
44
45
46
47 public class PageFilter extends InstrumentedFilter {
48
49 private static final Log LOG = LogFactory.getLog(PageFilter.class);
50
51 private boolean verbose = false;
52 private long pageSize = Long.MAX_VALUE;
53 private int rowsAccepted = 0;
54 private Filter filter;
55
56
57
58
59
60 public PageFilter() {
61 super();
62 }
63
64
65
66
67
68
69 public PageFilter(final long pageSize, final Filter filter) {
70 this.pageSize = pageSize;
71 this.filter = filter;
72 }
73
74 private Filter getFilter() {
75 return this.filter;
76 }
77
78 public void setVerbose(final boolean verbose) {
79 this.verbose = verbose;
80 }
81
82 public boolean getVerbose() {
83 return this.verbose;
84 }
85
86 public void reset() {
87 if (this.getFilter() != null)
88 this.getFilter().reset();
89 }
90
91
92 public boolean filterAllRemaining() {
93 if (this.getVerbose())
94 LOG.debug("In PageFilter.filterAllRemaining() " + (this.rowsAccepted >= this.pageSize)
95 + " " + this.rowsAccepted + " of " + this.pageSize);
96 return this.rowsAccepted >= this.pageSize;
97 }
98
99 public boolean filterRowKey(byte[] rowKey, int offset, int length) {
100 if (this.getFilter() != null)
101 return this.getFilter().filterRowKey(rowKey, offset, length);
102 else
103 return false;
104 }
105
106 public ReturnCode filterKeyValue(KeyValue v) {
107 if (this.getFilter() != null)
108 return this.getFilter().filterKeyValue(v);
109 else
110 return ReturnCode.INCLUDE;
111 }
112
113 public boolean filterRow() {
114
115 if (this.rowsAccepted > this.pageSize)
116 return true;
117
118 if (this.getFilter() != null) {
119 if (this.getFilter().filterRow())
120 return true;
121 }
122
123 this.rowsAccepted++;
124
125 return this.rowsAccepted > this.pageSize;
126 }
127
128 public void readFields(final DataInput in) throws IOException {
129 Configuration conf = HBaseConfiguration.create();
130 this.pageSize = in.readLong();
131 this.verbose = in.readBoolean();
132 this.filter = (Filter)HbaseObjectWritable.readObject(in, conf);
133 }
134
135 public void write(final DataOutput out) throws IOException {
136 Configuration conf = HBaseConfiguration.create();
137 out.writeLong(this.pageSize);
138 out.writeBoolean(this.getVerbose());
139 HbaseObjectWritable.writeObject(out, this.getFilter(), Writable.class, conf);
140 }
141 }