@Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, ConsistentVisitor<T, K> v) throws K { bv.batchAccept(pageSize, v); } });
@Override public <K extends Exception> boolean batchAccept(int batchSize, AbortingVisitor<? super List<T>, K> v) throws K { // This cast is safe because an abortingVisitor can consume more specific values @SuppressWarnings("unchecked") AbortingVisitor<? super List<S>, K> specificVisitor = (AbortingVisitor<? super List<S>, K>) v; return visitable.batchAccept(batchSize, specificVisitor); } };
@Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, ConsistentVisitor<T, K> v) throws K { for (BatchingVisitable<? extends T> bv : inputs) { // This is safe because cast is never passed to anything and it's function // batchAccept is covariant @SuppressWarnings("unchecked") BatchingVisitable<T> cast = (BatchingVisitable<T>) bv; if (!cast.batchAccept(batchSizeHint, v)) { return; } } } });
@Override protected <K extends Exception> void batchAcceptSizeHint(final int batchSizeHint, final ConsistentVisitor<T, K> v) throws K { inputs.batchAccept(outerBatchHint, (AbortingVisitor<List<? extends BatchingVisitable<? extends T>>, K>) bvs -> { for (BatchingVisitable<? extends T> bv : bvs) { // This is safe because cast is never passed to anything and it's function // batchAccept is covariant @SuppressWarnings("unchecked") BatchingVisitable<T> cast = (BatchingVisitable<T>) bv; if (!cast.batchAccept(batchSizeHint, v)) { return false; } } return true; }); } });
@Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, final ConsistentVisitor<T, K> v) throws K { visitable.batchAccept(batchSizeHint, new AbortingVisitor<List<T>, K>() { long visited = 0; @Override public boolean visit(List<T> batch) throws K { for (T item : batch) { if (visited < toSkip) { visited++; continue; } if (!v.visitOne(item)) { return false; } } return true; } }); } });
@Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, final ConsistentVisitor<T, K> v) throws K { if (batchSizeHint > limit) { batchSizeHint = (int) limit; } visitable.batchAccept(batchSizeHint, new AbortingVisitor<List<T>, K>() { long visited = 0; @Override public boolean visit(List<T> batch) throws K { for (T item : batch) { if (!v.visitOne(item)) { return false; } visited++; if (visited >= limit) { // Stop visiting early by returning false. // The underlying ConsistentVisitor will still cause #batchAccept // to return true. return false; } } return true; } }); } });
@Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, final ConsistentVisitor<T, K> v) throws K { visitable.batchAccept(batchSizeHint, new AbortingVisitor<List<T>, K>() { boolean hasVisitedFirst = false; Object lastVisited = null; @Override public boolean visit(List<T> batch) throws K { for (T item : batch) { Object itemKey = function.apply(item); if (!hasVisitedFirst || !Objects.equal(itemKey, lastVisited)) { if (!v.visitOne(item)) { return false; } hasVisitedFirst = true; lastVisited = itemKey; } } return true; } }); } });
@Override public <K extends Exception> boolean batchAccept( int batchSize, AbortingVisitor<? super List<T>, K> visitor) throws K { return delegate().batchAccept(batchSize, visitor); }
private static <T> long countInternal(BatchingVisitable<T> visitable, int batchSize) { final long[] count = new long[1]; visitable.batchAccept(batchSize, AbortingVisitors.<T, RuntimeException>batching( item -> { count[0]++; return true; })); return count[0]; }
@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))); } });
/** * Returns an immutable copy of the elements in this visitable. * * @throws NullPointerException if any elements in the visitable are null */ public ImmutableList<T> immutableCopy() { final ImmutableList.Builder<T> builder = ImmutableList.builder(); delegate().batchAccept( BatchingVisitables.DEFAULT_BATCH_SIZE, items -> { builder.addAll(items); return true; }); return builder.build(); }
/** * Returns an immutable copy of the elements in this visitable. * * @throws NullPointerException if any elements in the visitable are null */ public ImmutableSet<T> immutableSetCopy() { final ImmutableSet.Builder<T> builder = ImmutableSet.builder(); delegate().batchAccept( BatchingVisitables.DEFAULT_BATCH_SIZE, items -> { builder.addAll(items); return true; }); return builder.build(); }
/** * Copies the elements in this visitable into an existing collection. This method has * equivalent behaviour to {@code Iterables.addAll(collection, this)}. * * @return a reference to {@code collection}, for convenience */ public <S extends Collection<? super T>> S copyInto(final S collection) { Preconditions.checkNotNull(collection, "Cannot copy the visitable into a null collection"); delegate().batchAccept( BatchingVisitables.DEFAULT_BATCH_SIZE, items -> { collection.addAll(items); return true; }); return collection; }
/** * Returns {@code true} iff one or more elements satisfy the predicate. */ public boolean any(final Predicate<? super T> predicate) { Preconditions.checkNotNull(predicate, "Cannot check against a null predicate"); return !delegate().batchAccept( BatchingVisitables.DEFAULT_BATCH_SIZE, items -> { for (T t : items) { if (predicate.apply(t)) { return false; } } return true; }); }
/** * Returns {@code true} iff every element satisfies the * predicate. If empty, {@code true} is returned. */ public boolean all(final Predicate<? super T> predicate) { Preconditions.checkNotNull(predicate, "Cannot check against a null predicate"); return delegate().batchAccept( BatchingVisitables.DEFAULT_BATCH_SIZE, items -> { for (T t : items) { if (!predicate.apply(t)) { return false; } } return true; }); }
@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; }); } });
public static <T> boolean isEqual(BatchingVisitable<T> v, final Iterator<T> it) { boolean ret = v.batchAccept(DEFAULT_BATCH_SIZE, batch -> { Iterator<T> toMatch = Iterators.limit(it, batch.size()); return Iterators.elementsEqual(toMatch, batch.iterator()); }); if (it.hasNext()) { return false; } return ret; }
@Override public <K extends Exception> boolean batchAccept( int batchSize, AbortingVisitor<? super List<RowResult<byte[]>>, K> visitor) throws K { boolean hitEnd = ret.batchAccept(batchSize, items -> { if (items.size() < batchSize) { reachedEndOfRange(tableRef, rangeRequest); } markRangeRead(tableRef, rangeRequest, items); return visitor.visit(items); }); if (hitEnd) { reachedEndOfRange(tableRef, rangeRequest); } return hitEnd; } };
public static long estimateSize(Transaction transaction, TableReference table, int batchSize, Function<byte[], byte[]> uniformizer) { final AtomicLong estimate = new AtomicLong(); transaction.getRange(table, RangeRequest.all()).batchAccept(batchSize, batch -> { if (batch.size() < batchSize) { estimate.set(batch.size()); } else { byte[] row = uniformizer.apply(batch.get(batchSize - 1).getRowName()); estimate.set(BigInteger.valueOf(2) .pow(row.length * 8) .multiply(BigInteger.valueOf(batchSize)) .divide(new BigInteger(1, row)) .longValue()); } return false; }); return estimate.get(); }
public void forEach(int batchSize, final Visitor<T> visitor) { delegate().batchAccept(batchSize, batch -> { for (T item : batch) { visitor.visit(item); } return true; }); }