<commit_before><?php

namespace Maghead\Sharding\Manager;

use Maghead\Sharding\Hasher\FlexihashHasher;
use Maghead\Sharding\ShardDispatcher;
use Maghead\Sharding\ShardMapping;
use Maghead\Sharding\Shard;
use Maghead\Manager\ConnectionManager;
use Maghead\Manager\DatabaseManager;
use Maghead\Config;

use Maghead\DSN\DSNParser;
use Maghead\DSN\DSN;

use LogicException;
use Exception;
use ArrayIterator;
use Iterator;
use IteratorAggregate;

class ChunkManager
{

    protected $config;

    protected $connectionManager;

    protected $shardManager;

    public function __construct(Config $config, ConnectionManager $connectionManager, ShardManager $shardManager = null)
    {
        $this->config = $config;
        $this->connectionManager = $connectionManager;
        $this->shardManager = $shardManager ?: new ShardManager($config, $connectionManager);
    }

    public function removeChunks(ShardMapping $mapping)
    {
        $dbManager = new DatabaseManager($this->connectionManager);
        $chunks = $mapping->getChunks();
        foreach ($chunks as $chunkId => $chunk) {
            $shardId = $chunk['shard'];

            // get shard from the chunk
            $shard = $this->shardManager->getShard($shardId);
            $nodeId = $shard->selectWriteNode();
            $dbManager->drop($nodeId, $chunkId);
        }
    }

    public function initChunks(ShardMapping $mapping, $numberOfChunks = 32)
    {
        // Get the dbname from master datasource
        $masterDs = $this->connectionManager->getMasterDataSource();
        $dsn = DSNParser::parse($masterDs['dsn']);
        $dbname = $dsn->getDatabaseName();

        // Get shards use in this mapping<commit_msg>test chunk format and dsn format<commit_after><?php

namespace Maghead\Sharding\Manager;

use Maghead\Sharding\Hasher\FlexihashHasher;
use Maghead\Sharding\ShardDispatcher;
use Maghead\Sharding\ShardMapping;
use Maghead\Sharding\Shard;
use Maghead\Manager\ConnectionManager;
use Maghead\Manager\DatabaseManager;
use Maghead\Config;

use Maghead\DSN\DSNParser;
use Maghead\DSN\DSN;

use LogicException;
use Exception;
use ArrayIterator;
use Iterator;
use IteratorAggregate;

class ChunkManager
{

    protected $config;

    protected $connectionManager;

    protected $shardManager;

    public function __construct(Config $config, ConnectionManager $connectionManager, ShardManager $shardManager = null)
    {
        $this->config = $config;
        $this->connectionManager = $connectionManager;
        $this->shardManager = $shardManager ?: new ShardManager($config, $connectionManager);
    }

    public function removeChunks(ShardMapping $mapping)
    {
        $dbManager = new DatabaseManager($this->connectionManager);
        $chunks = $mapping->getChunks();
        foreach ($chunks as $chunkId => $chunk) {
            $shardId = $chunk['shard'];

            // get shard from the chunk
            $shard = $this->shardManager->getShard($shardId);
            $nodeId = $shard->selectWriteNode();
            $dbManager->drop($nodeId, $chunkId);
        }
    }

    /**
     * Initialize chunks for one shard mapping.
     */
    public function initChunks(ShardMapping $mapping, $numberOfChunks = 4)
    {
        // Get the dbname from master datasource
        $masterDs = $this->connectionManager->getMasterDataSource();
        $dsn = DSNParser::parse($masterDs['dsn']);
        $dbname = $dsn->getDatabaseName();

        // Get shards use in this mapping
{'fraction_non_alphanumeric': 0.1119102416570771, 'fraction_numerical': 0.0008630609896432681, 'mean_word_length': 3.34625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '5270541', 'n_tokens_mistral': 1041, 'n_tokens_neox': 1013, 'n_words': 226}