Commit 5ea6f35d authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-81: Cache cursors for collection and index enumeration

This will allow the returned CollectionInfoIterator and IndexInfoIterator classes to be rewound and iterated multiple times.
parent b2a76bfb
...@@ -22,6 +22,7 @@ use MongoDB\Driver\Query; ...@@ -22,6 +22,7 @@ use MongoDB\Driver\Query;
use MongoDB\Driver\Server; use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException; use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Model\CachingIterator;
use MongoDB\Model\CollectionInfoCommandIterator; use MongoDB\Model\CollectionInfoCommandIterator;
use MongoDB\Model\CollectionInfoIterator; use MongoDB\Model\CollectionInfoIterator;
use MongoDB\Model\CollectionInfoLegacyIterator; use MongoDB\Model\CollectionInfoLegacyIterator;
...@@ -107,7 +108,7 @@ class ListCollections implements Executable ...@@ -107,7 +108,7 @@ class ListCollections implements Executable
$cursor = $server->executeCommand($this->databaseName, new Command($cmd)); $cursor = $server->executeCommand($this->databaseName, new Command($cmd));
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']); $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
return new CollectionInfoCommandIterator($cursor); return new CollectionInfoCommandIterator(new CachingIterator($cursor));
} }
/** /**
...@@ -138,6 +139,6 @@ class ListCollections implements Executable ...@@ -138,6 +139,6 @@ class ListCollections implements Executable
$cursor = $server->executeQuery($this->databaseName . '.system.namespaces', new Query($filter, $options)); $cursor = $server->executeQuery($this->databaseName . '.system.namespaces', new Query($filter, $options));
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']); $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
return new CollectionInfoLegacyIterator($cursor); return new CollectionInfoLegacyIterator(new CachingIterator($cursor));
} }
} }
...@@ -22,6 +22,7 @@ use MongoDB\Driver\Query; ...@@ -22,6 +22,7 @@ use MongoDB\Driver\Query;
use MongoDB\Driver\Server; use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException; use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Model\CachingIterator;
use MongoDB\Model\IndexInfoIterator; use MongoDB\Model\IndexInfoIterator;
use MongoDB\Model\IndexInfoIteratorIterator; use MongoDB\Model\IndexInfoIteratorIterator;
use EmptyIterator; use EmptyIterator;
...@@ -114,7 +115,7 @@ class ListIndexes implements Executable ...@@ -114,7 +115,7 @@ class ListIndexes implements Executable
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']); $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
return new IndexInfoIteratorIterator($cursor); return new IndexInfoIteratorIterator(new CachingIterator($cursor));
} }
/** /**
...@@ -136,6 +137,6 @@ class ListIndexes implements Executable ...@@ -136,6 +137,6 @@ class ListIndexes implements Executable
$cursor = $server->executeQuery($this->databaseName . '.system.indexes', new Query($filter, $options)); $cursor = $server->executeQuery($this->databaseName . '.system.indexes', new Query($filter, $options));
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']); $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
return new IndexInfoIteratorIterator($cursor); return new IndexInfoIteratorIterator(new CachingIterator($cursor));
} }
} }
...@@ -20,8 +20,9 @@ class ListCollectionsFunctionalTest extends FunctionalTestCase ...@@ -20,8 +20,9 @@ class ListCollectionsFunctionalTest extends FunctionalTestCase
$this->assertEquals(1, $writeResult->getInsertedCount()); $this->assertEquals(1, $writeResult->getInsertedCount());
$operation = new ListCollections($this->getDatabaseName(), ['filter' => ['name' => $this->getCollectionName()]]); $operation = new ListCollections($this->getDatabaseName(), ['filter' => ['name' => $this->getCollectionName()]]);
// Convert the CollectionInfoIterator to an array since we cannot rewind its cursor $collections = $operation->execute($server);
$collections = iterator_to_array($operation->execute($server));
$this->assertInstanceOf('MongoDB\Model\CollectionInfoIterator', $collections);
$this->assertCount(1, $collections); $this->assertCount(1, $collections);
......
...@@ -22,9 +22,6 @@ class ListIndexesFunctionalTest extends FunctionalTestCase ...@@ -22,9 +22,6 @@ class ListIndexesFunctionalTest extends FunctionalTestCase
$this->assertInstanceOf('MongoDB\Model\IndexInfoIterator', $indexes); $this->assertInstanceOf('MongoDB\Model\IndexInfoIterator', $indexes);
// Convert the CursorInfoIterator to an array since we cannot rewind its cursor
$indexes = iterator_to_array($indexes);
$this->assertCount(1, $indexes); $this->assertCount(1, $indexes);
foreach ($indexes as $index) { foreach ($indexes as $index) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment