Skip to content

Commit

Permalink
infoschema: change infoschem v2 btree element from tableItem to *tabl…
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Dec 17, 2024
1 parent 40eb7dd commit db2776a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions pkg/infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,8 +965,8 @@ func (b *Builder) createSchemaTablesForDB(di *model.DBInfo, tableFromMeta tableF
tableID: id,
schemaVersion: schemaVersion,
}
b.infoData.byID.Set(item)
b.infoData.byName.Set(item)
b.infoData.byID.Set(&item)
b.infoData.byName.Set(&item)
}
}
b.addDB(schemaVersion, di, schTbls)
Expand Down
62 changes: 31 additions & 31 deletions pkg/infoschema/infoschema_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ type Data struct {
//
// It means as long as we can find an item in it, the item is available, even through the
// schema version maybe smaller than required.
byName *btree.BTreeG[tableItem]
byName *btree.BTreeG[*tableItem]

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

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

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

func (isd *Data) remove(item tableItem) {
item.tomb = true
isd.byID.Set(item)
isd.byName.Set(item)
isd.byID.Set(&item)
isd.byName.Set(&item)
isd.tableInfoResident.Set(tableInfoItem{
dbName: item.dbName,
tableID: item.tableID,
Expand Down Expand Up @@ -271,7 +271,7 @@ func (isd *Data) resetBeforeFullLoad(schemaVersion int64) {
resetPID2TIDBeforeFullLoad(isd.pid2tid, schemaVersion)
}

func resetByIDBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) {
func resetByIDBeforeFullLoad(bt *btree.BTreeG[*tableItem], schemaVersion int64) {
pivot, ok := bt.Max()
if !ok {
return
Expand All @@ -281,10 +281,10 @@ func resetByIDBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) {
if bt.Len() < batchSize {
batchSize = bt.Len()
}
items := make([]tableItem, 0, batchSize)
items := make([]*tableItem, 0, batchSize)
items = append(items, pivot)
for {
bt.Descend(pivot, func(item tableItem) bool {
bt.Descend(pivot, func(item *tableItem) bool {
if pivot.tableID == item.tableID {
return true // skip MVCC version
}
Expand All @@ -296,7 +296,7 @@ func resetByIDBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) {
break
}
for _, item := range items {
bt.Set(tableItem{
bt.Set(&tableItem{
dbName: item.dbName,
dbID: item.dbID,
tableName: item.tableName,
Expand All @@ -309,7 +309,7 @@ func resetByIDBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) {
}
}

func resetByNameBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) {
func resetByNameBeforeFullLoad(bt *btree.BTreeG[*tableItem], schemaVersion int64) {
pivot, ok := bt.Max()
if !ok {
return
Expand All @@ -319,10 +319,10 @@ func resetByNameBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64)
if bt.Len() < batchSize {
batchSize = bt.Len()
}
items := make([]tableItem, 0, batchSize)
items := make([]*tableItem, 0, batchSize)
items = append(items, pivot)
for {
bt.Descend(pivot, func(item tableItem) bool {
bt.Descend(pivot, func(item *tableItem) bool {
if pivot.dbName == item.dbName && pivot.tableName == item.tableName {
return true // skip MVCC version
}
Expand All @@ -334,7 +334,7 @@ func resetByNameBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64)
break
}
for _, item := range items {
bt.Set(tableItem{
bt.Set(&tableItem{
dbName: item.dbName,
dbID: item.dbID,
tableName: item.tableName,
Expand Down Expand Up @@ -457,7 +457,7 @@ func resetPID2TIDBeforeFullLoad(bt *btree.BTreeG[partitionItem], schemaVersion i
}
}

func compareByID(a, b tableItem) bool {
func compareByID(a, b *tableItem) bool {
if a.tableID < b.tableID {
return true
}
Expand All @@ -468,7 +468,7 @@ func compareByID(a, b tableItem) bool {
return a.schemaVersion < b.schemaVersion
}

func compareByName(a, b tableItem) bool {
func compareByName(a, b *tableItem) bool {
if a.dbName.L < b.dbName.L {
return true
}
Expand Down Expand Up @@ -553,12 +553,12 @@ func NewInfoSchemaV2(r autoid.Requirement, factory func() (pools.Resource, error
}
}

func search(bt *btree.BTreeG[tableItem], schemaVersion int64, end tableItem, matchFn func(a, b *tableItem) bool) (tableItem, bool) {
func search(bt *btree.BTreeG[*tableItem], schemaVersion int64, end tableItem, matchFn func(a, b *tableItem) bool) (*tableItem, bool) {
var ok bool
var target tableItem
var target *tableItem
// Iterate through the btree, find the query item whose schema version is the largest one (latest).
bt.Descend(end, func(item tableItem) bool {
if !matchFn(&end, &item) {
bt.Descend(&end, func(item *tableItem) bool {
if !matchFn(&end, item) {
return false
}
if item.schemaVersion > schemaVersion {
Expand Down Expand Up @@ -594,7 +594,7 @@ func (is *infoschemaV2) CloneAndUpdateTS(startTS uint64) *infoschemaV2 {
return &tmp
}

func (is *infoschemaV2) searchTableItemByID(tableID int64) (tableItem, bool) {
func (is *infoschemaV2) searchTableItemByID(tableID int64) (*tableItem, bool) {
eq := func(a, b *tableItem) bool { return a.tableID == b.tableID }
return search(
is.byID,
Expand Down Expand Up @@ -679,7 +679,7 @@ func (is *infoschemaV2) IterateAllTableItems(visit func(TableItem) bool) {
return
}
var pivot *tableItem
is.byName.Descend(maxv, func(item tableItem) bool {
is.byName.Descend(maxv, func(item *tableItem) bool {
if item.schemaVersion > is.schemaMetaVersion {
// skip MVCC version, those items are not visible to the queried schema version
return true
Expand All @@ -688,7 +688,7 @@ func (is *infoschemaV2) IterateAllTableItems(visit func(TableItem) bool) {
// skip MVCC version, this db.table has been visited already
return true
}
pivot = &item
pivot = item
if !item.tomb {
return visit(TableItem{DBName: item.dbName, TableName: item.tableName})
}
Expand Down Expand Up @@ -743,10 +743,10 @@ type tableByNameHelper struct {
end tableItem
schemaVersion int64
found bool
res tableItem
res *tableItem
}

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

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

// Ascend is much more difficult than Descend.
// So the data is taken out first and then dedup in Descend order.
var tableItems []tableItem
is.byName.Ascend(tableItem{dbName: schema}, func(item tableItem) bool {
var tableItems []*tableItem
is.byName.Ascend(&tableItem{dbName: schema}, func(item *tableItem) bool {
if item.dbName.L != schema.L {
return false
}
Expand All @@ -923,7 +923,7 @@ func (is *infoschemaV2) SchemaSimpleTableInfos(ctx context.Context, schema pmode
tblInfos := make([]*model.TableNameInfo, 0, len(tableItems))
var curr *tableItem
for i := len(tableItems) - 1; i >= 0; i-- {
item := &tableItems[i]
item := tableItems[i]
if curr == nil || curr.tableName != tableItems[i].tableName {
curr = item
if !item.tomb {
Expand Down
12 changes: 6 additions & 6 deletions pkg/infoschema/infoschema_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,10 +653,10 @@ func TestDataStructFieldsCorrectnessInSchemaChange(t *testing.T) {
require.NoError(t, err)
_, err = builder.ApplyDiff(meta.NewMutator(txn), &model.SchemaDiff{Type: model.ActionCreateTable, Version: 2, SchemaID: dbInfo.ID, TableID: tblInfo.ID})
require.NoError(t, err)
tblItem, ok := v2.Data.byName.Get(tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 2})
tblItem, ok := v2.Data.byName.Get(&tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 2})
require.True(t, ok)
require.Equal(t, tblItem.tableID, tblInfo.ID)
tblItem, ok = v2.Data.byID.Get(tableItem{tableID: tblInfo.ID, schemaVersion: 2})
tblItem, ok = v2.Data.byID.Get(&tableItem{tableID: tblInfo.ID, schemaVersion: 2})
require.True(t, ok)
require.Equal(t, tblItem.dbID, dbInfo.ID)
tbl, ok := v2.Data.tableCache.Get(tableCacheKey{tableID: tblInfo.ID, schemaVersion: 2})
Expand Down Expand Up @@ -699,10 +699,10 @@ func TestDataStructFieldsCorrectnessInSchemaChange(t *testing.T) {
_, err = builder.ApplyDiff(m, &model.SchemaDiff{Type: model.ActionDropTable, Version: 5, SchemaID: dbInfo.ID, TableID: tblInfo.ID})
require.NoError(t, err)
// at first, the table will not be removed
tblItem, ok = v2.Data.byName.Get(tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 5})
tblItem, ok = v2.Data.byName.Get(&tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 5})
require.True(t, ok)
require.False(t, tblItem.tomb)
tblItem, ok = v2.Data.byID.Get(tableItem{tableID: tblInfo.ID, schemaVersion: 5})
tblItem, ok = v2.Data.byID.Get(&tableItem{tableID: tblInfo.ID, schemaVersion: 5})
require.True(t, ok)
require.False(t, tblItem.tomb)
_, ok = v2.Data.tableCache.Get(tableCacheKey{tableID: tblInfo.ID, schemaVersion: 5})
Expand All @@ -716,10 +716,10 @@ func TestDataStructFieldsCorrectnessInSchemaChange(t *testing.T) {
_, err = builder.ApplyDiff(m, &model.SchemaDiff{Type: model.ActionDropTable, Version: 5, SchemaID: dbInfo.ID, TableID: tblInfo.ID})
require.NoError(t, err)
// at first, the table will not be removed
tblItem, ok = v2.Data.byName.Get(tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 5})
tblItem, ok = v2.Data.byName.Get(&tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 5})
require.True(t, ok)
require.True(t, tblItem.tomb)
tblItem, ok = v2.Data.byID.Get(tableItem{tableID: tblInfo.ID, schemaVersion: 5})
tblItem, ok = v2.Data.byID.Get(&tableItem{tableID: tblInfo.ID, schemaVersion: 5})
require.True(t, ok)
require.True(t, tblItem.tomb)
_, ok = v2.Data.tableCache.Get(tableCacheKey{tableID: tblInfo.ID, schemaVersion: 5})
Expand Down

0 comments on commit db2776a

Please sign in to comment.