/**
  * Apply a mutation to the Datastore (commit)
  *
  * @param \Google_Service_Datastore_Mutation $obj_mutation
  * @return \Google_Service_Datastore_CommitResponse
  * @throws Contention
  * @throws \Google_Service_Exception
  */
 private function commitMutation(\Google_Service_Datastore_Mutation $obj_mutation)
 {
     $obj_request = new \Google_Service_Datastore_CommitRequest();
     if (null === $this->str_next_transaction) {
         $obj_request->setMode('NON_TRANSACTIONAL');
     } else {
         $obj_request->setMode('TRANSACTIONAL');
         $obj_request->setTransaction($this->str_next_transaction);
         $this->str_next_transaction = NULL;
     }
     $obj_request->setMutation($obj_mutation);
     try {
         $this->obj_last_response = $this->obj_datasets->commit($this->str_dataset_id, $obj_request);
     } catch (\Google_Service_Exception $obj_exception) {
         $this->obj_last_response = NULL;
         if (409 == $obj_exception->getCode()) {
             throw new Contention('Datastore contention', 409, $obj_exception);
         } else {
             throw $obj_exception;
         }
     }
 }
Esempio n. 2
0
 /**
  * Apply a mutation to the Datastore (commit)
  *
  * @param \Google_Service_Datastore_Mutation $obj_mutation
  * @return \Google_Service_Datastore_CommitResponse
  */
 private function commitMutation(\Google_Service_Datastore_Mutation $obj_mutation)
 {
     $obj_request = new \Google_Service_Datastore_CommitRequest();
     if (null === $this->str_next_transaction) {
         $obj_request->setMode('NON_TRANSACTIONAL');
     } else {
         $obj_request->setMode('TRANSACTIONAL');
         $obj_request->setTransaction($this->str_next_transaction);
         $this->str_next_transaction = null;
     }
     $obj_request->setMutation($obj_mutation);
     $this->obj_last_response = $this->obj_datasets->commit($this->str_dataset_id, $obj_request);
     return $this->obj_last_response;
 }
 public static function batchTxnMutate($txn, $batchput, $deletes = [])
 {
     if (!$txn) {
         throw new UnexpectedValueException('Transaction value not set.');
     }
     $insert_auto_id_items = [];
     $upsert_items = [];
     $delete_items = [];
     foreach ($batchput as $item) {
         $entity = $item->create_entity();
         if ($item->key_id || $item->key_name) {
             $upsert_items[] = $entity;
         } else {
             $insert_auto_id_items[] = $entity;
         }
     }
     foreach ($deletes as $delitem) {
         $delitem->beforeItemDelete();
         $delete_items[] = self::createKeyForItem($delitem);
     }
     $mutation = new Google_Service_Datastore_Mutation();
     if (!empty($insert_auto_id_items)) {
         $mutation->setInsertAutoId($insert_auto_id_items);
     }
     if (!empty($upsert_items)) {
         $mutation->setUpsert($upsert_items);
     }
     if (!empty($delete_items)) {
         $mutation->setDelete($delete_items);
     }
     $req = new Google_Service_Datastore_CommitRequest();
     $req->setMutation($mutation);
     $req->setTransaction($txn);
     // will throw Google_Service_Exception if there is contention
     DatastoreService::getInstance()->commit($req);
     // successful commit. Call the onItemWrite method on each of the batch put items
     foreach ($batchput as $item) {
         $item->onItemWrite();
     }
 }
 /**
  * @param Google_Service_Datastore_Mutation|Mutation\Builder $mutation
  * @param string $transaction
  * @param bool $ignoreReadOnly
  * @param array $options
  * @return Commit\Results
  */
 public function commitMutation($mutation, $transaction = null, $ignoreReadOnly = false, $options = [])
 {
     if ($mutation instanceof Mutation\Builder) {
         $mutation = $mutation->build();
     }
     $request = new Google_Service_Datastore_CommitRequest();
     $request->setMutation($mutation);
     if ($transaction === null) {
         $request->setTransaction($transaction);
     }
     $request->setMode($transaction === null ? Commit\Mode::_NON_TRANSACTIONAL : Commit\Mode::_TRANSACTIONAL);
     $request->setIgnoreReadOnly($ignoreReadOnly);
     $response = $this->datastoreService->datasets->commit($this->datasetId, $request, $options);
     return new Commit\Results($response);
 }