DominionEnterprises\Mongo\Queue::ensureGetIndex PHP Method

ensureGetIndex() public method

Ensure an index for the get() method.
public ensureGetIndex ( array $beforeSort = [], array $afterSort = [] ) : void
$beforeSort array Fields in get() call to index before the sort field in same format as \MongoDB\Collection::ensureIndex()
$afterSort array Fields in get() call to index after the sort field in same format as \MongoDB\Collection::ensureIndex()
return void
    public function ensureGetIndex(array $beforeSort = [], array $afterSort = [])
    {
        //using general rule: equality, sort, range or more equality tests in that order for index
        $completeFields = ['running' => 1];
        self::verifySort($beforeSort, 'beforeSort', $completeFields);
        $completeFields['priority'] = 1;
        $completeFields['created'] = 1;
        self::verifySort($afterSort, 'afterSort', $completeFields);
        $completeFields['earliestGet'] = 1;
        //for the main query in get()
        $this->ensureIndex($completeFields);
        //for the stuck messages query in get()
        $this->ensureIndex(['running' => 1, 'resetTimestamp' => 1]);
    }

Usage Example

 /**
  * @test
  * @covers ::ensureGetIndex
  * @expectedException \Exception
  */
 public function ensureGetIndexWithTooLongCollectionName()
 {
     $collectionName = 'messages012345678901234567890123456789012345678901234567890123456789';
     $collectionName .= '012345678901234567890123456789012345678901234567890123456789';
     //128 chars
     $queue = new Queue($this->mongoUrl, 'testing', $collectionName);
     $queue->ensureGetIndex([]);
 }