/** * @param Driver $tripod */ public function __construct(Driver $tripod) { $this->tripod = $tripod; $this->storeName = $tripod->getStoreName(); $this->labeller = new Labeller(); $this->config = Config::getInstance(); }
public function testRemoveTableSpecDoesNotAffectInvalidation() { foreach (\Tripod\Mongo\Config::getInstance()->getTableSpecifications($this->tripod->getStoreName()) as $specId => $spec) { $this->generateTableRows($specId); } $context = 'http://talisaspire.com/'; $uri = "http://talisaspire.com/works/4d101f63c10a6"; $collection = \Tripod\Mongo\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_resource'); $this->assertGreaterThan(0, $collection->count(array('_id.type' => 't_resource', 'value._impactIndex' => array(_ID_RESOURCE => $uri, _ID_CONTEXT => $context)))); $config = \Tripod\Mongo\Config::getConfig(); unset($config['stores']['tripod_php_testing']['table_specifications'][0]); \Tripod\Mongo\Config::setConfig($config); /** @var PHPUnit_Framework_MockObject_MockObject|\Tripod\Mongo\Driver $mockTripod */ $mockTripod = $this->getMockBuilder('\\Tripod\\Mongo\\Driver')->setMethods(array('getComposite'))->setConstructorArgs(array('CBD_testing', 'tripod_php_testing', array('defaultContext' => $context, OP_ASYNC => array(OP_VIEWS => true, OP_TABLES => false, OP_SEARCH => true))))->getMock(); $mockTables = $this->getMockBuilder('\\Tripod\\Mongo\\Composites\\Tables')->setMethods(array('update'))->setConstructorArgs(array('tripod_php_testing', \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), $context))->getMock(); $labeller = new \Tripod\Mongo\Labeller(); $mockTripod->expects($this->once())->method('getComposite')->with(OP_TABLES)->will($this->returnValue($mockTables)); $mockTables->expects($this->never())->method('update'); $originalGraph = $mockTripod->describeResource($uri); $updatedGraph = $originalGraph->get_subject_subgraph($uri); $updatedGraph->add_literal_triple($uri, $labeller->qname_to_uri('dct:description'), 'Physics textbook'); $mockTripod->saveChanges($originalGraph, $updatedGraph); // The table row should still be there, even if the tablespec no longer exists $this->assertGreaterThan(0, $collection->count(array('_id.type' => 't_resource', 'value._impactIndex' => array(_ID_RESOURCE => $uri, _ID_CONTEXT => $context)))); }
/** * This test verifies that if a document was previously added to mongo without any timestamps i.e. _UPDATED_TS and _CREATED_TS * then on a tripod write only the _UPDATED_TS will be added to the document */ public function testOnlyDocumentUpdatedTimestampIsAddedToDocumentThatDidntHaveTimestampsToBeginWith() { // add the initial document, but not through Driver! $_id = array("r" => "http://talisaspire.com/resources/testDocument2", "c" => "http://talisaspire.com/"); $document = array('_id' => $_id, 'dct:title' => array('l' => 'some title'), '_version' => 0); // verify initial document before we proceed, should have the triple we added, and a _version but not a // _UPDATED_TS or a _CREATED_TS $this->addDocument($document); $this->assertDocumentExists($_id); $this->assertDocumentHasProperty($_id, _VERSION, 0); $this->assertDocumentHasProperty($_id, "dct:title", array("l" => "some title")); $this->assertDocumentDoesNotHaveProperty($_id, _UPDATED_TS); $this->assertDocumentDoesNotHaveProperty($_id, _CREATED_TS); // change the document through tripod, for this im just doing a new addition $graph = new \Tripod\Mongo\MongoGraph(); $graph->add_literal_triple($_id["r"], $graph->qname_to_uri('searchterms:title'), 'a new property'); $this->tripod->saveChanges(new \Tripod\Mongo\MongoGraph(), $graph); // Now assert, document should contain the additiona triple we added, an updated _version. // Should now also contain an _UPDATED_TS but not a _CREATED_TS $this->assertDocumentExists($_id); $this->assertDocumentHasProperty($_id, _VERSION, 1); $this->assertDocumentHasProperty($_id, _UPDATED_TS); $this->assertDocumentHasProperty($_id, "dct:title", array("l" => "some title")); $this->assertDocumentHasProperty($_id, "searchterms:title", array("l" => "a new property")); $this->assertDocumentDoesNotHaveProperty($_id, _CREATED_TS); }
/** * @param \Tripod\Mongo\Driver $tripod * @param string $readPreference * @throws \Tripod\Exceptions\SearchException */ public function __construct(\Tripod\Mongo\Driver $tripod, $readPreference = ReadPreference::RP_PRIMARY) { $this->tripod = $tripod; $this->storeName = $tripod->getStoreName(); $this->podName = $tripod->podName; $this->labeller = new Labeller(); $this->stat = $tripod->getStat(); $this->config = Config::getInstance(); $provider = $this->config->getSearchProviderClassName($this->tripod->getStoreName()); if (class_exists($provider)) { $this->configuredProvider = new $provider($this->tripod); } else { throw new \Tripod\Exceptions\SearchException("Did not recognise Search Provider, or could not find class: {$provider}"); } $this->readPreference = $readPreference; }
/** * This test addes a set of precanned transactions to the transaction log, and then verifies that * only the transactions $gte the given date are replayed */ public function testReplayTransactionsBetweenTwoDates() { $transaction_1 = $this->buildTransactionDocument(1, 'http://example.com/resources/1', '2013-01-21T13:00:00.000Z', '2013-01-21T13:01:00.000Z', 0); $transaction_2 = $this->buildTransactionDocument(2, 'http://example.com/resources/2', '2013-01-21T13:00:00.000Z', '2013-01-21T13:02:00.000Z', 0); $transaction_3 = $this->buildTransactionDocument(3, 'http://example.com/resources/3', '2013-01-21T13:00:00.000Z', '2013-01-21T13:03:00.000Z', 0); $transaction_4 = $this->buildTransactionDocument(4, 'http://example.com/resources/4', '2013-01-21T13:00:00.000Z', '2013-01-21T13:04:00.000Z', 0); $transaction_5 = $this->buildTransactionDocument(5, 'http://example.com/resources/5', '2013-01-21T13:00:00.000Z', '2013-01-21T13:05:00.000Z', 0); $this->addDocument($transaction_1, true); $this->addDocument($transaction_2, true); $this->addDocument($transaction_3, true); $this->addDocument($transaction_4, true); $this->addDocument($transaction_5, true); $this->getTripodCollection($this->tripod)->drop(); $this->tripod->replayTransactionLog("2013-01-21T13:02:00.000Z", "2013-01-21T13:04:00.000Z"); $g = $this->tripod->describeResources(array('http://example.com/resources/1', 'http://example.com/resources/2', 'http://example.com/resources/3', 'http://example.com/resources/4', 'http://example.com/resources/5')); $this->assertFalse($g->has_triples_about('http://example.com/resources/1'), "Should not contain anything about /resources/1"); $this->assertTrue($g->has_triples_about('http://example.com/resources/2'), "Should contain triples about /resources/2"); $this->assertTrue($g->has_triples_about('http://example.com/resources/3'), "Should contain triples about /resources/3"); $this->assertTrue($g->has_triples_about('http://example.com/resources/4'), "Should contain triples about /resources/4"); $this->assertFalse($g->has_triples_about('http://example.com/resources/5'), "Should not contain triples about /resources/5"); }
/** * Processes each subject synchronously * @param array $subjectsAndPredicatesOfChange * @param string $contextAlias */ protected function processSyncOperations(array $subjectsAndPredicatesOfChange, $contextAlias) { foreach ($this->getSyncOperations() as $op) { /** @var \Tripod\Mongo\Composites\IComposite $composite */ $composite = $this->tripod->getComposite($op); $opSubjects = $composite->getImpactedSubjects($subjectsAndPredicatesOfChange, $contextAlias); if (!empty($opSubjects)) { foreach ($opSubjects as $subject) { /* @var $subject ImpactedSubject */ $t = new \Tripod\Timer(); $t->start(); // Call update on the subject, rather than the composite directly, in case the change was to // another pod $subject->update($subject); $t->stop(); $this->timingLog(MONGO_ON_THE_FLY_MR, array("duration" => $t->result(), "storeName" => $subject->getStoreName(), "podName" => $subject->getPodName(), "resourceId" => $subject->getResourceId())); $this->getStat()->timer(MONGO_ON_THE_FLY_MR, $t->result()); } } } }
/** * This is a private method that performs exactly the same operation as Driver::lockSingleDocument, the reason this is duplicated here * is so that we can simulate the correct locking of documents as part of mocking a workflow that will lock a document correctly but not another * @param $s * @param $transaction_id * @param $contextAlias * @return array */ public function lockSingleDocumentCallback($s, $transaction_id, $contextAlias) { $lCollection = \Tripod\Mongo\Config::getInstance()->getCollectionForLocks($this->tripod->getStoreName()); $countEntriesInLocksCollection = $lCollection->count(array('_id' => array(_ID_RESOURCE => $this->labeller->uri_to_alias($s), _ID_CONTEXT => $contextAlias))); if ($countEntriesInLocksCollection > 0) { //Subject is already locked return false; } else { try { //Add a entry to locks collection for this subject, will throws exception if an entry already there $result = $lCollection->insertOne(array('_id' => array(_ID_RESOURCE => $this->labeller->uri_to_alias($s), _ID_CONTEXT => $contextAlias), _LOCKED_FOR_TRANS => $transaction_id, _LOCKED_FOR_TRANS_TS => \Tripod\Mongo\DateUtil::getMongoDate()), array("w" => 1)); if (!$result->isAcknowledged()) { throw new Exception("Failed to lock document with error message- " . $this->getLastDBError()); } } catch (Exception $e) { //Subject is already locked or unable to lock $this->debugLog(MONGO_LOCK, array('description' => 'Driver::lockSingleDocument - failed with exception', 'transaction_id' => $transaction_id, 'subject' => $s, 'exception-message' => $e->getMessage())); return false; } //Let's get original document for processing. $document = $this->getTripodCollection($this->tripod)->findOne(array('_id' => array(_ID_RESOURCE => $this->labeller->uri_to_alias($s), _ID_CONTEXT => $contextAlias))); if (empty($document)) { //if document is not there, create it try { $result = $this->getTripodCollection($this->tripod)->insertOne(array('_id' => array(_ID_RESOURCE => $this->labeller->uri_to_alias($s), _ID_CONTEXT => $contextAlias)), array("w" => 1)); if (!$result->isAcknowledged()) { throw new Exception("Failed to create new document with error message- " . $this->getLastDBError()); } $document = $this->getTripodCollection($this->tripod)->findOne(array('_id' => array(_ID_RESOURCE => $this->labeller->uri_to_alias($s), _ID_CONTEXT => $contextAlias))); } catch (\Exception $e) { $this->errorLog(MONGO_LOCK, array('description' => 'Driver::lockSingleDocument - failed when creating new document', 'transaction_id' => $transaction_id, 'subject' => $s, 'exception-message' => $e->getMessage())); return false; } } return $document; } }
/** * @param \Tripod\Mongo\Driver $tripod * @param array $specs * @return int */ protected function getCountForSearchSpecs(\Tripod\Mongo\Driver $tripod, $specs = array()) { $count = 0; if (empty($specs)) { $specs = \Tripod\Mongo\Config::getInstance()->getSearchDocumentSpecifications($tripod->getStoreName(), null, true); } foreach ($specs as $spec) { $count += \Tripod\Mongo\Config::getInstance()->getCollectionForSearchDocument($tripod->getStoreName(), $spec)->count(array('_id.type' => $spec)); } return $count; }
/** * @param \Tripod\Mongo\Driver $tripod * @return Collection */ protected function getTripodCollection(\Tripod\Mongo\Driver $tripod) { $config = \Tripod\Mongo\Config::getInstance(); $podName = $tripod->getPodName(); $dataSource = $config->getDataSourceForPod($tripod->getStoreName(), $podName); return $config->getDatabase($tripod->getStoreName(), $dataSource)->selectCollection($tripod->getPodName()); }
/** * @param \Tripod\Mongo\Driver $tripod * @return SearchDocuments */ protected function getSearchDocuments(\Tripod\Mongo\Driver $tripod) { return new \Tripod\Mongo\SearchDocuments($tripod->getStoreName(), $this->getTripodCollection($tripod), 'http://talisaspire.com/'); }