Example #1
0
 /**
  * @param \Tripod\ChangeSet $cs Change-set to apply
  * @param string $contextAlias
  * @throws \Tripod\Exceptions\Exception
  * @return array An array of subjects and predicates that have been changed
  */
 protected function storeChanges(\Tripod\ChangeSet $cs, $contextAlias)
 {
     $t = new \Tripod\Timer();
     $t->start();
     $subjectsOfChange = $cs->get_subjects_of_change();
     $transaction_id = $this->generateTransactionId();
     // store the details of the transaction in the transaction log
     $mongoGraph = new MongoGraph();
     $mongoGraph->_index = $cs->_index;
     $csDoc = $mongoGraph->to_tripod_view_array("changes", $contextAlias);
     // todo - this changed to tripod view array, why is "changes" the docId?
     $originalCBDs = array();
     // apply the changes
     try {
         // 1. lock all documents
         // 2. create new transaction
         // 3. apply changes
         // 4. unlock all documents
         // 5. complete transaction
         $originalCBDs = $this->lockAllDocuments($subjectsOfChange, $transaction_id, $contextAlias);
         $this->getTransactionLog()->createNewTransaction($transaction_id, $csDoc['value'][_GRAPHS], $originalCBDs, $this->getStoreName(), $this->getPodName());
         if (empty($originalCBDs)) {
             $this->getTransactionLog()->failTransaction($transaction_id, new \Exception('Did not obtain locks on documents'));
             throw new \Exception('Did not obtain locks on documents');
         }
         $changes = $this->applyChangeSet($cs, $originalCBDs, $contextAlias, $transaction_id);
         $this->debugLog(MONGO_LOCK, array('description' => 'Driver::storeChanges - Unlocking documents, apply change-set completed', 'transaction_id' => $transaction_id));
         $this->unlockAllDocuments($transaction_id);
         $this->getTransactionLog()->completeTransaction($transaction_id, $changes['newCBDs']);
         $t->stop();
         $this->timingLog(MONGO_WRITE, array('duration' => $t->result(), 'subjectsOfChange' => implode(", ", $subjectsOfChange)));
         $this->getStat()->timer(MONGO_WRITE . ".{$this->getPodName()}", $t->result());
         return $changes;
     } catch (\Exception $e) {
         $this->getStat()->increment(MONGO_ROLLBACK);
         $this->errorLog(MONGO_ROLLBACK, array('description' => 'Save Failed Rolling back transaction:' . $e->getMessage(), 'transaction_id' => $transaction_id, 'subjectsOfChange' => implode(",", $subjectsOfChange), 'mongoDriverError' => $this->getLastDBError($this->getDatabase()), 'exceptionMessage' => $e->getMessage()));
         $this->rollbackTransaction($transaction_id, $originalCBDs, $e);
         throw new \Tripod\Exceptions\Exception('Error storing changes: ' . $e->getMessage() . " >>>" . $e->getTraceAsString());
     }
 }