Inheritance: extends MongoWriteBatch
 public function testValidateItem()
 {
     $collection = $this->getCollection();
     $batch = new \MongoUpdateBatch($collection);
     $this->setExpectedException('Exception', 'invalid item');
     $batch->add([]);
 }
 public function testValidateItem()
 {
     $collection = $this->getCollection();
     $batch = new \MongoUpdateBatch($collection);
     $this->setExpectedException('Exception', "Expected \$item to contain 'q' key");
     $batch->add([]);
 }
 /**
  * Reverse the migrations.
  *
  * @return void
  */
 public function down()
 {
     $db = \DB::getMongoDB();
     $statementsCollection = new MongoCollection($db, 'statements');
     $statementsCollection->deleteIndex('stored');
     $statementsCollection->deleteIndex(['lrs_id' => 1, 'stored' => -1]);
     $statementsCollection->update([], ['$unset' => ["stored" => ""]], ['multiple' => true]);
     $statementsCursor = $statementsCollection->find();
     $remaining = $statementsCursor->count();
     print $remaining . ' statements total' . PHP_EOL;
     $maxBatchSize = 10000;
     while ($statementsCursor->hasNext()) {
         $batch = new MongoUpdateBatch($statementsCollection);
         $batchSize = 0;
         $shouldExecute = false;
         while ($batchSize < $maxBatchSize && $statementsCursor->hasNext()) {
             $batchSize++;
             $statement = $statementsCursor->next();
             if (isset($statement['refs'])) {
                 $query = ['q' => ['_id' => $statement['_id']], 'u' => ['$set' => []], 'multi' => false, 'upsert' => false];
                 foreach ($statement['refs'] as $key => $refStatement) {
                     if (isset($refStatement['timestamp']) && $refStatement['timestamp'] instanceof MongoDate) {
                         $query['u']['$set']['refs.' . $key . '.timestamp'] = date('Y-m-d\\TH:i:s.uP', $refStatement['timestamp']->sec);
                     }
                     if (isset($refStatement['stored']) && $refStatement['stored'] instanceof MongoDate) {
                         $query['u']['$set']['refs.' . $key . '.stored'] = date('Y-m-d\\TH:i:s.uP', $refStatement['stored']->sec);
                     }
                 }
                 if (!empty($query['u']['$set'])) {
                     $batch->add((object) $query);
                     $shouldExecute = true;
                 }
             }
         }
         if ($shouldExecute) {
             $batch->execute();
         }
         $remaining -= $batchSize;
         print $remaining . ' remaining' . PHP_EOL;
     }
 }
Example #4
0
 /**
  * 批量操作后,取模结果余数内容的处理。
  * @param 
  * @return null
  */
 public function flushBuffer()
 {
     $arr =& $this->_getBatchArr('update');
     if (count($arr) > 0) {
         $c = $this->_getCollection();
         $batch = new MongoUpdateBatch($c);
         foreach ($arr as $doc) {
             $batch->add((object) $doc);
         }
         $batch->execute();
     }
 }
Example #5
0
 /**
  * @param $data
  */
 public function batchUpdate($data = array())
 {
     if ($data && is_array($data)) {
         try {
             $docs =& $this->_getBatchBuff('update');
             if (count($docs) > self::MAXBATCHNUM) {
                 $c = $this->_getColctObj();
                 $batch = new MongoUpdateBatch($c);
                 foreach ($docs as $doc) {
                     $batch->add((object) $doc);
                 }
                 $batch->execute();
                 $docs = array();
             } else {
                 if (isset($data['userkey'])) {
                     $update = array('q' => array('userkey' => $data['userkey']), 'u' => array('$set' => $data), 'multi' => false, 'upsert' => true);
                     array_push($docs, $update);
                 }
             }
         } catch (MongoException $e) {
             Logger::logInfo('mongo in lib batchupdate failed, message [' . $e->getMessage() . '] code: [' . $e->getCode() . ']');
             return false;
         }
     } else {
         Logger::logInfo('data of batchupdate is invaild');
         return false;
     }
 }
Example #6
0
 public function multiUpdate($data, $writeOptions = array('w' => 1))
 {
     global $logger;
     $logger->debug("Mongo multiUpdate() :: Data: " . json_encode($data));
     $logger->debug("Mongo multiUpdate() :: Option: " . json_encode($writeOptions));
     $batch = new MongoUpdateBatch($this->collection, $writeOptions);
     foreach ($data as $document) {
         $batch->add($document);
     }
     $result = $batch->execute($writeOptions);
     $logger->debug("Mongo multiUpdate() :: Result: " . json_encode($result));
     return $result;
 }