use of io.mycat.net.mysql.RequestFilePacket in project Mycat-Server by MyCATApache.
the class ServerLoadDataInfileHandler method start.
@Override
public void start(String sql) {
clear();
this.sql = sql;
SQLStatementParser parser = new MycatStatementParser(sql);
statement = (MySqlLoadDataInFileStatement) parser.parseStatement();
fileName = parseFileName(sql);
if (fileName == null) {
serverConnection.writeErrMessage(ErrorCode.ER_FILE_NOT_FOUND, " file name is null !");
clear();
return;
}
schema = MycatServer.getInstance().getConfig().getSchemas().get(serverConnection.getSchema());
tableId2DataNodeCache = (LayerCachePool) MycatServer.getInstance().getCacheService().getCachePool("TableID2DataNodeCache");
tableName = statement.getTableName().getSimpleName().toUpperCase();
tableConfig = schema.getTables().get(tableName);
if (tableConfig.getRule().getRuleAlgorithm() instanceof SlotFunction) {
shoudAddSlot = true;
}
tempPath = SystemConfig.getHomePath() + File.separator + "temp" + File.separator + serverConnection.getId() + File.separator;
tempFile = tempPath + "clientTemp.txt";
tempByteBuffer = new ByteArrayOutputStream();
List<SQLExpr> columns = statement.getColumns();
if (tableConfig != null) {
String pColumn = getPartitionColumn();
if (pColumn != null && columns != null && columns.size() > 0) {
for (int i = 0, columnsSize = columns.size(); i < columnsSize; i++) {
String column = StringUtil.removeBackquote(columns.get(i).toString());
if (pColumn.equalsIgnoreCase(column)) {
partitionColumnIndex = i;
}
if ("_slot".equalsIgnoreCase(column)) {
shoudAddSlot = false;
}
}
}
}
if (shoudAddSlot) {
columns.add(new SQLIdentifierExpr("_slot"));
}
parseLoadDataPram();
if (statement.isLocal()) {
isStartLoadData = true;
//向客户端请求发送文件
ByteBuffer buffer = serverConnection.allocate();
RequestFilePacket filePacket = new RequestFilePacket();
filePacket.fileName = fileName.getBytes();
filePacket.packetId = 1;
filePacket.write(buffer, serverConnection, true);
} else {
if (!new File(fileName).exists()) {
serverConnection.writeErrMessage(ErrorCode.ER_FILE_NOT_FOUND, fileName + " is not found!");
clear();
} else {
parseFileByLine(fileName, loadData.getCharset(), loadData.getLineTerminatedBy());
RouteResultset rrs = buildResultSet(routeResultMap);
if (rrs != null) {
flushDataToFile();
isStartLoadData = false;
serverConnection.getSession2().execute(rrs, ServerParse.LOAD_DATA_INFILE_SQL);
}
}
}
}
Aggregations