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 static org.junit.Assert.assertEquals;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.hbase.Cell;
29 import org.apache.hadoop.hbase.HBaseTestingUtility;
30 import org.apache.hadoop.hbase.client.HTable;
31 import org.apache.hadoop.hbase.client.Put;
32 import org.apache.hadoop.hbase.client.Result;
33 import org.apache.hadoop.hbase.client.ResultScanner;
34 import org.apache.hadoop.hbase.client.Scan;
35 import org.apache.hadoop.hbase.filter.MultiRowRangeFilter.RowRange;
36 import org.apache.hadoop.hbase.testclassification.MediumTests;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.junit.AfterClass;
39 import org.junit.Assert;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
43
44 @Category(MediumTests.class)
45 public class TestMultiRowRangeFilter {
46
47 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
48 private final Log LOG = LogFactory.getLog(this.getClass());
49 private byte[] family = Bytes.toBytes("family");
50 private byte[] qf = Bytes.toBytes("qf");
51 private byte[] value = Bytes.toBytes("val");
52 private byte[] tableName;
53 private int numRows = 100;
54
55
56
57
58 @BeforeClass
59 public static void setUpBeforeClass() throws Exception {
60 TEST_UTIL.startMiniCluster();
61 }
62
63
64
65
66 @AfterClass
67 public static void tearDownAfterClass() throws Exception {
68 TEST_UTIL.shutdownMiniCluster();
69 }
70
71 @Test
72 public void testMergeAndSortWithEmptyStartRow() throws IOException {
73 List<RowRange> ranges = new ArrayList<RowRange>();
74 ranges.add(new RowRange(Bytes.toBytes(""), true, Bytes.toBytes(20), false));
75 ranges.add(new RowRange(Bytes.toBytes(15), true, Bytes.toBytes(40), false));
76 List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges);
77 List<RowRange> expectedRanges = new ArrayList<RowRange>();
78 expectedRanges.add(new RowRange(Bytes.toBytes(""), true, Bytes.toBytes(40), false));
79 assertRangesEqual(expectedRanges, actualRanges);
80 }
81
82 @Test
83 public void testMergeAndSortWithEmptyStopRow() throws IOException {
84 List<RowRange> ranges = new ArrayList<RowRange>();
85 ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
86 ranges.add(new RowRange(Bytes.toBytes(15), true, Bytes.toBytes(""), false));
87 ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(70), false));
88 List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges);
89 List<RowRange> expectedRanges = new ArrayList<RowRange>();
90 expectedRanges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(""), false));
91 assertRangesEqual(expectedRanges, actualRanges);
92 }
93
94 @Test
95 public void testMergeAndSortWithEmptyStartRowAndStopRow() throws IOException {
96 List<RowRange> ranges = new ArrayList<RowRange>();
97 ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
98 ranges.add(new RowRange(Bytes.toBytes(""), true, Bytes.toBytes(""), false));
99 ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(70), false));
100 List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges);
101 List<RowRange> expectedRanges = new ArrayList<RowRange>();
102 expectedRanges.add(new RowRange(Bytes.toBytes(""), true, Bytes.toBytes(""), false));
103 assertRangesEqual(expectedRanges, actualRanges);
104 }
105
106 @Test(expected=IllegalArgumentException.class)
107 public void testMultiRowRangeWithoutRange() throws IOException {
108 List<RowRange> ranges = new ArrayList<RowRange>();
109 new MultiRowRangeFilter(ranges);
110 }
111
112 @Test(expected=IllegalArgumentException.class)
113 public void testMultiRowRangeWithInvalidRange() throws IOException {
114 List<RowRange> ranges = new ArrayList<RowRange>();
115 ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
116
117 ranges.add(new RowRange(Bytes.toBytes(80), true, Bytes.toBytes(20), false));
118 ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(70), false));
119 new MultiRowRangeFilter(ranges);
120 }
121
122 @Test
123 public void testMergeAndSortWithoutOverlap() throws IOException {
124 List<RowRange> ranges = new ArrayList<RowRange>();
125 ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
126 ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false));
127 ranges.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(70), false));
128 List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges);
129 List<RowRange> expectedRanges = new ArrayList<RowRange>();
130 expectedRanges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
131 expectedRanges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false));
132 expectedRanges.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(70), false));
133 assertRangesEqual(expectedRanges, actualRanges);
134 }
135
136 @Test
137 public void testMergeAndSortWithOverlap() throws IOException {
138 List<RowRange> ranges = new ArrayList<RowRange>();
139 ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
140 ranges.add(new RowRange(Bytes.toBytes(15), true, Bytes.toBytes(40), false));
141 ranges.add(new RowRange(Bytes.toBytes(20), true, Bytes.toBytes(30), false));
142 ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(50), false));
143 ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(70), false));
144 ranges.add(new RowRange(Bytes.toBytes(90), true, Bytes.toBytes(100), false));
145 ranges.add(new RowRange(Bytes.toBytes(95), true, Bytes.toBytes(100), false));
146 List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges);
147 List<RowRange> expectedRanges = new ArrayList<RowRange>();
148 expectedRanges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(70), false));
149 expectedRanges.add(new RowRange(Bytes.toBytes(90), true, Bytes.toBytes(100), false));
150 assertRangesEqual(expectedRanges, actualRanges);
151 }
152
153 @Test
154 public void testMergeAndSortWithStartRowInclusive() throws IOException {
155 List<RowRange> ranges = new ArrayList<RowRange>();
156 ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
157 ranges.add(new RowRange(Bytes.toBytes(20), true, Bytes.toBytes(""), false));
158 List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges);
159 List<RowRange> expectedRanges = new ArrayList<RowRange>();
160 expectedRanges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(""), false));
161 assertRangesEqual(expectedRanges, actualRanges);
162 }
163
164 @Test
165 public void testMergeAndSortWithRowExclusive() throws IOException {
166 List<RowRange> ranges = new ArrayList<RowRange>();
167 ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
168 ranges.add(new RowRange(Bytes.toBytes(20), false, Bytes.toBytes(""), false));
169 List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges);
170 List<RowRange> expectedRanges = new ArrayList<RowRange>();
171 expectedRanges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
172 expectedRanges.add(new RowRange(Bytes.toBytes(20), false, Bytes.toBytes(""), false));
173 assertRangesEqual(expectedRanges, actualRanges);
174 }
175
176 @Test
177 public void testMergeAndSortWithRowInclusive() throws IOException {
178 List<RowRange> ranges = new ArrayList<RowRange>();
179 ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), true));
180 ranges.add(new RowRange(Bytes.toBytes(20), false, Bytes.toBytes(""), false));
181 List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges);
182 List<RowRange> expectedRanges = new ArrayList<RowRange>();
183 expectedRanges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(""), false));
184 assertRangesEqual(expectedRanges, actualRanges);
185 }
186
187 public void assertRangesEqual(List<RowRange> expected, List<RowRange> actual) {
188 assertEquals(expected.size(), actual.size());
189 for(int i = 0; i < expected.size(); i++) {
190 Assert.assertTrue(Bytes.equals(expected.get(i).getStartRow(), actual.get(i).getStartRow()));
191 Assert.assertTrue(expected.get(i).isStartRowInclusive() ==
192 actual.get(i).isStartRowInclusive());
193 Assert.assertTrue(Bytes.equals(expected.get(i).getStopRow(), actual.get(i).getStopRow()));
194 Assert.assertTrue(expected.get(i).isStopRowInclusive() ==
195 actual.get(i).isStopRowInclusive());
196 }
197 }
198
199 @Test
200 public void testMultiRowRangeFilterWithRangeOverlap() throws IOException {
201 tableName = Bytes.toBytes("testMultiRowRangeFilterWithRangeOverlap");
202 HTable ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE);
203 generateRows(numRows, ht, family, qf, value);
204
205 Scan scan = new Scan();
206 scan.setMaxVersions();
207
208 List<RowRange> ranges = new ArrayList<RowRange>();
209 ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
210 ranges.add(new RowRange(Bytes.toBytes(15), true, Bytes.toBytes(40), false));
211 ranges.add(new RowRange(Bytes.toBytes(65), true, Bytes.toBytes(75), false));
212 ranges.add(new RowRange(Bytes.toBytes(60), true, null, false));
213 ranges.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(80), false));
214
215 MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges);
216 scan.setFilter(filter);
217 int resultsSize = getResultsSize(ht, scan);
218 LOG.info("found " + resultsSize + " results");
219 List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(40), ht);
220 List<Cell> results2 = getScanResult(Bytes.toBytes(60), Bytes.toBytes(""), ht);
221
222 assertEquals(results1.size() + results2.size(), resultsSize);
223
224 ht.close();
225 }
226
227 @Test
228 public void testMultiRowRangeFilterWithoutRangeOverlap() throws IOException {
229 tableName = Bytes.toBytes("testMultiRowRangeFilterWithoutRangeOverlap");
230 HTable ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE);
231 generateRows(numRows, ht, family, qf, value);
232
233 Scan scan = new Scan();
234 scan.setMaxVersions();
235
236 List<RowRange> ranges = new ArrayList<RowRange>();
237 ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false));
238 ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
239 ranges.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(70), false));
240
241 MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges);
242 scan.setFilter(filter);
243 int resultsSize = getResultsSize(ht, scan);
244 LOG.info("found " + resultsSize + " results");
245 List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(20), ht);
246 List<Cell> results2 = getScanResult(Bytes.toBytes(30), Bytes.toBytes(40), ht);
247 List<Cell> results3 = getScanResult(Bytes.toBytes(60), Bytes.toBytes(70), ht);
248
249 assertEquals(results1.size() + results2.size() + results3.size(), resultsSize);
250
251 ht.close();
252 }
253
254 @Test
255 public void testMultiRowRangeFilterWithEmptyStartRow() throws IOException {
256 tableName = Bytes.toBytes("testMultiRowRangeFilterWithEmptyStartRow");
257 HTable ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE);
258 generateRows(numRows, ht, family, qf, value);
259 Scan scan = new Scan();
260 scan.setMaxVersions();
261
262 List<RowRange> ranges = new ArrayList<RowRange>();
263 ranges.add(new RowRange(Bytes.toBytes(""), true, Bytes.toBytes(10), false));
264 ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false));
265
266 MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges);
267 scan.setFilter(filter);
268 int resultsSize = getResultsSize(ht, scan);
269 List<Cell> results1 = getScanResult(Bytes.toBytes(""), Bytes.toBytes(10), ht);
270 List<Cell> results2 = getScanResult(Bytes.toBytes(30), Bytes.toBytes(40), ht);
271 assertEquals(results1.size() + results2.size(), resultsSize);
272
273 ht.close();
274 }
275
276 @Test
277 public void testMultiRowRangeFilterWithEmptyStopRow() throws IOException {
278 tableName = Bytes.toBytes("testMultiRowRangeFilterWithEmptyStopRow");
279 HTable ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE);
280 generateRows(numRows, ht, family, qf, value);
281 Scan scan = new Scan();
282 scan.setMaxVersions();
283
284 List<RowRange> ranges = new ArrayList<RowRange>();
285 ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(""), false));
286 ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false));
287
288 MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges);
289 scan.setFilter(filter);
290 int resultsSize = getResultsSize(ht, scan);
291 List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(""), ht);
292 assertEquals(results1.size(), resultsSize);
293
294 ht.close();
295 }
296
297 @Test
298 public void testMultiRowRangeFilterWithInclusive() throws IOException {
299 tableName = Bytes.toBytes("testMultiRowRangeFilterWithInclusive");
300 HTable ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE);
301 generateRows(numRows, ht, family, qf, value);
302
303 Scan scan = new Scan();
304 scan.setMaxVersions();
305
306 List<RowRange> ranges = new ArrayList<RowRange>();
307 ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
308 ranges.add(new RowRange(Bytes.toBytes(20), true, Bytes.toBytes(40), false));
309 ranges.add(new RowRange(Bytes.toBytes(65), true, Bytes.toBytes(75), false));
310 ranges.add(new RowRange(Bytes.toBytes(60), true, null, false));
311 ranges.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(80), false));
312
313 MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges);
314 scan.setFilter(filter);
315 int resultsSize = getResultsSize(ht, scan);
316 LOG.info("found " + resultsSize + " results");
317 List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(40), ht);
318 List<Cell> results2 = getScanResult(Bytes.toBytes(60), Bytes.toBytes(""), ht);
319
320 assertEquals(results1.size() + results2.size(), resultsSize);
321
322 ht.close();
323 }
324
325 @Test
326 public void testMultiRowRangeFilterWithExclusive() throws IOException {
327 tableName = Bytes.toBytes("testMultiRowRangeFilterWithExclusive");
328 HTable ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE);
329 generateRows(numRows, ht, family, qf, value);
330
331 Scan scan = new Scan();
332 scan.setMaxVersions();
333
334 List<RowRange> ranges = new ArrayList<RowRange>();
335 ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
336 ranges.add(new RowRange(Bytes.toBytes(20), false, Bytes.toBytes(40), false));
337 ranges.add(new RowRange(Bytes.toBytes(65), true, Bytes.toBytes(75), false));
338
339 MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges);
340 scan.setFilter(filter);
341 int resultsSize = getResultsSize(ht, scan);
342 LOG.info("found " + resultsSize + " results");
343 List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(40), ht);
344 List<Cell> results2 = getScanResult(Bytes.toBytes(65), Bytes.toBytes(75), ht);
345
346 assertEquals((results1.size() - 1) + results2.size(), resultsSize);
347
348 ht.close();
349 }
350
351 @Test
352 public void testMultiRowRangeWithFilterListAndOperator() throws IOException {
353 tableName = Bytes.toBytes("TestMultiRowRangeFilterWithFilterListAndOperator");
354 HTable ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE);
355 generateRows(numRows, ht, family, qf, value);
356
357 Scan scan = new Scan();
358 scan.setMaxVersions();
359
360 List<RowRange> ranges1 = new ArrayList<RowRange>();
361 ranges1.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
362 ranges1.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false));
363 ranges1.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(70), false));
364
365 MultiRowRangeFilter filter1 = new MultiRowRangeFilter(ranges1);
366
367 List<RowRange> ranges2 = new ArrayList<RowRange>();
368 ranges2.add(new RowRange(Bytes.toBytes(20), true, Bytes.toBytes(40), false));
369 ranges2.add(new RowRange(Bytes.toBytes(80), true, Bytes.toBytes(90), false));
370
371 MultiRowRangeFilter filter2 = new MultiRowRangeFilter(ranges2);
372
373 FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
374 filterList.addFilter(filter1);
375 filterList.addFilter(filter2);
376 scan.setFilter(filterList);
377 int resultsSize = getResultsSize(ht, scan);
378 LOG.info("found " + resultsSize + " results");
379 List<Cell> results1 = getScanResult(Bytes.toBytes(30), Bytes.toBytes(40), ht);
380
381 assertEquals(results1.size(), resultsSize);
382
383 ht.close();
384 }
385
386 @Test
387 public void testMultiRowRangeWithFilterListOrOperator() throws IOException {
388 tableName = Bytes.toBytes("TestMultiRowRangeFilterWithFilterListOrOperator");
389 HTable ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE);
390 generateRows(numRows, ht, family, qf, value);
391
392 Scan scan = new Scan();
393 scan.setMaxVersions();
394
395 List<RowRange> ranges1 = new ArrayList<RowRange>();
396 ranges1.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false));
397 ranges1.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
398 ranges1.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(70), false));
399
400 MultiRowRangeFilter filter1 = new MultiRowRangeFilter(ranges1);
401
402 List<RowRange> ranges2 = new ArrayList<RowRange>();
403 ranges2.add(new RowRange(Bytes.toBytes(20), true, Bytes.toBytes(40), false));
404 ranges2.add(new RowRange(Bytes.toBytes(80), true, Bytes.toBytes(90), false));
405
406 MultiRowRangeFilter filter2 = new MultiRowRangeFilter(ranges2);
407
408 FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
409 filterList.addFilter(filter1);
410 filterList.addFilter(filter2);
411 scan.setFilter(filterList);
412 int resultsSize = getResultsSize(ht, scan);
413 LOG.info("found " + resultsSize + " results");
414 List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(40), ht);
415 List<Cell> results2 = getScanResult(Bytes.toBytes(60), Bytes.toBytes(70), ht);
416 List<Cell> results3 = getScanResult(Bytes.toBytes(80), Bytes.toBytes(90), ht);
417
418 assertEquals(results1.size() + results2.size() + results3.size(),resultsSize);
419
420 ht.close();
421 }
422
423 private void generateRows(int numberOfRows, HTable ht, byte[] family, byte[] qf, byte[] value)
424 throws IOException {
425 for (int i = 0; i < numberOfRows; i++) {
426 byte[] row = Bytes.toBytes(i);
427 Put p = new Put(row);
428 p.add(family, qf, value);
429 ht.put(p);
430 }
431 TEST_UTIL.flush();
432 }
433
434 private List<Cell> getScanResult(byte[] startRow, byte[] stopRow, HTable ht) throws IOException {
435 Scan scan = new Scan();
436 scan.setMaxVersions();
437 if(!Bytes.toString(startRow).isEmpty()) {
438 scan.setStartRow(startRow);
439 }
440 if(!Bytes.toString(stopRow).isEmpty()) {
441 scan.setStopRow(stopRow);
442 }
443 ResultScanner scanner = ht.getScanner(scan);
444 List<Cell> kvList = new ArrayList<Cell>();
445 Result r;
446 while ((r = scanner.next()) != null) {
447 for (Cell kv : r.listCells()) {
448 kvList.add(kv);
449 }
450 }
451 return kvList;
452 }
453
454 private int getResultsSize(HTable ht, Scan scan) throws IOException {
455 ResultScanner scanner = ht.getScanner(scan);
456 List<Cell> results = new ArrayList<Cell>();
457 Result r;
458 while ((r = scanner.next()) != null) {
459 for (Cell kv : r.listCells()) {
460 results.add(kv);
461 }
462 }
463 return results.size();
464 }
465 }