public function testReplaceLiteralTriple() { $graph = new ExtendedGraph(); $graph->add_literal_triple('http://some/subject/s1', 'http://some/predicate', 'some object'); $this->assertTrue($graph->replace_literal_triple('http://some/subject/s1', 'http://some/predicate', 'some object', 'replacement object'), 'Should return true on replacement'); $index = $graph->get_index(); $this->assertEquals('replacement object', $index['http://some/subject/s1']['http://some/predicate'][0]['value'], "should be 'replacement object'"); }
/** * Create and apply a changeset which is the delta between $oldGraph and $newGraph * @param \Tripod\ExtendedGraph $oldGraph * @param \Tripod\ExtendedGraph $newGraph * @param string|null $context * @param string|null $description * @throws \Exception * @return bool */ public function saveChanges(\Tripod\ExtendedGraph $oldGraph, \Tripod\ExtendedGraph $newGraph, $context = null, $description = null) { $this->applyHooks($this::HOOK_FN_PRE, $this->saveChangesHooks, array("pod" => $this->getPodName(), "oldGraph" => $oldGraph, "newGraph" => $newGraph, "context" => $context)); $this->setReadPreferenceToPrimary(); try { $contextAlias = $this->getContextAlias($context); if (!Config::getInstance()->isPodWithinStore($this->getStoreName(), $this->getPodName())) { throw new \Tripod\Exceptions\Exception("database:collection " . $this->getStoreName() . ":" . $this->getPodName() . " is not referenced within config, so cannot be written to"); } $this->validateGraphCardinality($newGraph); $oldIndex = $oldGraph->get_index(); $newIndex = $newGraph->get_index(); $args = array('before' => $oldIndex, 'after' => $newIndex, 'changeReason' => $description); $cs = new \Tripod\ChangeSet($args); $subjectsAndPredicatesOfChange = array(); $transaction_id = null; if ($cs->has_changes()) { // store the actual CBDs $result = $this->storeChanges($cs, $contextAlias); if (!isset($result['subjectsAndPredicatesOfChange']) || !isset($result['transaction_id'])) { $this->errorLog("Result of storeChanges malformed, should have transaction_id and subjectsAndPredicatesOfChange array keys", array("result" => $result)); throw new Exception("Result of storeChanges malformed, should have transaction_id and subjectsAndPredicatesOfChange array keys"); } extract($result); // will unpack into $subjectsAndPredicatesOfChange // Process any syncronous operations $this->processSyncOperations($subjectsAndPredicatesOfChange, $contextAlias); // Schedule calculation of any async activity $this->queueAsyncOperations($subjectsAndPredicatesOfChange, $contextAlias); } $this->applyHooks($this::HOOK_FN_SUCCESS, $this->saveChangesHooks, array("pod" => $this->getPodName(), "oldGraph" => $oldGraph, "newGraph" => $newGraph, "context" => $context, "changeSet" => $cs, "subjectsAndPredicatesOfChange" => $subjectsAndPredicatesOfChange, "transaction_id" => $transaction_id)); } catch (\Exception $e) { // ensure we reset the original read preference in the event of an exception $this->resetOriginalReadPreference(); $this->applyHooks($this::HOOK_FN_FAILURE, $this->saveChangesHooks, array("pod" => $this->getPodName(), "oldGraph" => $oldGraph, "newGraph" => $newGraph, "context" => $context)); throw $e; } $this->resetOriginalReadPreference(); return true; }