public Collection<Batch> split() { if (futures.values().size() <= batchSizes) { return ImmutableList.of(this); } List<Entry<ByteString, SettableFuture<FlatRow>>> toSplit = new ArrayList<>(futures.entries()); Collections.sort(toSplit, ENTRY_SORTER); List<Batch> batches = new ArrayList<>(); for (List<Entry<ByteString, SettableFuture<FlatRow>>> entries : Iterables.partition(toSplit, batchSizes)) { Batch batch = new Batch(filter); for (Entry<ByteString, SettableFuture<FlatRow>> entry : entries) { batch.futures.put(entry.getKey(), entry.getValue()); } batches.add(batch); } return batches; }
/** * Sends all remaining requests to the server. This method does not wait for the method to * complete. */ public void flush() { for (Batch batch : batches.values()) { Collection<Batch> subbatches = batch.split(); for (Batch miniBatch : subbatches) { threadPool.submit(miniBatch); } } batches.clear(); }
/** * Adds the key in the request to a batch read. The future will be resolved when the batch response * is received. * * @param request a {@link com.google.bigtable.v2.ReadRowsRequest} with a single row key. * @return a {@link com.google.common.util.concurrent.ListenableFuture} that will be populated * with the {@link FlatRow} that corresponds to the request */ public ListenableFuture<FlatRow> add(ReadRowsRequest request) { Preconditions.checkNotNull(request); Preconditions.checkArgument(request.getRows().getRowKeysCount() == 1); ByteString rowKey = request.getRows().getRowKeysList().get(0); Preconditions.checkArgument(!rowKey.equals(ByteString.EMPTY)); final RowFilter filter = request.getFilter(); Batch batch = batches.get(filter); if (batch == null) { batch = new Batch(filter); batches.put(filter, batch); } return batch.addKey(rowKey); }