Batch operations

@throws ForbiddenException
546276
public function batch() {
		$success = array();
		$action = $this->request->query('batch_action');

		if (!in_array($action, $this->batchActions, true)) {
			throw new ForbiddenException("Method '$action' not allowed");
		}
		
		if ($this->request->query('batch_conditions')) {
			$ids = array_values($this->TaskClient->find('list', array(
						'conditions' => $this->_paginationFilter()
			)));
		} else {
			$ids = array_filter((array)$this->request->query('ids'));
		}
		
		if ($ids) {
			foreach ($ids as $id) {
				$success[$id] = $this->TaskClient->{$action}($id);
			}
		}

		if (empty($success)) {
			$this->Session->setFlash("No ids specified", 'alert/simple', array(
				'class' => 'alert-warning', 'title' => 'Warning!'
			));
		} elseif (array_filter($success) == $success) {
			$this->Session->setFlash("Batch $action successfully applied to " . count($success) . " task(s)", 'alert/simple', array(
				'class' => 'alert-success', 'title' => 'Ok!'
			));
		} else {
			$failIds = array_keys(
					array_filter($success, function($a) {
						return !$a;
					})
			);
			$this->Session->setFlash(count($failIds) . " of " . count($success) . " operations was errored (with ids: " . implode(', ', $failIds) . ")", 'alert/simple', array(
				'class' => 'alert-error', 'title' => 'Error!'
			));
		}

		$this->redirect($this->referer());
	}
725870
private void processNow() {
        List<T> toProcess;
        boolean scheduleNextBatchImmediately = false;
        synchronized (mutex) {
            int count = inbox.size();
            Log.v(Log.TAG_BATCHER, "%s: processNow() called, inbox size: %d", this, count);
            if (count == 0)
                return;
            else if (count <= capacity) {
                toProcess = new ArrayList<T>(inbox);
                inbox.clear();
            } else {
                toProcess = new ArrayList<T>(inbox.subList(0, capacity));
                for (int i = 0; i < capacity; i++)
                    inbox.remove(0);
                scheduleNextBatchImmediately = true;
            }
            mutex.notifyAll();
        }

        synchronized (processMutex) {
            if (toProcess != null && toProcess.size() > 0) {
                Log.v(Log.TAG_BATCHER, "%s: invoking processor %s with %d items",
                        this, processor, toProcess.size());
                processor.process(toProcess);
            } else
                Log.v(Log.TAG_BATCHER, "%s: nothing to process", this);

            synchronized (mutex) {
                lastProcessedTime = System.currentTimeMillis();
                scheduled = false;
                scheduleBatchProcess(scheduleNextBatchImmediately);
                Log.v(Log.TAG_BATCHER, "%s: invoking processor done",
                        this, processor, toProcess.size());
            }
            processMutex.notifyAll();
        }
    }
238734
private void onFlushBatch(Map<String, BulkWriteOperation> bulkWriteOperationMap)
    {
        if (!bulkWriteOperationMap.isEmpty())
        {
            for (BulkWriteOperation builder : bulkWriteOperationMap.values())
            {
                try
                {
                    builder.execute(getWriteConcern());
                }
                catch (BulkWriteException bwex)
                {
                    log.error("Batch insertion is not performed due to error in write command. Caused By: ", bwex);
                    throw new KunderaException(
                            "Batch insertion is not performed due to error in write command. Caused By: ", bwex);
                }
                catch (MongoException mex)
                {
                    log.error("Batch insertion is not performed. Caused By: ", mex);
                    throw new KunderaException("Batch insertion is not performed. Caused By: ", mex);
                }
            }
        }
    }
826534
@Override
    protected void doHandle(CommandContext ctx) throws CommandLineException {

        final BatchManager batchManager = ctx.getBatchManager();
        if(batchManager.isBatchActive()) {
            throw new CommandFormatException("try is not allowed while in batch mode.");
        }
        ctx.registerRedirection(new TryCatchFinallyControlFlow(ctx));
    }
355069
public static void useResource(String host, int port, String user, String password, Authentication authType)
    throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException
  {
    // create the client
    DatabaseClient client = DatabaseClientFactory.newClient(host, port, user, password, authType);

    setUpExample(client);

    // create the batch manager
    BatchManager batchMgr = new BatchManager(client);

    // collect the request items
    BatchManager.BatchRequest request = batchMgr.newBatchRequest();
    request.withRead("/batch/read1.xml", "application/xml");
    request.withRead("/batch/read2.xml", Metadata.COLLECTIONS, "application/xml");
    request.withDelete("/batch/delete1.xml");
    request.withWrite("/batch/write1.xml",
      new StringHandle().withFormat(Format.XML).with("<write1/>"));
    DocumentMetadataHandle meta2 = new DocumentMetadataHandle();
    meta2.getCollections().add("/batch/collection2");
    request.withWrite("/batch/write2.xml",
      meta2,
      new StringHandle().withFormat(Format.XML).with("<write2/>"));

    // apply the request
    BatchManager.BatchResponse response = batchMgr.apply(request);

    System.out.println("Applied the batch request with " +
      (response.getSuccess() ? "success" : "failure")+":");

    // iterate over the response items
    while (response.hasNext()){
      BatchManager.OutputItem item = response.next();
      BatchManager.OperationType itemType = item.getOperationType();
      String itemUri = item.getUri();
      if (item.getSuccess()) {
        System.out.println(itemType.name().toLowerCase()+" on "+itemUri);

        // inspect the response for a document read request
        if (itemType == BatchManager.OperationType.READ) {
          BatchManager.ReadOutput readItem = (BatchManager.ReadOutput) item;

          // show the document metadata read from the database
          if (readItem.hasMetadata()) {
            StringHandle metadataHandle = readItem.getMetadata(new StringHandle());
            System.out.println("with metadata:\n"+metadataHandle.get());
          }
          // show the document content read from the database
          if (readItem.hasContent()) {
            StringHandle contentHandle = readItem.getContent(new StringHandle());
            System.out.println("with content:\n"+contentHandle.get());
          }
        }
      } else {
        System.out.println(itemType.name().toLowerCase()+" failed on "+itemUri);
        if (item.hasException()) {
          StringHandle exceptionHandle = item.getException(new StringHandle());
          System.out.println("with exception:\n"+exceptionHandle.get());
        }
      }
    }

    // release the client
    client.release();
  }
68793
@Override
    protected void doHandle(CommandContext ctx) throws CommandFormatException {

        BatchManager batchManager = ctx.getBatchManager();
        if(!batchManager.isBatchActive()) {
            throw new CommandFormatException("No active batch to holdback.");
        }

        String name = null;
        ParsedCommandLine args = ctx.getParsedCommandLine();
        if(args.hasProperties()) {
            name = args.getOtherProperties().get(0);
        }

        if(batchManager.isHeldback(name)) {
            throw new CommandFormatException("There already is " + (name == null ? "unnamed" : "'" + name + "'") + " batch held back.");
        }

        if(!batchManager.holdbackActiveBatch(name)) {
            throw new CommandFormatException("Failed to holdback the batch.");
        }
    }
1951
private void batchImport() {
        m_jdbcTemplate.batchUpdate(createInsertQuery(),
                                   new BatchPreparedStatementSetter() {
                                       public void setValues(
                                               PreparedStatement ps, int i)
                                               throws SQLException {
                                           int j = 1;
                                           for (String property : m_propertyNames) {
                                               Map params = (Map) m_batch.get(
                                                       i);
                                               ps.setObject(j++, params.get(
                                                       property));
                                           }
                                       }

                                       public int getBatchSize() {
                                           return m_batch.size();
                                       }
                                   });
    }
342446
private <T extends IEntity> IntuitMessage prepareBatch(BatchOperation batchOperation) throws FMSException {
        IntuitMessage intuitMessage = new IntuitMessage();
        RequestElements requestElements = intuitMessage.getRequestElements();

        //set the request params
        Map<String, String> requestParameters = requestElements.getRequestParameters();
        requestParameters.put(RequestElements.REQ_PARAM_METHOD_TYPE, MethodType.POST.toString());

        IntuitBatchRequest intuitBatchRequest = new IntuitBatchRequest();
        intuitBatchRequest.setBatchItemRequest(batchOperation.getBatchItemRequests());
        
        requestElements.setAction(OperationType.BATCH.toString());
        requestElements.setContext(context);
        requestElements.setObjectToSerialize(getSerializableRequestObject(intuitBatchRequest));
        requestElements.setBatchOperation(batchOperation);

        return intuitMessage;
    }
775222
private void executeInternalBatch(int size) throws SQLException {
    executeQueryPrologue(true);
    results = new Results(this, 0, true, size, false, resultSetScrollType,
            resultSetConcurrency, autoGeneratedKeys, protocol.getAutoIncrementIncrement());
    if (protocol
        .executeBatchClient(protocol.isMasterConnection(), results, prepareResult, parameterList,
            hasLongData)) {
      return;
    }

    //send query one by one, reading results for each query before sending another one
    SQLException exception = null;

    if (queryTimeout > 0) {
      for (int batchQueriesCount = 0; batchQueriesCount < size; batchQueriesCount++) {
        protocol.stopIfInterrupted();
        try {
          protocol.executeQuery(protocol.isMasterConnection(), results, prepareResult,
              parameterList.get(batchQueriesCount));
        } catch (SQLException e) {
          if (options.continueBatchOnError) {
            exception = e;
          } else {
            throw e;
          }
        }
      }

    } else {
      for (int batchQueriesCount = 0; batchQueriesCount < size; batchQueriesCount++) {
        try {
          protocol.executeQuery(protocol.isMasterConnection(), results, prepareResult,
              parameterList.get(batchQueriesCount));
        } catch (SQLException e) {
          if (options.continueBatchOnError) {
            exception = e;
          } else {
            throw e;
          }
        }
      }
    }
    if (exception != null) {
      throw exception;
    }

  }
858187
void doBatchEnd(String batch, Object args) {
    checkOpen();
    JsonObject message = createMessage(args)
        .putString("action", "endBatch")
        .putString("batch", batch);
    if (open && !paused) {
      if (log.isDebugEnabled()) {
        log.debug(String.format("%s - Batch end: Batch[batch=%s, args=%s]", this, batch, args));
      }
      eventBus.send(inAddress, message);
    }
    if (currentBatch != null && currentBatch.id().equals(batch)) {
      currentBatch = null;
    }
  }
534120
private void doFlush() throws SQLException {
		if (batchCount > 0) {
			try {
				statement.executeBatch();
				batchCount = 0;
			} catch (SQLException ex) {
				resetConnection();
				throw ex;
			}
		}
	}
697039
private List<FailedBatch> writeOneBatch(
            StringListMap<WriteRequest> batch,
            BatchWriteRetryStrategy batchWriteRetryStrategy) {

        List<FailedBatch> failedBatches = new LinkedList<FailedBatch>();
        FailedBatch failedBatch = doBatchWriteItemWithRetry(batch, batchWriteRetryStrategy);

        if (failedBatch != null) {
            // If the exception is request entity too large, we divide the batch
            // into smaller parts.

            if (failedBatch.isRequestEntityTooLarge()) {

                // If only one item left, the item size must beyond 64k, which
                // exceedes the limit.

                if (failedBatch.size() == 1) {
                    failedBatches.add(failedBatch);
                } else {
                    for (final StringListMap<WriteRequest> subBatch : batch.subMaps(2, false)) {
                        failedBatches.addAll(writeOneBatch(subBatch, batchWriteRetryStrategy));
                    }
                }

            } else {
                failedBatches.add(failedBatch);
            }

        }
        return failedBatches;
    }
1264507
public function process()
    {
        // try to read input
        try {
            $json = file_get_contents($this->input);
        } catch (\Exception $e) {
            $message = "Server unable to read request body.";
            $message .= PHP_EOL . $e->getMessage();
            throw new Serverside\Exception($message);
        }

        // handle communication errors
        if ($json === false) {
            throw new Serverside\Exception("Server unable to read request body.");
        }

        // create request object
        $request = $this->makeRequest($json);

        // set content type to json if not testing
        if (!(defined('ENV') && ENV == 'TEST')) {
            header('Content-type: application/json');
        }

        // handle json parse error and empty batch
        if ($request->error_code && $request->error_message) {
            echo $request->toResponseJSON();
            return;
        }

        // respond with json
        echo $this->handleRequest($request);
    }
197292
@Override
    protected void doHandle(CommandContext ctx) throws CommandFormatException {

        boolean result = ctx.getBatchManager().discardActiveBatch();
        if(!result) {
            throw new CommandFormatException("There is no active batch to discard.");
        }
    }
1302063
void onSealExclusive(Batch batch, long elapsedTimeMillis)
  {
    try {
      doOnSealExclusive(batch, elapsedTimeMillis);
    }
    catch (Throwable t) {
      try {
        if (!concurrentBatch.compareAndSet(batch, batch.batchNumber)) {
          log.error("Unexpected failure to set currentBatch to the failed Batch.batchNumber");
        }
        log.error(t, "Serious error during onSealExclusive(), set currentBatch to the failed Batch.batchNumber");
      }
      catch (Throwable t2) {
        t.addSuppressed(t2);
      }
      throw t;
    }
  }
370473
def do_batch(resource_root, elements):
  
  return call(resource_root.post, BATCH_PATH, ApiBatchResponseList,
      data=elements, api_version=6)
463568
public void batch(List<? extends Row> actions, @Nullable Object[] results)
      throws IOException, InterruptedException {
    if (results == null) {
      results = new Object[actions.size()];
    }
    batchCallback(actions, results, null);
  }