<commit_before><?php

namespace FOS\ElasticaBundle;

use Elastica\Exception\ExceptionInterface;
use Elastica\Index;
use Elastica\Type\Mapping;

/**
 * Deletes and recreates indexes
 */
class Resetter
{
    protected $indexConfigsByName;

    /**
     * Constructor.
     *
     * @param array $indexConfigsByName
     */
    public function __construct(array $indexConfigsByName)
    {
        $this->indexConfigsByName = $indexConfigsByName;
    }

    /**
     * Deletes and recreates all indexes
     */
    public function resetAllIndexes()
    {
        foreach (array_keys($this->indexConfigsByName) as $name) {
            $this->resetIndex($name);
        }
    }

    /**
     * Deletes and recreates the named index
     *
     * @param string $indexName
     * @throws \InvalidArgumentException if no index exists for the given name
     */
    public function resetIndex($indexName)
    {
        $indexConfig = $this->getIndexConfig($indexName);
        $esIndex = $indexConfig['index'];
        if (isset($indexConfig['use_alias']) && $indexConfig['use_alias']) {
            $name = $indexConfig['name_or_alias'];
            $name .= date('-Y-m-d-Gis');
            $esIndex->overrideName($name);
            $esIndex->create($indexConfig['config']);

            return;
        }

        $esIndex->create($indexConfig['config'], true);
    }

    /**
     * Deletes and recreates a mapping type for the named index
     *
     * @param string $indexName
     * @param string $typeName
     * @throws \InvalidArgumentException if no index or type mapping exists for the given names
     */
    public function resetIndexType($indexName, $typeName)
    {
        $indexConfig = $this->getIndexConfig($indexName);

        if (!isset($indexConfig['config']['mappings'][$typeName]['properties'])) {
            throw new \InvalidArgumentException(sprintf('The mapping for index "%s" and type "%s" does not exist.', $indexName, $typeName));
        }

        $type = $indexConfig['index']->getType($typeName);
        $type->delete();
        $mapping = $this->createMapping($indexConfig['config']['mappings'][$typeName]);
        $type->setMapping($mapping);
    }

    /**
     * create type mapping object
     *
     * @param array $indexConfig
     * @return Mapping
     */
    protected function createMapping($indexConfig)
    {
        $mapping = Mapping::create($indexConfig['properties']);

        if (isset($indexConfig['_parent'])) {
            $mapping->setParam('_parent', array('type' => $indexConfig['_parent']['type']));
        }

        if (isset($indexConfig['dynamic_templates'])) {
            $mapping->setParam('dynamic_templates', $indexConfig['dynamic_templates']);
        }

        return $mapping;
    }

    /**
     * Gets an index config by its name
     *
     * @param string $indexName Index name
     *
     * @param $indexName
     * @return array
     * @throws \InvalidArgumentException if no index config exists for the given name
     */
    protected function getIndexConfig($indexName)
    {
        if (!isset($this->indexConfigsByName[$indexName])) {
            throw new \InvalidArgumentException(sprintf('The configuration for index "%s" does not exist.', $indexName));
        }

        return $this->indexConfigsByName[$indexName];
    }

    public function postPopulate($indexName)
    {
        $indexConfig = $this->getIndexConfig($indexName);
        if (isset($indexConfig['use_alias']) && $indexConfig['use_alias']) {
            $this->switchIndexAlias($indexName);
        }
    }

    /**
     * Switches the alias for given index to the newly populated index
     * and deletes the old index
     *
     * @param string $indexName Index name
     *
     * @throws \RuntimeException
     */
    private function switchIndexAlias($indexName)
    {
        $indexConfig = $this->getIndexConfig($indexName);
        $esIndex = $indexConfig['index'];
        $aliasName = $indexConfig['name_or_alias'];
        $oldIndexName = false;
        $newIndexName = $esIndex->getName();

        $aliasedIndexes = $this->getAliasedIndexes($esIndex, $aliasName);

        if (count($aliasedIndexes) > 1) {
            throw new \RuntimeException(
                sprintf(
                    'Alias %s is used for multiple indexes: [%s].
                    Make sure it\'s either not used or is assigned to one index only',
                    $aliasName,
                    join(', ', $aliasedIndexes)
                )
            );
        }<commit_msg>Make real index name use uniqid so can be reset within a second<commit_after><?php

namespace FOS\ElasticaBundle;

use Elastica\Exception\ExceptionInterface;
use Elastica\Index;
use Elastica\Type\Mapping;

/**
 * Deletes and recreates indexes
 */
class Resetter
{
    protected $indexConfigsByName;

    /**
     * Constructor.
     *
     * @param array $indexConfigsByName
     */
    public function __construct(array $indexConfigsByName)
    {
        $this->indexConfigsByName = $indexConfigsByName;
    }

    /**
     * Deletes and recreates all indexes
     */
    public function resetAllIndexes()
    {
        foreach (array_keys($this->indexConfigsByName) as $name) {
            $this->resetIndex($name);
        }
    }

    /**
     * Deletes and recreates the named index
     *
     * @param string $indexName
     * @throws \InvalidArgumentException if no index exists for the given name
     */
    public function resetIndex($indexName)
    {
        $indexConfig = $this->getIndexConfig($indexName);
        $esIndex = $indexConfig['index'];
        if (isset($indexConfig['use_alias']) && $indexConfig['use_alias']) {
            $name = $indexConfig['name_or_alias'];
            $name .= uniqid();
            $esIndex->overrideName($name);
            $esIndex->create($indexConfig['config']);

            return;
        }

        $esIndex->create($indexConfig['config'], true);
    }

    /**
     * Deletes and recreates a mapping type for the named index
     *
     * @param string $indexName
     * @param string $typeName
     * @throws \InvalidArgumentException if no index or type mapping exists for the given names
     */
    public function resetIndexType($indexName, $typeName)
    {
        $indexConfig = $this->getIndexConfig($indexName);

        if (!isset($indexConfig['config']['mappings'][$typeName]['properties'])) {
            throw new \InvalidArgumentException(sprintf('The mapping for index "%s" and type "%s" does not exist.', $indexName, $typeName));
        }

        $type = $indexConfig['index']->getType($typeName);
        $type->delete();
        $mapping = $this->createMapping($indexConfig['config']['mappings'][$typeName]);
        $type->setMapping($mapping);
    }

    /**
     * create type mapping object
     *
     * @param array $indexConfig
     * @return Mapping
     */
    protected function createMapping($indexConfig)
    {
        $mapping = Mapping::create($indexConfig['properties']);

        if (isset($indexConfig['_parent'])) {
            $mapping->setParam('_parent', array('type' => $indexConfig['_parent']['type']));
        }

        if (isset($indexConfig['dynamic_templates'])) {
            $mapping->setParam('dynamic_templates', $indexConfig['dynamic_templates']);
        }

        return $mapping;
    }

    /**
     * Gets an index config by its name
     *
     * @param string $indexName Index name
     *
     * @param $indexName
     * @return array
     * @throws \InvalidArgumentException if no index config exists for the given name
     */
    protected function getIndexConfig($indexName)
    {
        if (!isset($this->indexConfigsByName[$indexName])) {
            throw new \InvalidArgumentException(sprintf('The configuration for index "%s" does not exist.', $indexName));
        }

        return $this->indexConfigsByName[$indexName];
    }

    public function postPopulate($indexName)
    {
        $indexConfig = $this->getIndexConfig($indexName);
        if (isset($indexConfig['use_alias']) && $indexConfig['use_alias']) {
            $this->switchIndexAlias($indexName);
        }
    }

    /**
     * Switches the alias for given index (by key) to the newly populated index
     * and deletes the old index
     *
     * @param string $indexName Index name
     *
     * @throws \RuntimeException
     */
    private function switchIndexAlias($indexName)
    {
        $indexConfig = $this->getIndexConfig($indexName);
        $esIndex = $indexConfig['index'];
        $aliasName = $indexConfig['name_or_alias'];
        $oldIndexName = false;
        $newIndexName = $esIndex->getName();

        $aliasedIndexes = $this->getAliasedIndexes($esIndex, $aliasName);

        if (count($aliasedIndexes) > 1) {
            throw new \RuntimeException(
                sprintf(
                    'Alias %s is used for multiple indexes: [%s].
                    Make sure it\'s either not used or is assigned to one index only',
                    $aliasName,
                    join(', ', $aliasedIndexes)
                )
            );
        }
{'fraction_non_alphanumeric': 0.12808964077776558, 'fraction_numerical': 0.00021970778864110734, 'mean_word_length': 2.4187007134810363, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5468920', 'n_tokens_mistral': 2521, 'n_tokens_neox': 2469, 'n_words': 584}