private Result getReverseScanResult(byte[] tableName, byte[] row, byte[] family) throws IOException { Scan scan = new Scan(row); scan.setReversed(true); scan.addFamily(family); scan.setStartRow(row); try (Table table = getTable(tableName); ResultScanner scanner = table.getScanner(scan)) { return scanner.next(); } }
/** * Set the scan attribute * * @param reversed if true, scan will be backward order * @param mobScanRaw if true, scan will get the mob reference * @return this */ public void setScan(Scan scan, boolean reversed, boolean mobScanRaw) { scan.setReversed(reversed); scan.setMaxVersions(4); if(mobScanRaw) { scan.setAttribute(MobConstants.MOB_SCAN_RAW, Bytes.toBytes(Boolean.TRUE)); } }
/** * Setting the max result size allows us to control how many cells we expect to see on each call * to next on the scanner. Test a variety of different sizes for correctness * @throws Exception */ @Test public void testExpectedNumberOfCellsPerPartialResult() throws Exception { Scan scan = new Scan(); testExpectedNumberOfCellsPerPartialResult(scan); scan.setReversed(true); testExpectedNumberOfCellsPerPartialResult(scan); }
@Test public void testReverseScanNoStopKey() throws Exception { int start = 765; List<Result> results = doScan( createScan().withStartRow(Bytes.toBytes(String.format("%03d", start))).setReversed(true)); assertEquals(start + 1, results.size()); IntStream.range(0, start + 1).forEach(i -> assertResultEquals(results.get(i), start - i)); }
/** * Test the method {@link Result#createCompleteResult(Iterable)} * @throws Exception */ @Test public void testPartialResultsReassembly() throws Exception { Scan scan = new Scan(); testPartialResultsReassembly(scan); scan.setReversed(true); testPartialResultsReassembly(scan); }
@Test public void testReversedScanAll() throws Exception { List<Result> results = doScan(createScan().setReversed(true)); assertEquals(COUNT, results.size()); IntStream.range(0, COUNT).forEach(i -> assertResultEquals(results.get(i), COUNT - i - 1)); }
@Test public void testPrefixFilterWithReverseScan() throws Exception { // Grab rows from group one (half of total) long expectedRows = this.numRows / 2; long expectedKeys = this.colsPerRow; Scan s = new Scan(); s.setReversed(true); s.setFilter(new PrefixFilter(Bytes.toBytes("testRowOne"))); verifyScan(s, expectedRows, expectedKeys); }
private void testReversedScan(int start, boolean startInclusive, int stop, boolean stopInclusive, int limit) throws Exception { Scan scan = createScan().withStartRow(Bytes.toBytes(String.format("%03d", start)), startInclusive) .withStopRow(Bytes.toBytes(String.format("%03d", stop)), stopInclusive).setReversed(true); if (limit > 0) { scan.setLimit(limit); } List<Result> results = doScan(scan); int actualStart = startInclusive ? start : start - 1; int actualStop = stopInclusive ? stop - 1 : stop; int count = actualStart - actualStop; if (limit > 0) { count = Math.min(count, limit); } assertEquals(count, results.size()); IntStream.range(0, count).forEach(i -> assertResultEquals(results.get(i), actualStart - i)); }
private void scanTestNull(Table ht, byte[] row, byte[] family, byte[] value, boolean isReversedScan) throws Exception { Scan scan = new Scan(); scan.setReversed(isReversedScan); scan.addColumn(family, null); Result result = getSingleScanResult(ht, scan); assertSingleResult(result, row, family, HConstants.EMPTY_BYTE_ARRAY, value); scan = new Scan(); scan.setReversed(isReversedScan); scan.addColumn(family, HConstants.EMPTY_BYTE_ARRAY); result = getSingleScanResult(ht, scan); assertSingleResult(result, row, family, HConstants.EMPTY_BYTE_ARRAY, value); scan = new Scan(); scan.setReversed(isReversedScan); scan.addFamily(family); result = getSingleScanResult(ht, scan); assertSingleResult(result, row, family, HConstants.EMPTY_BYTE_ARRAY, value); scan = new Scan(); scan.setReversed(isReversedScan); result = getSingleScanResult(ht, scan); assertSingleResult(result, row, family, HConstants.EMPTY_BYTE_ARRAY, value); }
private void testReversedScan(int start, boolean startInclusive, int stop, boolean stopInclusive, int limit) throws Exception { Scan scan = createScan() .withStartRow(Bytes.toBytes(String.format("%03d", start)), startInclusive) .withStopRow(Bytes.toBytes(String.format("%03d", stop)), stopInclusive).setReversed(true); if (limit > 0) { scan.setLimit(limit); } List<Result> results = doScan(scan); int actualStart = startInclusive ? start : start - 1; int actualStop = stopInclusive ? stop - 1 : stop; int count = actualStart - actualStop; if (limit > 0) { count = Math.min(count, limit); } assertEquals(count, results.size()); for (int i = 0; i < count; i++) { assertResultEquals(results.get(i), actualStart - i); } }
private void testSmallReversedScanInternal(String[] inputRowKeys) throws IOException { int rowCount = inputRowKeys.length; for (int i = 0; i < rowCount; i++) { Put put = new Put(Bytes.toBytes(inputRowKeys[i])); put.addColumn(COLUMN_FAMILY, null, Bytes.toBytes(i)); htable.put(put); } Scan scan = new Scan(); scan.setReversed(true); scan.setSmall(true); ResultScanner scanner = htable.getScanner(scan); Result r; int value = rowCount; while ((r = scanner.next()) != null) { Assert.assertArrayEquals(r.getValue(COLUMN_FAMILY, null), Bytes.toBytes(--value)); Assert.assertArrayEquals(r.getRow(), Bytes.toBytes(inputRowKeys[value])); } Assert.assertEquals(0, value); }
private Result getReverseScanResult(Table table, byte[] row, byte[] fam) throws IOException { Scan scan = new Scan(row); scan.setSmall(true); scan.setReversed(true); scan.setCaching(1); scan.addFamily(fam); try (ResultScanner scanner = table.getScanner(scan)) { return scanner.next(); } }
private void initiateScan(Table table) throws IOException { Scan scan = new Scan(); if (reverse) { scan.setReversed(true); } CustomInnerRegionObserver.getCdl().set(latch); ResultScanner resScanner = table.getScanner(scan); int i = (reverse ? ROWS.length - 1 : 0); boolean resultFound = false; for (Result result : resScanner) { resultFound = true; System.out.println(result); if (!reverse) { assertTrue(Bytes.equals(result.getRow(), ROWS[i])); i++; } else { assertTrue(Bytes.equals(result.getRow(), ROWS[i])); i--; } } assertTrue(resultFound); } }
@Test public void testReversedBatchDoNotAllowPartial() throws InterruptedException, ExecutionException { // we set batch to 2 and max result size to 1, then server will only returns one result per call // but we should get 2 + 1 for every row. List<Result> results = TABLE.scanAll(new Scan().setBatch(2).setMaxResultSize(1).setReversed(true)).get(); assertEquals(2 * COUNT, results.size()); for (int i = 0; i < COUNT; i++) { int row = COUNT - i - 1; Result firstTwo = results.get(2 * i); assertEquals(String.format("%02d", row), Bytes.toString(firstTwo.getRow())); assertEquals(2, firstTwo.size()); assertEquals(row, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[0]))); assertEquals(2 * row, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[1]))); Result secondOne = results.get(2 * i + 1); assertEquals(String.format("%02d", row), Bytes.toString(secondOne.getRow())); assertEquals(1, secondOne.size()); assertEquals(3 * row, Bytes.toInt(secondOne.getValue(FAMILY, CQS[2]))); } } }
/** * Run through a variety of test configurations with a small scan * @param table * @param reversed * @param rows * @param columns * @throws Exception */ private void testSmallScan(Table table, boolean reversed, int rows, int columns) throws Exception { Scan baseScan = new Scan(); baseScan.setReversed(reversed); baseScan.setSmall(true); Scan scan = new Scan(baseScan); verifyExpectedCounts(table, scan, rows, columns); scan = new Scan(baseScan); scan.setMaxResultSize(1); verifyExpectedCounts(table, scan, rows, columns); scan = new Scan(baseScan); scan.setMaxResultSize(1); scan.setCaching(Integer.MAX_VALUE); verifyExpectedCounts(table, scan, rows, columns); }
/** * Order of cells in partial results matches the ordering of cells from complete results * @throws Exception */ @Test public void testOrderingOfCellsInPartialResults() throws Exception { Scan scan = new Scan(); for (int col = 1; col <= NUM_COLS; col++) { scan.setMaxResultSize(getResultSizeForNumberOfCells(col)); testOrderingOfCellsInPartialResults(scan); // Test again with a reversed scanner scan.setReversed(true); testOrderingOfCellsInPartialResults(scan); } }
protected Scan createReversedScanWithSparseFilter() { return new Scan().setMaxResultSize(Long.MAX_VALUE).setCaching(Integer.MAX_VALUE) .setReversed(true).setNeedCursorResult(true).setAllowPartialResults(true) .setFilter(new SparseFilter(true)); }
public Result getClosestRowBefore(Region r, byte[] row, byte[] family) throws IOException { Scan scan = new Scan(row); scan.setSmall(true); scan.setCaching(1); scan.setReversed(true); scan.addFamily(family); try (RegionScanner scanner = r.getScanner(scan)) { List<Cell> cells = new ArrayList<>(1); scanner.next(cells); if (r.getRegionInfo().isMetaRegion() && !isTargetTable(row, cells.get(0))) { return null; } return Result.create(cells); } }
/** * @return Get closest metatable region row to passed <code>row</code> */ @NonNull private static RegionInfo getClosestRegionInfo(Connection connection, @NonNull final TableName tableName, @NonNull final byte[] row) throws IOException { byte[] searchRow = RegionInfo.createRegionName(tableName, row, HConstants.NINES, false); Scan scan = getMetaScan(connection, 1); scan.setReversed(true); scan.withStartRow(searchRow); try (ResultScanner resultScanner = getMetaHTable(connection).getScanner(scan)) { Result result = resultScanner.next(); if (result == null) { throw new TableNotFoundException("Cannot find row in META " + " for table: " + tableName + ", row=" + Bytes.toStringBinary(row)); } RegionInfo regionInfo = getRegionInfo(result); if (regionInfo == null) { throw new IOException("RegionInfo was null or empty in Meta for " + tableName + ", row=" + Bytes.toStringBinary(row)); } return regionInfo; } }
public void testExpectedValuesOfPartialResults(boolean reversed) throws Exception { Scan partialScan = new Scan(); partialScan.setMaxVersions(); // Max result size of 1 ensures that each RPC request will return a single cell. The scanner // will need to reconstruct the results into a complete result before returning to the caller partialScan.setMaxResultSize(1); partialScan.setReversed(reversed); ResultScanner partialScanner = TABLE.getScanner(partialScan); final int startRow = reversed ? ROWS.length - 1 : 0; final int endRow = reversed ? -1 : ROWS.length; final int loopDelta = reversed ? -1 : 1; String message; for (int row = startRow; row != endRow; row = row + loopDelta) { message = "Ensuring the expected keyValues are present for row " + row; List<Cell> expectedKeyValues = createKeyValuesForRow(ROWS[row], FAMILIES, QUALIFIERS, VALUE); Result result = partialScanner.next(); assertFalse(result.mayHaveMoreCellsInRow()); verifyResult(result, expectedKeyValues, message); } partialScanner.close(); }