public function run() { $conf = Config::get('sharding'); $shard = new Adapters\MysqlShard($conf); $strategy = new Strategy\LinearStrategy(null, self::name, $shard->getConfig()); $name = self::name; $pos = strlen(self::name) + 2; $sql = "SELECT substr( TABLE_NAME, {$pos}) AS num, TABLE_ROWS as `count` \n FROM information_schema.TABLES \n WHERE TABLE_SCHEMA \n LIKE '{$name}_%' \n AND TABLE_NAME REGEXP '^({$name}_)[0-9]+\$'"; $tables = []; for ($cnn_id = 0; $cnn_id < $shard->getInstanceCount(); $cnn_id++) { try { $mysql = $shard->getConnectionById($cnn_id); } catch (Exception $e) { echo $e->getMessage(); } $res = $mysql->query($sql); while (($row = $res->fetch_assoc()) != null) { $tables[$row['num']] = $row['count']; } } $sum = count($tables); $fillTables = 0; $startTabId = 0; foreach ($tables as $tab_id => $count) { if ($count > 0) { $fillTables++; } if ($tab_id > $startTabId) { $startTabId = (int) $tab_id; } } if ($sum - $fillTables > self::emptyTables) { $out = sprintf('fulling tables %d from %d %s', $fillTables, $sum, date('Y-m-d')); echo $out, PHP_EOL; exit; } $out = sprintf('generate: full tables %d from %d', $fillTables, $sum); $startTabId++; echo 'generate tables:', PHP_EOL; // $shard->setNoExec(); $table_names = ''; for ($i = $startTabId; $i < $startTabId + self::count; $i++) { $table_names .= self::name . '_' . $i . PHP_EOL; $strategy->setId(Strategy\LinearStrategy::MAX_RECORD_COUNT * $i + 1); $table = Generator\LinesGenerator::getCreateTableSQL($i); $shard->query($table, $strategy); // echo $shard->getQuery(), PHP_EOL,PHP_EOL; } echo $table_names, PHP_EOL; }
/** * */ public function testCheckQueryForAnyShards() { $strategy = new LinearStrategy(null, 'lines', $this->sharding->getConfig()); $strategy->setId(799999890); $this->sharding->setNoExec(); $this->sharding->query("TRUNCATE TABLE %db.lines_%t", $strategy); $sql = $this->sharding->getQuery(); $this->assertEquals('TRUNCATE TABLE lines_2.lines_7', $sql); $sql = $this->sqlResultProvider(); // проверка на соответствие данным из self::sqlResultProvider() for ($i = 0; $i < 10; $i++) { unset($strategy); $strategy = new LinearStrategy(null, 'lines', $this->sharding->getConfig()); $strategy->setId(LinearStrategy::MAX_RECORD_COUNT * $i); $this->sharding->query("TRUNCATE TABLE %db.lines_%t", $strategy); $this->assertEquals($sql[$i], $this->sharding->getQuery()); } // то же но с одним классом стратегии for ($i = 0; $i < 10; $i++) { $id = LinearStrategy::MAX_RECORD_COUNT * $i + 1; $strategy->setId($id); $this->sharding->query("TRUNCATE TABLE %db.lines_%t", $strategy); $this->assertEquals($sql[$i], $this->sharding->getQuery()); } }
<?php require '../src/autoload.php'; use MysqlShard\Adapters\MysqlShard; use MysqlShard\Tools\Config; use MysqlShard\Strategy\LinearStrategy; use MysqlShard\Generator\LinesGenerator; $shard = new MysqlShard(); $strategy = new LinearStrategy(null, 'lines', $shard->getConfig()); // create 10 tables for shards 0..9 for ($i = 0; $i < 10; $i++) { $query = LinesGenerator::getCreateTableSQL($i); $strategy->setId($i * LinearStrategy::MAX_RECORD_COUNT + 1); $shard->query($query, $strategy); }