private static List<MultiRowRangeFilter.RowRange> parseRowRangeParameter( String arg, String rangeSwitch) { final String[] ranges = arg.substring(rangeSwitch.length()).split(";"); final List<MultiRowRangeFilter.RowRange> rangeList = new ArrayList<>(); for (String range : ranges) { String[] startEnd = range.split(",", 2); if (startEnd.length != 2 || startEnd[1].contains(",")) { printUsage("Please specify range in such format as \"--range=a,b\" " + "or, with only one boundary, \"--range=,b\" or \"--range=a,\""); throw new IllegalArgumentException("Wrong range specification: " + range); } String startKey = startEnd[0]; String endKey = startEnd[1]; rangeList.add(new MultiRowRangeFilter.RowRange( Bytes.toBytesBinary(startKey), true, Bytes.toBytesBinary(endKey), false)); } return rangeList; }
@Test(expected=IllegalArgumentException.class) public void testMultiRowRangeWithInvalidRange() throws IOException { List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); // the start row larger than the stop row ranges.add(new RowRange(Bytes.toBytes(80), true, Bytes.toBytes(20), false)); ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(70), false)); new MultiRowRangeFilter(ranges); }
@Test public void testMergeAndSortWithRowExclusive() throws IOException { List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); ranges.add(new RowRange(Bytes.toBytes(20), false, Bytes.toBytes(""), false)); List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges); List<RowRange> expectedRanges = new ArrayList<>(); expectedRanges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); expectedRanges.add(new RowRange(Bytes.toBytes(20), false, Bytes.toBytes(""), false)); assertRangesEqual(expectedRanges, actualRanges); }
/** * @param pbBytes A pb serialized instance * @return An instance of MultiRowRangeFilter * @throws org.apache.hadoop.hbase.exceptions.DeserializationException */ public static MultiRowRangeFilter parseFrom(final byte[] pbBytes) throws DeserializationException { FilterProtos.MultiRowRangeFilter proto; try { proto = FilterProtos.MultiRowRangeFilter.parseFrom(pbBytes); } catch (InvalidProtocolBufferException e) { throw new DeserializationException(e); } int length = proto.getRowRangeListCount(); List<FilterProtos.RowRange> rangeProtos = proto.getRowRangeListList(); List<RowRange> rangeList = new ArrayList<>(length); for (FilterProtos.RowRange rangeProto : rangeProtos) { RowRange range = new RowRange(rangeProto.hasStartRow() ? rangeProto.getStartRow() .toByteArray() : null, rangeProto.getStartRowInclusive(), rangeProto.hasStopRow() ? rangeProto.getStopRow().toByteArray() : null, rangeProto.getStopRowInclusive()); rangeList.add(range); } return new MultiRowRangeFilter(rangeList); }
@Test public void testMultiRowRangeFilterWithExclusive() throws IOException { tableName = TableName.valueOf(name.getMethodName()); TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, 6000000); Table ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE); ht.setReadRpcTimeout(600000); ht.setOperationTimeout(6000000); generateRows(numRows, ht, family, qf, value); Scan scan = new Scan(); scan.setMaxVersions(); List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); ranges.add(new RowRange(Bytes.toBytes(20), false, Bytes.toBytes(40), false)); ranges.add(new RowRange(Bytes.toBytes(65), true, Bytes.toBytes(75), false)); MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges); scan.setFilter(filter); int resultsSize = getResultsSize(ht, scan); LOG.info("found " + resultsSize + " results"); List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(40), ht); List<Cell> results2 = getScanResult(Bytes.toBytes(65), Bytes.toBytes(75), ht); assertEquals((results1.size() - 1) + results2.size(), resultsSize); ht.close(); }
@Test public void testMultiRowRangeFilterWithInclusive() throws IOException { tableName = TableName.valueOf(name.getMethodName()); Table ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE); generateRows(numRows, ht, family, qf, value); Scan scan = new Scan(); scan.setMaxVersions(); List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); ranges.add(new RowRange(Bytes.toBytes(20), true, Bytes.toBytes(40), false)); ranges.add(new RowRange(Bytes.toBytes(65), true, Bytes.toBytes(75), false)); ranges.add(new RowRange(Bytes.toBytes(60), true, null, false)); ranges.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(80), false)); MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges); scan.setFilter(filter); int resultsSize = getResultsSize(ht, scan); LOG.info("found " + resultsSize + " results"); List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(40), ht); List<Cell> results2 = getScanResult(Bytes.toBytes(60), Bytes.toBytes(""), ht); assertEquals(results1.size() + results2.size(), resultsSize); ht.close(); }
@Test public void testMultiRowRangeFilterWithRangeOverlap() throws IOException { tableName = TableName.valueOf(name.getMethodName()); Table ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE); generateRows(numRows, ht, family, qf, value); Scan scan = new Scan(); scan.setMaxVersions(); List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); ranges.add(new RowRange(Bytes.toBytes(15), true, Bytes.toBytes(40), false)); ranges.add(new RowRange(Bytes.toBytes(65), true, Bytes.toBytes(75), false)); ranges.add(new RowRange(Bytes.toBytes(60), true, null, false)); ranges.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(80), false)); MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges); scan.setFilter(filter); int resultsSize = getResultsSize(ht, scan); LOG.info("found " + resultsSize + " results"); List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(40), ht); List<Cell> results2 = getScanResult(Bytes.toBytes(60), Bytes.toBytes(""), ht); assertEquals(results1.size() + results2.size(), resultsSize); ht.close(); }
@Test public void testRanges() throws IOException { byte[] key1Start = new byte[] {-3}; byte[] key1End = new byte[] {-2}; byte[] key2Start = new byte[] {5}; byte[] key2End = new byte[] {6}; byte[] badKey = new byte[] {-10}; MultiRowRangeFilter filter = new MultiRowRangeFilter(Arrays.asList( new MultiRowRangeFilter.RowRange(key1Start, true, key1End, false), new MultiRowRangeFilter.RowRange(key2Start, true, key2End, false) )); filter.filterRowKey(KeyValueUtil.createFirstOnRow(badKey)); /* * FAILS -- includes BAD key! * Expected :SEEK_NEXT_USING_HINT * Actual :INCLUDE * */ assertEquals(Filter.ReturnCode.SEEK_NEXT_USING_HINT, filter.filterCell(null)); }
@Test public void testMultiRowRangeFilterWithoutRangeOverlap() throws IOException { tableName = TableName.valueOf(name.getMethodName()); Table ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE); generateRows(numRows, ht, family, qf, value); Scan scan = new Scan(); scan.setMaxVersions(); List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false)); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); ranges.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(70), false)); MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges); scan.setFilter(filter); int resultsSize = getResultsSize(ht, scan); LOG.info("found " + resultsSize + " results"); List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(20), ht); List<Cell> results2 = getScanResult(Bytes.toBytes(30), Bytes.toBytes(40), ht); List<Cell> results3 = getScanResult(Bytes.toBytes(60), Bytes.toBytes(70), ht); assertEquals(results1.size() + results2.size() + results3.size(), resultsSize); ht.close(); }
@Test public void testOutOfOrderScannerNextException() throws Exception { MultiRowRangeFilter filter = new MultiRowRangeFilter(Arrays.asList( new MultiRowRangeFilter.RowRange(Bytes.toBytes("b"), true, Bytes.toBytes("c"), true), new MultiRowRangeFilter.RowRange(Bytes.toBytes("d"), true, Bytes.toBytes("e"), true) )); filter.filterRowKey(KeyValueUtil.createFirstOnRow(Bytes.toBytes("a"))); assertEquals(Filter.ReturnCode.SEEK_NEXT_USING_HINT, filter.filterCell(null)); filter.filterRowKey(KeyValueUtil.createFirstOnRow(Bytes.toBytes("b"))); assertEquals(Filter.ReturnCode.INCLUDE, filter.filterCell(null)); filter.filterRowKey(KeyValueUtil.createFirstOnRow(Bytes.toBytes("c"))); assertEquals(Filter.ReturnCode.INCLUDE, filter.filterCell(null)); filter.filterRowKey(KeyValueUtil.createFirstOnRow(Bytes.toBytes("d"))); assertEquals(Filter.ReturnCode.INCLUDE, filter.filterCell(null)); filter.filterRowKey(KeyValueUtil.createFirstOnRow(Bytes.toBytes("e"))); assertEquals(Filter.ReturnCode.INCLUDE, filter.filterCell(null)); }
@Test public void testMultiRowRangeFilterWithEmptyStopRow() throws IOException { tableName = TableName.valueOf(name.getMethodName()); Table ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE); generateRows(numRows, ht, family, qf, value); Scan scan = new Scan(); scan.setMaxVersions(); List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(""), false)); ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false)); MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges); scan.setFilter(filter); int resultsSize = getResultsSize(ht, scan); List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(""), ht); assertEquals(results1.size(), resultsSize); ht.close(); }
@Test public void testMultiRowRangeFilterWithEmptyStartRow() throws IOException { tableName = TableName.valueOf(name.getMethodName()); Table ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE); generateRows(numRows, ht, family, qf, value); Scan scan = new Scan(); scan.setMaxVersions(); List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(""), true, Bytes.toBytes(10), false)); ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false)); MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges); scan.setFilter(filter); int resultsSize = getResultsSize(ht, scan); List<Cell> results1 = getScanResult(Bytes.toBytes(""), Bytes.toBytes(10), ht); List<Cell> results2 = getScanResult(Bytes.toBytes(30), Bytes.toBytes(40), ht); assertEquals(results1.size() + results2.size(), resultsSize); ht.close(); }
@Test public void testMergeAndSortWithOverlap() throws IOException { List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); ranges.add(new RowRange(Bytes.toBytes(15), true, Bytes.toBytes(40), false)); ranges.add(new RowRange(Bytes.toBytes(20), true, Bytes.toBytes(30), false)); ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(50), false)); ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(70), false)); ranges.add(new RowRange(Bytes.toBytes(90), true, Bytes.toBytes(100), false)); ranges.add(new RowRange(Bytes.toBytes(95), true, Bytes.toBytes(100), false)); List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges); List<RowRange> expectedRanges = new ArrayList<>(); expectedRanges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(70), false)); expectedRanges.add(new RowRange(Bytes.toBytes(90), true, Bytes.toBytes(100), false)); assertRangesEqual(expectedRanges, actualRanges); }
@Test public void testMergeAndSortWithoutOverlap() throws IOException { List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false)); ranges.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(70), false)); List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges); List<RowRange> expectedRanges = new ArrayList<>(); expectedRanges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); expectedRanges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false)); expectedRanges.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(70), false)); assertRangesEqual(expectedRanges, actualRanges); }
@Test public void testMergeAndSortWithEmptyStopRow() throws IOException { List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); ranges.add(new RowRange(Bytes.toBytes(15), true, Bytes.toBytes(""), false)); ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(70), false)); List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges); List<RowRange> expectedRanges = new ArrayList<>(); expectedRanges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(""), false)); assertRangesEqual(expectedRanges, actualRanges); }
@Test public void testMergeAndSortWithEmptyStartRowAndStopRow() throws IOException { List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); ranges.add(new RowRange(Bytes.toBytes(""), true, Bytes.toBytes(""), false)); ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(70), false)); List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges); List<RowRange> expectedRanges = new ArrayList<>(); expectedRanges.add(new RowRange(Bytes.toBytes(""), true, Bytes.toBytes(""), false)); assertRangesEqual(expectedRanges, actualRanges); }
@Test public void testMergeAndSortWithRowInclusive() throws IOException { List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), true)); ranges.add(new RowRange(Bytes.toBytes(20), false, Bytes.toBytes(""), false)); List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges); List<RowRange> expectedRanges = new ArrayList<>(); expectedRanges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(""), false)); assertRangesEqual(expectedRanges, actualRanges); }
@Test public void testMergeAndSortWithStartRowInclusive() throws IOException { List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); ranges.add(new RowRange(Bytes.toBytes(20), true, Bytes.toBytes(""), false)); List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges); List<RowRange> expectedRanges = new ArrayList<>(); expectedRanges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(""), false)); assertRangesEqual(expectedRanges, actualRanges); }
@Test public void testMultiRowRangeFilter() throws Exception { List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false)); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); ranges.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(70), false)); MultiRowRangeFilter multiRowRangeFilter = new MultiRowRangeFilter(ranges); assertTrue(multiRowRangeFilter.areSerializedFieldsEqual( ProtobufUtil.toFilter(ProtobufUtil.toFilter(multiRowRangeFilter)))); }
@Test public void testMergeAndSortWithEmptyStartRow() throws IOException { List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(""), true, Bytes.toBytes(20), false)); ranges.add(new RowRange(Bytes.toBytes(15), true, Bytes.toBytes(40), false)); List<RowRange> actualRanges = MultiRowRangeFilter.sortAndMerge(ranges); List<RowRange> expectedRanges = new ArrayList<>(); expectedRanges.add(new RowRange(Bytes.toBytes(""), true, Bytes.toBytes(40), false)); assertRangesEqual(expectedRanges, actualRanges); }