github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/model/codec/v1/convert.go (about) 1 // Copyright 2021 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package v1 15 16 import ( 17 "bytes" 18 19 timodel "github.com/pingcap/tidb/pkg/parser/model" 20 "github.com/pingcap/tiflow/cdc/model" 21 ) 22 23 // PreMarshal should be called before marshal a RedoLog. 24 func PreMarshal(r *RedoLog) { 25 if r.RedoRow != nil && r.RedoRow.Row != nil { 26 adjustRowPreMarshal(r.RedoRow) 27 } 28 if r.RedoDDL != nil && r.RedoDDL.DDL != nil { 29 adjustDDLPreMarshal(r.RedoDDL) 30 } 31 } 32 33 func adjustRowPreMarshal(redoLog *RedoRowChangedEvent) { 34 row := redoLog.Row 35 for _, column := range row.Columns { 36 var redoColumn *RedoColumn 37 if column != nil { 38 // workaround msgp issue(Decode replaces empty slices with nil https://github.com/tinylib/msgp/issues/247) 39 // if []byte("") send with RowChangedEvent after UnmarshalMsg, 40 // the value will become nil, which is unexpected. 41 switch v := column.Value.(type) { 42 case []byte: 43 if bytes.Equal(v, []byte("")) { 44 column.Value = "" 45 } 46 } 47 redoColumn = &RedoColumn{Column: column, Flag: uint64(column.Flag)} 48 } 49 redoLog.Columns = append(redoLog.Columns, redoColumn) 50 } 51 for _, column := range row.PreColumns { 52 var redoColumn *RedoColumn 53 if column != nil { 54 switch v := column.Value.(type) { 55 case []byte: 56 if bytes.Equal(v, []byte("")) { 57 column.Value = "" 58 } 59 } 60 redoColumn = &RedoColumn{Column: column, Flag: uint64(column.Flag)} 61 } 62 redoLog.PreColumns = append(redoLog.PreColumns, redoColumn) 63 } 64 } 65 66 func adjustDDLPreMarshal(redoDDL *RedoDDLEvent) { 67 redoDDL.Type = byte(redoDDL.DDL.Type) 68 } 69 70 // PostUnmarshal should be called after unmarshal a RedoLog. 71 func PostUnmarshal(r *RedoLog) { 72 if r.RedoRow != nil && r.RedoRow.Row != nil { 73 adjustRowPostUnmarshal(r.RedoRow) 74 } 75 if r.RedoDDL != nil && r.RedoDDL.DDL != nil { 76 adjustDDLPostUnmarshal(r.RedoDDL) 77 } 78 } 79 80 func adjustRowPostUnmarshal(redoLog *RedoRowChangedEvent) { 81 row := redoLog.Row 82 row.Columns = make([]*Column, 0, len(redoLog.Columns)) 83 row.PreColumns = make([]*Column, 0, len(redoLog.PreColumns)) 84 for _, column := range redoLog.PreColumns { 85 if column == nil { 86 row.PreColumns = append(row.PreColumns, nil) 87 continue 88 } 89 column.Column.Flag = model.ColumnFlagType(column.Flag) 90 row.PreColumns = append(row.PreColumns, column.Column) 91 } 92 for _, column := range redoLog.Columns { 93 if column == nil { 94 row.Columns = append(row.Columns, nil) 95 continue 96 } 97 column.Column.Flag = model.ColumnFlagType(column.Flag) 98 row.Columns = append(row.Columns, column.Column) 99 } 100 } 101 102 func adjustDDLPostUnmarshal(redoDDL *RedoDDLEvent) { 103 redoDDL.DDL.Type = timodel.ActionType(redoDDL.Type) 104 }