-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)); | ||
} | ||
} | ||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use sparse There was a problem hiding this comment. Choose a reason for hiding this commentThe 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." |
||
} | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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 spacehttp://php.net/manual/en/mongocollection.ensureindex.php