2011-04-17 19:56:32 +02:00
|
|
|
<?php
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle\Doctrine;
|
2011-04-17 19:56:32 +02:00
|
|
|
|
2012-03-10 00:51:08 +01:00
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry;
|
2013-12-10 18:07:22 +01:00
|
|
|
use Elastica\Exception\Bulk\ResponseException as BulkResponseException;
|
2013-03-27 18:44:06 +01:00
|
|
|
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
|
|
|
|
use FOS\ElasticaBundle\Provider\AbstractProvider as BaseAbstractProvider;
|
2011-04-17 19:56:32 +02:00
|
|
|
|
2012-03-10 00:51:08 +01:00
|
|
|
abstract class AbstractProvider extends BaseAbstractProvider
|
2011-04-17 19:56:32 +02:00
|
|
|
{
|
2012-03-10 00:51:08 +01:00
|
|
|
protected $managerRegistry;
|
2011-06-07 20:13:34 +02:00
|
|
|
|
|
|
|
/**
|
2012-03-10 00:51:08 +01:00
|
|
|
* Constructor.
|
2011-06-07 20:13:34 +02:00
|
|
|
*
|
2012-03-10 00:51:08 +01:00
|
|
|
* @param ObjectPersisterInterface $objectPersister
|
|
|
|
* @param string $objectClass
|
|
|
|
* @param array $options
|
|
|
|
* @param ManagerRegistry $managerRegistry
|
2011-06-07 20:13:34 +02:00
|
|
|
*/
|
2012-03-10 00:51:08 +01:00
|
|
|
public function __construct(ObjectPersisterInterface $objectPersister, $objectClass, array $options, $managerRegistry)
|
2011-04-17 19:56:32 +02:00
|
|
|
{
|
2012-03-10 00:51:08 +01:00
|
|
|
parent::__construct($objectPersister, $objectClass, array_merge(array(
|
|
|
|
'clear_object_manager' => true,
|
2014-02-19 11:01:54 +01:00
|
|
|
'ignore_errors' => false,
|
2012-03-10 00:51:08 +01:00
|
|
|
'query_builder_method' => 'createQueryBuilder',
|
|
|
|
), $options));
|
|
|
|
|
|
|
|
$this->managerRegistry = $managerRegistry;
|
2011-04-17 19:56:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-27 18:44:06 +01:00
|
|
|
* @see FOS\ElasticaBundle\Provider\ProviderInterface::populate()
|
2011-04-17 19:56:32 +02:00
|
|
|
*/
|
2013-10-08 12:00:04 +02:00
|
|
|
public function populate(\Closure $loggerClosure = null, array $options = array())
|
2011-04-17 19:56:32 +02:00
|
|
|
{
|
|
|
|
$queryBuilder = $this->createQueryBuilder();
|
2012-03-10 00:51:08 +01:00
|
|
|
$nbObjects = $this->countObjects($queryBuilder);
|
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'];
|
2014-02-19 11:01:54 +01:00
|
|
|
$ignoreErrors = isset($options['ignore-errors']) ? $options['ignore-errors'] : $this->options['ignore_errors'];
|
2011-04-17 19:56:32 +02:00
|
|
|
|
2013-10-08 12:00:04 +02:00
|
|
|
for (; $offset < $nbObjects; $offset += $batchSize) {
|
2012-03-10 00:51:08 +01:00
|
|
|
if ($loggerClosure) {
|
|
|
|
$stepStartTime = microtime(true);
|
|
|
|
}
|
2013-10-08 12:00:04 +02:00
|
|
|
$objects = $this->fetchSlice($queryBuilder, $batchSize, $offset);
|
2011-04-20 22:53:59 +02:00
|
|
|
|
2014-02-19 11:01:54 +01:00
|
|
|
if (!$ignoreErrors) {
|
2013-12-10 18:07:22 +01:00
|
|
|
$this->objectPersister->insertMany($objects);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$this->objectPersister->insertMany($objects);
|
|
|
|
} catch(BulkResponseException $e) {
|
|
|
|
if ($loggerClosure) {
|
|
|
|
$loggerClosure(sprintf('<error>%s</error>',$e->getMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-17 19:56:32 +02:00
|
|
|
|
|
|
|
if ($this->options['clear_object_manager']) {
|
2012-03-10 00:51:08 +01:00
|
|
|
$this->managerRegistry->getManagerForClass($this->objectClass)->clear();
|
2011-04-17 19:56:32 +02:00
|
|
|
}
|
2011-04-27 09:59:04 +02:00
|
|
|
|
2013-10-08 12:00:04 +02:00
|
|
|
usleep($sleep);
|
|
|
|
|
2012-03-10 00:51:08 +01:00
|
|
|
if ($loggerClosure) {
|
|
|
|
$stepNbObjects = count($objects);
|
|
|
|
$stepCount = $stepNbObjects + $offset;
|
|
|
|
$percentComplete = 100 * $stepCount / $nbObjects;
|
2014-03-24 16:18:55 +01:00
|
|
|
$timeDifference = microtime(true) - $stepStartTime;
|
|
|
|
$objectsPerSecond = $timeDifference ? ($stepNbObjects / $timeDifference) : $stepNbObjects;
|
2014-02-03 18:12:20 +01:00
|
|
|
$loggerClosure(sprintf('%0.1f%% (%d/%d), %d objects/s %s', $percentComplete, $stepCount, $nbObjects, $objectsPerSecond, $this->getMemoryUsage()));
|
2012-03-10 00:51:08 +01:00
|
|
|
}
|
2011-04-17 19:56:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-09 18:48:25 +02:00
|
|
|
/**
|
2012-03-10 00:51:08 +01:00
|
|
|
* Counts objects that would be indexed using the query builder.
|
2011-05-09 18:48:25 +02:00
|
|
|
*
|
2012-03-10 00:51:08 +01:00
|
|
|
* @param object $queryBuilder
|
|
|
|
* @return integer
|
|
|
|
*/
|
2011-05-09 18:48:25 +02:00
|
|
|
protected abstract function countObjects($queryBuilder);
|
|
|
|
|
2011-05-09 19:56:27 +02:00
|
|
|
/**
|
2012-03-10 00:51:08 +01:00
|
|
|
* Fetches a slice of objects using the query builder.
|
2011-05-09 19:56:27 +02:00
|
|
|
*
|
2012-03-10 00:51:08 +01:00
|
|
|
* @param object $queryBuilder
|
|
|
|
* @param integer $limit
|
|
|
|
* @param integer $offset
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-05-09 19:56:27 +02:00
|
|
|
protected abstract function fetchSlice($queryBuilder, $limit, $offset);
|
|
|
|
|
2011-04-17 19:56:32 +02:00
|
|
|
/**
|
2012-03-10 00:51:08 +01:00
|
|
|
* Creates the query builder, which will be used to fetch objects to index.
|
2011-04-17 19:56:32 +02:00
|
|
|
*
|
2012-03-10 00:51:08 +01:00
|
|
|
* @return object
|
|
|
|
*/
|
2011-05-09 23:17:38 +02:00
|
|
|
protected abstract function createQueryBuilder();
|
2011-04-17 19:56:32 +02:00
|
|
|
}
|