From 214250416aabf72a51aa3dc6115eadff8c1d286c Mon Sep 17 00:00:00 2001 From: ornicar Date: Tue, 7 Jun 2011 14:19:50 -0700 Subject: [PATCH] Replace the type inspector with a mapping registry to improve performances --- Command/PopulateCommand.php | 2 +- DependencyInjection/FOQElasticaExtension.php | 12 ++-- MappingRegistry.php | 61 ++++++++++++++++++++ MappingSetter.php | 39 ------------- Persister/ObjectPersister.php | 25 ++------ Resources/config/config.xml | 6 +- TypeInspector.php | 39 ------------- 7 files changed, 75 insertions(+), 109 deletions(-) create mode 100644 MappingRegistry.php delete mode 100644 MappingSetter.php delete mode 100644 TypeInspector.php diff --git a/Command/PopulateCommand.php b/Command/PopulateCommand.php index fceef5b..6dda14e 100644 --- a/Command/PopulateCommand.php +++ b/Command/PopulateCommand.php @@ -33,7 +33,7 @@ class PopulateCommand extends Command $this->container->get('foq_elastica.reseter')->reset(); $output->writeln('Setting mappings'); - $this->container->get('foq_elastica.mapping_setter')->setMappings(); + $this->container->get('foq_elastica.mapping_registry')->applyMappings(); $output->writeln('Populating indexes'); $this->container->get('foq_elastica.populator')->populate(function($text) use ($output) { diff --git a/DependencyInjection/FOQElasticaExtension.php b/DependencyInjection/FOQElasticaExtension.php index 87a8050..98640e1 100644 --- a/DependencyInjection/FOQElasticaExtension.php +++ b/DependencyInjection/FOQElasticaExtension.php @@ -48,7 +48,7 @@ class FOQElasticaExtension extends Extension }, $indexIdsByName); $this->loadIndexManager($indexDefsByName, $container->getDefinition($indexIdsByName[$config['default_index']]), $container); - $this->loadMappingSetter($this->typeMappings, $container); + $this->loadMappingRegistry($this->typeMappings, $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'])); @@ -123,8 +123,9 @@ 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[] = array(new Reference($typeId), $type['mappings']); + $this->typeMappings[$key] = array(new Reference($typeId), $type['mappings']); } if (isset($type['doctrine'])) { $this->loadTypeDoctrineIntegration($type['doctrine'], $container, $typeDef, $indexName, $name); @@ -223,7 +224,6 @@ class FOQElasticaExtension extends Extension $serviceDef->replaceArgument(0, $typeDef); $serviceDef->replaceArgument(1, new Reference($transformerId)); $serviceDef->replaceArgument(2, $typeConfig['model']); - $serviceDef->replaceArgument(3, new Reference('foq_elastica.type_inspector')); $container->setDefinition($serviceId, $serviceDef); return $serviceId; @@ -305,13 +305,13 @@ class FOQElasticaExtension extends Extension } /** - * Loads the mapping setter + * Loads the mapping registry * * @return null **/ - protected function loadMappingSetter(array $mappings, ContainerBuilder $container) + protected function loadMappingRegistry(array $mappings, ContainerBuilder $container) { - $managerDef = $container->getDefinition('foq_elastica.mapping_setter'); + $managerDef = $container->getDefinition('foq_elastica.mapping_registry'); $managerDef->replaceArgument(0, $mappings); } diff --git a/MappingRegistry.php b/MappingRegistry.php new file mode 100644 index 0000000..aeaf073 --- /dev/null +++ b/MappingRegistry.php @@ -0,0 +1,61 @@ + array(type_object, mapping_array) + * ) + * + * @var array + */ + protected $mappings = null; + + /** + * Instanciates a new MappingSetter + * + * @param array mappings + */ + public function __construct($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]); + } +} diff --git a/MappingSetter.php b/MappingSetter.php deleted file mode 100644 index 82ef654..0000000 --- a/MappingSetter.php +++ /dev/null @@ -1,39 +0,0 @@ -mappings = $mappings; - } - - /** - * Apply mappings to all elastica types - * - * @return null - **/ - public function setMappings() - { - foreach ($this->mappings as $pair) { - list($type, $mappings) = $pair; - $type->setMapping($mappings); - } - } -} diff --git a/Persister/ObjectPersister.php b/Persister/ObjectPersister.php index 893c3da..ae26562 100644 --- a/Persister/ObjectPersister.php +++ b/Persister/ObjectPersister.php @@ -4,7 +4,7 @@ namespace FOQ\ElasticaBundle\Persister; use FOQ\ElasticaBundle\Provider\ProviderInterface; use FOQ\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface; -use FOQ\ElasticaBundle\TypeInspector; +use FOQ\ElasticaBundle\MappingRegistry; use Symfony\Component\HttpKernel\Log\LoggerInterface; use Elastica_Type; use Elastica_Document; @@ -21,17 +21,16 @@ class ObjectPersister implements ObjectPersisterInterface protected $type; protected $transformer; protected $objectClass; - protected $typeInspector; + protected $mappingRegistry; protected $logger; protected $throwExceptions; - protected $fields; - public function __construct(Elastica_Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, TypeInspector $typeInspector, LoggerInterface $logger = null, $throwExceptions = true) + public function __construct(Elastica_Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, MappingRegistry $mappingRegistry, LoggerInterface $logger = null, $throwExceptions = true) { $this->type = $type; $this->transformer = $transformer; $this->objectClass = $objectClass; - $this->typeInspector = $typeInspector; + $this->mappingRegistry = $mappingRegistry; $this->logger = $logger; $this->throwExceptions = true; } @@ -111,21 +110,7 @@ class ObjectPersister implements ObjectPersisterInterface */ protected function transformToElasticaDocument($object) { - return $this->transformer->transform($object, $this->getTypeFields()); - } - - /** - * Gets the list of the type fields - * - * @return array of strings - */ - protected function getTypeFields() - { - if (null === $this->fields) { - $this->fields = $this->typeInspector->getMappingFieldsNames($this->type); - } - - return $this->fields; + return $this->transformer->transform($object, $this->mappingRegistry->getTypeFieldNames($this->type)); } /** diff --git a/Resources/config/config.xml b/Resources/config/config.xml index 0aae9f5..1b01b0e 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -26,17 +26,15 @@ - + - - - + %kernel.debug% diff --git a/TypeInspector.php b/TypeInspector.php deleted file mode 100644 index 00fd28b..0000000 --- a/TypeInspector.php +++ /dev/null @@ -1,39 +0,0 @@ - - */ -class TypeInspector -{ - /** - * Gets the type mapping fields - * - * @param Elastica_Type $type - * @return array list of fields names - */ - public function getMappingFieldsNames(Elastica_Type $type) - { - $mappings = $type->getMapping(); - // skip index and type name - // < 0.16.0 has both index and type levels - // >= 0.16.0 has only type level - do { - $mappings = reset($mappings); - } while (is_array($mappings) && !isset($mappings['properties'])); - if (!isset($mappings['properties'])) { - return array(); - } - $mappings = $mappings['properties']; - if (array_key_exists('__isInitialized__', $mappings)) { - unset($mappings['__isInitialized__']); - } - - return array_keys($mappings); - } -}