Skip to content

Commit db2776a

Browse files
authored
infoschema: change infoschem v2 btree element from tableItem to *tableItem (#58324)
ref #58321
1 parent 40eb7dd commit db2776a

File tree

3 files changed

+39
-39
lines changed

3 files changed

+39
-39
lines changed

pkg/infoschema/builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -965,8 +965,8 @@ func (b *Builder) createSchemaTablesForDB(di *model.DBInfo, tableFromMeta tableF
965965
tableID: id,
966966
schemaVersion: schemaVersion,
967967
}
968-
b.infoData.byID.Set(item)
969-
b.infoData.byName.Set(item)
968+
b.infoData.byID.Set(&item)
969+
b.infoData.byName.Set(&item)
970970
}
971971
}
972972
b.addDB(schemaVersion, di, schTbls)

pkg/infoschema/infoschema_v2.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ type Data struct {
8585
//
8686
// It means as long as we can find an item in it, the item is available, even through the
8787
// schema version maybe smaller than required.
88-
byName *btree.BTreeG[tableItem]
88+
byName *btree.BTreeG[*tableItem]
8989

9090
// For the TableByID API, sorted by {tableID, schemaVersion} => dbID
9191
// To reload model.TableInfo, we need both table ID and database ID for meta kv API.
9292
// It provides the tableID => databaseID mapping.
9393
// This mapping MUST be synced with byName.
94-
byID *btree.BTreeG[tableItem]
94+
byID *btree.BTreeG[*tableItem]
9595

9696
// For the SchemaByName API, sorted by {dbName, schemaVersion} => model.DBInfo
9797
// Stores the full data in memory.
@@ -186,8 +186,8 @@ type tableCacheKey struct {
186186
// NewData creates an infoschema V2 data struct.
187187
func NewData() *Data {
188188
ret := &Data{
189-
byID: btree.NewBTreeG[tableItem](compareByID),
190-
byName: btree.NewBTreeG[tableItem](compareByName),
189+
byID: btree.NewBTreeG[*tableItem](compareByID),
190+
byName: btree.NewBTreeG[*tableItem](compareByName),
191191
schemaMap: btree.NewBTreeG[schemaItem](compareSchemaItem),
192192
schemaID2Name: btree.NewBTreeG[schemaIDName](compareSchemaByID),
193193
tableCache: newSieve[tableCacheKey, table.Table](1024 * 1024 * size.MB),
@@ -209,8 +209,8 @@ func (isd *Data) SetCacheCapacity(capacity uint64) {
209209
}
210210

211211
func (isd *Data) add(item tableItem, tbl table.Table) {
212-
isd.byID.Set(item)
213-
isd.byName.Set(item)
212+
isd.byID.Set(&item)
213+
isd.byName.Set(&item)
214214
isd.tableCache.Set(tableCacheKey{item.tableID, item.schemaVersion}, tbl)
215215
ti := tbl.Meta()
216216
if pi := ti.GetPartitionInfo(); pi != nil {
@@ -240,8 +240,8 @@ func (isd *Data) addDB(schemaVersion int64, dbInfo *model.DBInfo) {
240240

241241
func (isd *Data) remove(item tableItem) {
242242
item.tomb = true
243-
isd.byID.Set(item)
244-
isd.byName.Set(item)
243+
isd.byID.Set(&item)
244+
isd.byName.Set(&item)
245245
isd.tableInfoResident.Set(tableInfoItem{
246246
dbName: item.dbName,
247247
tableID: item.tableID,
@@ -271,7 +271,7 @@ func (isd *Data) resetBeforeFullLoad(schemaVersion int64) {
271271
resetPID2TIDBeforeFullLoad(isd.pid2tid, schemaVersion)
272272
}
273273

274-
func resetByIDBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) {
274+
func resetByIDBeforeFullLoad(bt *btree.BTreeG[*tableItem], schemaVersion int64) {
275275
pivot, ok := bt.Max()
276276
if !ok {
277277
return
@@ -281,10 +281,10 @@ func resetByIDBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) {
281281
if bt.Len() < batchSize {
282282
batchSize = bt.Len()
283283
}
284-
items := make([]tableItem, 0, batchSize)
284+
items := make([]*tableItem, 0, batchSize)
285285
items = append(items, pivot)
286286
for {
287-
bt.Descend(pivot, func(item tableItem) bool {
287+
bt.Descend(pivot, func(item *tableItem) bool {
288288
if pivot.tableID == item.tableID {
289289
return true // skip MVCC version
290290
}
@@ -296,7 +296,7 @@ func resetByIDBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) {
296296
break
297297
}
298298
for _, item := range items {
299-
bt.Set(tableItem{
299+
bt.Set(&tableItem{
300300
dbName: item.dbName,
301301
dbID: item.dbID,
302302
tableName: item.tableName,
@@ -309,7 +309,7 @@ func resetByIDBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) {
309309
}
310310
}
311311

312-
func resetByNameBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) {
312+
func resetByNameBeforeFullLoad(bt *btree.BTreeG[*tableItem], schemaVersion int64) {
313313
pivot, ok := bt.Max()
314314
if !ok {
315315
return
@@ -319,10 +319,10 @@ func resetByNameBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64)
319319
if bt.Len() < batchSize {
320320
batchSize = bt.Len()
321321
}
322-
items := make([]tableItem, 0, batchSize)
322+
items := make([]*tableItem, 0, batchSize)
323323
items = append(items, pivot)
324324
for {
325-
bt.Descend(pivot, func(item tableItem) bool {
325+
bt.Descend(pivot, func(item *tableItem) bool {
326326
if pivot.dbName == item.dbName && pivot.tableName == item.tableName {
327327
return true // skip MVCC version
328328
}
@@ -334,7 +334,7 @@ func resetByNameBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64)
334334
break
335335
}
336336
for _, item := range items {
337-
bt.Set(tableItem{
337+
bt.Set(&tableItem{
338338
dbName: item.dbName,
339339
dbID: item.dbID,
340340
tableName: item.tableName,
@@ -457,7 +457,7 @@ func resetPID2TIDBeforeFullLoad(bt *btree.BTreeG[partitionItem], schemaVersion i
457457
}
458458
}
459459

460-
func compareByID(a, b tableItem) bool {
460+
func compareByID(a, b *tableItem) bool {
461461
if a.tableID < b.tableID {
462462
return true
463463
}
@@ -468,7 +468,7 @@ func compareByID(a, b tableItem) bool {
468468
return a.schemaVersion < b.schemaVersion
469469
}
470470

471-
func compareByName(a, b tableItem) bool {
471+
func compareByName(a, b *tableItem) bool {
472472
if a.dbName.L < b.dbName.L {
473473
return true
474474
}
@@ -553,12 +553,12 @@ func NewInfoSchemaV2(r autoid.Requirement, factory func() (pools.Resource, error
553553
}
554554
}
555555

556-
func search(bt *btree.BTreeG[tableItem], schemaVersion int64, end tableItem, matchFn func(a, b *tableItem) bool) (tableItem, bool) {
556+
func search(bt *btree.BTreeG[*tableItem], schemaVersion int64, end tableItem, matchFn func(a, b *tableItem) bool) (*tableItem, bool) {
557557
var ok bool
558-
var target tableItem
558+
var target *tableItem
559559
// Iterate through the btree, find the query item whose schema version is the largest one (latest).
560-
bt.Descend(end, func(item tableItem) bool {
561-
if !matchFn(&end, &item) {
560+
bt.Descend(&end, func(item *tableItem) bool {
561+
if !matchFn(&end, item) {
562562
return false
563563
}
564564
if item.schemaVersion > schemaVersion {
@@ -594,7 +594,7 @@ func (is *infoschemaV2) CloneAndUpdateTS(startTS uint64) *infoschemaV2 {
594594
return &tmp
595595
}
596596

597-
func (is *infoschemaV2) searchTableItemByID(tableID int64) (tableItem, bool) {
597+
func (is *infoschemaV2) searchTableItemByID(tableID int64) (*tableItem, bool) {
598598
eq := func(a, b *tableItem) bool { return a.tableID == b.tableID }
599599
return search(
600600
is.byID,
@@ -679,7 +679,7 @@ func (is *infoschemaV2) IterateAllTableItems(visit func(TableItem) bool) {
679679
return
680680
}
681681
var pivot *tableItem
682-
is.byName.Descend(maxv, func(item tableItem) bool {
682+
is.byName.Descend(maxv, func(item *tableItem) bool {
683683
if item.schemaVersion > is.schemaMetaVersion {
684684
// skip MVCC version, those items are not visible to the queried schema version
685685
return true
@@ -688,7 +688,7 @@ func (is *infoschemaV2) IterateAllTableItems(visit func(TableItem) bool) {
688688
// skip MVCC version, this db.table has been visited already
689689
return true
690690
}
691-
pivot = &item
691+
pivot = item
692692
if !item.tomb {
693693
return visit(TableItem{DBName: item.dbName, TableName: item.tableName})
694694
}
@@ -743,10 +743,10 @@ type tableByNameHelper struct {
743743
end tableItem
744744
schemaVersion int64
745745
found bool
746-
res tableItem
746+
res *tableItem
747747
}
748748

749-
func (h *tableByNameHelper) onItem(item tableItem) bool {
749+
func (h *tableByNameHelper) onItem(item *tableItem) bool {
750750
if item.dbName.L != h.end.dbName.L || item.tableName.L != h.end.tableName.L {
751751
h.found = false
752752
return false
@@ -779,7 +779,7 @@ func (is *infoschemaV2) TableByName(ctx context.Context, schema, tbl pmodel.CISt
779779
var h tableByNameHelper
780780
h.end = tableItem{dbName: schema, tableName: tbl, schemaVersion: math.MaxInt64}
781781
h.schemaVersion = is.infoSchema.schemaMetaVersion
782-
is.byName.Descend(h.end, h.onItem)
782+
is.byName.Descend(&h.end, h.onItem)
783783

784784
if !h.found {
785785
return nil, ErrTableNotExists.FastGenByArgs(schema, tbl)
@@ -907,8 +907,8 @@ func (is *infoschemaV2) SchemaSimpleTableInfos(ctx context.Context, schema pmode
907907

908908
// Ascend is much more difficult than Descend.
909909
// So the data is taken out first and then dedup in Descend order.
910-
var tableItems []tableItem
911-
is.byName.Ascend(tableItem{dbName: schema}, func(item tableItem) bool {
910+
var tableItems []*tableItem
911+
is.byName.Ascend(&tableItem{dbName: schema}, func(item *tableItem) bool {
912912
if item.dbName.L != schema.L {
913913
return false
914914
}
@@ -923,7 +923,7 @@ func (is *infoschemaV2) SchemaSimpleTableInfos(ctx context.Context, schema pmode
923923
tblInfos := make([]*model.TableNameInfo, 0, len(tableItems))
924924
var curr *tableItem
925925
for i := len(tableItems) - 1; i >= 0; i-- {
926-
item := &tableItems[i]
926+
item := tableItems[i]
927927
if curr == nil || curr.tableName != tableItems[i].tableName {
928928
curr = item
929929
if !item.tomb {

pkg/infoschema/infoschema_v2_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,10 @@ func TestDataStructFieldsCorrectnessInSchemaChange(t *testing.T) {
653653
require.NoError(t, err)
654654
_, err = builder.ApplyDiff(meta.NewMutator(txn), &model.SchemaDiff{Type: model.ActionCreateTable, Version: 2, SchemaID: dbInfo.ID, TableID: tblInfo.ID})
655655
require.NoError(t, err)
656-
tblItem, ok := v2.Data.byName.Get(tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 2})
656+
tblItem, ok := v2.Data.byName.Get(&tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 2})
657657
require.True(t, ok)
658658
require.Equal(t, tblItem.tableID, tblInfo.ID)
659-
tblItem, ok = v2.Data.byID.Get(tableItem{tableID: tblInfo.ID, schemaVersion: 2})
659+
tblItem, ok = v2.Data.byID.Get(&tableItem{tableID: tblInfo.ID, schemaVersion: 2})
660660
require.True(t, ok)
661661
require.Equal(t, tblItem.dbID, dbInfo.ID)
662662
tbl, ok := v2.Data.tableCache.Get(tableCacheKey{tableID: tblInfo.ID, schemaVersion: 2})
@@ -699,10 +699,10 @@ func TestDataStructFieldsCorrectnessInSchemaChange(t *testing.T) {
699699
_, err = builder.ApplyDiff(m, &model.SchemaDiff{Type: model.ActionDropTable, Version: 5, SchemaID: dbInfo.ID, TableID: tblInfo.ID})
700700
require.NoError(t, err)
701701
// at first, the table will not be removed
702-
tblItem, ok = v2.Data.byName.Get(tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 5})
702+
tblItem, ok = v2.Data.byName.Get(&tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 5})
703703
require.True(t, ok)
704704
require.False(t, tblItem.tomb)
705-
tblItem, ok = v2.Data.byID.Get(tableItem{tableID: tblInfo.ID, schemaVersion: 5})
705+
tblItem, ok = v2.Data.byID.Get(&tableItem{tableID: tblInfo.ID, schemaVersion: 5})
706706
require.True(t, ok)
707707
require.False(t, tblItem.tomb)
708708
_, ok = v2.Data.tableCache.Get(tableCacheKey{tableID: tblInfo.ID, schemaVersion: 5})
@@ -716,10 +716,10 @@ func TestDataStructFieldsCorrectnessInSchemaChange(t *testing.T) {
716716
_, err = builder.ApplyDiff(m, &model.SchemaDiff{Type: model.ActionDropTable, Version: 5, SchemaID: dbInfo.ID, TableID: tblInfo.ID})
717717
require.NoError(t, err)
718718
// at first, the table will not be removed
719-
tblItem, ok = v2.Data.byName.Get(tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 5})
719+
tblItem, ok = v2.Data.byName.Get(&tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 5})
720720
require.True(t, ok)
721721
require.True(t, tblItem.tomb)
722-
tblItem, ok = v2.Data.byID.Get(tableItem{tableID: tblInfo.ID, schemaVersion: 5})
722+
tblItem, ok = v2.Data.byID.Get(&tableItem{tableID: tblInfo.ID, schemaVersion: 5})
723723
require.True(t, ok)
724724
require.True(t, tblItem.tomb)
725725
_, ok = v2.Data.tableCache.Get(tableCacheKey{tableID: tblInfo.ID, schemaVersion: 5})

0 commit comments

Comments
 (0)