Java Code Examples for com.taobao.tddl.dbsync.binlog.LogBuffer#limit()

The following examples show how to use com.taobao.tddl.dbsync.binlog.LogBuffer#limit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RotateLogEvent.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new <code>Rotate_log_event</code> object read normally from
 * log.
 * 
 * @throws MySQLExtractException
 */
public RotateLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
    super(header);

    final int headerSize = descriptionEvent.commonHeaderLen;
    final int postHeaderLen = descriptionEvent.postHeaderLen[ROTATE_EVENT - 1];

    buffer.position(headerSize + R_POS_OFFSET);
    position = (postHeaderLen != 0) ? buffer.getLong64() : 4; // !uint8korr(buf
                                                              // +
                                                              // R_POS_OFFSET)

    final int filenameOffset = headerSize + postHeaderLen;
    int filenameLen = buffer.limit() - filenameOffset;
    if (filenameLen > FN_REFLEN - 1) filenameLen = FN_REFLEN - 1;
    buffer.position(filenameOffset);

    filename = buffer.getFixString(filenameLen);
}
 
Example 2
Source File: RotateLogEvent.java    From canal with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new <code>Rotate_log_event</code> object read normally from
 * log.
 * 
 * @throws MySQLExtractException
 */
public RotateLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
    super(header);

    final int headerSize = descriptionEvent.commonHeaderLen;
    final int postHeaderLen = descriptionEvent.postHeaderLen[ROTATE_EVENT - 1];

    buffer.position(headerSize + R_POS_OFFSET);
    position = (postHeaderLen != 0) ? buffer.getLong64() : 4; // !uint8korr(buf
                                                              // +
                                                              // R_POS_OFFSET)

    final int filenameOffset = headerSize + postHeaderLen;
    int filenameLen = buffer.limit() - filenameOffset;
    if (filenameLen > FN_REFLEN - 1) filenameLen = FN_REFLEN - 1;
    buffer.position(filenameOffset);

    filename = buffer.getFixString(filenameLen);
}
 
Example 3
Source File: CreateFileLogEvent.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
public CreateFileLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
    super(header, buffer, descriptionEvent);

    final int headerLen = descriptionEvent.commonHeaderLen;
    final int loadHeaderLen = descriptionEvent.postHeaderLen[LOAD_EVENT - 1];
    final int createFileHeaderLen = descriptionEvent.postHeaderLen[CREATE_FILE_EVENT - 1];

    copyLogEvent(buffer,
        ((header.type == LOAD_EVENT) ? (loadHeaderLen + headerLen) : (headerLen + loadHeaderLen + createFileHeaderLen)),
        descriptionEvent);

    if (descriptionEvent.binlogVersion != 1) {
        fileId = buffer.getUint32(headerLen + loadHeaderLen + CF_FILE_ID_OFFSET);
        /*
         * Note that it's ok to use get_data_size() below, because it is
         * computed with values we have already read from this event
         * (because we called copy_log_event()); we are not using slave's
         * format info to decode master's format, we are really using
         * master's format info. Anyway, both formats should be identical
         * (except the common_header_len) as these Load events are not
         * changed between 4.0 and 5.0 (as logging of LOAD DATA INFILE does
         * not use Load_log_event in 5.0).
         */
        blockLen = buffer.limit() - buffer.position();
        blockBuf = buffer.duplicate(blockLen);
    } else {
        initedFromOld = true;
    }
}
 
Example 4
Source File: AppendBlockLogEvent.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
public AppendBlockLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
    super(header);

    final int commonHeaderLen = descriptionEvent.commonHeaderLen;
    final int postHeaderLen = descriptionEvent.postHeaderLen[header.type - 1];
    final int totalHeaderLen = commonHeaderLen + postHeaderLen;

    buffer.position(commonHeaderLen + AB_FILE_ID_OFFSET);
    fileId = buffer.getUint32();

    buffer.position(postHeaderLen);
    blockLen = buffer.limit() - totalHeaderLen;
    blockBuf = buffer.duplicate(blockLen);
}
 
Example 5
Source File: HeartbeatLogEvent.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
public HeartbeatLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
    super(header);

    final int commonHeaderLen = descriptionEvent.commonHeaderLen;
    identLen = buffer.limit() - commonHeaderLen;
    if (identLen > FN_REFLEN - 1) {
        identLen = FN_REFLEN - 1;
    }

    logIdent = buffer.getFullString(commonHeaderLen, identLen, LogBuffer.ISO_8859_1);
}
 
Example 6
Source File: FormatDescriptionLogEvent.java    From canal with Apache License 2.0 5 votes vote down vote up
public FormatDescriptionLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent)
                                                                                                                throws IOException{
    /* Start_log_event_v3 */
    super(header, buffer, descriptionEvent);

    buffer.position(LOG_EVENT_MINIMAL_HEADER_LEN + ST_COMMON_HEADER_LEN_OFFSET);
    commonHeaderLen = buffer.getUint8();
    if (commonHeaderLen < OLD_HEADER_LEN) /* sanity check */
    {
        throw new IOException("Format Description event header length is too short");
    }

    numberOfEventTypes = buffer.limit() - (LOG_EVENT_MINIMAL_HEADER_LEN + ST_COMMON_HEADER_LEN_OFFSET + 1);

    // buffer.position(LOG_EVENT_MINIMAL_HEADER_LEN
    // + ST_COMMON_HEADER_LEN_OFFSET + 1);
    postHeaderLen = new short[numberOfEventTypes];
    for (int i = 0; i < numberOfEventTypes; i++) {
        postHeaderLen[i] = (short) buffer.getUint8();
    }

    calcServerVersionSplit();
    long calc = getVersionProduct();
    if (calc >= checksumVersionProduct) {
        /*
         * the last bytes are the checksum alg desc and value (or value's
         * room)
         */
        numberOfEventTypes -= BINLOG_CHECKSUM_ALG_DESC_LEN;
    }

    if (logger.isInfoEnabled()) logger.info("common_header_len= " + commonHeaderLen + ", number_of_event_types= "
                                            + numberOfEventTypes);
}
 
Example 7
Source File: RowsQueryLogEvent.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
public RowsQueryLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
    super(header, buffer, descriptionEvent);

    final int commonHeaderLen = descriptionEvent.commonHeaderLen;
    final int postHeaderLen = descriptionEvent.postHeaderLen[header.type - 1];

    /*
     * m_rows_query length is stored using only one byte, but that length is
     * ignored and the complete query is read.m_rows_query长度只使用一个字节存储,但是这个长度是
     *被忽略,然后读取完整的查询。
     */
    int offset = commonHeaderLen + postHeaderLen + 1;
    int len = buffer.limit() - offset;
    rowsQuery = buffer.getFullString(offset, len, LogBuffer.ISO_8859_1);
}
 
Example 8
Source File: AnnotateRowsEvent.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
public AnnotateRowsEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
    super(header, buffer, descriptionEvent);

    final int commonHeaderLen = descriptionEvent.getCommonHeaderLen();
    final int postHeaderLen = descriptionEvent.getPostHeaderLen()[header.getType() - 1];

    int offset = commonHeaderLen + postHeaderLen;
    int len = buffer.limit() - offset;
    rowsQuery = buffer.getFullString(offset, len, LogBuffer.ISO_8859_1);
}
 
Example 9
Source File: FormatDescriptionLogEvent.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
public FormatDescriptionLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent)
                                                                                                                throws IOException{
    /* Start_log_event_v3 */
    super(header, buffer, descriptionEvent);

    buffer.position(LOG_EVENT_MINIMAL_HEADER_LEN + ST_COMMON_HEADER_LEN_OFFSET);
    commonHeaderLen = buffer.getUint8();
    if (commonHeaderLen < OLD_HEADER_LEN) /* sanity check */
    {
        throw new IOException("Format Description event header length is too short");
    }

    numberOfEventTypes = buffer.limit() - (LOG_EVENT_MINIMAL_HEADER_LEN + ST_COMMON_HEADER_LEN_OFFSET + 1);

    // buffer.position(LOG_EVENT_MINIMAL_HEADER_LEN
    // + ST_COMMON_HEADER_LEN_OFFSET + 1);
    postHeaderLen = new short[numberOfEventTypes];
    for (int i = 0; i < numberOfEventTypes; i++) {
        postHeaderLen[i] = (short) buffer.getUint8();
    }

    calcServerVersionSplit();
    long calc = getVersionProduct();
    if (calc >= checksumVersionProduct) {
        /*
         * the last bytes are the checksum alg desc and value (or value's
         * room)
         */
        numberOfEventTypes -= BINLOG_CHECKSUM_ALG_DESC_LEN;
    }

    if (logger.isInfoEnabled()) logger.info("common_header_len= " + commonHeaderLen + ", number_of_event_types= "
                                            + numberOfEventTypes);
}
 
Example 10
Source File: AnnotateRowsEvent.java    From canal with Apache License 2.0 5 votes vote down vote up
public AnnotateRowsEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
    super(header, buffer, descriptionEvent);

    final int commonHeaderLen = descriptionEvent.getCommonHeaderLen();
    final int postHeaderLen = descriptionEvent.getPostHeaderLen()[header.getType() - 1];

    int offset = commonHeaderLen + postHeaderLen;
    int len = buffer.limit() - offset;
    rowsQuery = buffer.getFullString(offset, len, LogBuffer.ISO_8859_1);
}
 
Example 11
Source File: CreateFileLogEvent.java    From canal with Apache License 2.0 5 votes vote down vote up
public CreateFileLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
    super(header, buffer, descriptionEvent);

    final int headerLen = descriptionEvent.commonHeaderLen;
    final int loadHeaderLen = descriptionEvent.postHeaderLen[LOAD_EVENT - 1];
    final int createFileHeaderLen = descriptionEvent.postHeaderLen[CREATE_FILE_EVENT - 1];

    copyLogEvent(buffer,
        ((header.type == LOAD_EVENT) ? (loadHeaderLen + headerLen) : (headerLen + loadHeaderLen + createFileHeaderLen)),
        descriptionEvent);

    if (descriptionEvent.binlogVersion != 1) {
        fileId = buffer.getUint32(headerLen + loadHeaderLen + CF_FILE_ID_OFFSET);
        /*
         * Note that it's ok to use get_data_size() below, because it is
         * computed with values we have already read from this event
         * (because we called copy_log_event()); we are not using slave's
         * format info to decode master's format, we are really using
         * master's format info. Anyway, both formats should be identical
         * (except the common_header_len) as these Load events are not
         * changed between 4.0 and 5.0 (as logging of LOAD DATA INFILE does
         * not use Load_log_event in 5.0).
         */
        blockLen = buffer.limit() - buffer.position();
        blockBuf = buffer.duplicate(blockLen);
    } else {
        initedFromOld = true;
    }
}
 
Example 12
Source File: RowsQueryLogEvent.java    From canal with Apache License 2.0 5 votes vote down vote up
public RowsQueryLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
    super(header, buffer, descriptionEvent);

    final int commonHeaderLen = descriptionEvent.commonHeaderLen;
    final int postHeaderLen = descriptionEvent.postHeaderLen[header.type - 1];

    /*
     * m_rows_query length is stored using only one byte, but that length is
     * ignored and the complete query is read.
     */
    int offset = commonHeaderLen + postHeaderLen + 1;
    int len = buffer.limit() - offset;
    rowsQuery = buffer.getFullString(offset, len, LogBuffer.ISO_8859_1);
}
 
Example 13
Source File: AppendBlockLogEvent.java    From canal with Apache License 2.0 5 votes vote down vote up
public AppendBlockLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
    super(header);

    final int commonHeaderLen = descriptionEvent.commonHeaderLen;
    final int postHeaderLen = descriptionEvent.postHeaderLen[header.type - 1];
    final int totalHeaderLen = commonHeaderLen + postHeaderLen;

    buffer.position(commonHeaderLen + AB_FILE_ID_OFFSET);
    fileId = buffer.getUint32();

    buffer.position(postHeaderLen);
    blockLen = buffer.limit() - totalHeaderLen;
    blockBuf = buffer.duplicate(blockLen);
}
 
Example 14
Source File: HeartbeatLogEvent.java    From canal with Apache License 2.0 5 votes vote down vote up
public HeartbeatLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
    super(header);

    final int commonHeaderLen = descriptionEvent.commonHeaderLen;
    identLen = buffer.limit() - commonHeaderLen;
    if (identLen > FN_REFLEN - 1) {
        identLen = FN_REFLEN - 1;
    }

    logIdent = buffer.getFullString(commonHeaderLen, identLen, LogBuffer.ISO_8859_1);
}
 
Example 15
Source File: RowsLogEvent.java    From canal with Apache License 2.0 4 votes vote down vote up
public RowsLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent, boolean partial){
    super(header);

    final int commonHeaderLen = descriptionEvent.commonHeaderLen;
    final int postHeaderLen = descriptionEvent.postHeaderLen[header.type - 1];
    int headerLen = 0;
    buffer.position(commonHeaderLen + RW_MAPID_OFFSET);
    if (postHeaderLen == 6) {
        /*
         * Master is of an intermediate source tree before 5.1.4. Id is 4
         * bytes
         */
        tableId = buffer.getUint32();
    } else {
        tableId = buffer.getUlong48(); // RW_FLAGS_OFFSET
    }
    flags = buffer.getUint16();

    if (postHeaderLen == FormatDescriptionLogEvent.ROWS_HEADER_LEN_V2) {
        headerLen = buffer.getUint16();
        headerLen -= 2;
        int start = buffer.position();
        int end = start + headerLen;
        for (int i = start; i < end;) {
            switch (buffer.getUint8(i++)) {
                case RW_V_EXTRAINFO_TAG:
                    // int infoLen = buffer.getUint8();
                    buffer.position(i + EXTRA_ROW_INFO_LEN_OFFSET);
                    int checkLen = buffer.getUint8(); // EXTRA_ROW_INFO_LEN_OFFSET
                    int val = checkLen - EXTRA_ROW_INFO_HDR_BYTES;
                    assert (buffer.getUint8() == val); // EXTRA_ROW_INFO_FORMAT_OFFSET
                    for (int j = 0; j < val; j++) {
                        assert (buffer.getUint8() == val); // EXTRA_ROW_INFO_HDR_BYTES
                                                           // + i
                    }
                    break;
                default:
                    i = end;
                    break;
            }
        }
    }

    buffer.position(commonHeaderLen + postHeaderLen + headerLen);
    columnLen = (int) buffer.getPackedLong();
    this.partial = partial;
    columns = buffer.getBitmap(columnLen);

    if (header.type == UPDATE_ROWS_EVENT_V1 || header.type == UPDATE_ROWS_EVENT
        || header.type == PARTIAL_UPDATE_ROWS_EVENT) {
        changeColumns = buffer.getBitmap(columnLen);
    } else {
        changeColumns = columns;
    }

    // XXX: Don't handle buffer in another thread.
    int dataSize = buffer.limit() - buffer.position();
    rowsBuf = buffer.duplicate(dataSize);
}
 
Example 16
Source File: LoadLogEvent.java    From canal with Apache License 2.0 4 votes vote down vote up
/**
 * @see mysql-5.1.60/sql/log_event.cc - Load_log_event::copy_log_event
 */
protected final void copyLogEvent(LogBuffer buffer, final int bodyOffset, FormatDescriptionLogEvent descriptionEvent) {
    /* this is the beginning of the post-header */
    buffer.position(descriptionEvent.commonHeaderLen + L_EXEC_TIME_OFFSET);

    execTime = buffer.getUint32(); // L_EXEC_TIME_OFFSET
    skipLines = (int) buffer.getUint32(); // L_SKIP_LINES_OFFSET
    final int tableNameLen = buffer.getUint8(); // L_TBL_LEN_OFFSET
    final int dbLen = buffer.getUint8(); // L_DB_LEN_OFFSET
    numFields = (int) buffer.getUint32(); // L_NUM_FIELDS_OFFSET

    buffer.position(bodyOffset);
    /*
     * Sql_ex.init() on success returns the pointer to the first byte after
     * the sql_ex structure, which is the start of field lengths array.
     */
    if (header.type != LOAD_EVENT /* use_new_format */) {
        /*
         * The code below assumes that buf will not disappear from under our
         * feet during the lifetime of the event. This assumption holds true
         * in the slave thread if the log is in new format, but is not the
         * case when we have old format because we will be reusing net
         * buffer to read the actual file before we write out the
         * Create_file event.
         */
        fieldTerm = buffer.getString();
        enclosed = buffer.getString();
        lineTerm = buffer.getString();
        lineStart = buffer.getString();
        escaped = buffer.getString();
        optFlags = buffer.getInt8();
        emptyFlags = 0;
    } else {
        fieldTerm = buffer.getFixString(1);
        enclosed = buffer.getFixString(1);
        lineTerm = buffer.getFixString(1);
        lineStart = buffer.getFixString(1);
        escaped = buffer.getFixString(1);
        optFlags = buffer.getUint8();
        emptyFlags = buffer.getUint8();

        if ((emptyFlags & FIELD_TERM_EMPTY) != 0) fieldTerm = null;
        if ((emptyFlags & ENCLOSED_EMPTY) != 0) enclosed = null;
        if ((emptyFlags & LINE_TERM_EMPTY) != 0) lineTerm = null;
        if ((emptyFlags & LINE_START_EMPTY) != 0) lineStart = null;
        if ((emptyFlags & ESCAPED_EMPTY) != 0) escaped = null;
    }

    final int fieldLenPos = buffer.position();
    buffer.forward(numFields);
    fields = new String[numFields];
    for (int i = 0; i < numFields; i++) {
        final int fieldLen = buffer.getUint8(fieldLenPos + i);
        fields[i] = buffer.getFixString(fieldLen + 1);
    }

    table = buffer.getFixString(tableNameLen + 1);
    db = buffer.getFixString(dbLen + 1);

    // null termination is accomplished by the caller
    final int from = buffer.position();
    final int end = from + buffer.limit();
    int found = from;
    for (; (found < end) && buffer.getInt8(found) != '\0'; found++)
        /* empty loop */;
    fname = buffer.getString(found);
    buffer.forward(1); // The + 1 is for \0 terminating fname
}
 
Example 17
Source File: UserVarLogEvent.java    From canal with Apache License 2.0 4 votes vote down vote up
public UserVarLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent)
                                                                                                      throws IOException{
    super(header);

    /* The Post-Header is empty. The Variable Data part begins immediately. */
    buffer.position(descriptionEvent.commonHeaderLen + descriptionEvent.postHeaderLen[USER_VAR_EVENT - 1]);
    final int nameLen = (int) buffer.getUint32();
    name = buffer.getFixString(nameLen); // UV_NAME_LEN_SIZE
    isNull = (0 != buffer.getInt8());

    if (isNull) {
        type = STRING_RESULT;
        charsetNumber = 63; /* binary */
        value = null;
    } else {
        type = buffer.getInt8(); // UV_VAL_IS_NULL
        charsetNumber = (int) buffer.getUint32(); // buf + UV_VAL_TYPE_SIZE
        final int valueLen = (int) buffer.getUint32(); // buf +
                                                       // UV_CHARSET_NUMBER_SIZE
        final int limit = buffer.limit(); /* for restore */
        buffer.limit(buffer.position() + valueLen);

        /* @see User_var_log_event::print */
        switch (type) {
            case REAL_RESULT:
                value = Double.valueOf(buffer.getDouble64()); // float8get
                break;
            case INT_RESULT:
                if (valueLen == 8) value = Long.valueOf(buffer.getLong64()); // !uint8korr
                else if (valueLen == 4) value = Long.valueOf(buffer.getUint32());
                else throw new IOException("Error INT_RESULT length: " + valueLen);
                break;
            case DECIMAL_RESULT:
                final int precision = buffer.getInt8();
                final int scale = buffer.getInt8();
                value = buffer.getDecimal(precision, scale); // bin2decimal
                break;
            case STRING_RESULT:
                String charsetName = CharsetConversion.getJavaCharset(charsetNumber);
                value = buffer.getFixString(valueLen, charsetName);
                break;
            case ROW_RESULT:
                // this seems to be banned in MySQL altogether
                throw new IOException("ROW_RESULT is unsupported");
            default:
                value = null;
                break;
        }
        buffer.limit(limit);
    }
}
 
Example 18
Source File: RowsLogEvent.java    From canal-1.1.3 with Apache License 2.0 4 votes vote down vote up
public RowsLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent, boolean partial){
    super(header);

    final int commonHeaderLen = descriptionEvent.commonHeaderLen;
    final int postHeaderLen = descriptionEvent.postHeaderLen[header.type - 1];
    int headerLen = 0;
    buffer.position(commonHeaderLen + RW_MAPID_OFFSET);
    if (postHeaderLen == 6) {
        /*
         * Master is of an intermediate source tree before 5.1.4. Id is 4
         * bytes
         */
        tableId = buffer.getUint32();
    } else {
        tableId = buffer.getUlong48(); // RW_FLAGS_OFFSET
    }
    flags = buffer.getUint16();

    if (postHeaderLen == FormatDescriptionLogEvent.ROWS_HEADER_LEN_V2) {
        headerLen = buffer.getUint16();
        headerLen -= 2;
        int start = buffer.position();
        int end = start + headerLen;
        for (int i = start; i < end;) {
            switch (buffer.getUint8(i++)) {
                case RW_V_EXTRAINFO_TAG:
                    // int infoLen = buffer.getUint8();
                    buffer.position(i + EXTRA_ROW_INFO_LEN_OFFSET);
                    int checkLen = buffer.getUint8(); // EXTRA_ROW_INFO_LEN_OFFSET
                    int val = checkLen - EXTRA_ROW_INFO_HDR_BYTES;
                    assert (buffer.getUint8() == val); // EXTRA_ROW_INFO_FORMAT_OFFSET
                    for (int j = 0; j < val; j++) {
                        assert (buffer.getUint8() == val); // EXTRA_ROW_INFO_HDR_BYTES
                                                           // + i
                    }
                    break;
                default:
                    i = end;
                    break;
            }
        }
    }

    buffer.position(commonHeaderLen + postHeaderLen + headerLen);
    columnLen = (int) buffer.getPackedLong();
    this.partial = partial;
    columns = buffer.getBitmap(columnLen);

    if (header.type == UPDATE_ROWS_EVENT_V1 || header.type == UPDATE_ROWS_EVENT
        || header.type == PARTIAL_UPDATE_ROWS_EVENT) {
        changeColumns = buffer.getBitmap(columnLen);
    } else {
        changeColumns = columns;
    }

    // XXX: Don't handle buffer in another thread.
    int dataSize = buffer.limit() - buffer.position();
    rowsBuf = buffer.duplicate(dataSize);
}
 
Example 19
Source File: LoadLogEvent.java    From canal-1.1.3 with Apache License 2.0 4 votes vote down vote up
/**
 * @see mysql-5.1.60/sql/log_event.cc - Load_log_event::copy_log_event
 */
protected final void copyLogEvent(LogBuffer buffer, final int bodyOffset, FormatDescriptionLogEvent descriptionEvent) {
    /* this is the beginning of the post-header */
    buffer.position(descriptionEvent.commonHeaderLen + L_EXEC_TIME_OFFSET);

    execTime = buffer.getUint32(); // L_EXEC_TIME_OFFSET
    skipLines = (int) buffer.getUint32(); // L_SKIP_LINES_OFFSET
    final int tableNameLen = buffer.getUint8(); // L_TBL_LEN_OFFSET
    final int dbLen = buffer.getUint8(); // L_DB_LEN_OFFSET
    numFields = (int) buffer.getUint32(); // L_NUM_FIELDS_OFFSET

    buffer.position(bodyOffset);
    /*
     * Sql_ex.init() on success returns the pointer to the first byte after
     * the sql_ex structure, which is the start of field lengths array.
     */
    if (header.type != LOAD_EVENT /* use_new_format */) {
        /*
         * The code below assumes that buf will not disappear from under our
         * feet during the lifetime of the event. This assumption holds true
         * in the slave thread if the log is in new format, but is not the
         * case when we have old format because we will be reusing net
         * buffer to read the actual file before we write out the
         * Create_file event.
         */
        fieldTerm = buffer.getString();
        enclosed = buffer.getString();
        lineTerm = buffer.getString();
        lineStart = buffer.getString();
        escaped = buffer.getString();
        optFlags = buffer.getInt8();
        emptyFlags = 0;
    } else {
        fieldTerm = buffer.getFixString(1);
        enclosed = buffer.getFixString(1);
        lineTerm = buffer.getFixString(1);
        lineStart = buffer.getFixString(1);
        escaped = buffer.getFixString(1);
        optFlags = buffer.getUint8();
        emptyFlags = buffer.getUint8();

        if ((emptyFlags & FIELD_TERM_EMPTY) != 0) fieldTerm = null;
        if ((emptyFlags & ENCLOSED_EMPTY) != 0) enclosed = null;
        if ((emptyFlags & LINE_TERM_EMPTY) != 0) lineTerm = null;
        if ((emptyFlags & LINE_START_EMPTY) != 0) lineStart = null;
        if ((emptyFlags & ESCAPED_EMPTY) != 0) escaped = null;
    }

    final int fieldLenPos = buffer.position();
    buffer.forward(numFields);
    fields = new String[numFields];
    for (int i = 0; i < numFields; i++) {
        final int fieldLen = buffer.getUint8(fieldLenPos + i);
        fields[i] = buffer.getFixString(fieldLen + 1);
    }

    table = buffer.getFixString(tableNameLen + 1);
    db = buffer.getFixString(dbLen + 1);

    // null termination is accomplished by the caller
    final int from = buffer.position();
    final int end = from + buffer.limit();
    int found = from;
    for (; (found < end) && buffer.getInt8(found) != '\0'; found++)
        /* empty loop */;
    fname = buffer.getString(found);
    buffer.forward(1); // The + 1 is for \0 terminating fname
}
 
Example 20
Source File: UserVarLogEvent.java    From canal-1.1.3 with Apache License 2.0 4 votes vote down vote up
public UserVarLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent)
                                                                                                      throws IOException{
    super(header);

    /* The Post-Header is empty. The Variable Data part begins immediately. */
    buffer.position(descriptionEvent.commonHeaderLen + descriptionEvent.postHeaderLen[USER_VAR_EVENT - 1]);
    final int nameLen = (int) buffer.getUint32();
    name = buffer.getFixString(nameLen); // UV_NAME_LEN_SIZE
    isNull = (0 != buffer.getInt8());

    if (isNull) {
        type = STRING_RESULT;
        charsetNumber = 63; /* binary */
        value = null;
    } else {
        type = buffer.getInt8(); // UV_VAL_IS_NULL
        charsetNumber = (int) buffer.getUint32(); // buf + UV_VAL_TYPE_SIZE
        final int valueLen = (int) buffer.getUint32(); // buf +
                                                       // UV_CHARSET_NUMBER_SIZE
        final int limit = buffer.limit(); /* for restore */
        buffer.limit(buffer.position() + valueLen);

        /* @see User_var_log_event::print */
        switch (type) {
            case REAL_RESULT:
                value = Double.valueOf(buffer.getDouble64()); // float8get
                break;
            case INT_RESULT:
                if (valueLen == 8) value = Long.valueOf(buffer.getLong64()); // !uint8korr
                else if (valueLen == 4) value = Long.valueOf(buffer.getUint32());
                else throw new IOException("Error INT_RESULT length: " + valueLen);
                break;
            case DECIMAL_RESULT:
                final int precision = buffer.getInt8();
                final int scale = buffer.getInt8();
                value = buffer.getDecimal(precision, scale); // bin2decimal
                break;
            case STRING_RESULT:
                String charsetName = CharsetConversion.getJavaCharset(charsetNumber);
                value = buffer.getFixString(valueLen, charsetName);
                break;
            case ROW_RESULT:
                // this seems to be banned in MySQL altogether
                throw new IOException("ROW_RESULT is unsupported");
            default:
                value = null;
                break;
        }
        buffer.limit(limit);
    }
}