1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver;
20
21 import java.io.IOException;
22 import java.util.NavigableSet;
23
24 import org.apache.hadoop.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.HConstants;
26 import org.apache.hadoop.hbase.KeyValue;
27 import org.apache.hadoop.hbase.regionserver.ScanQueryMatcher.MatchCode;
28 import org.apache.hadoop.hbase.util.Bytes;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 @InterfaceAudience.Private
54 public class ExplicitColumnTracker implements ColumnTracker {
55
56 private final int maxVersions;
57 private final int minVersions;
58
59
60
61 private final int lookAhead;
62
63
64
65
66
67
68 private final ColumnCount[] columns;
69 private int index;
70 private ColumnCount column;
71
72
73 private long latestTSOfCurrentColumn;
74 private long oldestStamp;
75 private int skipCount;
76
77
78
79
80
81
82
83
84
85
86
87 public ExplicitColumnTracker(NavigableSet<byte[]> columns, int minVersions,
88 int maxVersions, long oldestUnexpiredTS, int lookAhead) {
89 this.maxVersions = maxVersions;
90 this.minVersions = minVersions;
91 this.lookAhead = lookAhead;
92 this.oldestStamp = oldestUnexpiredTS;
93 this.columns = new ColumnCount[columns.size()];
94 int i=0;
95 for(byte [] column : columns) {
96 this.columns[i++] = new ColumnCount(column);
97 }
98 reset();
99 }
100
101
102
103
104 public boolean done() {
105 return this.index >= columns.length;
106 }
107
108 public ColumnCount getColumnHint() {
109 return this.column;
110 }
111
112
113
114
115 @Override
116 public ScanQueryMatcher.MatchCode checkColumn(byte [] bytes, int offset,
117 int length, byte type) {
118
119
120 assert !KeyValue.isDelete(type);
121 do {
122
123 if(done()) {
124 return ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW;
125 }
126
127
128 if(this.column == null) {
129 return ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW;
130 }
131
132
133 int ret = Bytes.compareTo(column.getBuffer(), column.getOffset(),
134 column.getLength(), bytes, offset, length);
135
136
137
138 if(ret == 0) {
139 return ScanQueryMatcher.MatchCode.INCLUDE;
140 }
141
142 resetTS();
143
144 if (ret > 0) {
145
146
147 return this.skipCount++ < this.lookAhead ? ScanQueryMatcher.MatchCode.SKIP
148 : ScanQueryMatcher.MatchCode.SEEK_NEXT_COL;
149 }
150
151
152
153
154
155 if (ret <= -1) {
156 ++this.index;
157 this.skipCount = 0;
158 if (done()) {
159
160 return ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW;
161 }
162
163 this.column = this.columns[this.index];
164 }
165 } while(true);
166 }
167
168 @Override
169 public ScanQueryMatcher.MatchCode checkVersions(byte[] bytes, int offset, int length,
170 long timestamp, byte type, boolean ignoreCount) throws IOException {
171 assert !KeyValue.isDelete(type);
172 if (ignoreCount) return ScanQueryMatcher.MatchCode.INCLUDE;
173
174 if (sameAsPreviousTS(timestamp)) {
175
176 return ScanQueryMatcher.MatchCode.SKIP;
177 }
178 int count = this.column.increment();
179 if (count >= maxVersions || (count >= minVersions && isExpired(timestamp))) {
180
181 ++this.index;
182 this.skipCount = 0;
183 resetTS();
184 if (done()) {
185
186 this.column = null;
187 return ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_ROW;
188 }
189
190
191 this.column = this.columns[this.index];
192 return ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL;
193 }
194 setTS(timestamp);
195 return ScanQueryMatcher.MatchCode.INCLUDE;
196 }
197
198
199 public void reset() {
200 this.index = 0;
201 this.skipCount = 0;
202 this.column = this.columns[this.index];
203 for(ColumnCount col : this.columns) {
204 col.setCount(0);
205 }
206 resetTS();
207 }
208
209 private void resetTS() {
210 latestTSOfCurrentColumn = HConstants.LATEST_TIMESTAMP;
211 }
212
213 private void setTS(long timestamp) {
214 latestTSOfCurrentColumn = timestamp;
215 }
216
217 private boolean sameAsPreviousTS(long timestamp) {
218 return timestamp == latestTSOfCurrentColumn;
219 }
220
221 private boolean isExpired(long timestamp) {
222 return timestamp < oldestStamp;
223 }
224
225
226
227
228
229
230
231
232
233
234 public void doneWithColumn(byte [] bytes, int offset, int length) {
235 while (this.column != null) {
236 int compare = Bytes.compareTo(column.getBuffer(), column.getOffset(),
237 column.getLength(), bytes, offset, length);
238 resetTS();
239 if (compare <= 0) {
240 ++this.index;
241 this.skipCount = 0;
242 if (done()) {
243
244 this.column = null;
245 } else {
246 this.column = this.columns[this.index];
247 }
248 if (compare <= -1)
249 continue;
250 }
251 return;
252 }
253 }
254
255 public MatchCode getNextRowOrNextColumn(byte[] bytes, int offset,
256 int qualLength) {
257 doneWithColumn(bytes, offset,qualLength);
258
259 if (getColumnHint() == null) {
260 return MatchCode.SEEK_NEXT_ROW;
261 } else {
262 return MatchCode.SEEK_NEXT_COL;
263 }
264 }
265
266 public boolean isDone(long timestamp) {
267 return minVersions <= 0 && isExpired(timestamp);
268 }
269 }