function create_test_request() { $entity = create_entity(); $mutation = new Google_Service_Datastore_Mutation(); $mutation->setUpsert([$entity]); $req = new Google_Service_Datastore_CommitRequest(); $req->setMode('NON_TRANSACTIONAL'); $req->setMutation($mutation); return $req; }
/** * Creates the request to store a DatastoreComment item in datastore */ public function createCommentRequest(\Google_Service_Datastore_Key $id, $name, $body) { $entity = $this->createEntity($id, $name, $body); $mutation = new \Google_Service_Datastore_Mutation(); $mutation->setUpsert([$entity]); $req = new \Google_Service_Datastore_CommitRequest(); $req->setMode('NON_TRANSACTIONAL'); $req->setMutation($mutation); return $req; }
function create_request($hashkey, $text) { $entity = create_entity($hashkey, $text); $mutation = new Google_Service_Datastore_Mutation(); $mutation->setInsertAutoId([$entity]); // Causes ID to be allocated. # $mutation->setUpsert([$entity]); $req = new Google_Service_Datastore_CommitRequest(); $req->setMode('NON_TRANSACTIONAL'); $req->setMutation($mutation); return $req; }
/** * 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; } } }
/** * Do a non-transactional batch put. Split into sub-batches * if the list is too big. */ public static function putBatch($batchput) { $insert_auto_id_items = []; $upsert_items = []; $batch_limit = 490; $count = 0; // process the inserts/updates foreach ($batchput as $item) { $entity = $item->create_entity(); if ($item->key_id || $item->key_name) { $upsert_items[] = $entity; } else { $insert_auto_id_items[] = $entity; } $count++; if ($count > $batch_limit) { // we've reached the batch limit-- write what we have so far $mutation = new Google_Service_Datastore_Mutation(); if (!empty($insert_auto_id_items)) { $mutation->setInsertAutoId($insert_auto_id_items); } // TODO -- why was this an 'else'? // else if (!empty($upsert_items)) { if (!empty($upsert_items)) { $mutation->setUpsert($upsert_items); } $req = new Google_Service_Datastore_CommitRequest(); $req->setMutation($mutation); $req->setMode('NON_TRANSACTIONAL'); DatastoreService::getInstance()->commit($req); // reset the batch count and lists $count = 0; $insert_auto_id_items = []; $upsert_items = []; } } // insert the remainder. $mutation = new Google_Service_Datastore_Mutation(); syslog(LOG_DEBUG, "inserts " . count($insert_auto_id_items) . ", upserts " . count($upsert_items)); if (!empty($insert_auto_id_items)) { $mutation->setInsertAutoId($insert_auto_id_items); } if (!empty($upsert_items)) { $mutation->setUpsert($upsert_items); } $req = null; $req = new Google_Service_Datastore_CommitRequest(); $req->setMutation($mutation); $req->setMode('NON_TRANSACTIONAL'); DatastoreService::getInstance()->commit($req); //now, call the onItemWrite method on each of the batch put items foreach ($batchput as $item) { $item->onItemWrite(); } }
/** * 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; }
/** * @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); }