Ejemplo n.º 1
0
 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;
 }
Ejemplo n.º 2
0
 /**
  * чистка БД
  * шарды 3 и 4 5  на разных физ серверах (коннекциях)
  */
 private function dbInit()
 {
     $this->sharding->setNoExec(false);
     // делаем запрос исполняемым
     $strategy = new LinearStrategy(null, 'lines', $this->sharding->getConfig());
     $strategy->setId(899999890);
     $shard_id = 8;
     $this->sharding->query("DROP TABLE %db.lines_%t", $strategy);
     $sql = LinesGenerator::getCreateTableSQL($shard_id);
     $this->sharding->query($sql, $strategy);
     $strategy->setId(499999890);
     $shard_id = 4;
     $this->sharding->query("DROP TABLE %db.lines_%t", $strategy);
     $sql = LinesGenerator::getCreateTableSQL($shard_id);
     $this->sharding->query($sql, $strategy);
     $strategy->setId(999999890);
     $shard_id = 9;
     $this->sharding->query("DROP TABLE %db.lines_%t", $strategy);
     $sql = LinesGenerator::getCreateTableSQL($shard_id);
     $this->sharding->query($sql, $strategy);
 }
Ejemplo n.º 3
0
<?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);
}
Ejemplo n.º 4
0
<?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());
for ($i = 0; $i < 10; $i++) {
    $query = LinesGenerator::getDropTableSQL($i);
    $strategy->setId($i * LinearStrategy::MAX_RECORD_COUNT + 1);
    $shard->query($query, $strategy);
}