Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2013 Google Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 import java.util.List;

BatchHelper abstracts out the logic for maximum requests per batch, and also allows a workaround for the fact that BatchRequest was made a "final class" for some reason, making it impossible to unittest. Instead, batch interactions with a Storage API client will be funneled through this class, while unittests can inject a mock batch helper.

This class is not thread-safe; expected usage is to create a new BatchHelper instance per single-threaded logical grouping of requests.

 public class BatchHelper {
Since each BatchHelper instance should be tied to a particular related set of requests, use cases will generally interact via an injectable BatchHelper.Factory.
   public static class Factory {
     public BatchHelper newBatchHelper(HttpRequestInitializer requestInitializerStorage gcs,
         long maxRequestsPerBatch) {
       return new BatchHelper(requestInitializergcsmaxRequestsPerBatch);

Callback that causes a single StorageRequest to be added to the BatchRequest.
   protected static interface QueueRequestCallback {
     void enqueue() throws IOException;
   private final BatchRequest batch;
   // Number of requests that can be queued into a single actual HTTP request
   // before a sub-batch is sent.
   private final long maxRequestsPerBatch;
   // Flag that indicates whether there is an in-progress flush.
   private boolean flushing = false;

Primary constructor, generally accessed only via the inner Factory class.
   private BatchHelper(HttpRequestInitializer requestInitializerStorage gcs,
       long maxRequestsPerBatch) {
     this. = new LinkedList<>();
     this. = gcs.batch(requestInitializer);
     this. = maxRequestsPerBatch;
   protected BatchHelper() {
     this. = new LinkedList<>();
     this. = null;
     this. = -1;

Adds an additional request to the batch, and possibly flushes the current contents of the batch if maxRequestsPerBatch has been reached.
   public <T> void queue(final StorageRequest<T> reqfinal JsonBatchCallback<T> callback)
       throws IOException {
       QueueRequestCallback queueCallback = new QueueRequestCallback() {
         public void enqueue() throws IOException {
  // Flush our buffer if we have more pending entries than maxRequestsPerBatch
  private void flushIfPossibleAndRequired() throws IOException {
  // Flush our buffer if we are not already in a flush operation and we have data to flush.
  private void flushIfPossible() throws IOException {
    if (! && .size() > 0) {
       = true;
      try {
        while (.size() < 
            && .size() > 0) {
          QueueRequestCallback head = .remove(0);
      } finally {
         = false;

Sends any currently remaining requests in the batch; should be caleld at the end of any series of batched requests to ensure everything has been sent.
  public void flush()
      throws IOException {

Returns true if there are no currently queued entries in the batch helper.
  public boolean isEmpty() {
    return .size() == 0;
New to GrepCode? Check out our FAQ X