izzum\statemachine\persistence\MongoDB::checkAndCreateIndexesIfNecessary PHP Method

checkAndCreateIndexesIfNecessary() protected method

since we do not want to burden a client of this code with the responsiblity of creating indexes, we take a statistical approach to check if we need to create an index in the background. This will only be done once.
protected checkAndCreateIndexesIfNecessary ( number $check_index_once_in = 1000 )
$check_index_once_in number check for index creation. on average, every times the index should be created if it is not there already
    protected function checkAndCreateIndexesIfNecessary($check_index_once_in = 1000)
    {
        //statistical approach to building the index on average once every x times
        $check_index_once_in = min(1000, $check_index_once_in);
        if (rand(1, $check_index_once_in) % $check_index_once_in === 0) {
            $this->createIndexes();
        }
    }