Remove mapping and setting registries, use the reseter to configure the indexes

This commit is contained in:
ornicar 2011-09-12 18:28:59 +02:00
parent e83a3344e9
commit 451a5b4fc2
7 changed files with 40 additions and 160 deletions

View file

@ -32,17 +32,16 @@ class PopulateCommand extends ContainerAwareCommand
$output->writeln('Reseting indexes');
$this->getContainer()->get('foq_elastica.reseter')->reset();
$output->writeln('Applying index settings');
$this->getContainer()->get('foq_elastica.setting_registry')->applySettings();
$output->writeln('Applying type mappings');
$this->getContainer()->get('foq_elastica.mapping_registry')->applyMappings();
$output->writeln('Populating indexes');
$this->getContainer()->get('foq_elastica.populator')->populate(function($text) use ($output) {
$output->writeLn($text);
});
$output->writeln('Refreshing indexes');
array_map(function($index) {
$index->refresh();
}, $this->getContainer()->get('foq_elastica.index_manager')->getAllIndexes());
$output->writeln('Done');
}
}

View file

@ -15,8 +15,8 @@ use InvalidArgumentException;
class FOQElasticaExtension extends Extension
{
protected $supportedProviderDrivers = array('mongodb', 'orm');
protected $typeMappings = array();
protected $indexSettings = array();
protected $indexConfigs = array();
protected $typeFields = array();
protected $loadedDoctrineDrivers = array();
public function load(array $configs, ContainerBuilder $container)
@ -49,8 +49,7 @@ class FOQElasticaExtension extends Extension
}, $indexIdsByName);
$this->loadIndexManager($indexDefsByName, $container->getDefinition($indexIdsByName[$config['default_index']]), $container);
$this->loadMappingRegistry($this->typeMappings, $container);
$this->loadSettingRegistry($this->indexSettings, $container);
$this->loadReseter($this->indexConfigs, $container);
$container->setAlias('foq_elastica.client', sprintf('foq_elastica.client.%s', $config['default_client']));
$container->setAlias('foq_elastica.index', sprintf('foq_elastica.index.%s', $config['default_index']));
@ -102,11 +101,15 @@ class FOQElasticaExtension extends Extension
$indexDef->setFactoryMethod('getIndex');
$container->setDefinition($indexId, $indexDef);
$typePrototypeConfig = isset($index['type_prototype']) ? $index['type_prototype'] : array();
$this->loadTypes(isset($index['types']) ? $index['types'] : array(), $container, $name, $indexId, $typePrototypeConfig);
$indexIds[$name] = $indexId;
if (isset($index['settings'])) {
$this->indexSettings[$name] = array(new Reference($indexId), $index['settings']);
}
$this->indexConfigs[$name] = array(
'index' => new Reference($indexId),
'config' => array(
'settings' => $index['settings'],
'mappings' => array()
)
);
$this->loadTypes(isset($index['types']) ? $index['types'] : array(), $container, $name, $indexId, $typePrototypeConfig);
}
return $indexIds;
@ -128,9 +131,10 @@ class FOQElasticaExtension extends Extension
$typeDef->setFactoryService($indexId);
$typeDef->setFactoryMethod('getType');
$container->setDefinition($typeId, $typeDef);
$key = sprintf('%s/%s', $indexName, $name);
if (isset($type['mappings'])) {
$this->typeMappings[$key] = array(new Reference($typeId), $type['mappings']);
$this->indexConfigs[$indexName]['config']['mappings'][$name] = array('properties' => $type['mappings']);
$typeName = sprintf('%s/%s', $indexName, $name);
$this->typeFields[$typeName] = array_keys($type['mappings']);
}
if (isset($type['doctrine'])) {
$this->loadTypeDoctrineIntegration($type['doctrine'], $container, $typeDef, $indexName, $name);
@ -229,6 +233,7 @@ class FOQElasticaExtension extends Extension
$serviceDef->replaceArgument(0, $typeDef);
$serviceDef->replaceArgument(1, new Reference($transformerId));
$serviceDef->replaceArgument(2, $typeConfig['model']);
$serviceDef->replaceArgument(3, $this->typeFields[sprintf('%s/%s', $indexName, $typeName)]);
$container->setDefinition($serviceId, $serviceDef);
return $serviceId;
@ -310,25 +315,14 @@ class FOQElasticaExtension extends Extension
}
/**
* Loads the mapping registry
* Loads the reseter
*
* @return null
**/
protected function loadMappingRegistry(array $mappings, ContainerBuilder $container)
protected function loadReseter(array $indexConfigs, ContainerBuilder $container)
{
$managerDef = $container->getDefinition('foq_elastica.mapping_registry');
$managerDef->replaceArgument(0, $mappings);
}
/**
* Loads the setting registry
*
* @return null
**/
protected function loadSettingRegistry(array $settings, ContainerBuilder $container)
{
$managerDef = $container->getDefinition('foq_elastica.setting_registry');
$managerDef->replaceArgument(0, $settings);
$reseterDef = $container->getDefinition('foq_elastica.reseter');
$reseterDef->replaceArgument(0, $indexConfigs);
}
protected function loadDoctrineDriver(ContainerBuilder $container, $driver)

View file

@ -4,7 +4,6 @@ namespace FOQ\ElasticaBundle\Persister;
use FOQ\ElasticaBundle\Provider\ProviderInterface;
use FOQ\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface;
use FOQ\ElasticaBundle\Registry\MappingRegistry;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Elastica_Type;
use Elastica_Document;
@ -21,16 +20,16 @@ class ObjectPersister implements ObjectPersisterInterface
protected $type;
protected $transformer;
protected $objectClass;
protected $mappingRegistry;
protected $fields;
protected $logger;
protected $throwExceptions;
public function __construct(Elastica_Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, MappingRegistry $mappingRegistry, LoggerInterface $logger = null, $throwExceptions = true)
public function __construct(Elastica_Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields, LoggerInterface $logger = null, $throwExceptions = true)
{
$this->type = $type;
$this->transformer = $transformer;
$this->objectClass = $objectClass;
$this->mappingRegistry = $mappingRegistry;
$this->fields = $fields;
$this->logger = $logger;
$this->throwExceptions = true;
}
@ -110,7 +109,7 @@ class ObjectPersister implements ObjectPersisterInterface
*/
protected function transformToElasticaDocument($object)
{
return $this->transformer->transform($object, $this->mappingRegistry->getTypeFieldNames($this->type));
return $this->transformer->transform($object, $this->fields);
}
/**

View file

@ -1,60 +0,0 @@
<?php
namespace FOQ\ElasticaBundle\Registry;
use Elastica_Type;
use InvalidArgumentException;
/**
* Stores the configured mappings for all types
* Responsible for applying configured mappings to elastica types
*/
class MappingRegistry
{
/**
* Configured mappings. See http://www.elasticsearch.org/guide/reference/mapping/
* array(
* "index_name/type_name" => array(type_object, mapping_array)
* )
*
* @var array
*/
protected $mappings;
/**
* Instanciates a new MappingSetter
*
* @param array mappings
*/
public function __construct(array $mappings)
{
$this->mappings = $mappings;
}
/**
* Apply mappings to all elastica types
**/
public function applyMappings()
{
foreach ($this->mappings as $pair) {
list($type, $mappings) = $pair;
$type->setMapping($mappings);
}
}
/**
* Gets the type mapping field names
*
* @param Elastica_Type $type
* @return array list of fields names
*/
public function getTypeFieldNames(Elastica_Type $type)
{
$key = sprintf('%s/%s', $type->getIndex()->getName(), $type->getType());
if (!isset($this->mappings[$key])) {
throw new InvalidArgumentException(sprintf('This type is not registered: "%s".', $key));
}
return array_keys($this->mappings[$key][1]);
}
}

View file

@ -1,44 +0,0 @@
<?php
namespace FOQ\ElasticaBundle\Registry;
use Elastica_Type;
use InvalidArgumentException;
/**
* Stores the configured mappings for all indexes
* Responsible for applying configured mappings to elastica indexes
*/
class SettingRegistry
{
/**
* Configured settings.
* array(
* "index_name" => array(index_object, setting_array)
* )
*
* @var array
*/
protected $settings;
/**
* Instanciates a new SettingSetter
*
* @param array settings
*/
public function __construct(array $settings)
{
$this->settings = $settings;
}
/**
* Apply settings to all elastica indexes
**/
public function applySettings()
{
foreach ($this->settings as $pair) {
list($index, $settings) = $pair;
$index->setSettings($settings);
}
}
}

View file

@ -9,11 +9,16 @@ use Elastica_Exception_Response;
**/
class Reseter
{
protected $indexManager;
/**
* Index settings and mappings
*
* @var array
*/
protected $indexConfigs;
public function __construct(IndexManager $indexManager)
public function __construct(array $indexConfigs)
{
$this->indexManager = $indexManager;
$this->indexConfigs = $indexConfigs;
}
/**
@ -23,13 +28,8 @@ class Reseter
**/
public function reset()
{
foreach ($this->indexManager->getAllIndexes() as $index) {
try {
$index->delete();
} catch (Elastica_Exception_Response $e) {
// The index does not exist
}
$index->create();
foreach ($this->indexConfigs as $indexConfig) {
$indexConfig['index']->create($indexConfig['config'], true);
}
}
}

View file

@ -23,22 +23,14 @@
</service>
<service id="foq_elastica.reseter" class="FOQ\ElasticaBundle\Reseter">
<argument type="service" id="foq_elastica.index_manager" />
</service>
<service id="foq_elastica.mapping_registry" class="FOQ\ElasticaBundle\Registry\MappingRegistry">
<argument /> <!-- mappings -->
</service>
<service id="foq_elastica.setting_registry" class="FOQ\ElasticaBundle\Registry\SettingRegistry">
<argument /> <!-- settings -->
<argument /> <!-- index configs -->
</service>
<service id="foq_elastica.object_persister.prototype" class="FOQ\ElasticaBundle\Persister\ObjectPersister" public="false" abstract="true">
<argument /> <!-- type -->
<argument /> <!-- model to elastica transformer -->
<argument /> <!-- model -->
<argument type="service" id="foq_elastica.mapping_registry" />
<argument /> <!-- properties mapping -->
<argument type="service" id="logger" on-invalid="null" />
<argument>%kernel.debug%</argument>
</service>