use of com.actiontech.dble.net.mysql.RowDataPacket in project dble by actiontech.
the class SortedResultDiskBuffer method addRows.
@Override
public final int addRows(List<RowDataPacket> rows) {
/**
* we should make rows sorted first, then write them into file
*/
if (logger.isDebugEnabled()) {
logger.debug(" convert list to array start:" + TimeUtil.currentTimeMillis());
}
RowDataPacket[] rowArray = new RowDataPacket[rows.size()];
rows.toArray(rowArray);
final long start = file.getFilePointer();
for (RowDataPacket row : rowArray) {
byte[] b = row.toBytes();
writeBuffer = writeToBuffer(b, writeBuffer);
}
// help for gc
rowArray = null;
writeBuffer.flip();
file.write(writeBuffer);
writeBuffer.clear();
/* make a new tape */
ResultDiskTape tape = makeResultDiskTape();
tape.start = start;
tape.filePos = start;
tape.end = file.getFilePointer();
tapes.add(tape);
rowCount += rows.size();
if (logger.isDebugEnabled()) {
logger.debug("write rows to disk end:" + TimeUtil.currentTimeMillis());
}
return rowCount;
}
use of com.actiontech.dble.net.mysql.RowDataPacket in project dble by actiontech.
the class SortedResultDiskBuffer method next.
@Override
public RowDataPacket next() {
if (heap.isEmpty())
return null;
TapeItem tapeItem = heap.poll();
RowDataPacket newRow = tapeItem.tape.nextRow();
if (newRow != null) {
heap.add(new TapeItem(newRow, tapeItem.tape));
}
return tapeItem.row;
}
use of com.actiontech.dble.net.mysql.RowDataPacket in project dble by actiontech.
the class ShowServerLog method showLogSum.
private static PackageBufINf showLogSum(ManagerConnection c, ByteBuffer buffer, byte packetId) {
PackageBufINf bufINf = new PackageBufINf();
File[] logFiles = new File(SystemConfig.getHomePath(), "logs").listFiles();
StringBuilder fileNames = new StringBuilder();
if (logFiles != null) {
for (File f : logFiles) {
if (f.isFile()) {
fileNames.append(" ");
fileNames.append(f.getName());
}
}
}
File file = getLogFile(DEFAULT_LOGFILE);
BufferedReader br = null;
int totalLines = 0;
CircularArrayList<String> queue = new CircularArrayList<>(50);
try {
br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
totalLines++;
if (queue.size() == queue.capacity()) {
queue.remove(0);
}
queue.add(line);
}
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode("files in log dir:" + totalLines + fileNames, c.getCharset().getResults()));
row.setPacketId(++packetId);
buffer = row.write(buffer, c, true);
row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode("Total lines " + totalLines + " ,tail " + queue.size() + " line is following:", c.getCharset().getResults()));
row.setPacketId(++packetId);
buffer = row.write(buffer, c, true);
int size = queue.size() - 1;
for (int i = size; i >= 0; i--) {
String data = queue.get(i);
row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode(data, c.getCharset().getResults()));
row.setPacketId(++packetId);
buffer = row.write(buffer, c, true);
}
bufINf.setBuffer(buffer);
bufINf.setPacketId(packetId);
return bufINf;
} catch (Exception e) {
LOGGER.info("showLogSumError", e);
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode(e.toString(), c.getCharset().getResults()));
row.setPacketId(++packetId);
buffer = row.write(buffer, c, true);
bufINf.setBuffer(buffer);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
LOGGER.info("showLogSumError", e);
}
}
}
bufINf.setPacketId(packetId);
return bufINf;
}
use of com.actiontech.dble.net.mysql.RowDataPacket in project dble by actiontech.
the class ShowServerLog method showLogRange.
public static PackageBufINf showLogRange(ManagerConnection c, ByteBuffer buffer, byte packetId, String key, String regex, int start, int end, String logFile) {
PackageBufINf bufINf = new PackageBufINf();
Pattern pattern = null;
if (regex != null) {
pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
}
if (key != null) {
key = key.toLowerCase();
}
File file = getLogFile(logFile);
BufferedReader br = null;
int curLine = 0;
try {
br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
curLine++;
if (curLine >= start && curLine <= end && ((pattern != null && pattern.matcher(line).find()) || (pattern == null && key == null) || (key != null && line.toLowerCase().contains(key)))) {
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode(curLine + "->" + line, c.getCharset().getResults()));
row.setPacketId(++packetId);
buffer = row.write(buffer, c, true);
}
}
bufINf.setBuffer(buffer);
bufINf.setPacketId(packetId);
return bufINf;
} catch (Exception e) {
LOGGER.info("showLogRangeError", e);
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode(e.toString(), c.getCharset().getResults()));
row.setPacketId(++packetId);
buffer = row.write(buffer, c, true);
bufINf.setBuffer(buffer);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
LOGGER.info("showLogRangeError", e);
}
}
}
bufINf.setPacketId(packetId);
return bufINf;
}
use of com.actiontech.dble.net.mysql.RowDataPacket in project dble by actiontech.
the class ShowSQL method execute.
public static void execute(ManagerConnection c, boolean isClear) {
ByteBuffer buffer = c.allocate();
// write header
buffer = HEADER.write(buffer, c, true);
// write fields
for (FieldPacket field : FIELDS) {
buffer = field.write(buffer, c, true);
}
// write eof
buffer = EOF.write(buffer, c, true);
// write rows
byte packetId = EOF.getPacketId();
Map<String, UserStat> statMap = UserStatAnalyzer.getInstance().getUserStatMap();
for (UserStat userStat : statMap.values()) {
String user = userStat.getUser();
List<UserSqlLastStat.SqlLast> queries = userStat.getSqlLastStat().getQueries();
int i = 1;
for (UserSqlLastStat.SqlLast sqlLast : queries) {
if (sqlLast != null) {
RowDataPacket row = getRow(user, sqlLast, i, c.getCharset().getResults());
row.setPacketId(++packetId);
i++;
buffer = row.write(buffer, c, true);
}
}
if (isClear) {
userStat.getSqlLastStat().clear();
}
}
// write last eof
EOFPacket lastEof = new EOFPacket();
lastEof.setPacketId(++packetId);
buffer = lastEof.write(buffer, c, true);
// write buffer
c.write(buffer);
}
Aggregations