/** * @param string $filename */ private function loadDataViaTripod($filename) { $docs = json_decode(file_get_contents(dirname(__FILE__) . $filename), true); foreach ($docs as $d) { $g = new \Tripod\Mongo\MongoGraph(); $g->add_tripod_array($d); $this->tripod->saveChanges(new \Tripod\ExtendedGraph(), $g, $d['_id'][_ID_CONTEXT]); } }
public function testWriteLockedDocDoesNotExposeVersionOrLockPropertyInGraph() { $doc = array("_id" => array("r" => "http://example.com/things/1", "c" => "http://example.com/"), _VERSION => 0, _LOCKED_FOR_TRANS => "transaction_234"); $g = new \Tripod\Mongo\MongoGraph(); $g->add_tripod_array($doc); $this->assertTrue(count($g->get_index()) == 0, "Graph should contain no data"); }
/** * Save several new resources in a single operation. Only one of the resources has a type that is applicable based on specifications, * therefore only one ImpactedSubject should be created */ public function testSavingMultipleNewEntitiesResultsInOneImpactedSubject() { $tripod = $this->getMockBuilder('\\Tripod\\Mongo\\Driver')->setMethods(array('getDataUpdater'))->setConstructorArgs(array('CBD_testing', 'tripod_php_testing', array('defaultContext' => 'http://talisaspire.com/', OP_ASYNC => array(OP_VIEWS => true, OP_TABLES => true, OP_SEARCH => true))))->getMock(); $tripodUpdates = $this->getMockBuilder('\\Tripod\\Mongo\\Updates')->setMethods(array('submitJob'))->setConstructorArgs(array($tripod, array('defaultContext' => 'http://talisaspire.com/', OP_ASYNC => array(OP_VIEWS => true, OP_TABLES => true, OP_SEARCH => true))))->getMock(); $tripod->expects($this->once())->method('getDataUpdater')->will($this->returnValue($tripodUpdates)); // first lets add a book, which should trigger a search doc, view and table gen for a single item $g = new \Tripod\Mongo\MongoGraph(); $newSubjectUri1 = "http://talisaspire.com/resources/newdoc1"; $newSubjectUri2 = "http://talisaspire.com/resources/newdoc2"; $newSubjectUri3 = "http://talisaspire.com/resources/newdoc3"; $g->add_resource_triple($newSubjectUri1, $g->qname_to_uri("rdf:type"), $g->qname_to_uri("bibo:Article")); // there are no specs that are applicable for this type alone $g->add_resource_triple($newSubjectUri1, $g->qname_to_uri("dct:creator"), "http://talisaspire.com/authors/1"); $g->add_literal_triple($newSubjectUri1, $g->qname_to_uri("dct:title"), "This is a new resource"); $g->add_literal_triple($newSubjectUri1, $g->qname_to_uri("dct:subject"), "history"); $g->add_literal_triple($newSubjectUri1, $g->qname_to_uri("dct:subject"), "philosophy"); $g->add_resource_triple($newSubjectUri2, $g->qname_to_uri("rdf:type"), $g->qname_to_uri("bibo:Book")); // this is the only resource that should be queued $g->add_resource_triple($newSubjectUri2, $g->qname_to_uri("rdf:type"), $g->qname_to_uri("acorn:Resource")); $g->add_resource_triple($newSubjectUri2, $g->qname_to_uri("dct:creator"), "http://talisaspire.com/authors/1"); $g->add_literal_triple($newSubjectUri2, $g->qname_to_uri("dct:title"), "This is another new resource"); $g->add_literal_triple($newSubjectUri2, $g->qname_to_uri("dct:subject"), "maths"); $g->add_literal_triple($newSubjectUri2, $g->qname_to_uri("dct:subject"), "science"); $g->add_resource_triple($newSubjectUri3, $g->qname_to_uri("rdf:type"), $g->qname_to_uri("bibo:Journal")); // there are no specs that are applicable for this type alone $g->add_resource_triple($newSubjectUri3, $g->qname_to_uri("dct:creator"), "http://talisaspire.com/authors/1"); $g->add_literal_triple($newSubjectUri3, $g->qname_to_uri("dct:title"), "This is yet another new resource"); $g->add_literal_triple($newSubjectUri3, $g->qname_to_uri("dct:subject"), "art"); $g->add_literal_triple($newSubjectUri3, $g->qname_to_uri("dct:subject"), "design"); $subjectsAndPredicatesOfChange = array($newSubjectUri1 => array('rdf:type', 'dct:creator', 'dct:title', 'dct:subject'), $newSubjectUri2 => array('rdf:type', 'dct:creator', 'dct:title', 'dct:subject'), $newSubjectUri3 => array('rdf:type', 'dct:creator', 'dct:title', 'dct:subject')); $tripod->saveChanges(new \Tripod\Mongo\MongoGraph(), $g); /** @var \Tripod\Mongo\Tables $tables */ $search = $tripod->getComposite(OP_SEARCH); $expectedImpactedSubjects = array(new \Tripod\Mongo\ImpactedSubject(array(_ID_RESOURCE => $newSubjectUri2, _ID_CONTEXT => 'http://talisaspire.com/'), OP_SEARCH, 'tripod_php_testing', 'CBD_testing', array())); $impactedSubjects = $search->getImpactedSubjects($subjectsAndPredicatesOfChange, 'http://talisaspire.com/'); $this->assertEquals($expectedImpactedSubjects, $impactedSubjects); }
$graph = new \Tripod\Mongo\MongoGraph(); $graph->add_rdf($rawGraphData); $tripod->saveChanges(new \Tripod\ExtendedGraph(), $graph); }); }); $app->group('/change', function () use($app, $tripodOptions) { $app->post('/', function ($storeName, $podName) use($app, $tripodOptions) { \Tripod\Mongo\Config::setConfig(json_decode(file_get_contents('./config/tripod-config-' . $storeName . '.json'), true)); $app->response()->setStatus(500); $tripodOptions['statsConfig'] = getStat($app, $tripodOptions); $tripod = new \Tripod\Mongo\Driver($podName, $storeName, $tripodOptions); $rawChangeData = $app->request()->post('data'); if ($rawChangeData) { $changeData = json_decode($rawChangeData, true); $from = new \Tripod\Mongo\MongoGraph(); $to = new \Tripod\Mongo\MongoGraph(); if (isset($changeData['originalCBDs'])) { foreach ($changeData['originalCBDs'] as $change) { if (is_array($change) && isset($change[_ID_KEY])) { $from->add_tripod_array($change); } } } if (isset($changeData['newCBDs'])) { foreach ($changeData['newCBDs'] as $change) { if (is_array($change) && isset($change[_ID_KEY])) { $to->add_tripod_array($change); } } } try {
public function testTransactionsLoggedCorrectlyFromMultipleTripods() { // Create two tripods onto different collection/dbname and make them use the same transaction log $tripod1 = $this->getMock('\\Tripod\\Mongo\\Driver', array('generateViewsAndSearchDocumentsForResources'), array('CBD_testing', 'tripod_php_testing')); $tripod1->expects($this->any())->method('generateViewsAndSearchDocumentsForResources'); $this->getTripodCollection($tripod1)->drop(); $tripod1->setTransactionLog($this->tripodTransactionLog); $tripod2 = $this->getMock('\\Tripod\\Mongo\\Driver', array('generateViewsAndSearchDocumentsForResources'), array('CBD_testing_2', 'tripod_php_testing')); $tripod2->expects($this->any())->method('generateViewsAndSearchDocumentsForResources'); $this->getTripodCollection($tripod2)->drop(); $tripod2->setTransactionLog($this->tripodTransactionLog); $uri = 'http://example.com/resources/1'; $g = new \Tripod\Mongo\MongoGraph(); $g->add_literal_triple($uri, $g->qname_to_uri('searchterms:title'), "Some title"); $g->add_literal_triple($uri, $g->qname_to_uri('searchterms:author'), "Some author"); // save entity using both tripods ( creates same doc in two different collections ) $tripod1->saveChanges(new \Tripod\ExtendedGraph(), $g, 'http://talisaspire.com/'); $tripod2->saveChanges(new \Tripod\ExtendedGraph(), $g, 'http://talisaspire.com/'); // assert the document is in both collections $this->assertDocumentVersion(array("r" => $uri, "c" => "http://talisaspire.com/"), 0, true, $tripod1); $this->assertDocumentVersion(array("r" => $uri, "c" => "http://talisaspire.com/"), 0, true, $tripod2); // assert the transaction log contains two transactions $this->assertEquals(2, $this->tripodTransactionLog->getTotalTransactionCount()); // change one of the documents $oG = new \Tripod\Mongo\MongoGraph(); $oG->add_literal_triple($uri, $g->qname_to_uri('searchterms:title'), "Some title"); $nG = new \Tripod\Mongo\MongoGraph(); $nG->add_literal_triple($uri, $g->qname_to_uri('searchterms:title'), "Changed title"); $tripod1->saveChanges($oG, $nG, 'http://talisaspire.com/'); // assert the documents and transaction count $this->assertDocumentVersion(array("r" => $uri, "c" => "http://talisaspire.com/"), 1, true, $tripod1); $this->assertDocumentVersion(array("r" => $uri, "c" => "http://talisaspire.com/"), 0, true, $tripod2); $this->assertEquals(3, $this->tripodTransactionLog->getTotalTransactionCount()); }
<?php require_once dirname(__FILE__) . '/common.inc.php'; require_once dirname(dirname(dirname(__FILE__))) . '/src/tripod.inc.php'; if ($argc != 2) { echo "usage: ./BSONToQuads.php tripodConfig.json < bsondata\n"; echo " When exporting bson data from Mongo use: \n"; echo " mongoexport -d <dbname> -c <collectionName> > bsondata.txt \n"; die; } array_shift($argv); $config = json_decode(file_get_contents($argv[0]), true); \Tripod\Mongo\Config::setConfig($config); $tu = new \Tripod\Mongo\TriplesUtil(); while (($line = fgets(STDIN)) !== false) { $line = rtrim($line); $doc = json_decode($line, true); $context = $doc['_id']['c']; $graph = new \Tripod\Mongo\MongoGraph(); $graph->add_tripod_array($doc); echo $graph->to_nquads($context); }
require_once dirname(__FILE__) . '/common.inc.php'; require_once dirname(dirname(dirname(__FILE__))) . '/src/tripod.inc.php'; if ($argc != 2) { echo "usage: ./BSONToTriples.php tripodConfig.json < bsondata\n"; echo " When exporting bson data from Mongo use: \n"; echo " mongoexport -d <dbname> -c <collectionName> > bsondata.txt \n"; die; } array_shift($argv); $config = json_decode(file_get_contents($argv[0]), true); \Tripod\Mongo\Config::setConfig($config); $tu = new \Tripod\Mongo\TriplesUtil(); while (($line = fgets(STDIN)) !== false) { $line = rtrim($line); $graph = new \Tripod\Mongo\MongoGraph(); $doc = json_decode($line, true); if (array_key_exists("_id", $doc)) { $subject = $doc['_id']; unset($doc["_id"]); if (array_key_exists("_version", $doc)) { unset($doc["_version"]); } foreach ($doc as $property => $values) { if (isset($values['value'])) { $doc[$property] = array($values); } } foreach ($doc as $property => $values) { foreach ($values as $value) { if ($value['type'] == "literal") {
/** * 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); }
public function testTransactionRollbackDuringApplyChanges() { // Save some basic data into the db before we create a transaction to modify it $subjectOne = 'http://example.com/resources/1'; $subjectTwo = 'http://example.com/resources/2'; $doc1 = array('_id' => array('r' => $subjectOne, 'c' => 'http://talisaspire.com/'), 'rdf:type' => array('u' => 'acorn:Resource'), 'dct:title' => array(array('l' => 'Title one'), array('l' => 'Title two')), '_version' => 0, '_cts' => \Tripod\Mongo\DateUtil::getMongoDate(), '_uts' => \Tripod\Mongo\DateUtil::getMongoDate()); $doc2 = array('_id' => array('r' => $subjectTwo, 'c' => 'http://talisaspire.com/'), 'rdf:type' => array('u' => 'acorn:Book'), 'dct:title' => array(array('l' => 'Title three'), array('l' => 'Title four')), '_version' => 0, '_cts' => \Tripod\Mongo\DateUtil::getMongoDate(), '_uts' => \Tripod\Mongo\DateUtil::getMongoDate()); $this->addDocument($doc1); $this->addDocument($doc2); // now lets modify the data using tripod $g1 = $this->tripod->describeResources(array($subjectOne), 'http://talisaspire.com/'); $g2 = $this->tripod->describeResources(array($subjectTwo), 'http://talisaspire.com/'); $oG = new \Tripod\Mongo\MongoGraph(); $oG->add_graph($g1); $oG->add_graph($g2); $nG = new \Tripod\Mongo\MongoGraph(); $nG->add_graph($oG); $nG->remove_literal_triple($subjectOne, $nG->qname_to_uri("dct:title"), "Title one"); $nG->add_literal_triple($subjectOne, $nG->qname_to_uri("dct:title"), "Updated Title one"); $nG->remove_literal_triple($subjectTwo, $nG->qname_to_uri("dct:title"), "Title three"); $nG->add_literal_triple($subjectTwo, $nG->qname_to_uri("dct:title"), "Updated Title three"); $mockTransactionId = 'transaction_1'; $mockTripod = $this->getMock('\\Tripod\\Mongo\\Driver', array('getDataUpdater'), array('CBD_testing', 'tripod_php_testing', array('defaultContext' => 'http://talisaspire.com/'))); $mockTripodUpdate = $this->getMock('\\Tripod\\Mongo\\Updates', array('generateTransactionId', 'lockSingleDocument', 'applyChangeSet'), array($mockTripod)); $mockTripodUpdate->expects($this->exactly(1))->method('generateTransactionId')->will($this->returnValue($mockTransactionId)); $mockTripodUpdate->expects($this->exactly(2))->method('lockSingleDocument')->will($this->returnCallback(array($this, 'lockSingleDocumentCallback'))); $mockTripodUpdate->expects($this->once())->method('applyChangeSet')->will($this->throwException(new Exception("Exception throw by mock test during applychangeset"))); $mockTripod->expects($this->atLeastOnce())->method('getDataUpdater')->will($this->returnValue($mockTripodUpdate)); /** @var $mockTripod \Tripod\Mongo\Driver */ $mockTripod->setTransactionLog($this->tripodTransactionLog); try { $mockTripod->saveChanges($oG, $nG, "http://talisaspire.com/"); $this->fail('Exception should have been thrown'); } catch (\Tripod\Exceptions\Exception $e) { // Squash the exception here as we need to continue running the assertions. } // make sure the subjects werent changed $uG = $this->tripod->describeResources(array($subjectOne, $subjectTwo)); $this->assertTrue($uG->has_resource_triple($subjectOne, $uG->qname_to_uri("rdf:type"), $uG->qname_to_uri("acorn:Resource"))); $this->assertTrue($uG->has_literal_triple($subjectOne, $uG->qname_to_uri("dct:title"), 'Title one')); $this->assertTrue($uG->has_literal_triple($subjectOne, $uG->qname_to_uri("dct:title"), 'Title two')); $this->assertFalse($uG->has_literal_triple($subjectOne, $uG->qname_to_uri("dct:title"), 'Updated Title two')); $this->assertTrue($uG->has_resource_triple($subjectTwo, $uG->qname_to_uri("rdf:type"), $uG->qname_to_uri("acorn:Book"))); $this->assertTrue($uG->has_literal_triple($subjectTwo, $uG->qname_to_uri("dct:title"), 'Title three')); $this->assertTrue($uG->has_literal_triple($subjectTwo, $uG->qname_to_uri("dct:title"), 'Title four')); $this->assertFalse($uG->has_literal_triple($subjectTwo, $uG->qname_to_uri("dct:title"), 'Updated Title three')); $this->assertDocumentDoesNotHaveProperty(array('r' => $subjectOne, 'c' => 'http://talisaspire.com/'), _LOCKED_FOR_TRANS, $this->tripod); $this->assertDocumentDoesNotHaveProperty(array('r' => $subjectOne, 'c' => 'http://talisaspire.com/'), _LOCKED_FOR_TRANS_TS, $this->tripod); $this->assertDocumentDoesNotHaveProperty(array('r' => $subjectTwo, 'c' => 'http://talisaspire.com/'), _LOCKED_FOR_TRANS, $this->tripod); $this->assertDocumentDoesNotHaveProperty(array('r' => $subjectTwo, 'c' => 'http://talisaspire.com/'), _LOCKED_FOR_TRANS_TS, $this->tripod); $transaction = $mockTripodUpdate->getTransactionLog()->getTransaction($mockTransactionId); $this->assertNotNull($transaction); $this->assertEquals("Exception throw by mock test during applychangeset", $transaction['error']['reason']); $this->assertEquals("failed", $transaction['status']); }
/** * this test verifies that if we simply want to add some data to a document that exists in we dont need to specify an oldgraph; we just need to specify the new graph * the cs builder should translate that into a single addition statement and apply it. * This builds on the previous test, by operating on data in mongo where _id.r and _id.c are namespaced AND passing context into the save method */ public function testTripodSaveChangesAddsLiteralTripleUsingEmptyOldGraphWithNamespacedContext() { $oG = new \Tripod\Mongo\MongoGraph(); $nG = new \Tripod\Mongo\MongoGraph(); $nG->add_graph($oG); // resource and context are namespaced in base data this time around... $nG->add_literal_triple('http://basedata.com/b/1', $nG->qname_to_uri('searchterms:title'), 'TEST TITLE'); $this->tripod->saveChanges($oG, $nG, "baseData:DefaultGraph", 'my changes'); $uG = $this->tripod->describeResource('http://basedata.com/b/1', "http://basedata.com/b/DefaultGraph"); $this->assertHasLiteralTriple($uG, 'http://basedata.com/b/1', $nG->qname_to_uri('searchterms:title'), 'TEST TITLE'); }
public function testTransactionLogIsWrittenToCorrectDBAndCollection() { $storeName = 'tripod_php_testing'; $newConfig = \Tripod\Mongo\Config::getConfig(); $newConfig['transaction_log']['database'] = 'tripod_php_testing_transaction_log'; $newConfig['transaction_log']['collection'] = 'transaction_log'; \Tripod\Mongo\Config::setConfig($newConfig); $config = \Tripod\Mongo\Config::getInstance(); // Clear out any old data $tlogDB = $config->getTransactionLogDatabase(); $tlogDB->drop(); // Make sure the dbs do not exist $transactionConnInfo = $newConfig['data_sources'][$newConfig['transaction_log']['data_source']]; $options = isset($transactionConnInfo['replicaSet']) && !empty($transactionConnInfo['replicaSet']) ? array('replicaSet' => $transactionConnInfo['replicaSet']) : array(); $transactionMongo = new Client($transactionConnInfo['connection'], $options); $transactionDbInfo = $transactionMongo->listDatabases(); foreach ($transactionDbInfo as $db) { $this->assertNotEquals($db->getName(), $newConfig['transaction_log']['database']); } $tqueuesConnInfo = $newConfig['data_sources'][$newConfig['transaction_log']['data_source']]; $options = isset($tqueuesConnInfo['replicaSet']) && !empty($tqueuesConnInfo['replicaSet']) ? array('replicaSet' => $tqueuesConnInfo['replicaSet']) : array(); $queuesMongo = new Client($tqueuesConnInfo['connection'], $options); $queuesDbInfo = $queuesMongo->listDatabases(); foreach ($queuesDbInfo as $db) { $this->assertNotEquals($db->getName(), $newConfig['transaction_log']['database']); } // Start adding some data $this->tripod = new \Tripod\Mongo\Driver('CBD_testing', $storeName, array(OP_ASYNC => array(OP_VIEWS => true, OP_TABLES => false, OP_SEARCH => false))); $this->loadResourceDataViaTripod(); $graph = new \Tripod\Mongo\MongoGraph(); $subject = 'http://example.com/' . uniqid(); $labeller = new \Tripod\Mongo\Labeller(); $graph->add_resource_triple($subject, RDF_TYPE, $labeller->qname_to_uri('foaf:Person')); $graph->add_literal_triple($subject, FOAF_NAME, "Anne Example"); $this->tripod->saveChanges(new \Tripod\ExtendedGraph(), $graph); $newGraph = $this->tripod->describeResource($subject); $newGraph->add_literal_triple($subject, $labeller->qname_to_uri('foaf:email'), '*****@*****.**'); $this->tripod->saveChanges($graph, $newGraph); // Make sure the dbs do now exist $transactionDbInfo = $transactionMongo->listDatabases(); $transactionDbExists = false; foreach ($transactionDbInfo as $db) { if ($db->getName() === $newConfig['transaction_log']['database']) { $transactionDbExists = true; } } $this->assertTrue($transactionDbExists); // Make sure the data in the dbs look right $transactionColletion = $transactionMongo->selectCollection($newConfig['transaction_log']['database'], $newConfig['transaction_log']['collection']); $transactionCount = $transactionColletion->count(); $transactionExampleDocument = $transactionColletion->findOne(); $this->assertEquals(24, $transactionCount); $this->assertContains('transaction_', $transactionExampleDocument["_id"]); }