techfromsage / tripod-php

Object Graph Mapper for managing RDF data in Mongo
MIT License
29 stars 4 forks source link

ensureIndexes in IndexUtils doesn't create all the indexes if reindex is set to true #105

Closed malcyL closed 3 months ago

malcyL commented 8 years ago

Calling the ensureIndex method on IndexUtils does not correctly create indexes if the reindex flag is set to true.

Looking at this line of code: https://github.com/talis/tripod-php/blob/master/src/mongo/util/IndexUtils.class.php#L78

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

I think what is happening is that each index is created correctly, but but before it is added, a call is made to $collection->deleteIndexes(). This deletes all the indexes created in previous iterations of the loop.