private Iterable<PairBatchStatementTuples> build() { Iterable<List<PairStatementTuple>> partition = Iterables.partition(statements, batchSizeRows); return Iterables.transform(partition, new Function<List<PairStatementTuple>, PairBatchStatementTuples>() { @Override public PairBatchStatementTuples apply(List<PairStatementTuple> l) { final List<Tuple> inputs = new LinkedList<>(); final BatchStatement batch = new BatchStatement(BatchStatement.Type.UNLOGGED); for (PairStatementTuple pair : l) { batch.add(pair.getStatement()); inputs.add(pair.getTuple()); } return new PairBatchStatementTuples(inputs, batch); } }); } }
private static String formatHexDump(byte[] bytes, int bytesPerLine) { // hex pairs: ["61", "62", "63"] Iterable<String> hexPairs = createHexPairs(bytes); // hex lines: [["61", "62", "63], [...]] Iterable<List<String>> hexLines = partition(hexPairs, bytesPerLine); // lines: ["61 62 63", ...] Iterable<String> lines = transform(hexLines, HEX_BYTE_JOINER::join); // joined: "61 62 63\n..." return HEX_LINE_JOINER.join(lines); }
/** * Ensure values {@code inputs} are unique (which avoids useless arguments) and sorted before creating the partition. */ public static <INPUT extends Comparable<INPUT>> Iterable<List<INPUT>> toUniqueAndSortedPartitions(Collection<INPUT> inputs, IntFunction<Integer> partitionSizeManipulations) { int partitionSize = partitionSizeManipulations.apply(PARTITION_SIZE_FOR_ORACLE); return Iterables.partition(toUniqueAndSortedList(inputs), partitionSize); }
public void testPartition_badSize() { Iterable<Integer> source = Collections.singleton(1); try { Iterables.partition(source, 0); fail(); } catch (IllegalArgumentException expected) { } }
public void testPartition_empty() { Iterable<Integer> source = Collections.emptySet(); Iterable<List<Integer>> partitions = Iterables.partition(source, 1); assertTrue(Iterables.isEmpty(partitions)); }
public void testPartition_view() { List<Integer> list = asList(1, 2); Iterable<List<Integer>> partitions = Iterables.partition(list, 2); // Changes before the partition is retrieved are reflected list.set(0, 3); Iterator<List<Integer>> iterator = partitions.iterator(); // Changes before the partition is retrieved are reflected list.set(1, 4); List<Integer> first = iterator.next(); // Changes after are not list.set(0, 5); assertEquals(ImmutableList.of(3, 4), first); }
public void testPartition_singleton1() { Iterable<Integer> source = Collections.singleton(1); Iterable<List<Integer>> partitions = Iterables.partition(source, 1); assertEquals(1, Iterables.size(partitions)); assertEquals(Collections.singletonList(1), partitions.iterator().next()); }
@GwtIncompatible // ? // TODO: Figure out why this is failing in GWT. public void testPartitionNonRandomAccessInput() { Iterable<Integer> source = Lists.newLinkedList(asList(1, 2, 3)); Iterable<List<Integer>> partitions = Iterables.partition(source, 2); Iterator<List<Integer>> iterator = partitions.iterator(); // Even though the input list doesn't implement RandomAccess, the output // lists do. assertTrue(iterator.next() instanceof RandomAccess); assertTrue(iterator.next() instanceof RandomAccess); }
@GwtIncompatible // ? // TODO: Figure out why this is failing in GWT. public void testPartitionRandomAccessInput() { Iterable<Integer> source = asList(1, 2, 3); Iterable<List<Integer>> partitions = Iterables.partition(source, 2); Iterator<List<Integer>> iterator = partitions.iterator(); assertTrue(iterator.next() instanceof RandomAccess); assertTrue(iterator.next() instanceof RandomAccess); }
List<String> partitionNames = delegate.getPartitionNames(schemaTableName.getSchemaName(), schemaTableName.getTableName()) .orElse(ImmutableList.of()); for (List<String> partitionNameBatch : Iterables.partition(partitionNames, 10)) { Collection<Optional<Partition>> partitions = delegate.getPartitionsByNames(schemaTableName.getSchemaName(), schemaTableName.getTableName(), partitionNameBatch).values(); partitions.stream()
if (isConnected() && cnx.getRemoteEndpointProtocolVersion() >= ProtocolVersion.v2.getNumber()) { int messagesFromQueue = removeExpiredMessagesFromQueue(messageIds); Iterable<List<MessageIdImpl>> batches = Iterables.partition( messageIds.stream() .map(messageId -> (MessageIdImpl)messageId)
@Override public Iterable<List<T>> partitionIntoBatches(List<T> collection, int batchSizeHint) { return Iterables.partition(collection, batchSizeHint); } }
@Override public Iterable<? extends Map<K, V>> partitionIntoBatches(Map<K, V> collection, int batchSizeHint) { return Iterables.transform( Iterables.partition(collection.entrySet(), batchSizeHint), Maps2::fromEntries); } }
@Override public Map<Cell, Value> getRows(TableReference tableRef, Iterable<byte[]> rows, ColumnSelection columnSelection, long timestamp) { HashMap<Cell, Value> ret = Maps.newHashMap(); for (List<byte[]> part : Iterables.partition(rows, rowBatchSize)) { ret.putAll(getRowsPartition(tableRef, part, columnSelection, timestamp)); } return ret; }
/** * Returns the subset of {@link Cell}s whose latest values prior to {@code maxTimestampExclusive} are empty. */ public Set<Cell> execute() { Set<Cell> result = Sets.newHashSet(); for (List<CellWithTimestamps> batch : Iterables.partition(cellTimestamps, batchSize)) { result.addAll(getSingleBatch(batch)); } return result; }
public Map<RangeRequest, TokenBackedBasicResultsPage<RowResult<Value>, byte[]>> getFirstBatchForRanges( TableReference tableRef, Iterable<RangeRequest> rangeRequests, long timestamp) { Map<RangeRequest, TokenBackedBasicResultsPage<RowResult<Value>, byte[]>> results = Maps.newHashMap(); for (List<RangeRequest> batch : Iterables.partition(rangeRequests, 500)) { results.putAll(getFirstPages(tableRef, batch, timestamp)); } return results; }
builder.forcedBreak(); boolean first = true; for (Iterable<? extends ExpressionTree> row : Iterables.partition(expressions, cols)) { if (!first) { builder.forcedBreak();
@Override public Map<Long, Long> loadAll(Iterable<? extends Long> nonCachedKeys) { List<Long> missingKeys = ImmutableList.copyOf(nonCachedKeys); Map<Long, Long> result = new HashMap<>(); Streams.stream(Iterables.partition(missingKeys, AtlasDbConstants.TRANSACTION_TIMESTAMP_LOAD_BATCH_LIMIT)) .forEach(batch -> result.putAll(transactionService.get(batch))); // roll back any uncommitted transactions missingKeys.stream() .filter(startTs -> !result.containsKey(startTs)) .forEach(startTs -> result.put(startTs, load(startTs))); return result; }
@Test public void iterablesSpliterator() { IterableView<List<String>> view = IterableView.of(Iterables.partition(ImmutableSet.of("a", "b", "c"), 1)); Spliterator<List<String>> spliterator = view.spliterator(); assertThat(spliterator.estimateSize()).isEqualTo(Long.MAX_VALUE); assertThat(spliterator.characteristics()).isZero(); assertThat(StreamSupport.stream(spliterator, false)).containsExactly( ImmutableList.of("a"), ImmutableList.of("b"), ImmutableList.of("c")); } }
public IterableView<List<T>> partition(final int size) { if (delegate() instanceof List) { /* * Use the more efficient Lists.partition which utilizes sublists * without allocating new lists for the returned partitions. */ return of(Lists.partition((List<T>) delegate(), size)); } return of(Iterables.partition(castAsIterable(), size)); }