@Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, final ConsistentVisitor<T, K> v) throws K { visitable.batchAccept(batchSizeHint, batch -> { for (T t : batch) { if (!condition.apply(t)) { return false; } boolean keepGoing = v.visitOne(t); if (!keepGoing) { return false; } } return true; }); } });
@Override public <K extends Exception> void batchAcceptSizeHint(int batchSize, ConsistentVisitor<T, K> v) throws K { /* * Iterables.partition allocates an array of size batchSize, so avoid an OOM by making sure * it's not too big. */ batchSize = Math.min(batchSize, MAX_BATCH_SIZE); for (List<T> list : Iterables.partition(iterable, batchSize)) { if (!v.visit(list)) { return; } } } }
@Override final public <K extends Exception> boolean batchAccept(int batchSize, AbortingVisitor<? super List<T>, K> v) throws K { Preconditions.checkArgument(batchSize > 0); if (v instanceof ConsistentVisitor) { @SuppressWarnings("unchecked") AbortingVisitor<List<T>, K> v2 = (AbortingVisitor<List<T>, K>) v; ConsistentVisitor<T, K> consistentVisitor = (ConsistentVisitor<T, K>) v2; Preconditions.checkState(consistentVisitor.visitorAlwaysReturnedTrue, "passed a visitor that has already said stop"); batchAcceptSizeHint(batchSize, consistentVisitor); return consistentVisitor.visitorAlwaysReturnedTrue; } AbstractBatchingVisitable.ConsistentVisitor<T, K> consistentVisitor = AbstractBatchingVisitable.ConsistentVisitor.create(batchSize, v); batchAcceptSizeHint(batchSize, consistentVisitor); if (consistentVisitor.visitorAlwaysReturnedTrue && !consistentVisitor.buffer.isEmpty()) { Preconditions.checkState(consistentVisitor.buffer.size() < batchSize); return v.visit(Collections.unmodifiableList(consistentVisitor.buffer)); } else { return consistentVisitor.visitorAlwaysReturnedTrue; } }
private boolean processBufferBatches() throws K { List<List<T>> batches = Lists.partition(buffer, batchSize); for (List<T> batch : batches) { if (batch.size() != batchSize) { continue; } if (!visitBufferWithDelegate(Collections.unmodifiableList(batch))) { return false; } } List<T> lastBatch = batches.get(batches.size()-1); if (lastBatch.size() == batchSize) { buffer = Lists.newArrayList(); } else { buffer = Lists.newArrayList(lastBatch); } return true; } }
@Override final public <K extends Exception> boolean batchAccept(int batchSize, AbortingVisitor<? super List<T>, K> v) throws K { Preconditions.checkArgument(batchSize > 0); if (v instanceof ConsistentVisitor) { @SuppressWarnings("unchecked") AbortingVisitor<List<T>, K> v2 = (AbortingVisitor<List<T>, K>) v; ConsistentVisitor<T, K> consistentVisitor = (ConsistentVisitor<T, K>) v2; Preconditions.checkState(consistentVisitor.visitorAlwaysReturnedTrue, "passed a visitor that has already said stop"); batchAcceptSizeHint(batchSize, consistentVisitor); return consistentVisitor.visitorAlwaysReturnedTrue; } AbstractBatchingVisitable.ConsistentVisitor<T, K> consistentVisitor = AbstractBatchingVisitable.ConsistentVisitor.create(batchSize, v); batchAcceptSizeHint(batchSize, consistentVisitor); if (consistentVisitor.visitorAlwaysReturnedTrue && !consistentVisitor.buffer.isEmpty()) { Preconditions.checkState(consistentVisitor.buffer.size() < batchSize); return v.visit(Collections.unmodifiableList(consistentVisitor.buffer)); } else { return consistentVisitor.visitorAlwaysReturnedTrue; } }
@Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, final ConsistentVisitor<T, K> v) throws K { visitable.batchAccept(batchSizeHint, batch -> { for (T t : batch) { if (!condition.apply(t)) { return false; } boolean keepGoing = v.visitOne(t); if (!keepGoing) { return false; } } return true; }); } });
@Override public <K extends Exception> void batchAcceptSizeHint(int batchSize, ConsistentVisitor<T, K> v) throws K { /* * Iterables.partition allocates an array of size batchSize, so avoid an OOM by making sure * it's not too big. */ batchSize = Math.min(batchSize, MAX_BATCH_SIZE); for (List<T> list : Iterables.partition(iterable, batchSize)) { if (!v.visit(list)) { return; } } } }
@Override public boolean visit(List<T> list) throws K { if (!visitorAlwaysReturnedTrue) { throw new IllegalStateException("Cannot keep visiting if visitor returns false."); } if (buffer.isEmpty() && list.size() == batchSize) { // Special case: We have exactly one batch. return visitBufferWithDelegate(Collections.unmodifiableList(list)); } buffer.addAll(list); if (buffer.size() < batchSize) { return true; } return processBufferBatches(); }
@Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, ConsistentVisitor<T, K> v) throws K { long nextId = startId; boolean abortedOrFinished = false; while (!abortedOrFinished) { List<T> resultBatchWithDuplicates = sublistProvider.getBatchAllowDuplicates(nextId, batchSizeHint); boolean abortedByVisitor = !v.visit(resultBatchWithDuplicates); nextId = nextIdStart(resultBatchWithDuplicates, batchSizeHint, idFunction); boolean finishedAllBatches = nextId == -1L; abortedOrFinished = abortedByVisitor || finishedAllBatches; } } };
public boolean visitOne(T t) throws K { return visit(ImmutableList.of(t)); }
@Override protected <K extends Exception> void batchAcceptSizeHint( int batchSizeHint, ConsistentVisitor<SortedMap<Long, Multimap<TableReference, Cell>>, K> visitor) throws K { ClosableIterator<RowResult<Value>> iterator = getIteratorToScrub(batchSizeHint, maxScrubTimestamp, startRow, endRow); try { BatchingVisitableFromIterable.create(iterator).batchAccept( batchSizeHint, batch -> visitor.visitOne(transformRows(batch))); } finally { iterator.close(); } } };
static <T, K extends Exception> ConsistentVisitor<T, K> create(int batchSize, AbortingVisitor<? super List<T>, K> v) { return new ConsistentVisitor<T, K>(batchSize, v); }
@Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, final ConsistentVisitor<T, K> v) throws K { visitable.batchAccept(batchSizeHint, batch -> v.visit(f.apply(batch))); } });
private boolean processBufferBatches() throws K { List<List<T>> batches = Lists.partition(buffer, batchSize); for (List<T> batch : batches) { if (batch.size() != batchSize) { continue; } if (!visitBufferWithDelegate(Collections.unmodifiableList(batch))) { return false; } } List<T> lastBatch = batches.get(batches.size()-1); if (lastBatch.size() == batchSize) { buffer = Lists.newArrayList(); } else { buffer = Lists.newArrayList(lastBatch); } return true; } }
@Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, ConsistentVisitor<T, K> v) throws K { long nextId = startId; boolean abortedOrFinished = false; while (!abortedOrFinished) { List<T> resultBatchWithDuplicates = sublistProvider.getBatchAllowDuplicates(nextId, batchSizeHint); boolean abortedByVisitor = !v.visit(resultBatchWithDuplicates); nextId = nextIdStart(resultBatchWithDuplicates, batchSizeHint, idFunction); boolean finishedAllBatches = nextId == -1L; abortedOrFinished = abortedByVisitor || finishedAllBatches; } } };
public boolean visitOne(T t) throws K { return visit(ImmutableList.of(t)); }
@Override public boolean visit(List<T> list) throws K { if (!visitorAlwaysReturnedTrue) { throw new IllegalStateException("Cannot keep visiting if visitor returns false."); } if (buffer.isEmpty() && list.size() == batchSize) { // Special case: We have exactly one batch. return visitBufferWithDelegate(Collections.unmodifiableList(list)); } buffer.addAll(list); if (buffer.size() < batchSize) { return true; } return processBufferBatches(); }
@Override protected <K extends Exception> void batchAcceptSizeHint( int batchSizeHint, ConsistentVisitor<SortedMap<Long, Multimap<TableReference, Cell>>, K> visitor) throws K { ClosableIterator<RowResult<Value>> iterator = getIteratorToScrub(batchSizeHint, maxScrubTimestamp, startRow, endRow); try { BatchingVisitableFromIterable.create(iterator).batchAccept( batchSizeHint, batch -> visitor.visitOne(transformRows(batch))); } finally { iterator.close(); } } };
static <T, K extends Exception> ConsistentVisitor<T, K> create(int batchSize, AbortingVisitor<? super List<T>, K> v) { return new ConsistentVisitor<T, K>(batchSize, v); }
@Override protected <K extends Exception> void batchAcceptSizeHint( int batchSize, ConsistentVisitor<T, K> bv) throws K { int actualBatchSize = Math.max(2, batchSize / 2); for (List<T> subList : Lists.partition(listToVisit, actualBatchSize)) { boolean moreToVisit = bv.visit(subList); if (!moreToVisit) { return; } } } }