2011-04-17 19:56:32 +02:00
|
|
|
<?php
|
|
|
|
|
2011-05-10 00:31:20 +02:00
|
|
|
namespace FOQ\ElasticaBundle\Doctrine;
|
2011-04-17 19:56:32 +02:00
|
|
|
|
2011-05-10 00:31:20 +02:00
|
|
|
use FOQ\ElasticaBundle\Provider\ProviderInterface;
|
2011-06-07 20:13:34 +02:00
|
|
|
use FOQ\ElasticaBundle\Persister\ObjectPersisterInterface;
|
2011-04-17 19:56:32 +02:00
|
|
|
use Elastica_Type;
|
|
|
|
use Elastica_Document;
|
|
|
|
use Closure;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
2011-05-10 00:31:20 +02:00
|
|
|
abstract class AbstractProvider implements ProviderInterface
|
2011-04-17 19:56:32 +02:00
|
|
|
{
|
2011-06-07 20:13:34 +02:00
|
|
|
/**
|
|
|
|
* Elastica type
|
|
|
|
*
|
|
|
|
* @var Elastica_Type
|
|
|
|
*/
|
2011-04-17 19:56:32 +02:00
|
|
|
protected $type;
|
2011-06-07 20:13:34 +02:00
|
|
|
|
|
|
|
/**
|
2011-12-13 11:22:24 +01:00
|
|
|
* Manager registry
|
2011-06-07 20:13:34 +02:00
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
2011-12-13 11:22:24 +01:00
|
|
|
protected $registry;
|
2011-06-07 20:13:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Object persister
|
|
|
|
*
|
|
|
|
* @var ObjectPersisterInterface
|
|
|
|
*/
|
|
|
|
protected $objectPersister;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provider options
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-04-17 19:56:32 +02:00
|
|
|
protected $options = array(
|
2011-04-20 22:53:59 +02:00
|
|
|
'batch_size' => 100,
|
|
|
|
'clear_object_manager' => true,
|
2011-04-27 09:59:04 +02:00
|
|
|
'query_builder_method' => 'createQueryBuilder'
|
2011-04-17 19:56:32 +02:00
|
|
|
);
|
|
|
|
|
2011-12-13 11:22:24 +01:00
|
|
|
public function __construct(Elastica_Type $type, $registry, ObjectPersisterInterface $objectPersister, $objectClass, array $options = array())
|
2011-04-17 19:56:32 +02:00
|
|
|
{
|
2011-06-07 20:13:34 +02:00
|
|
|
$this->type = $type;
|
2011-12-13 11:22:24 +01:00
|
|
|
$this->registry = $registry;
|
2011-06-07 20:13:34 +02:00
|
|
|
$this->objectClass = $objectClass;
|
|
|
|
$this->objectPersister = $objectPersister;
|
|
|
|
$this->options = array_merge($this->options, $options);
|
2011-04-17 19:56:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert the repository objects in the type index
|
|
|
|
*
|
|
|
|
* @param Closure $loggerClosure
|
|
|
|
*/
|
|
|
|
public function populate(Closure $loggerClosure)
|
|
|
|
{
|
|
|
|
$queryBuilder = $this->createQueryBuilder();
|
2011-05-09 18:56:31 +02:00
|
|
|
$nbObjects = $this->countObjects($queryBuilder);
|
2011-04-17 19:56:32 +02:00
|
|
|
|
|
|
|
for ($offset = 0; $offset < $nbObjects; $offset += $this->options['batch_size']) {
|
|
|
|
|
2011-04-27 09:59:04 +02:00
|
|
|
$stepStartTime = microtime(true);
|
2011-05-09 19:56:27 +02:00
|
|
|
$objects = $this->fetchSlice($queryBuilder, $this->options['batch_size'], $offset);
|
2011-04-20 22:53:59 +02:00
|
|
|
|
2011-06-07 20:13:34 +02:00
|
|
|
$this->objectPersister->insertMany($objects);
|
2011-04-17 19:56:32 +02:00
|
|
|
|
|
|
|
if ($this->options['clear_object_manager']) {
|
2011-12-13 11:22:24 +01:00
|
|
|
$this->registry->getManagerForClass($this->objectClass)->clear();
|
2011-04-17 19:56:32 +02:00
|
|
|
}
|
2011-04-27 09:59:04 +02:00
|
|
|
|
|
|
|
$stepNbObjects = count($objects);
|
|
|
|
$stepCount = $stepNbObjects+$offset;
|
|
|
|
$objectsPerSecond = $stepNbObjects / (microtime(true) - $stepStartTime);
|
|
|
|
$loggerClosure(sprintf('%0.1f%% (%d/%d), %d objects/s', 100*$stepCount/$nbObjects, $stepCount, $nbObjects, $objectsPerSecond));
|
2011-04-17 19:56:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-09 18:48:25 +02:00
|
|
|
/**
|
|
|
|
* Counts the objects of a query builder
|
|
|
|
*
|
2011-05-09 19:56:27 +02:00
|
|
|
* @param queryBuilder
|
2011-05-09 18:48:25 +02:00
|
|
|
* @return int
|
|
|
|
**/
|
|
|
|
protected abstract function countObjects($queryBuilder);
|
|
|
|
|
2011-05-09 19:56:27 +02:00
|
|
|
/**
|
|
|
|
* Fetches a slice of objects
|
|
|
|
*
|
|
|
|
* @param queryBuilder
|
|
|
|
* @param int limit
|
|
|
|
* @param int offset
|
|
|
|
* @return array of objects
|
|
|
|
**/
|
|
|
|
protected abstract function fetchSlice($queryBuilder, $limit, $offset);
|
|
|
|
|
2011-04-17 19:56:32 +02:00
|
|
|
/**
|
2011-05-09 23:17:38 +02:00
|
|
|
* Creates the query builder used to fetch the documents to index
|
2011-04-17 19:56:32 +02:00
|
|
|
*
|
2011-05-09 23:17:38 +02:00
|
|
|
* @return query builder
|
2011-04-17 19:56:32 +02:00
|
|
|
**/
|
2011-05-09 23:17:38 +02:00
|
|
|
protected abstract function createQueryBuilder();
|
2011-04-17 19:56:32 +02:00
|
|
|
}
|