Skip to content

Instantly share code, notes, and snippets.

@firstrow
Last active August 29, 2015 14:12
Show Gist options
  • Save firstrow/76fa235654ef991ae3cf to your computer and use it in GitHub Desktop.
Save firstrow/76fa235654ef991ae3cf to your computer and use it in GitHub Desktop.
php: construct
<?php
namespace Core\Db;
/**
* Helps to iterate over huge data collection.
* It loads records by small chunks
*
* Example:
* <pre>
* $collection = $app['mongo.default']->sample;
* $loader = new ChunkLoader($collection, 50);
* foreach($loader as $row) {
* var_dump($row);
* }
* </pre>
*/
class ChunkLoader implements \Iterator
{
/**
* @var MongoCollection
*/
protected $collection;
/**
* @var int how many items to load per one query
*/
protected $chunkSize;
/**
* @var array current chunk data from mongo
*/
protected $chunkData;
/**
* @var int how many rows to skip
*/
protected $skip = 0;
/**
* @var int cursor position in current chunk
*/
protected $position = 0;
public function __construct($collection, $chunkSize=100)
{
$this->collection = $collection;
$this->chunkSize = $chunkSize;
$this->total = $this->collection->count();
}
protected function load()
{
$this->chunkData = iterator_to_array($this->collection
->find()
->skip($this->skip)
->limit($this->chunkSize)
, false);
}
public function current()
{
return $this->chunkData[$this->position];
}
public function next()
{
++$this->position;
if ($this->position <= $this->total && $this->position >= $this->chunkSize) {
$this->skip += $this->chunkSize;
$this->position = 0;
$this->load();
}
}
public function key()
{
return $this->position;
}
public function valid()
{
return isset($this->chunkData[$this->position]);
}
public function rewind()
{
$this->skip = 0;
$this->position = 0;
$this->load();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment