Skip to content

Commit

Permalink
optimize: optimize the speed of buildLockKey
Browse files Browse the repository at this point in the history
  • Loading branch information
FinnTew committed Nov 24, 2024
1 parent ad092d5 commit 4d8e1e7
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions pkg/datasource/sql/undo/builder/basic_undo_log_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,35 +272,38 @@ func (b *BasicUndoLogBuilder) buildLockKey(rows driver.Rows, meta types.TableMet

// the string as local key. the local key example(multi pk): "t_user:1_a,2_b"
func (b *BasicUndoLogBuilder) buildLockKey2(records *types.RecordImage, meta types.TableMeta) string {
var (
lockKeys bytes.Buffer
filedSequence int
)
var lockKeys bytes.Buffer
lockKeys.WriteString(meta.TableName)
lockKeys.WriteString(":")

keys := meta.GetPrimaryKeyOnlyName()
keyIndexMap := make(map[string]int, len(keys))

for _, row := range records.Rows {
if filedSequence > 0 {
lockKeys.WriteString(",")
}
pkSplitIndex := 0
for idx, columnName := range keys {
keyIndexMap[columnName] = idx
}

primaryKeyRows := make([][]interface{}, len(records.Rows))

for i, row := range records.Rows {
primaryKeyValues := make([]interface{}, len(keys))
for _, column := range row.Columns {
var hasKeyColumn bool
for _, key := range keys {
if column.ColumnName == key {
hasKeyColumn = true
if pkSplitIndex > 0 {
lockKeys.WriteString("_")
}
lockKeys.WriteString(fmt.Sprintf("%v", column.Value))
pkSplitIndex++
}
if idx, exist := keyIndexMap[column.ColumnName]; exist {
primaryKeyValues[idx] = column.Value
}
if hasKeyColumn {
filedSequence++
}
primaryKeyRows[i] = primaryKeyValues
}

for i, primaryKeyValues := range primaryKeyRows {
if i > 0 {
lockKeys.WriteString(",")
}
for j, pkVal := range primaryKeyValues {
if j > 0 {
lockKeys.WriteString("_")
}
lockKeys.WriteString(fmt.Sprintf("%v", pkVal))
}
}

Expand Down

0 comments on commit 4d8e1e7

Please sign in to comment.