public static <T, TOKEN> TokenBackedBasicResultsPage<T, TOKEN> getFirstPage(BatchingVisitable<T> v, int numToVisitArg, Function<T, TOKEN> tokenExtractor) { Preconditions.checkArgument(numToVisitArg >= 0, "numToVisit cannot be negative. Value was: %d", numToVisitArg); if (numToVisitArg == Integer.MAX_VALUE) { // prevent issue with overflow numToVisitArg--; } final int numToVisit = numToVisitArg + 1; ImmutableList<T> list = BatchingVisitableView.of(v).limit(numToVisit).immutableCopy(); Preconditions.checkState(list.size() <= numToVisit); if (list.size() >= numToVisit) { TOKEN token = tokenExtractor.apply(list.get(list.size() - 1)); list = list.subList(0, numToVisit-1); return new SimpleTokenBackedResultsPage<T, TOKEN>(token, list, true); } return new SimpleTokenBackedResultsPage<T, TOKEN>(null, list, false); }
public void deleteRanges(Iterable<RangeRequest> ranges) { BatchingVisitables.concat(getRanges(ranges)) .transform(MetadataRowResult.getRowNameFun()) .batchAccept(1000, new AbortingVisitor<List<MetadataRow>, RuntimeException>() { @Override public boolean visit(List<MetadataRow> rows) { delete(rows); return true; } }); }
public void forEach(final Visitor<T> visitor) { forEach(BatchingVisitables.DEFAULT_BATCH_SIZE, visitor); }
public static <T> List<T> take(BatchingVisitable<T> v, final int howMany, final boolean includeFirst) { BatchingVisitableView<T> visitable = BatchingVisitableView.of(v); if (!includeFirst) { visitable = visitable.skip(1); } return visitable.limit(howMany).immutableCopy(); }
public static <T> List<T> copyToList(BatchingVisitable<T> v) { return BatchingVisitableView.of(v).immutableCopy(); }
@Test public void testLimit() { BatchingVisitable<Long> visitor = ListVisitor.create(Lists.newArrayList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L)); BatchingVisitableView<Long> limited = BatchingVisitableView.of(visitor).limit(3); ImmutableList<Long> copy = limited.immutableCopy(); assertEquals("limit produced unexpected result", ImmutableList.of(0L, 1L, 2L), copy); assertFalse("alwaysFalse should be false", limited.batchAccept(1, AbortingVisitors.alwaysFalse())); assertTrue("alwaysTrue should be true", limited.batchAccept(1, AbortingVisitors.alwaysTrue())); assertTrue("alwaysTrue should be true twice", limited.batchAccept(2, AbortingVisitors.alwaysTrue())); assertTrue("alwaysTrue should be true thrice", limited.batchAccept(3, AbortingVisitors.alwaysTrue())); assertTrue("batchAccept should be true", limited.batchAccept(2, AbortingVisitors.batching(cv))); assertEquals("CountingVisitor had wrong count", 3, cv.count); assertTrue("batchAccept should be true", limited.batchAccept(3, AbortingVisitors.batching(cv))); assertEquals("CountingVisitor had wrong count", 6, cv.count); assertTrue("batchAccept should be true", limited.batchAccept(4, AbortingVisitors.batching(cv))); assertEquals("CountingVisitor had wrong count", 9, cv.count); limited.skip(3).batchAccept(1, AbortingVisitors.alwaysFalse())); assertFalse("batchAccept should be false", limited.batchAccept(1, AbortingVisitors.batching(lv))); lv = new LimitVisitor<>(4); assertTrue("batchAccept should be true", limited.batchAccept(1, AbortingVisitors.batching(lv))); lv = new LimitVisitor<>(2); assertFalse("batchAccept should be false", limited.batchAccept(1, AbortingVisitors.batching(lv))); assertFalse("batchAccept should be false", limited.hintBatchSize(10) .batchAccept(1, AbortingVisitors.alwaysFalse()));
private List<SweepPriority> loadPriorities(Transaction tx) { SweepPriorityTable table = sweepTableFactory.getSweepPriorityTable(tx); // Load a single column first for each row. This is a much more efficient query on Cassandra // than the full table scan that occurs otherwise. List<SweepPriorityRowResult> rows = table.getRange(RangeRequest.builder() .retainColumns(SweepPriorityTable.getColumnSelection(SweepPriorityNamedColumn.CELLS_DELETED)) .batchHint(READ_BATCH_SIZE) .build()) .immutableCopy(); // Fetch all columns for the above rows directly return Lists.transform(table.getRows(Lists.transform(rows, SweepPriorityRowResult::getRowName)), SweepPriorityStoreImpl::hydrate); }
@Test public void testUnique() { BatchingVisitable<Long> visitor = ListVisitor.create(Lists.newArrayList( 0L, 1L, 1L, 2L, 2L, 2L, 3L, 4L, 5L, 5L, 6L, 7L, 7L)); BatchingVisitableView<Long> bv = BatchingVisitableView.of(visitor); assertEquals("unexpected result for unique view", ImmutableList.of(0L, 1L, 2L, 3L), bv.unique().limit(4).immutableCopy()); }
BatchingVisitableView.of(tx.getRowsColumnRange( TABLE, ImmutableList.of(row), BatchColumnRangeSelection.create(null, null, 10)).get(row)) .transform(Map.Entry::getKey) .immutableCopy()); assertEquals(ImmutableList.of(firstCell, secondCell), cells);
@Override protected List<byte[]> getRange(Transaction txn, long startInclusive, long endExclusive) { KvRowsTable table = BenchmarksTableFactory.of().getKvRowsTable(txn); return table.getRange( RangeRequest.builder() .startRowInclusive(KvRowsRow.of(bucket, startInclusive)) .endRowExclusive(KvRowsRow.of(bucket, endExclusive)) .batchHint(batchSize) .build()) .hintBatchSize(batchSize) .transform(KvRowsRowResult::getData) .immutableCopy(); }
@Test public void testVisitWhile() { List<Long> longList = Lists.newArrayList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L); BatchingVisitable<Long> visitable = ListVisitor.create(longList); BatchingVisitableView<Long> view = BatchingVisitables.visitWhile( BatchingVisitableView.of(visitable), (input) -> input.longValue() < 5L); assertEquals("visitWhile visited the wrong number of elements", 5L, view.size()); assertEquals("visitWhile visited the wrong element first", 0L, view.getFirst().longValue()); assertEquals("visitWhile visited the wrong element last", 4L, view.getLast().longValue()); assertTrue("visitWhile visited the wrong elements", view.immutableCopy().containsAll(ImmutableSet.of(0L, 1L, 2L, 3L, 4L))); visitable = ListVisitor.create(Lists.reverse(longList)); view = BatchingVisitables.visitWhile( BatchingVisitableView.of(visitable), (input) -> input.longValue() >= 5L); assertEquals("visitWhile visited the wrong number of elements", 5L, view.size()); assertEquals("visitWhile visited the wrong element first", 9L, view.getFirst().longValue()); assertEquals("visitWhile visited the wrong element last", 5L, view.getLast().longValue()); assertTrue("visitWhile visited the wrong elements", view.immutableCopy().containsAll(ImmutableSet.of(5L, 6L, 7L, 8L, 9L))); }
@Test public void testSkip() { BatchingVisitable<Long> visitor = ListVisitor.create(Lists.newArrayList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L)); ImmutableList<Long> copy = BatchingVisitableView.of(visitor).skip(5).immutableCopy(); assertEquals("unexpected list remnants after skipping", ImmutableList.of(5L, 6L, 7L), copy); }
private List<String> extractStringsFromVisitables(Iterable<BatchingVisitable<RowResult<byte[]>>> visitables) { return BatchingVisitables.concat(visitables) .transform(RowResult::getOnlyColumnValue) .transform(bytes -> new String(bytes, StandardCharsets.UTF_8)) .immutableCopy(); } }
@Test public void testImmutableCopyRespectsCustomBatchSize() throws Exception { AbstractBatchingVisitable bv = mock(AbstractBatchingVisitable.class); BatchingVisitableView bvv = BatchingVisitableView.of(bv); bvv.hintBatchSize(2).immutableCopy(); verify(bv).batchAcceptSizeHint(eq(2), any()); }
public static <T> BatchingVisitableView<T> singleton(final T t) { return BatchingVisitableView.of(new BatchingVisitable<T>() { @Override public <K extends Exception> boolean batchAccept(int batchSize, AbortingVisitor<? super List<T>, K> v) throws K { return v.visit(ImmutableList.of(t)); } }); }
public void deleteRanges(Iterable<RangeRequest> ranges) { BatchingVisitables.concat(getRanges(ranges)).batchAccept(1000, new AbortingVisitor<List<CreatedIdxRowResult>, RuntimeException>() { @Override public boolean visit(List<CreatedIdxRowResult> rowResults) { Multimap<CreatedIdxRow, CreatedIdxColumn> toRemove = HashMultimap.create(); for (CreatedIdxRowResult rowResult : rowResults) { for (CreatedIdxColumnValue columnValue : rowResult.getColumnValues()) { toRemove.put(rowResult.getRowName(), columnValue.getColumnName()); } } delete(toRemove); return true; } }); }
@Test public void testConcat() { BatchingVisitable<Long> visitor = ListVisitor.create(Lists.newArrayList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L)); BatchingVisitableView<Long> limited = BatchingVisitableView.of(visitor).limit(3); BatchingVisitableView<Long> concat = BatchingVisitables.concat(limited, visitor); assertTrue("concatenated batchAccept should be true", concat.batchAccept(2, AbortingVisitors .batching(AbortingVisitors.alwaysTrue()))); }
@Test public void testBatchHints() { BatchingVisitable<Long> visitor = ListVisitor.create(Lists.newArrayList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L)); Function<List<Long>, List<String>> trans = (input) -> { assertEquals("batched item had wrong size", 2, input.size()); return Lists.transform(input, Functions.toStringFunction()); }; BatchingVisitableView<String> visitable = BatchingVisitableView.of(visitor).transformBatch(trans) .hintBatchSize(2); final Mutable<Boolean> hasTripped = Mutables.newMutable(); visitable.batchAccept(10000, item -> { hasTripped.set(true); assertEquals("batched item had wrong size", 8, item.size()); return false; }); assertTrue("should have been tripped!", hasTripped.get()); }
byte[] row = cellValuesForRow.getKey(); BatchingVisitableView<Entry<Cell, byte[]>> visitable = BatchingVisitableView.of(cellValuesForRow.getValue()); NavigableMap<Cell, ByteBuffer> readsInRange = Maps.transformValues( getReadsInColumnRangeSkippingWrites(table, row, columnRange), ByteBuffer::wrap); boolean isEqual = visitable.transformBatch(cellValues -> filterWritesFromCells(cellValues, table)) .isEqual(readsInRange.entrySet()); if (!isEqual) { handleTransactionConflict(table);