/** * 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; }
public function test_should_get_last_error_works_fine() { $object = new Fsplit('/i/am/not/exits'); $this->assertEquals(false, $object->split(array(100))); $this->assertContains('No such file named as "/i/am/not/exits"', $object->lastError()); $object = new Fsplit(__FILE__); $this->assertEquals(false, $object->split(array(100), '/created/denied')); $this->assertContains('Directory "/created/denied" created failed', $object->lastError()); $fname = __DIR__ . '/tmp/fsplit_test.txt'; !is_dir(dirname($fname)) && @mkdir(dirname($fname), 0755, true); file_put_contents($fname, 'bbbbbbb'); $object = new Fsplit($fname); $this->assertEquals(false, $object->split(array(100), __DIR__ . '/tmp')); $this->assertContains('Unrecognized text formmat, or line size larger than ' . Fsplit::BUFFER_SIZE, $object->lastError()); }