Exemplo n.º 1
0
//This builds BinlogResponse for each transaction.
func (blp *Blp) buildTxnResponse() (txnResponseList []*mysqlctl.BinlogResponse, dmlCount int64) {
	var line []byte
	var keyspaceIdStr string
	var keyspaceId key.KeyspaceId

	dmlBuffer := make([]string, 0, 10)

	for _, event := range blp.txnLineBuffer {
		line = event.LogLine
		if bytes.HasPrefix(line, mysqlctl.BINLOG_BEGIN) {
			streamBuf := new(mysqlctl.BinlogResponse)
			streamBuf.BlPosition = event.BlPosition
			streamBuf.SqlType = mysqlctl.BEGIN
			txnResponseList = append(txnResponseList, streamBuf)
			continue
		}
		if bytes.HasPrefix(line, mysqlctl.BINLOG_COMMIT) {
			commitEvent := createCommitEvent(event)
			txnResponseList = append(txnResponseList, commitEvent)
			continue
		}
		sqlType := mysqlctl.GetSqlType(event.firstKw)
		if sqlType == mysqlctl.DML {
			keyspaceIdStr, keyspaceId = parseKeyspaceId(line, mysqlctl.GetDmlType(event.firstKw))
			if keyspaceIdStr == "" {
				continue
			}
			if !blp.keyspaceRange.Contains(keyspaceId) {
				dmlBuffer = dmlBuffer[:0]
				continue
			}
			dmlCount += 1
			//extract keyspace id - match it with client's request,
			//extract index ids.
			dmlBuffer = append(dmlBuffer, string(line))
			dmlEvent := blp.createDmlEvent(event, keyspaceIdStr)
			dmlEvent.Sql = make([]string, len(dmlBuffer))
			dmlLines := copy(dmlEvent.Sql, dmlBuffer)
			if dmlLines < len(dmlBuffer) {
				relog.Warning("The entire dml buffer was not properly copied")
			}
			txnResponseList = append(txnResponseList, dmlEvent)
			dmlBuffer = dmlBuffer[:0]
		} else {
			//add as prefixes to the DML from last DML.
			//start a new dml buffer and keep adding to it.
			dmlBuffer = append(dmlBuffer, string(line))
		}
	}
	return txnResponseList, dmlCount
}
Exemplo n.º 2
0
func (blp *Blp) createDmlEvent(eventBuf *eventBuffer, keyspaceId string) (dmlEvent *mysqlctl.BinlogResponse) {
	//parse keyspace id
	//for inserts check for index comments
	dmlEvent = new(mysqlctl.BinlogResponse)
	dmlEvent.BlPosition = eventBuf.BlPosition
	dmlEvent.SqlType = mysqlctl.GetDmlType(eventBuf.firstKw)
	dmlEvent.KeyspaceId = keyspaceId
	indexType, indexId, userId := parseIndex(eventBuf.LogLine)
	if userId != 0 {
		dmlEvent.UserId = userId
	}
	if indexType != "" {
		dmlEvent.IndexType = indexType
		dmlEvent.IndexId = indexId
	}
	return dmlEvent
}