/** * Partitions the given {@link RowsColumnRangeBatchRequest} into multiple, preserving the ordering of rows. Each * partitioned {@link RowsColumnRangeBatchRequest} will have exactly {@code partitionSize} rows in total, except * possibly for the last one, which may have fewer rows (but not more). No row will be split across partitions. */ public static List<RowsColumnRangeBatchRequest> partition(RowsColumnRangeBatchRequest batch, int partitionSize) { if (getAllRowsInOrder(batch).size() <= partitionSize) { return ImmutableList.of(batch); } List<List<byte[]>> partitionedRows = Lists.partition(getAllRowsInOrder(batch), partitionSize); List<RowsColumnRangeBatchRequest> partitions = new ArrayList<>(partitionedRows.size()); partitions.add(getFirstRequestInPartition(batch, partitionedRows.get(0))); for (int partitionNumber = 1; partitionNumber < partitionedRows.size() - 1; partitionNumber++) { RowsColumnRangeBatchRequest partition = ImmutableRowsColumnRangeBatchRequest.builder() .columnRangeSelection(batch.getColumnRangeSelection()) .rowsToLoadFully(partitionedRows.get(partitionNumber)) .build(); partitions.add(partition); } partitions.add(getLastRequestInPartition(batch, Iterables.getLast(partitionedRows))); return partitions; }
/** * Assumes that there are at least 2 requests after paritioning (i.e. that the last request in the partition is not * also the first). */ private static RowsColumnRangeBatchRequest getLastRequestInPartition( RowsColumnRangeBatchRequest originalRequest, List<byte[]> allRowsInLastPartition) { ImmutableRowsColumnRangeBatchRequest.Builder lastPartition = ImmutableRowsColumnRangeBatchRequest.builder() .columnRangeSelection(originalRequest.getColumnRangeSelection()); if (originalRequest.getPartialLastRow().isPresent()) { lastPartition.partialLastRow(originalRequest.getPartialLastRow()) .rowsToLoadFully(allRowsInLastPartition.subList(0, allRowsInLastPartition.size() - 1)); } else { lastPartition.rowsToLoadFully(allRowsInLastPartition); } return lastPartition.build(); } }
/** * Assumes that there are at least 2 requests after paritioning (i.e. that the first request in the partition is not * also the last). */ private static RowsColumnRangeBatchRequest getFirstRequestInPartition( RowsColumnRangeBatchRequest originalRequest, List<byte[]> allRowsInFirstPartition) { ImmutableRowsColumnRangeBatchRequest.Builder firstPartition = ImmutableRowsColumnRangeBatchRequest.builder() .columnRangeSelection(originalRequest.getColumnRangeSelection()); if (originalRequest.getPartialFirstRow().isPresent()) { firstPartition.partialFirstRow(originalRequest.getPartialFirstRow()) .rowsToLoadFully(Iterables.skip(allRowsInFirstPartition, 1)); } else { firstPartition.rowsToLoadFully(allRowsInFirstPartition); } return firstPartition.build(); }
private RowsColumnRangeBatchRequest createRequest(int numTotalRows) { ColumnRangeSelection fullColumnRange = new ColumnRangeSelection(col(0), col(5)); ImmutableRowsColumnRangeBatchRequest.Builder request = ImmutableRowsColumnRangeBatchRequest.builder().columnRangeSelection(fullColumnRange); if (hasPartialFirstRow) { request.partialFirstRow(Maps.immutableEntry(row(0), BatchColumnRangeSelection.create(col(3), col(5), 10))); } int firstFullRowIndex = hasPartialFirstRow ? 2 : 1; int lastFullRowIndex = hasPartialLastRow ? numTotalRows - 1 : numTotalRows; for (int rowNum = firstFullRowIndex; rowNum <= lastFullRowIndex; rowNum++) { request.addRowsToLoadFully(row(rowNum)); } if (hasPartialLastRow) { request.partialLastRow( Maps.immutableEntry(row(numTotalRows), BatchColumnRangeSelection.create(fullColumnRange, 10))); } return request.build(); }
/** * Partitions the given {@link RowsColumnRangeBatchRequest} into multiple, preserving the ordering of rows. Each * partitioned {@link RowsColumnRangeBatchRequest} will have exactly {@code partitionSize} rows in total, except * possibly for the last one, which may have fewer rows (but not more). No row will be split across partitions. */ public static List<RowsColumnRangeBatchRequest> partition(RowsColumnRangeBatchRequest batch, int partitionSize) { if (getAllRowsInOrder(batch).size() <= partitionSize) { return ImmutableList.of(batch); } List<List<byte[]>> partitionedRows = Lists.partition(getAllRowsInOrder(batch), partitionSize); List<RowsColumnRangeBatchRequest> partitions = new ArrayList<>(partitionedRows.size()); partitions.add(getFirstRequestInPartition(batch, partitionedRows.get(0))); for (int partitionNumber = 1; partitionNumber < partitionedRows.size() - 1; partitionNumber++) { RowsColumnRangeBatchRequest partition = ImmutableRowsColumnRangeBatchRequest.builder() .columnRangeSelection(batch.getColumnRangeSelection()) .rowsToLoadFully(partitionedRows.get(partitionNumber)) .build(); partitions.add(partition); } partitions.add(getLastRequestInPartition(batch, Iterables.getLast(partitionedRows))); return partitions; }
/** * Assumes that there are at least 2 requests after paritioning (i.e. that the last request in the partition is not * also the first). */ private static RowsColumnRangeBatchRequest getLastRequestInPartition( RowsColumnRangeBatchRequest originalRequest, List<byte[]> allRowsInLastPartition) { ImmutableRowsColumnRangeBatchRequest.Builder lastPartition = ImmutableRowsColumnRangeBatchRequest.builder() .columnRangeSelection(originalRequest.getColumnRangeSelection()); if (originalRequest.getPartialLastRow().isPresent()) { lastPartition.partialLastRow(originalRequest.getPartialLastRow()) .rowsToLoadFully(allRowsInLastPartition.subList(0, allRowsInLastPartition.size() - 1)); } else { lastPartition.rowsToLoadFully(allRowsInLastPartition); } return lastPartition.build(); } }
/** * Assumes that there are at least 2 requests after paritioning (i.e. that the first request in the partition is not * also the last). */ private static RowsColumnRangeBatchRequest getFirstRequestInPartition( RowsColumnRangeBatchRequest originalRequest, List<byte[]> allRowsInFirstPartition) { ImmutableRowsColumnRangeBatchRequest.Builder firstPartition = ImmutableRowsColumnRangeBatchRequest.builder() .columnRangeSelection(originalRequest.getColumnRangeSelection()); if (originalRequest.getPartialFirstRow().isPresent()) { firstPartition.partialFirstRow(originalRequest.getPartialFirstRow()) .rowsToLoadFully(Iterables.skip(allRowsInFirstPartition, 1)); } else { firstPartition.rowsToLoadFully(allRowsInFirstPartition); } return firstPartition.build(); }