1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.filter;
19
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.apache.hadoop.hbase.Cell;
26 import org.apache.hadoop.hbase.HConstants;
27 import org.apache.hadoop.hbase.KeyValueUtil;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.classification.InterfaceStability;
30 import org.apache.hadoop.hbase.exceptions.DeserializationException;
31 import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
32 import org.apache.hadoop.hbase.util.ByteStringer;
33 import org.apache.hadoop.hbase.util.Bytes;
34
35 import com.google.protobuf.InvalidProtocolBufferException;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 @InterfaceAudience.Public
53 @InterfaceStability.Evolving
54 public class MultiRowRangeFilter extends FilterBase {
55
56 private List<RowRange> rangeList;
57
58 private static final int ROW_BEFORE_FIRST_RANGE = -1;
59 private boolean EXCLUSIVE = false;
60 private boolean done = false;
61 private boolean initialized = false;
62 private int index;
63 private RowRange range;
64 private ReturnCode currentReturnCode;
65
66
67
68
69
70
71
72 public MultiRowRangeFilter(List<RowRange> list) throws IOException {
73 this.rangeList = sortAndMerge(list);
74 }
75
76 @Override
77 public boolean filterAllRemaining() {
78 return done;
79 }
80
81 public List<RowRange> getRowRanges() {
82 return this.rangeList;
83 }
84
85 @Override
86 public boolean filterRowKey(byte[] buffer, int offset, int length) {
87
88
89
90
91 if (!initialized || !range.contains(buffer, offset, length)) {
92 byte[] rowkey = new byte[length];
93 System.arraycopy(buffer, offset, rowkey, 0, length);
94 index = getNextRangeIndex(rowkey);
95 if (index >= rangeList.size()) {
96 done = true;
97 currentReturnCode = ReturnCode.NEXT_ROW;
98 return false;
99 }
100 if(index != ROW_BEFORE_FIRST_RANGE) {
101 range = rangeList.get(index);
102 } else {
103 range = rangeList.get(0);
104 }
105 if(EXCLUSIVE) {
106 EXCLUSIVE = false;
107 currentReturnCode = ReturnCode.NEXT_ROW;
108 return false;
109 }
110 if (!initialized) {
111 if(index != ROW_BEFORE_FIRST_RANGE) {
112 currentReturnCode = ReturnCode.INCLUDE;
113 } else {
114 currentReturnCode = ReturnCode.SEEK_NEXT_USING_HINT;
115 }
116 initialized = true;
117 } else {
118 currentReturnCode = ReturnCode.SEEK_NEXT_USING_HINT;
119 }
120 } else {
121 currentReturnCode = ReturnCode.INCLUDE;
122 }
123 return false;
124 }
125
126 @Override
127 public ReturnCode filterKeyValue(Cell ignored) {
128 return currentReturnCode;
129 }
130
131 @Override
132 public Cell getNextCellHint(Cell currentKV) {
133
134 return KeyValueUtil.createFirstOnRow(range.startRow);
135 }
136
137
138
139
140 public byte[] toByteArray() {
141 FilterProtos.MultiRowRangeFilter.Builder builder = FilterProtos.MultiRowRangeFilter
142 .newBuilder();
143 for (RowRange range : rangeList) {
144 if (range != null) {
145 FilterProtos.RowRange.Builder rangebuilder = FilterProtos.RowRange.newBuilder();
146 if (range.startRow != null)
147 rangebuilder.setStartRow(ByteStringer.wrap(range.startRow));
148 rangebuilder.setStartRowInclusive(range.startRowInclusive);
149 if (range.stopRow != null)
150 rangebuilder.setStopRow(ByteStringer.wrap(range.stopRow));
151 rangebuilder.setStopRowInclusive(range.stopRowInclusive);
152 range.isScan = Bytes.equals(range.startRow, range.stopRow) ? 1 : 0;
153 builder.addRowRangeList(rangebuilder.build());
154 }
155 }
156 return builder.build().toByteArray();
157 }
158
159
160
161
162
163
164 public static MultiRowRangeFilter parseFrom(final byte[] pbBytes)
165 throws DeserializationException {
166 FilterProtos.MultiRowRangeFilter proto;
167 try {
168 proto = FilterProtos.MultiRowRangeFilter.parseFrom(pbBytes);
169 } catch (InvalidProtocolBufferException e) {
170 throw new DeserializationException(e);
171 }
172 int length = proto.getRowRangeListCount();
173 List<FilterProtos.RowRange> rangeProtos = proto.getRowRangeListList();
174 List<RowRange> rangeList = new ArrayList<RowRange>(length);
175 for (FilterProtos.RowRange rangeProto : rangeProtos) {
176 RowRange range = new RowRange(rangeProto.hasStartRow() ? rangeProto.getStartRow()
177 .toByteArray() : null, rangeProto.getStartRowInclusive(), rangeProto.hasStopRow() ?
178 rangeProto.getStopRow().toByteArray() : null, rangeProto.getStopRowInclusive());
179 rangeList.add(range);
180 }
181 try {
182 return new MultiRowRangeFilter(rangeList);
183 } catch (IOException e) {
184 throw new DeserializationException("Fail to instantiate the MultiRowRangeFilter", e);
185 }
186 }
187
188
189
190
191
192
193 boolean areSerializedFieldsEqual(Filter o) {
194 if (o == this)
195 return true;
196 if (!(o instanceof MultiRowRangeFilter))
197 return false;
198
199 MultiRowRangeFilter other = (MultiRowRangeFilter) o;
200 if (this.rangeList.size() != other.rangeList.size())
201 return false;
202 for (int i = 0; i < rangeList.size(); ++i) {
203 RowRange thisRange = this.rangeList.get(i);
204 RowRange otherRange = other.rangeList.get(i);
205 if (!(Bytes.equals(thisRange.startRow, otherRange.startRow) && Bytes.equals(
206 thisRange.stopRow, otherRange.stopRow) && (thisRange.startRowInclusive ==
207 otherRange.startRowInclusive) && (thisRange.stopRowInclusive ==
208 otherRange.stopRowInclusive))) {
209 return false;
210 }
211 }
212 return true;
213 }
214
215
216
217
218
219
220
221 private int getNextRangeIndex(byte[] rowKey) {
222 RowRange temp = new RowRange(rowKey, true, null, true);
223 int index = Collections.binarySearch(rangeList, temp);
224 if (index < 0) {
225 int insertionPosition = -index - 1;
226
227 if (insertionPosition != 0 && rangeList.get(insertionPosition - 1).contains(rowKey)) {
228 return insertionPosition - 1;
229 }
230
231 if (insertionPosition == 0 && !rangeList.get(insertionPosition).contains(rowKey)) {
232 return ROW_BEFORE_FIRST_RANGE;
233 }
234 return insertionPosition;
235 }
236
237 if(rangeList.get(index).startRowInclusive == false) {
238 EXCLUSIVE = true;
239 }
240 return index;
241 }
242
243
244
245
246
247
248
249 public static List<RowRange> sortAndMerge(List<RowRange> ranges) {
250 if (ranges.size() == 0) {
251 throw new IllegalArgumentException("No ranges found.");
252 }
253 List<RowRange> invalidRanges = new ArrayList<RowRange>();
254 List<RowRange> newRanges = new ArrayList<RowRange>(ranges.size());
255 Collections.sort(ranges);
256 if(ranges.get(0).isValid()) {
257 if (ranges.size() == 1) {
258 newRanges.add(ranges.get(0));
259 }
260 } else {
261 invalidRanges.add(ranges.get(0));
262 }
263
264 byte[] lastStartRow = ranges.get(0).startRow;
265 boolean lastStartRowInclusive = ranges.get(0).startRowInclusive;
266 byte[] lastStopRow = ranges.get(0).stopRow;
267 boolean lastStopRowInclusive = ranges.get(0).stopRowInclusive;
268 int i = 1;
269 for (; i < ranges.size(); i++) {
270 RowRange range = ranges.get(i);
271 if (!range.isValid()) {
272 invalidRanges.add(range);
273 }
274 if(Bytes.equals(lastStopRow, HConstants.EMPTY_BYTE_ARRAY)) {
275 newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, lastStopRow,
276 lastStopRowInclusive));
277 break;
278 }
279
280 if ((Bytes.compareTo(lastStopRow, range.startRow) > 0) ||
281 (Bytes.compareTo(lastStopRow, range.startRow) == 0 && !(lastStopRowInclusive == false &&
282 range.isStartRowInclusive() == false))) {
283 if(Bytes.equals(range.stopRow, HConstants.EMPTY_BYTE_ARRAY)) {
284 newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, range.stopRow,
285 range.stopRowInclusive));
286 break;
287 }
288
289 if (Bytes.compareTo(lastStopRow, range.stopRow) >= 0) {
290 if((Bytes.compareTo(lastStopRow, range.stopRow) == 0)) {
291 if(lastStopRowInclusive == true || range.stopRowInclusive == true) {
292 lastStopRowInclusive = true;
293 }
294 }
295 if ((i + 1) == ranges.size()) {
296 newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, lastStopRow,
297 lastStopRowInclusive));
298 }
299 } else {
300 lastStopRow = range.stopRow;
301 lastStopRowInclusive = range.stopRowInclusive;
302 if ((i + 1) < ranges.size()) {
303 i++;
304 range = ranges.get(i);
305 if (!range.isValid()) {
306 invalidRanges.add(range);
307 }
308 } else {
309 newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, lastStopRow,
310 lastStopRowInclusive));
311 break;
312 }
313 while ((Bytes.compareTo(lastStopRow, range.startRow) > 0) ||
314 (Bytes.compareTo(lastStopRow, range.startRow) == 0 &&
315 (lastStopRowInclusive == true || range.startRowInclusive==true))) {
316 if(Bytes.equals(range.stopRow, HConstants.EMPTY_BYTE_ARRAY)) {
317 break;
318 }
319
320 if (Bytes.compareTo(lastStopRow, range.stopRow) >= 0) {
321 if(lastStopRowInclusive == true || range.stopRowInclusive == true) {
322 lastStopRowInclusive = true;
323 }
324 i++;
325 if (i < ranges.size()) {
326 range = ranges.get(i);
327 if (!range.isValid()) {
328 invalidRanges.add(range);
329 }
330 } else {
331 break;
332 }
333 } else {
334 lastStopRow = range.stopRow;
335 lastStopRowInclusive = range.stopRowInclusive;
336 i++;
337 if (i < ranges.size()) {
338 range = ranges.get(i);
339 if (!range.isValid()) {
340 invalidRanges.add(range);
341 }
342 } else {
343 break;
344 }
345 }
346 }
347 if(Bytes.equals(range.stopRow, HConstants.EMPTY_BYTE_ARRAY)) {
348 if((Bytes.compareTo(lastStopRow, range.startRow) < 0) ||
349 (Bytes.compareTo(lastStopRow, range.startRow) == 0 &&
350 lastStopRowInclusive == false && range.startRowInclusive == false)) {
351 newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, lastStopRow,
352 lastStopRowInclusive));
353 newRanges.add(range);
354 } else {
355 newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, range.stopRow,
356 range.stopRowInclusive));
357 break;
358 }
359 }
360 newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, lastStopRow,
361 lastStopRowInclusive));
362 if ((i + 1) == ranges.size()) {
363 newRanges.add(range);
364 }
365 lastStartRow = range.startRow;
366 lastStartRowInclusive = range.startRowInclusive;
367 lastStopRow = range.stopRow;
368 lastStopRowInclusive = range.stopRowInclusive;
369 }
370 } else {
371 newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, lastStopRow,
372 lastStopRowInclusive));
373 if ((i + 1) == ranges.size()) {
374 newRanges.add(range);
375 }
376 lastStartRow = range.startRow;
377 lastStartRowInclusive = range.startRowInclusive;
378 lastStopRow = range.stopRow;
379 lastStopRowInclusive = range.stopRowInclusive;
380 }
381 }
382
383 for(int j=i; j < ranges.size(); j++) {
384 if(!ranges.get(j).isValid()) {
385 invalidRanges.add(ranges.get(j));
386 }
387 }
388
389 if (invalidRanges.size() != 0) {
390 throwExceptionForInvalidRanges(invalidRanges, true);
391 }
392
393 if(newRanges.size() == 0) {
394 throw new IllegalArgumentException("No valid ranges found.");
395 }
396 return newRanges;
397 }
398
399 private static void throwExceptionForInvalidRanges(List<RowRange> invalidRanges,
400 boolean details) {
401 StringBuilder sb = new StringBuilder();
402 sb.append(invalidRanges.size()).append(" invaild ranges.\n");
403 if (details) {
404 for (RowRange range : invalidRanges) {
405 sb.append(
406 "Invalid range: start row => " + Bytes.toString(range.startRow) + ", stop row => "
407 + Bytes.toString(range.stopRow)).append('\n');
408 }
409 }
410 throw new IllegalArgumentException(sb.toString());
411 }
412
413 @InterfaceAudience.Public
414 @InterfaceStability.Evolving
415 public static class RowRange implements Comparable<RowRange> {
416 private byte[] startRow;
417 private boolean startRowInclusive = true;
418 private byte[] stopRow;
419 private boolean stopRowInclusive = false;
420 private int isScan = 0;
421
422 public RowRange() {
423 }
424
425
426
427
428
429 public RowRange(String startRow, boolean startRowInclusive, String stopRow,
430 boolean stopRowInclusive) {
431 this((startRow == null || startRow.isEmpty()) ? HConstants.EMPTY_BYTE_ARRAY :
432 Bytes.toBytes(startRow), startRowInclusive,
433 (stopRow == null || stopRow.isEmpty()) ? HConstants.EMPTY_BYTE_ARRAY :
434 Bytes.toBytes(stopRow), stopRowInclusive);
435 }
436
437 public RowRange(byte[] startRow, boolean startRowInclusive, byte[] stopRow,
438 boolean stopRowInclusive) {
439 this.startRow = (startRow == null) ? HConstants.EMPTY_BYTE_ARRAY : startRow;
440 this.startRowInclusive = startRowInclusive;
441 this.stopRow = (stopRow == null) ? HConstants.EMPTY_BYTE_ARRAY :stopRow;
442 this.stopRowInclusive = stopRowInclusive;
443 isScan = Bytes.equals(startRow, stopRow) ? 1 : 0;
444 }
445
446 public byte[] getStartRow() {
447 return startRow;
448 }
449
450 public byte[] getStopRow() {
451 return stopRow;
452 }
453
454
455
456
457 public boolean isStartRowInclusive() {
458 return startRowInclusive;
459 }
460
461
462
463
464 public boolean isStopRowInclusive() {
465 return stopRowInclusive;
466 }
467
468 public boolean contains(byte[] row) {
469 return contains(row, 0, row.length);
470 }
471
472 public boolean contains(byte[] buffer, int offset, int length) {
473 if(startRowInclusive) {
474 if(stopRowInclusive) {
475 return Bytes.compareTo(buffer, offset, length, startRow, 0, startRow.length) >= 0
476 && (Bytes.equals(stopRow, HConstants.EMPTY_BYTE_ARRAY) ||
477 Bytes.compareTo(buffer, offset, length, stopRow, 0, stopRow.length) <= isScan);
478 } else {
479 return Bytes.compareTo(buffer, offset, length, startRow, 0, startRow.length) >= 0
480 && (Bytes.equals(stopRow, HConstants.EMPTY_BYTE_ARRAY) ||
481 Bytes.compareTo(buffer, offset, length, stopRow, 0, stopRow.length) < isScan);
482 }
483 } else {
484 if(stopRowInclusive) {
485 return Bytes.compareTo(buffer, offset, length, startRow, 0, startRow.length) > 0
486 && (Bytes.equals(stopRow, HConstants.EMPTY_BYTE_ARRAY) ||
487 Bytes.compareTo(buffer, offset, length, stopRow, 0, stopRow.length) <= isScan);
488 } else {
489 return Bytes.compareTo(buffer, offset, length, startRow, 0, startRow.length) > 0
490 && (Bytes.equals(stopRow, HConstants.EMPTY_BYTE_ARRAY) ||
491 Bytes.compareTo(buffer, offset, length, stopRow, 0, stopRow.length) < isScan);
492 }
493 }
494 }
495
496 @Override
497 public int compareTo(RowRange other) {
498 return Bytes.compareTo(this.startRow, other.startRow);
499 }
500
501 public boolean isValid() {
502 return Bytes.equals(startRow, HConstants.EMPTY_BYTE_ARRAY)
503 || Bytes.equals(stopRow, HConstants.EMPTY_BYTE_ARRAY)
504 || Bytes.compareTo(startRow, stopRow) < 0
505 || (Bytes.compareTo(startRow, stopRow) == 0 && stopRowInclusive == true);
506 }
507 }
508 }