Skip to content

Commit 10f563e

Browse files
committed
simplify code
1 parent 6bf5194 commit 10f563e

2 files changed

Lines changed: 28 additions & 19 deletions

File tree

‎pkg/sink/codec/canal/canal_json_decoder.go‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,17 @@ func (d *decoder) queryTableInfo(
564564
schemaName := *msg.getSchema()
565565
tableName := *msg.getTable()
566566

567-
version, tableInfo := d.cache.ReadAt(schemaName, tableName, msg.getCommitTs())
568-
if tableInfo != nil {
569-
return tableInfo
567+
// A message rebuilt from a snapshot describes its own layout: the snapshot
568+
// reports enum/set as labels, while the shared layout holds the wire types.
569+
var version uint64
570+
if cacheable {
571+
var tableInfo *commonType.TableInfo
572+
version, tableInfo = d.cache.ReadAt(schemaName, tableName, msg.getCommitTs())
573+
if tableInfo != nil {
574+
return tableInfo
575+
}
570576
}
571-
tableInfo = newTableInfo(d.cache, msg)
577+
tableInfo := newTableInfo(d.cache, msg)
572578
d.cache.AddBlockTableID(schemaName, tableName, tableInfo.TableName.TableID)
573579
if cacheable && isCacheableCanalTableInfo(msg) {
574580
d.cache.Write(schemaName, tableName, version, tableInfo)

‎pkg/sink/codec/canal/canal_json_test.go‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/pingcap/tidb/pkg/kv"
3232
"github.com/pingcap/tidb/pkg/meta/model"
3333
"github.com/pingcap/tidb/pkg/parser/ast"
34+
"github.com/pingcap/tidb/pkg/parser/mysql"
3435
"github.com/pingcap/tidb/pkg/tablecodec"
3536
"github.com/pingcap/tidb/pkg/util/chunk"
3637
"github.com/stretchr/testify/require"
@@ -1609,26 +1610,28 @@ func TestColdDelete(t *testing.T) {
16091610
// columns as varchar for the snapshot rows, so caching that layout would decode
16101611
// every later row of the table with the wrong column types.
16111612
func TestSnapshotLayoutNotCached(t *testing.T) {
1612-
dec := &decoder{cache: common.NewTableInfoProvider()}
1613-
msg := &canalJSONMessageWithTiDBExtension{
1614-
JSONMessage: &JSONMessage{
1615-
Schema: "test",
1616-
Table: "t1",
1617-
PKNames: []string{"id"},
1618-
MySQLType: map[string]string{
1619-
"id": "int",
1620-
"val": "varchar(32)",
1613+
newMsg := func(mysqlType map[string]string) *canalJSONMessageWithTiDBExtension {
1614+
return &canalJSONMessageWithTiDBExtension{
1615+
JSONMessage: &JSONMessage{
1616+
Schema: "test",
1617+
Table: "t1",
1618+
PKNames: []string{"id"},
1619+
MySQLType: mysqlType,
16211620
},
1622-
},
1623-
Extensions: &tidbExtension{CommitTs: 100},
1621+
Extensions: &tidbExtension{CommitTs: 100},
1622+
}
16241623
}
1624+
dec := &decoder{cache: common.NewTableInfoProvider()}
1625+
shared := dec.queryTableInfo(newMsg(map[string]string{"id": "int", "val": "enum('a','b')"}), true)
16251626

1626-
built := dec.queryTableInfo(msg, false)
1627-
require.NotNil(t, built)
1627+
built := dec.queryTableInfo(newMsg(map[string]string{"id": "int", "val": "varchar(32)"}), false)
1628+
require.NotSame(t, shared, built)
1629+
require.Equal(t, mysql.TypeEnum, shared.GetColumns()[1].FieldType.GetType())
1630+
require.Equal(t, mysql.TypeVarchar, built.GetColumns()[1].FieldType.GetType())
16281631
_, cached := dec.cache.ReadAt("test", "t1", 101)
1629-
require.Nil(t, cached)
1632+
require.Same(t, shared, cached)
16301633

1631-
cachedInfo := dec.queryTableInfo(msg, true)
1634+
cachedInfo := dec.queryTableInfo(newMsg(map[string]string{"id": "int", "val": "varchar(32)"}), true)
16321635
_, cached = dec.cache.ReadAt("test", "t1", 101)
16331636
require.Same(t, cachedInfo, cached)
16341637
}

0 commit comments

Comments
 (0)