List<RowRange> newRanges = new ArrayList<>(ranges.size()); Collections.sort(ranges); if(ranges.get(0).isValid()) { if (ranges.size() == 1) { newRanges.add(ranges.get(0)); for (; i < ranges.size(); i++) { RowRange range = ranges.get(i); if (!range.isValid()) { invalidRanges.add(range); newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, lastStopRow, lastStopRowInclusive)); break; range.isStartRowInclusive() == false))) { if(Bytes.equals(range.stopRow, HConstants.EMPTY_BYTE_ARRAY)) { newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, range.stopRow, range.stopRowInclusive)); break; newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, lastStopRow, lastStopRowInclusive)); i++; range = ranges.get(i); if (!range.isValid()) { invalidRanges.add(range);
this.ranges = new ArrayList<>(); for(RowRange range : ((MultiRowRangeFilter)filter).getRowRanges()) { this.ranges.add(new RowRange(range.getStartRow(), range.isStartRowInclusive(), range.getStopRow(), range.isStopRowInclusive()));
List<RowRange> newRanges = new ArrayList<RowRange>(ranges.size()); Collections.sort(ranges); if(ranges.get(0).isValid()) { if (ranges.size() == 1) { newRanges.add(ranges.get(0)); for (; i < ranges.size(); i++) { RowRange range = ranges.get(i); if (!range.isValid()) { invalidRanges.add(range); newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, lastStopRow, lastStopRowInclusive)); break; range.isStartRowInclusive() == false))) { if(Bytes.equals(range.stopRow, HConstants.EMPTY_BYTE_ARRAY)) { newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, range.stopRow, range.stopRowInclusive)); break; newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, lastStopRow, lastStopRowInclusive)); i++; range = ranges.get(i); if (!range.isValid()) { invalidRanges.add(range);
List<RowRange> newRanges = new ArrayList<>(ranges.size()); Collections.sort(ranges); if(ranges.get(0).isValid()) { if (ranges.size() == 1) { newRanges.add(ranges.get(0)); for (; i < ranges.size(); i++) { RowRange range = ranges.get(i); if (!range.isValid()) { invalidRanges.add(range); newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, lastStopRow, lastStopRowInclusive)); break; range.isStartRowInclusive() == false))) { if(Bytes.equals(range.stopRow, HConstants.EMPTY_BYTE_ARRAY)) { newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, range.stopRow, range.stopRowInclusive)); break; newRanges.add(new RowRange(lastStartRow, lastStartRowInclusive, lastStopRow, lastStopRowInclusive)); i++; range = ranges.get(i); if (!range.isValid()) { invalidRanges.add(range);
this.ranges = new ArrayList<>(); for(RowRange range : ((MultiRowRangeFilter)filter).getRowRanges()) { this.ranges.add(new RowRange(range.getStartRow(), range.isStartRowInclusive(), range.getStopRow(), range.isStopRowInclusive()));
this.ranges = new ArrayList<>(); for(RowRange range : ((MultiRowRangeFilter)filter).getRowRanges()) { this.ranges.add(new RowRange(range.getStartRow(), range.isStartRowInclusive(), range.getStopRow(), range.isStopRowInclusive()));
rangeIterator.remove(); final RowRange rowRange = new RowRange(startRow, true, stopRow, false); if (regionRanges.size() == 1) { regionScan.setFilter(filter); regionScan.setStartRow(regionRanges.get(0).getStartRow()); regionScan.setStopRow(regionRanges.get(0).getStopRow()); } else if (regionRanges.size() > 1) { regionScan.setFilter(rowRangeFilter); regionScan.setStartRow(regionRanges.get(0).getStartRow()); regionScan.setStopRow(regionRanges.get(regionRanges.size() - 1).getStopRow());
/** * calculate the position where the row key in the ranges list. * * @param rowKey the row key to calculate * @return index the position of the row key */ private int getNextRangeIndex(byte[] rowKey) { RowRange temp = new RowRange(rowKey, true, null, true); int index = Collections.binarySearch(rangeList, temp); if (index < 0) { int insertionPosition = -index - 1; // check if the row key in the range before the insertion position if (insertionPosition != 0 && rangeList.get(insertionPosition - 1).contains(rowKey)) { return insertionPosition - 1; } // check if the row key is before the first range if (insertionPosition == 0 && !rangeList.get(insertionPosition).contains(rowKey)) { return ROW_BEFORE_FIRST_RANGE; } if (!initialized) { initialized = true; } return insertionPosition; } // the row key equals one of the start keys, and the the range exclude the start key if(rangeList.get(index).startRowInclusive == false) { EXCLUSIVE = true; } return index; }
@Test public void testMultiRangeFilterOrList() throws IOException { String prefix = "testMultiRangeFilterOrList_"; int rowCount = 10; byte[][] rowKeys = dataHelper.randomData(prefix, rowCount); Arrays.sort(rowKeys, Bytes.BYTES_COMPARATOR); List<Put> puts = new ArrayList<>(); for (byte[] rowKey : rowKeys) { puts.add( new Put(rowKey) .addColumn(COLUMN_FAMILY, Bytes.toBytes("q1"), Bytes.toBytes("val1"))); } Table table = getDefaultTable(); table.put(puts); MultiRowRangeFilter rangeFilter = new MultiRowRangeFilter(Arrays.asList( // rows 1 & 2 new RowRange(rowKeys[1], true, rowKeys[3], false) )); PrefixFilter prefixFilter = new PrefixFilter(rowKeys[8]); FilterList filterList = new FilterList(Operator.MUST_PASS_ONE, rangeFilter, prefixFilter); Scan scan = new Scan().addFamily(COLUMN_FAMILY).setFilter(filterList); ResultScanner scanner = table.getScanner(scan); Result[] results = scanner.next(rowCount + 2); Assert.assertEquals(3, results.length); // first range: rows 1 & 2 Assert.assertArrayEquals(rowKeys[1], results[0].getRow()); Assert.assertArrayEquals(rowKeys[2], results[1].getRow()); // second range: rows 9 Assert.assertArrayEquals(rowKeys[8], results[2].getRow()); }
/** * @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<RowRange>(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); } try { return new MultiRowRangeFilter(rangeList); } catch (IOException e) { throw new DeserializationException("Fail to instantiate the MultiRowRangeFilter", e); } }
/** * calculate the position where the row key in the ranges list. * * @param rowKey the row key to calculate * @return index the position of the row key */ private int getNextRangeIndex(byte[] rowKey) { RowRange temp = new RowRange(rowKey, true, null, true); int index = Collections.binarySearch(rangeList, temp); if (index < 0) { int insertionPosition = -index - 1; // check if the row key in the range before the insertion position if (insertionPosition != 0 && rangeList.get(insertionPosition - 1).contains(rowKey)) { return insertionPosition - 1; } // check if the row key is before the first range if (insertionPosition == 0 && !rangeList.get(insertionPosition).contains(rowKey)) { return ROW_BEFORE_FIRST_RANGE; } if (!initialized) { initialized = true; } return insertionPosition; } // the row key equals one of the start keys, and the the range exclude the start key if(rangeList.get(index).startRowInclusive == false) { EXCLUSIVE = true; } return index; }
ranges1.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(15), false)); ranges1.add(new RowRange(Bytes.toBytes(9980), true, Bytes.toBytes(9985), false)); ranges2.add(new RowRange(Bytes.toBytes(15), true, Bytes.toBytes(20), false)); ranges2.add(new RowRange(Bytes.toBytes(9985), true, Bytes.toBytes(9990), false));
public MultiRowRangeFilter getMultiRowRangeFilter(final List<ByteArrayRange> ranges) { // create the multi-row filter final List<RowRange> rowRanges = new ArrayList<>(); if ((ranges == null) || ranges.isEmpty()) { rowRanges.add( new RowRange(HConstants.EMPTY_BYTE_ARRAY, true, HConstants.EMPTY_BYTE_ARRAY, false)); } else { for (final ByteArrayRange range : ranges) { if (range.getStart() != null) { final byte[] startRow = range.getStart(); byte[] stopRow; if (!range.isSingleValue()) { stopRow = range.getEndAsNextPrefix(); } else { stopRow = ByteArrayUtils.getNextPrefix(range.getStart()); } final RowRange rowRange = new RowRange(startRow, true, stopRow, false); rowRanges.add(rowRange); } } } // Create the multi-range filter try { return new MultiRowRangeFilter(rowRanges); } catch (final IOException e) { LOGGER.error("Error creating range filter.", e); } return null; }
/** * @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 testOneRowRange() throws IOException { tableName = TableName.valueOf(name.getMethodName()); Table ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE); generateRows(numRows, ht, family, qf, value); ArrayList<MultiRowRangeFilter.RowRange> rowRangesList = new ArrayList<>(); rowRangesList .add(new MultiRowRangeFilter.RowRange(Bytes.toBytes(50), true, Bytes.toBytes(50), true)); Scan scan = new Scan(); scan.setFilter(new MultiRowRangeFilter(rowRangesList)); int resultsSize = getResultsSize(ht, scan); assertEquals(1, resultsSize); rowRangesList.clear(); rowRangesList .add(new MultiRowRangeFilter.RowRange(Bytes.toBytes(50), true, Bytes.toBytes(51), false)); scan = new Scan(); scan.setFilter(new MultiRowRangeFilter(rowRangesList)); resultsSize = getResultsSize(ht, scan); assertEquals(1, resultsSize); rowRangesList.clear(); rowRangesList .add(new MultiRowRangeFilter.RowRange(Bytes.toBytes(50), true, Bytes.toBytes(51), true)); scan = new Scan(); scan.setFilter(new MultiRowRangeFilter(rowRangesList)); resultsSize = getResultsSize(ht, scan); assertEquals(2, resultsSize); ht.close(); }
@Test public void testMultiRowRangeWithFilterListOrOperator() 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> ranges1 = new ArrayList<>(); ranges1.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false)); ranges1.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false)); ranges1.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(70), false)); MultiRowRangeFilter filter1 = new MultiRowRangeFilter(ranges1); List<RowRange> ranges2 = new ArrayList<>(); ranges2.add(new RowRange(Bytes.toBytes(20), true, Bytes.toBytes(40), false)); ranges2.add(new RowRange(Bytes.toBytes(80), true, Bytes.toBytes(90), false)); MultiRowRangeFilter filter2 = new MultiRowRangeFilter(ranges2); FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE); filterList.addFilter(filter1); filterList.addFilter(filter2); scan.setFilter(filterList); 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(70), ht); List<Cell> results3 = getScanResult(Bytes.toBytes(80), Bytes.toBytes(90), ht); assertEquals(results1.size() + results2.size() + results3.size(),resultsSize); ht.close(); }