Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

90%: Treat tables, views, and search differently #57

Merged
merged 2 commits into from
May 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/mongo/delegates/MongoTripodSearchIndexer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
require_once TRIPOD_DIR . 'mongo/providers/MongoSearchProvider.class.php';
require_once TRIPOD_DIR . 'exceptions/TripodSearchException.class.php';


/**
* Class MongoTripodSearchIndexer
*/
class MongoTripodSearchIndexer extends MongoTripodBase implements SplObserver
{
private $tripod = null;
Expand All @@ -18,6 +20,9 @@ class MongoTripodSearchIndexer extends MongoTripodBase implements SplObserver
*/
private $configuredProvider = null;

/**
* @param MongoTripod $tripod
*/
public function __construct(MongoTripod $tripod)
{
$this->tripod = $tripod;
Expand Down
3 changes: 2 additions & 1 deletion src/mongo/delegates/MongoTripodTables.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,9 @@ public function generateTableRows($tableType,$resource=null,$context=null)
return null;
}

// ensure both the ID field and the impactIndex indexes are correctly set up
// ensure that the ID field, view type, and the impactIndex indexes are correctly set up
$collection->ensureIndex(array('_id.r'=>1, '_id.c'=>1,'_id.type'=>1),array('background'=>1));
$collection->ensureIndex(array('_id.type'=>1),array('background'=>1));
$collection->ensureIndex(array('value.'._IMPACT_INDEX=>1),array('background'=>1));

// ensure any custom view indexes
Expand Down
3 changes: 2 additions & 1 deletion src/mongo/delegates/MongoTripodViews.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,9 @@ public function generateView($viewId,$resource=null,$context=null)
throw new TripodViewException('Could not find any joins in view specification - usecase better served with select()');
}

// ensure both the ID field and the impactIndex indexes are correctly set up
// ensure that the ID field, view type, and the impactIndex indexes are correctly set up
$collection->ensureIndex(array('_id.r'=>1, '_id.c'=>1,'_id.type'=>1),array('background'=>1));
$collection->ensureIndex(array('_id.type'=>1),array('background'=>1));
$collection->ensureIndex(array('value.'._IMPACT_INDEX=>1),array('background'=>1));

// ensure any custom view indexes
Expand Down
70 changes: 51 additions & 19 deletions src/mongo/util/IndexUtils.class.php
Original file line number Diff line number Diff line change
@@ -1,57 +1,89 @@
<?php
require_once(TRIPOD_DIR."mongo/MongoTripodConfig.class.php");

/**
* Class IndexUtils
*/
class IndexUtils
{
/**
* Ensures the index for the given $dbName. As a consequence, sets the global
* Ensures the index for the given $storeName. As a consequence, sets the global
* MongoCursor timeout to -1 for this thread, so use with caution from anything
* other than a setup script
* @param bool $reindex - force a reindex of existing data
* @param null $dbName - database name to ensure indexes for
* @param null $storeName - database name to ensure indexes for
* @param bool $background - index in the background (default) or lock DB whilst indexing
*/
public function ensureIndexes($reindex=false,$dbName=null,$background=true)
public function ensureIndexes($reindex=false,$storeName=null,$background=true)
{
//MongoCursor::$timeout = -1; // set this otherwise you'll see timeout errors for large indexes

$config = MongoTripodConfig::getInstance();
$dbs = ($dbName==null) ? $config->getDbs() : array($dbName);
foreach ($dbs as $dbName)
$dbs = ($storeName==null) ? $config->getDbs() : array($storeName);
foreach ($dbs as $storeName)
{
$collections = MongoTripodConfig::getInstance()->getIndexesGroupedByCollection($dbName);
$collections = MongoTripodConfig::getInstance()->getIndexesGroupedByCollection($storeName);
foreach ($collections as $collectionName=>$indexes)
{

// Don't do this for composites, which could be anywhere
if(in_array($collectionName, array(TABLE_ROWS_COLLECTION,VIEWS_COLLECTION,SEARCH_INDEX_COLLECTION)))
{
continue;
}
if ($reindex)
{
$config->getCollectionForCBD($dbName, $collectionName)->deleteIndexes();
$config->getCollectionForCBD($storeName, $collectionName)->deleteIndexes();
}
foreach ($indexes as $indexName=>$fields)
{
$indexName = substr($indexName,0,127); // ensure max 128 chars
if (is_numeric($indexName))
{
// no name
$config->getCollectionForCBD($dbName, $collectionName)->ensureIndex($fields,array("background"=>$background));
$config->getCollectionForCBD($storeName, $collectionName)->ensureIndex($fields,array("background"=>$background));
}
else
{
$config->getCollectionForCBD($dbName, $collectionName)->ensureIndex($fields,array('name'=>$indexName,"background"=>$background));
$config->getCollectionForCBD($storeName, $collectionName)->ensureIndex($fields,array('name'=>$indexName,"background"=>$background));
}
}
}
// finally, for views, make sure type is indexed
$dataSources = array();
foreach($config->getViewSpecifications($dbName) as $viewId=>$spec)

// Index views
foreach($config->getViewSpecifications($storeName) as $viewId=>$spec)
{
$dataSources[] = $spec['to'];
$collection = MongoTripodConfig::getInstance()->getCollectionForView($storeName, $viewId);
if($collection)
{
$indexes = array("_id.type"=>1);
if(isset($spec['ensureIndexes']))
{
$indexes = array_merge($indexes, $spec['ensureIndexes']);
}
if ($reindex)
{
$collection->deleteIndexes();
}
$collection->ensureIndex($indexes, array("background"=>$background));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use sparse='true' here to save disk space

http://php.net/manual/en/mongocollection.ensureindex.php

}
}
foreach(array_unique($dataSources) as $dataSource)

// Index table rows
foreach($config->getTableSpecifications($storeName) as $tableId=>$spec)
{
$config->getDatabase($dbName, $dataSource)
->selectCollection(VIEWS_COLLECTION)
->ensureIndex(array("_id.type"=>1),array("background"=>$background));
$collection = MongoTripodConfig::getInstance()->getCollectionForTable($storeName, $tableId);
if($collection)
{
$indexes = array("_id.type"=>1);
if(isset($spec['ensureIndexes']))
{
$indexes = array_merge($indexes, $spec['ensureIndexes']);
}
if ($reindex)
{
$collection->deleteIndexes();
}
$collection->ensureIndex($indexes, array("background"=>$background));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use sparse

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch that, docs say "If a sparse index would result in an incomplete result set for queries and sort operations, MongoDB will not use that index unless a hint() explicitly specifies the index."

}
}
}
}
Expand Down