<commit_before><?php

namespace Maghead\Command\ShardCommand;

use Maghead\Command\BaseCommand;
use Maghead\Manager\DatabaseManager;
use Maghead\Manager\DataSourceManager;
use Maghead\Manager\ConnectionManager;
use Maghead\Sharding\Manager\ConfigManager;
use Maghead\Sharding\Manager\ShardManager;
use Maghead\DSN\DSN;
use PDO;
use Exception;

class AllocateCommand extends BaseCommand
{
    public function brief()
    {
        return 'allocate a shard';
    }

    public function options($opts)
    {
        parent::options($opts);
        $opts->add('mapping:', 'the shard mapping where the new shard will be added to.');
        $opts->add('instance:', 'the instance id')
            ->defaultValue('local');
    }

    public function arguments($args)
    {
        $args->add('node-id');
        $args->add('dsn');
    }

    public function execute($nodeId)
    {
        $shardId = $nodeId;
        $config = $this->getConfig(true);

        $dataSourceManager = DataSourceManager::getInstance();

        // Create the instance connection manager
        $connectionManager = new ConnectionManager($config->getInstances());
        $conn = $connectionManager->connectInstance($this->options->instance);
        $newnode = $connectionManager->getNodeConfig($this->options->instance);
        $newnode['database'] = $nodeId;
        $newnode = DSN::update($newnode);
        $dataSourceManager->addNode($nodeId, $newnode);

        $shardManager = new ShardManager($config, $dataSourceManager);
        $mapping = $shardManager->loadShardMapping($this->options->mapping);
        $mapping->addShardId($nodeId);

        $configManager = new ConfigManager($config);
        $configManager->addShardMapping($mapping);
        $configManager->addDatabaseConfig($nodeId, $newnode);
        $configManager->save();

        // create the database
        $create = $this->createCommand('Maghead\\Command\\DbCommand\\CreateCommand');
        $create->execute($nodeId);
    }
}
<commit_msg>reuse allocate shard operation in shard allocate command<commit_after><?php

namespace Maghead\Command\ShardCommand;

use Maghead\Command\BaseCommand;
use Maghead\Manager\DatabaseManager;
use Maghead\Manager\DataSourceManager;
use Maghead\Manager\ConnectionManager;
use Maghead\Sharding\Manager\ConfigManager;
use Maghead\Sharding\Manager\ShardManager;
use Maghead\DSN\DSN;
use Maghead\ConfigWriter;
use Maghead\Sharding\Operations\AllocateShard;


use PDO;
use Exception;

class AllocateCommand extends BaseCommand
{
    public function brief()
    {
        return 'allocate a shard';
    }

    public function options($opts)
    {
        parent::options($opts);
        $opts->add('mapping:', 'the shard mapping where the new shard will be added to.');
        $opts->add('instance:', 'the instance id')
            ->defaultValue('local');
    }

    public function arguments($args)
    {
        $args->add('node-id');
        $args->add('dsn');
    }

    public function execute($nodeId)
    {
        $config = $this->getConfig(true);

        $op = new AllocateShard($config);
        $op->allocate($this->options->instance, $nodeId, $this->options->mapping);

        ConfigWriter::write($config);
    }
}

{'fraction_non_alphanumeric': 0.13387297633872977, 'fraction_numerical': 0.0, 'mean_word_length': 3.341891891891892, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '5926844', 'n_tokens_mistral': 928, 'n_tokens_neox': 916, 'n_words': 186}