Esempio n. 1
0
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;
}
Esempio n. 2
0
 /**
  * @return \Google_Service_Datastore_Mutation
  * @throws MissingFieldsException
  */
 public function build()
 {
     if (empty($this->upsert) && empty($this->update) && empty($this->insert) && empty($this->insertAutoId) && empty($this->delete)) {
         throw new MissingFieldsException(self::class, ['$upsert', '$update', '$insert', '$insertAutoId', '$delete']);
     }
     $mutation = new \Google_Service_Datastore_Mutation();
     if (!empty($this->upsert)) {
         $mutation->setUpsert($this->upsert);
     }
     if (!empty($this->update)) {
         $mutation->setUpdate($this->update);
     }
     if (!empty($this->insert)) {
         $mutation->setInsert($this->insert);
     }
     if (!empty($this->insertAutoId)) {
         $mutation->setInsertAutoId($this->insertAutoId);
     }
     if (!empty($this->delete)) {
         $mutation->setDelete($this->delete);
     }
     $mutation->setForce($this->force);
     return $mutation;
 }
 /**
  * Put an array of Entities into the Datastore. Return any that need AutoIDs
  *
  * @todo Validate support for per-entity Schemas
  *
  * @param \GDS\Entity[] $arr_entities
  * @return \GDS\Entity[]
  */
 public function upsert(array $arr_entities)
 {
     $obj_mutation = new \Google_Service_Datastore_Mutation();
     /* @var $arr_auto_id_required \GDS\Entity[] */
     $arr_auto_id_required = [];
     $arr_mutation_auto_id = [];
     $arr_mutation_upsert = [];
     foreach ($arr_entities as $obj_gds_entity) {
         $obj_google_entity = $this->determineMapper($obj_gds_entity)->mapToGoogle($obj_gds_entity);
         $this->applyNamespace($obj_google_entity->getKey());
         if (null === $obj_gds_entity->getKeyId() && null === $obj_gds_entity->getKeyName()) {
             $arr_mutation_auto_id[] = $obj_google_entity;
             $arr_auto_id_required[] = $obj_gds_entity;
             // maintain reference to the array of requested auto-ids
         } else {
             $arr_mutation_upsert[] = $obj_google_entity;
         }
     }
     if (!empty($arr_mutation_auto_id)) {
         $obj_mutation->setInsertAutoId($arr_mutation_auto_id);
     }
     if (!empty($arr_mutation_upsert)) {
         $obj_mutation->setUpsert($arr_mutation_upsert);
     }
     $this->commitMutation($obj_mutation);
     return $arr_auto_id_required;
 }
 /**
  * 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();
     }
 }