Skip to content

Commit ce7879a

Browse files
committed
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.
1 parent f17156b commit ce7879a

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

src/Operation/ListCollections.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use MongoDB\Driver\Server;
2323
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2424
use MongoDB\Exception\InvalidArgumentException;
25+
use MongoDB\Model\CachingIterator;
2526
use MongoDB\Model\CollectionInfoCommandIterator;
2627
use MongoDB\Model\CollectionInfoIterator;
2728
use MongoDB\Model\CollectionInfoLegacyIterator;
@@ -107,7 +108,7 @@ private function executeCommand(Server $server)
107108
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
108109
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
109110

110-
return new CollectionInfoCommandIterator($cursor);
111+
return new CollectionInfoCommandIterator(new CachingIterator($cursor));
111112
}
112113

113114
/**
@@ -138,6 +139,6 @@ private function executeLegacy(Server $server)
138139
$cursor = $server->executeQuery($this->databaseName . '.system.namespaces', new Query($filter, $options));
139140
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
140141

141-
return new CollectionInfoLegacyIterator($cursor);
142+
return new CollectionInfoLegacyIterator(new CachingIterator($cursor));
142143
}
143144
}

src/Operation/ListIndexes.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use MongoDB\Driver\Server;
2323
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2424
use MongoDB\Exception\InvalidArgumentException;
25+
use MongoDB\Model\CachingIterator;
2526
use MongoDB\Model\IndexInfoIterator;
2627
use MongoDB\Model\IndexInfoIteratorIterator;
2728
use EmptyIterator;
@@ -114,7 +115,7 @@ private function executeCommand(Server $server)
114115

115116
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
116117

117-
return new IndexInfoIteratorIterator($cursor);
118+
return new IndexInfoIteratorIterator(new CachingIterator($cursor));
118119
}
119120

120121
/**
@@ -136,6 +137,6 @@ private function executeLegacy(Server $server)
136137
$cursor = $server->executeQuery($this->databaseName . '.system.indexes', new Query($filter, $options));
137138
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
138139

139-
return new IndexInfoIteratorIterator($cursor);
140+
return new IndexInfoIteratorIterator(new CachingIterator($cursor));
140141
}
141142
}

tests/Operation/ListCollectionsFunctionalTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public function testListCollectionsForNewlyCreatedDatabase()
2020
$this->assertEquals(1, $writeResult->getInsertedCount());
2121

2222
$operation = new ListCollections($this->getDatabaseName(), ['filter' => ['name' => $this->getCollectionName()]]);
23-
// Convert the CollectionInfoIterator to an array since we cannot rewind its cursor
24-
$collections = iterator_to_array($operation->execute($server));
23+
$collections = $operation->execute($server);
24+
25+
$this->assertInstanceOf('MongoDB\Model\CollectionInfoIterator', $collections);
2526

2627
$this->assertCount(1, $collections);
2728

tests/Operation/ListIndexesFunctionalTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ public function testListIndexesForNewlyCreatedCollection()
2222

2323
$this->assertInstanceOf('MongoDB\Model\IndexInfoIterator', $indexes);
2424

25-
// Convert the CursorInfoIterator to an array since we cannot rewind its cursor
26-
$indexes = iterator_to_array($indexes);
27-
2825
$this->assertCount(1, $indexes);
2926

3027
foreach ($indexes as $index) {

0 commit comments

Comments
 (0)