@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 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; }); }
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; }
public void forEach(int batchSize, final Visitor<T> visitor) { delegate().batchAccept(batchSize, batch -> { for (T item : batch) { visitor.visit(item); } return true; }); }
public static <T> boolean isEmpty(BatchingVisitable<T> v) { return v.batchAccept(1, AbortingVisitors.<T, RuntimeException>batching(AbortingVisitors.<T>alwaysFalse())); }
@Nullable public static <T> T getFirst(BatchingVisitable<T> visitable, @Nullable T defaultElement) { final Mutable<T> ret = Mutables.newMutable(defaultElement); visitable.batchAccept(1, AbortingVisitors.<T, RuntimeException>batching(item -> { ret.set(item); return false; })); return ret.get(); }
@Nullable public static <T> T getLast(BatchingVisitable<T> visitable, @Nullable T defaultElement) { final Mutable<T> ret = Mutables.newMutable(defaultElement); visitable.batchAccept(DEFAULT_BATCH_SIZE, AbortingVisitors.<T, RuntimeException>batching(item -> { ret.set(item); return true; })); return ret.get(); }
@Test public void testMultipleReadsToSameColumnRangeAcrossRows() { byte[] row = PtBytes.toBytes("row"); byte[] differentRow = PtBytes.toBytes("differentRow"); Transaction transaction = startTransaction(); BatchColumnRangeSelection sameColumnRangeSelection = BatchColumnRangeSelection.create(PtBytes.toBytes("col"), PtBytes.toBytes("col0"), 1); Map<byte[], BatchingVisitable<Map.Entry<Cell, byte[]>>> columnRangeResultForRow = transaction.getRowsColumnRange(TEST_TABLE, ImmutableList.of(row), sameColumnRangeSelection); columnRangeResultForRow.values().forEach(visitable -> visitable.batchAccept(10, t -> true)); Map<byte[], BatchingVisitable<Map.Entry<Cell, byte[]>>> columnRangeResultForDifferentRow = transaction.getRowsColumnRange(TEST_TABLE, ImmutableList.of(differentRow), sameColumnRangeSelection); columnRangeResultForDifferentRow.values().forEach(visitable -> visitable.batchAccept(10, t -> true)); put(transaction, "mutation to ensure", "conflict", "handling"); transaction.commit(); }
@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(); } } };
private byte[] internalCopyRange(BatchingVisitable<RowResult<byte[]>> bv, final long maxBytes, @Output final Map<Cell, byte[]> writeMap) { final Mutable<byte[]> lastRowName = Mutables.newMutable(null); final MutableLong bytesPut = new MutableLong(0L); bv.batchAccept(readBatchSize, AbortingVisitors.batching( // Replacing this with a lambda results in an unreported exception compile error // even though no exception can be thrown :-( new AbortingVisitor<RowResult<byte[]>, RuntimeException>() { @Override public boolean visit(RowResult<byte[]> rr) throws RuntimeException { return KvsRangeMigrator.this.internalCopyRow(rr, maxBytes, writeMap, bytesPut, lastRowName); } })); return lastRowName.get(); }