diff --git a/src/mongo/delegates/Tables.class.php b/src/mongo/delegates/Tables.class.php index 50371571..5e2f7165 100644 --- a/src/mongo/delegates/Tables.class.php +++ b/src/mongo/delegates/Tables.class.php @@ -513,15 +513,13 @@ public function generateTableRows($tableType,$resource=null,$context=null,$queue // ensure any custom view indexes foreach (Config::getInstance()->getTableSpecifications($this->storeName) as $tSpec) { - if (isset($tSpec['ensureIndexes'])) + if (isset($tSpec['ensureIndexes']) && $tSpec['to_data_source'] == $tableSpec['to_data_source']) // only ensure table_rows indexes for the data source that matches the table spec we're generating { foreach ($tSpec['ensureIndexes'] as $ensureIndex) { - $collection->ensureIndex( - $ensureIndex, - array( - 'background'=>1 - ) + $this->ensureIndex( + $collection, + $ensureIndex ); } } @@ -1426,6 +1424,21 @@ protected function checkIfTypeShouldTriggerOperation($rdfType, array $validTypes return false; } + /** + * Ensure $indexToEnsure on the given mongo $collection + * @param array $collection + * @param \MongoCollection $indexToEnsure + */ + protected function ensureIndex(\MongoCollection $collection,array $indexToEnsure) + { + $collection->ensureIndex( + $indexToEnsure, + array( + 'background'=>1 + ) + ); + + } /** * Apply a regex to the RDF property value defined in $value * @param $regex diff --git a/test/unit/mongo/MongoTripodTablesTest.php b/test/unit/mongo/MongoTripodTablesTest.php index eba9d701..9c60a582 100644 --- a/test/unit/mongo/MongoTripodTablesTest.php +++ b/test/unit/mongo/MongoTripodTablesTest.php @@ -695,6 +695,33 @@ public function testJoinLinkValueIsForJoinedResource() $this->assertEquals("http://talisaspire.com/works/4d101f63c10a6", $rows['results'][0]['workLink']); } + + /** + * Test to ensure only indexes on rs2 get ensured when table row generation happens + */ + public function testJoinLinkGenerationOnlyFiresEnsureIndexesForOwnDataSource() + { + /* @var $mockTables \Tripod\Mongo\Composites\Tables|PHPUnit_Framework_MockObject_MockObject*/ + $mockTables = $this->getMock('\Tripod\Mongo\Composites\Tables',array("ensureIndex"),array($this->tripod->getStoreName(),$this->getTripodCollection($this->tripod),null)); + + $mockTables->expects($this->once())->method("ensureIndex"); // should only ever get called once + + $mockTables->generateTableRows("t_join_link","http://somesubject"); + } + + /** + * Test to ensure only indexes on rs2 get ensured when table row generation happens + */ + public function testResourceGenerationOnlyFiresEnsureIndexesForOwnDataSource() + { + /* @var $mockTables \Tripod\Mongo\Composites\Tables|PHPUnit_Framework_MockObject_MockObject*/ + $mockTables = $this->getMock('\Tripod\Mongo\Composites\Tables',array("ensureIndex"),array($this->tripod->getStoreName(),$this->getTripodCollection($this->tripod),null)); + + $mockTables->expects($this->exactly(3))->method("ensureIndex"); // should only ever get called twice + + $mockTables->generateTableRows("t_resource","http://somesubject"); + } + /** * Test to ensure that impact index contains joined ids for resources that do not yet exist in the database (i.e. * allow open world model) diff --git a/test/unit/mongo/data/config.json b/test/unit/mongo/data/config.json index d904a8cb..1bcc3a18 100644 --- a/test/unit/mongo/data/config.json +++ b/test/unit/mongo/data/config.json @@ -614,6 +614,11 @@ "type": "bibo:Document", "from": "CBD_testing", "to_data_source": "rs2", + "ensureIndexes" : [ + { + "value.bookLink": 1 + } + ], "fields": [ { "fieldName": "bookLink",