Search in sources :

Example 26 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat-Server by MyCATApache.

the class CompressUtil method decompressMysqlPacket.

/**
 * 解压数据包,同时做分包处理
 *
 * @param data
 * @param decompressUnfinishedDataQueue
 * @return
 */
public static List<byte[]> decompressMysqlPacket(byte[] data, ConcurrentLinkedQueue<byte[]> decompressUnfinishedDataQueue) {
    MySQLMessage msg = new MySQLMessage(data);
    // 包头
    // -----------------------------------------
    // 压缩的包长
    int packetLength = msg.readUB3();
    // 压缩的包号
    byte packetId = msg.read();
    // 压缩前的长度
    int oldLen = msg.readUB3();
    // 未压缩, 直接返回
    if (packetLength == data.length - 4) {
        return Lists.newArrayList(data);
    // 压缩不成功的, 直接返回
    } else if (oldLen == 0) {
        byte[] readBytes = msg.readBytes();
        return splitPack(readBytes, decompressUnfinishedDataQueue);
    // 解压
    } else {
        byte[] de = decompress(data, 7, data.length - 7);
        return splitPack(de, decompressUnfinishedDataQueue);
    }
}
Also used : MySQLMessage(io.mycat.backend.mysql.MySQLMessage)

Example 27 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat-Server by MyCATApache.

the class CompressUtil method splitPack.

/**
 * 分包处理
 *
 * @param in
 * @param decompressUnfinishedDataQueue
 * @return
 */
private static List<byte[]> splitPack(byte[] in, ConcurrentLinkedQueue<byte[]> decompressUnfinishedDataQueue) {
    // 合并
    in = mergeBytes(in, decompressUnfinishedDataQueue);
    List<byte[]> smallPackList = new ArrayList<>();
    MySQLMessage msg = new MySQLMessage(in);
    while (msg.hasRemaining()) {
        int readLength = msg.length() - msg.position();
        int packetLength = 0;
        if (readLength > 3) {
            packetLength = msg.readUB3();
            msg.move(-3);
        }
        if (readLength < packetLength + 4) {
            byte[] packet = msg.readBytes(readLength);
            if (packet.length != 0) {
                decompressUnfinishedDataQueue.add(packet);
            }
        } else {
            byte[] packet = msg.readBytes(packetLength + 4);
            if (packet.length != 0) {
                smallPackList.add(packet);
            }
        }
    }
    return smallPackList;
}
Also used : ArrayList(java.util.ArrayList) MySQLMessage(io.mycat.backend.mysql.MySQLMessage)

Example 28 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat-Server by MyCATApache.

the class CompressUtil method compressMysqlPacket.

/**
 * 压缩数据包
 * @param data
 * @param con
 * @param compressUnfinishedDataQueue
 * @return
 */
private static ByteBuffer compressMysqlPacket(byte[] data, AbstractConnection con, ConcurrentLinkedQueue<byte[]> compressUnfinishedDataQueue) {
    ByteBuffer byteBuf = con.allocate();
    // TODO: 数据量大的时候, 此处是是性能的堵点
    byteBuf = con.checkWriteBuffer(byteBuf, data.length, false);
    MySQLMessage msg = new MySQLMessage(data);
    while (msg.hasRemaining()) {
        // 包体的长度
        int packetLength = 0;
        // 可读的长度
        int readLength = msg.length() - msg.position();
        if (readLength > 3) {
            packetLength = msg.readUB3();
            msg.move(-3);
        }
        // 校验数据包完整性
        if (readLength < packetLength + 4) {
            byte[] packet = msg.readBytes(readLength);
            if (packet.length != 0) {
                // 不完整的包
                compressUnfinishedDataQueue.add(packet);
            }
        } else {
            byte[] packet = msg.readBytes(packetLength + 4);
            if (packet.length != 0) {
                if (packet.length <= NO_COMPRESS_PACKET_LENGTH) {
                    // 压缩长度
                    BufferUtil.writeUB3(byteBuf, packet.length);
                    // 压缩序号
                    byteBuf.put(packet[3]);
                    // 压缩前的长度设置为0
                    BufferUtil.writeUB3(byteBuf, 0);
                    // 包体
                    byteBuf.put(packet);
                } else {
                    // 压缩
                    byte[] compress = compress(packet);
                    BufferUtil.writeUB3(byteBuf, compress.length);
                    byteBuf.put(packet[3]);
                    BufferUtil.writeUB3(byteBuf, packet.length);
                    byteBuf.put(compress);
                }
            }
        }
    }
    return byteBuf;
}
Also used : ByteBuffer(java.nio.ByteBuffer) MySQLMessage(io.mycat.backend.mysql.MySQLMessage)

Example 29 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat_plus by coderczp.

the class EOFPacket method read.

public void read(byte[] data) {
    MySQLMessage mm = new MySQLMessage(data);
    packetLength = mm.readUB3();
    packetId = mm.read();
    fieldCount = mm.read();
    warningCount = mm.readUB2();
    status = mm.readUB2();
}
Also used : MySQLMessage(io.mycat.backend.mysql.MySQLMessage)

Example 30 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat_plus by coderczp.

the class FrontendConnection method stmtPrepare.

public void stmtPrepare(byte[] data) {
    if (prepareHandler != null) {
        // 取得语句
        MySQLMessage mm = new MySQLMessage(data);
        mm.position(5);
        String sql = null;
        try {
            sql = mm.readString(charset);
        } catch (UnsupportedEncodingException e) {
            writeErrMessage(ErrorCode.ER_UNKNOWN_CHARACTER_SET, "Unknown charset '" + charset + "'");
            return;
        }
        if (sql == null || sql.length() == 0) {
            writeErrMessage(ErrorCode.ER_NOT_ALLOWED_COMMAND, "Empty SQL");
            return;
        }
        // 记录SQL
        this.setExecuteSql(sql);
        // 执行预处理
        prepareHandler.prepare(sql);
    } else {
        writeErrMessage(ErrorCode.ER_UNKNOWN_COM_ERROR, "Prepare unsupported!");
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) MySQLMessage(io.mycat.backend.mysql.MySQLMessage)

Aggregations

MySQLMessage (io.mycat.backend.mysql.MySQLMessage)56 Test (org.junit.Test)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ByteBuffer (java.nio.ByteBuffer)4 BindValue (io.mycat.backend.mysql.BindValue)2 BufferHolder (io.mycat.memory.unsafe.row.BufferHolder)2 UnsafeRow (io.mycat.memory.unsafe.row.UnsafeRow)2 UnsafeRowWriter (io.mycat.memory.unsafe.row.UnsafeRowWriter)2 EOFPacket (io.mycat.net.mysql.EOFPacket)2 ServerConnection (io.mycat.server.ServerConnection)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2