2011-12-20 17:44:39 +01:00
|
|
|
<?php
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle\Propel;
|
2011-12-20 17:44:39 +01:00
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
use FOS\ElasticaBundle\Provider\AbstractProvider;
|
2011-12-20 17:44:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Propel provider
|
|
|
|
*
|
|
|
|
* @author William Durand <william.durand1@gmail.com>
|
|
|
|
*/
|
2012-03-10 00:51:08 +01:00
|
|
|
class Provider extends AbstractProvider
|
2011-12-20 17:44:39 +01:00
|
|
|
{
|
|
|
|
/**
|
2015-01-22 01:18:51 +01:00
|
|
|
* {@inheritDoc}
|
2011-12-20 17:44:39 +01:00
|
|
|
*/
|
2013-10-08 12:00:04 +02:00
|
|
|
public function populate(\Closure $loggerClosure = null, array $options = array())
|
2011-12-20 17:44:39 +01:00
|
|
|
{
|
|
|
|
$queryClass = $this->objectClass . 'Query';
|
2012-03-10 00:51:08 +01:00
|
|
|
$nbObjects = $queryClass::create()->count();
|
2013-10-08 12:00:04 +02:00
|
|
|
$offset = isset($options['offset']) ? intval($options['offset']) : 0;
|
|
|
|
$sleep = isset($options['sleep']) ? intval($options['sleep']) : 0;
|
|
|
|
$batchSize = isset($options['batch-size']) ? intval($options['batch-size']) : $this->options['batch_size'];
|
2011-12-20 17:44:39 +01:00
|
|
|
|
2013-10-08 12:00:04 +02:00
|
|
|
for (; $offset < $nbObjects; $offset += $batchSize) {
|
2011-12-20 17:44:39 +01:00
|
|
|
$objects = $queryClass::create()
|
2013-10-08 12:00:04 +02:00
|
|
|
->limit($batchSize)
|
2011-12-20 17:44:39 +01:00
|
|
|
->offset($offset)
|
2014-06-17 02:41:11 +02:00
|
|
|
->find()
|
|
|
|
->getArrayCopy();
|
2014-06-19 03:14:13 +02:00
|
|
|
|
2015-01-22 01:18:51 +01:00
|
|
|
$objects = array_filter($objects, array($this, 'isObjectIndexable'));
|
|
|
|
if ($objects) {
|
|
|
|
$this->objectPersister->insertMany($objects);
|
2014-06-19 03:14:13 +02:00
|
|
|
}
|
2011-12-20 17:44:39 +01:00
|
|
|
|
2013-10-08 12:00:04 +02:00
|
|
|
usleep($sleep);
|
|
|
|
|
2015-01-22 01:18:51 +01:00
|
|
|
if ($loggerClosure) {
|
|
|
|
$loggerClosure($batchSize, $nbObjects);
|
2012-03-10 00:51:08 +01:00
|
|
|
}
|
2011-12-20 17:44:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|