e09225eb09
* Created AbstractProvider class (for all DB services), which handles the default batch_size option. * The logger Closure is now optional for populate(). * Removed unused Elastica_Type argument from Provider constructors. * Added unit tests for Doctrine's AbstractProvider class. * The extra argument (ManagerRegistry) for Doctrine providers is now an appended constructor argument, so the extension no longer needs to use different replacement indexes for Propel/Doctrine providers.
44 lines
1.3 KiB
PHP
44 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace FOQ\ElasticaBundle\Propel;
|
|
|
|
use FOQ\ElasticaBundle\Provider\AbstractProvider;
|
|
|
|
/**
|
|
* Propel provider
|
|
*
|
|
* @author William Durand <william.durand1@gmail.com>
|
|
*/
|
|
class Provider extends AbstractProvider
|
|
{
|
|
/**
|
|
* @see FOQ\ElasticaBundle\Provider\ProviderInterface::populate()
|
|
*/
|
|
public function populate(\Closure $loggerClosure = null)
|
|
{
|
|
$queryClass = $this->objectClass . 'Query';
|
|
$nbObjects = $queryClass::create()->count();
|
|
|
|
for ($offset = 0; $offset < $nbObjects; $offset += $this->options['batch_size']) {
|
|
if ($loggerClosure) {
|
|
$stepStartTime = microtime(true);
|
|
}
|
|
|
|
$objects = $queryClass::create()
|
|
->limit($this->options['batch_size'])
|
|
->offset($offset)
|
|
->find();
|
|
|
|
$this->objectPersister->insertMany($objects->getArrayCopy());
|
|
|
|
if ($loggerClosure) {
|
|
$stepNbObjects = count($objects);
|
|
$stepCount = $stepNbObjects + $offset;
|
|
$percentComplete = 100 * $stepCount / $nbObjects;
|
|
$objectsPerSecond = $stepNbObjects / (microtime(true) - $stepStartTime);
|
|
$loggerClosure(sprintf('%0.1f%% (%d/%d), %d objects/s', $percentComplete, $stepCount, $nbObjects, $objectsPerSecond));
|
|
}
|
|
}
|
|
}
|
|
}
|