public function test_should_table_create_sql_works_fine()
 {
     $this->assertEquals("cid int(10) unsigned not null default '0',\n" . "cname varchar(255) not null default '',\n" . "autokid int(10) unsigned not null auto_increment primary key", Table::instance('mirror_v2')->sqlcreate());
     $this->assertEquals("cid int(10) not null default '0',\n" . "cname varchar(255) not null default ''", Table::instance('mirror_v2')->sqlcreate('ib'));
     $create = array("thedate date not null default '0000-00-00'", "cid int(10) unsigned not null default '0'", "num1 int(10) unsigned not null default '0'", "num2 decimal(18,4) not null default '0.00'", "char1 varchar(32) not null default ''", "autokid int(10) unsigned not null auto_increment primary key", "KEY idx_cid (cid)");
     $this->assertEquals(implode(",\n", $create), Table::instance('numsplit_v2')->sqlcreate());
     $createib = array("thedate date not null default '0000-00-00'", "cid int(10) not null default '0'", "num1 int(10) not null default '0'", "num2 decimal(18,4) not null default '0.00'", "char1 varchar(32) not null default ''");
     $this->assertEquals(implode(",\n", $createib), Table::instance('numsplit_v2')->sqlcreate('ib'));
 }
 public function test_should_sharding_table_router_set_and_get_works_fine()
 {
     $table = \Myfox\App\Model\Table::instance('numsplit');
     try {
         Router::set('numsplit', array(array('field' => array('thedate' => '20110610'), 'count' => 1201)));
         $this->assertTrue(false);
     } catch (\Exception $e) {
         $this->assertTrue($e instanceof \Myfox\Lib\Exception);
         $this->assertContains('Column "cid" required for table "numsplit"', $e->getMessage());
     }
     $this->assertEquals(array('cid:1,thedate:20110610' => array(array('rows' => 1000, 'hosts' => '3,1', 'table' => 'numsplit_0.t_' . $table->get('autokid') . '_0'), array('rows' => 201, 'hosts' => '1,2', 'table' => 'numsplit_0.t_' . $table->get('autokid') . '_1')), 'cid:2,thedate:20110610' => array(array('rows' => 998, 'hosts' => '1,2', 'table' => 'numsplit_0.t_' . $table->get('autokid') . '_1'))), Router::set('numsplit', array(array('field' => array('thedate' => '2011-06-10', 'cid' => 1), 'count' => 1201), array('field' => array('thedate' => '2011-06-10', 'cid' => 2), 'count' => 998))));
     $this->assertEquals(array(), Router::get('numsplit', array('thedate' => '2011-6-10', 'cid' => 1, 'blablala' => 2)));
     Router::effect('numsplit', array('thedate' => 20110610, 'cid' => 1), 'numsplit_0.t_' . $table->get('autokid') . '_1', '1,2') && Router::flush();
     $routes = Router::get('numsplit', array('thedate' => '2011-6-10', 'cid' => 1, 'blablala' => 2));
     $result = array();
     foreach ($routes as $item) {
         unset($item['tabid'], $item['seqid']);
         $item['mtime'] = $item['mtime'] > 0 ? true : false;
         $result[] = $item;
     }
     $this->assertEquals(array(array('tbidx' => 'test_route_info_c', 'mtime' => true, 'hosts' => '1,2', 'table' => sprintf('numsplit_0.t_%d_1', $table->get('autokid')))), $result);
 }
 /**
  * 单机装入
  *
  * @access private
  * @return Mixture
  */
 private function onehost($host, $fname, $engine = 'MYISAM')
 {
     self::metadata($flush);
     if (!isset(self::$hosts[$host])) {
         return;
     }
     $table = Table::instance($this->option('table'));
     $mysql = Server::instance(self::$hosts[$host]['name'])->getlink();
     $this->pools[$host] = array('server' => self::$hosts[$host]['name'], 'handle' => null, 'commit' => array(), 'rollback' => array());
     list($dbname, $tbname) = explode('.', $this->option('bucket'), 2);
     $querys = array(sprintf('CREATE DATABASE IF NOT EXISTS %s', $dbname), sprintf('CREATE TABLE IF NOT EXISTS %s (%s) ENGINE=%s DEFAULT CHARSET=UTF8', $this->option('bucket'), 'BRIGHTHOUSE' == $engine ? $table->sqlcreate('ib') : $table->sqlcreate(), $engine));
     if ($this->option('replace', $table->get('load_type', 0))) {
         array_unshift($querys, sprintf('DROP TABLE IF EXISTS %s', $this->option('bucket')));
         $this->pools[$host]['commit'] = array();
         $this->pools[$host]['rollback'] = array(sprintf('DROP TABLE IF EXISTS %s', $this->option('bucket')));
     } else {
         if ('BRIGHTHOUSE' == $engine) {
             $this->pools[$host]['commit'] = array('COMMIT');
             $this->pools[$host]['rollback'] = array('ROLLBACK');
         } else {
             $maxid = (int) $mysql->getOne($mysql->query(sprintf('SELECT MAX(%s) FROM %s', $table->autokid(), $this->option('bucket'))));
             if ($maxid) {
                 $this->pools[$host]['rollback'] = array(sprintf('DELETE FROM %s WHERE %s > %u', $this->option('bucket'), $table->autokid(), $maxid));
             } else {
                 $this->pools[$host]['rollback'] = array('DROP TABLE IF EXISTS %s', $this->option('bucket'));
             }
         }
     }
     if ('BRIGHTHOUSE' == $engine) {
         array_push($querys, 'SET AUTOCOMMIT=0');
     }
     foreach ($querys as $sql) {
         if (false === $mysql->query($sql)) {
             $this->setError($mysql->lastError());
             return false;
         }
     }
     $import = $table->get('sql_import');
     if (empty($import)) {
         $import = self::IMPORTSQL;
     }
     if ('MYISAM' == $engine) {
         $import = preg_replace('/\\s+ENCLOSED\\s+BY\\s+("|\')?NULL("|\')?/i', ' ENCLOSED BY ""', $import);
     }
     $this->pools[$host]['handle'] = $mysql->async(strtr($import, array('{FILE}' => $fname, '{TABLE}' => $this->option('bucket'), '{FS}' => chr(1), '{TAB}' => chr(9))));
 }
Beispiel #4
0
 /**
  * 构造函数
  *
  * @access private
  * @return void
  */
 private function __construct($tbname)
 {
     $this->table = Table::instance($tbname);
     if (!$this->table->get('autokid')) {
         throw new \Myfox\Lib\Exception(sprintf('Undefined table named as "%s"', $tbname));
     }
     $this->tbname = $this->table->get('table_name', '');
     if (empty(self::$mysql)) {
         self::$mysql = \Myfox\Lib\Mysql::instance('default');
     }
     if (null === self::$hostall || null === self::$onlines) {
         self::metadata();
     }
 }
Beispiel #5
0
 /**
  * OPTIONS:
  * ----------------------------------------------------------------------
  * @file    : complete file url, such as "ftp://*****:*****@hostname/path"
  * @table   : name of the logic table
  * @route   : route value of the file, ex: thedate=20111001,cid=210
  * @lines   :
  * ---------------------------------------------------------------------
  */
 public function execute()
 {
     if (!$this->isReady('table', 'route', 'lines', 'file', 'priority')) {
         return self::IGNO;
     }
     $table = Table::instance($this->option('table'));
     if (!$table->get('autokid')) {
         $this->setError(sprintf('Undefined table named as "%s".', $this->option('table')));
         return self::IGNO;
     }
     try {
         $routes = Router::set($this->option('table'), array(array('field' => Router::parse($this->option('route'), $this->option('table')), 'count' => (int) $this->option('lines'))));
         if (!is_array($routes)) {
             $this->setError('route failed.');
             return self::FAIL;
         }
         $config = Config::instance('default');
         $fname = Fileset::getfile($this->option('file'), $config->get('path/download'));
         if (empty($fname)) {
             $this->setError(sprintf('getfile:%s', Fileset::lastError()));
             return self::FAIL;
         }
         $splits = array();
         foreach ($routes as $key => $bucket) {
             foreach ($bucket as $item) {
                 $splits[] = $item['rows'];
             }
         }
         $fsplit = new Fsplit($fname, "\n", 16777216);
         $chunks = $fsplit->split($splits, $config->get('path/filesplit'));
         if (empty($chunks)) {
             $this->setError(sprintf('fsplit failed,%s', $fsplit->lastError()));
             return self::FAIL;
         }
         if (preg_match('/^\\w+:/i', $this->option('file'))) {
             @unlink($fname);
         }
         $result = array();
         $queque = Queque::instance();
         foreach ($routes as $key => $bucket) {
             foreach ($bucket as $item) {
                 $fname = array_shift($chunks);
                 if (empty($fname)) {
                     break 2;
                 }
                 $info = array('table' => $this->option('table'), 'route' => $key, 'file' => $fname, 'bucket' => $item['table'], 'hosts' => $item['hosts']);
                 $option = array('openrace' => 0, 'priority' => (int) $this->option('priority'), 'trytimes' => 3, 'task_flag' => Queque::FLAG_WAIT, 'adduser' => 'rsplit');
                 if (!$queque->insert('import', $info, -1, $option)) {
                     $this->setError(sprintf('queque: %s', $queque->lastError()));
                     return self::FAIL;
                 }
                 $result[] = $queque->lastId();
             }
         }
         $this->result = implode(',', $result);
     } catch (\Exception $e) {
         $this->setError($e->getMessage());
         return self::FAIL;
     }
     return self::SUCC;
 }
Beispiel #6
0
 /**
  * 单机装入
  *
  * @access private
  * @return Mixture
  */
 private function onehost($host, $fname)
 {
     self::metadata($flush);
     if (!isset(self::$hosts[$host])) {
         return;
     }
     $table = Table::instance($this->option('table'));
     $mysql = Server::instance(self::$hosts[$host]['name'])->getlink();
     $this->pools[$host] = array('server' => self::$hosts[$host]['name'], 'handle' => null, 'commmit' => array(), 'rollback' => array());
     list($dbname, $tbname) = explode('.', $this->option('bucket'), 2);
     $querys = array(sprintf('CREATE DATABASE IF NOT EXISTS %s', $dbname), sprintf('CREATE TABLE IF NOT EXISTS %s (%s) ENGINE=MyISAM DEFAULT CHARSET=UTF8', $this->option('bucket'), $table->sqlcreate()));
     if ($this->option('replace', $table->get('load_type', 0))) {
         array_unshift($querys, sprintf('DROP TABLE IF EXISTS %s', $this->option('bucket')));
         $this->pools[$host]['commmit'] = array();
         $this->pools[$host]['rollback'] = array(sprintf('DROP TABLE IF EXISTS %s', $this->option('bucket')));
     } else {
         $maxid = (int) $mysql->getOne($mysql->query(sprintf('SELECT MAX(%s) FROM %s', $table->autokid(), $this->option('bucket'))));
         if ($maxid) {
             $this->pools[$host]['rollback'] = array(sprintf('DELETE FROM %s WHERE %s > %u', $this->option('bucket'), $table->autokid(), $maxid));
         } else {
             $this->pools[$host]['rollback'] = array('DROP TABLE IF EXISTS %s', $this->option('bucket'));
         }
     }
     foreach ($querys as $sql) {
         if (false === $mysql->query($sql)) {
             $this->setError($mysql->lastError());
             return false;
         }
     }
     $import = $table->get('sql_import');
     if (empty($import)) {
         $import = self::IMPORTSQL;
     }
     $this->pools[$host]['handle'] = $mysql->async(strtr($import, array('{FILE}' => $fname, '{TABLE}' => $this->option('bucket'))));
 }