Skip to content

Commit f46bded

Browse files
authored
infoschema: split InfoSchemaMisc from InfoSchema interface (pingcap#51015)
ref pingcap#50959
1 parent fe31308 commit f46bded

File tree

3 files changed

+45
-48
lines changed

3 files changed

+45
-48
lines changed

pkg/infoschema/builder.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ func (b *Builder) applyTableUpdate(m *meta.Meta, diff *model.SchemaDiff) ([]int6
531531
// TODO: Check how this would work with ADD/REMOVE Partitioning,
532532
// which may have AutoID not connected to tableID
533533
// TODO: can there be _tidb_rowid AutoID per partition?
534-
oldAllocs, _ := b.is.AllocByID(oldTableID)
534+
oldAllocs, _ := allocByID(b.is, oldTableID)
535535
allocs = filterAllocators(diff, oldAllocs)
536536
}
537537

@@ -1149,12 +1149,14 @@ func NewBuilder(r autoid.Requirement, factory func() (pools.Resource, error)) *B
11491149
return &Builder{
11501150
Requirement: r,
11511151
is: &infoSchema{
1152-
schemaMap: map[string]*schemaTables{},
1153-
policyMap: map[string]*model.PolicyInfo{},
1154-
resourceGroupMap: map[string]*model.ResourceGroupInfo{},
1155-
ruleBundleMap: map[int64]*placement.Bundle{},
1156-
sortedTablesBuckets: make([]sortedTables, bucketCount),
1157-
referredForeignKeyMap: make(map[SchemaAndTableName][]*model.ReferredFKInfo),
1152+
infoSchemaMisc: infoSchemaMisc{
1153+
policyMap: map[string]*model.PolicyInfo{},
1154+
resourceGroupMap: map[string]*model.ResourceGroupInfo{},
1155+
ruleBundleMap: map[int64]*placement.Bundle{},
1156+
referredForeignKeyMap: make(map[SchemaAndTableName][]*model.ReferredFKInfo),
1157+
},
1158+
schemaMap: map[string]*schemaTables{},
1159+
sortedTablesBuckets: make([]sortedTables, bucketCount),
11581160
},
11591161
dirtyDB: make(map[string]bool),
11601162
factory: factory,

pkg/infoschema/infoschema.go

+36-35
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,24 @@ import (
3535
// InfoSchema is the interface used to retrieve the schema information.
3636
// It works as a in memory cache and doesn't handle any schema change.
3737
// InfoSchema is read-only, and the returned value is a copy.
38-
// TODO: add more methods to retrieve tables and columns.
3938
type InfoSchema interface {
4039
SchemaByName(schema model.CIStr) (*model.DBInfo, bool)
4140
SchemaExists(schema model.CIStr) bool
4241
TableByName(schema, table model.CIStr) (table.Table, error)
4342
TableExists(schema, table model.CIStr) bool
4443
SchemaByID(id int64) (*model.DBInfo, bool)
45-
PolicyByName(name model.CIStr) (*model.PolicyInfo, bool)
46-
ResourceGroupByName(name model.CIStr) (*model.ResourceGroupInfo, bool)
4744
TableByID(id int64) (table.Table, bool)
48-
AllocByID(id int64) (autoid.Allocators, bool)
4945
AllSchemas() []*model.DBInfo
50-
Clone() (result []*model.DBInfo)
5146
SchemaTables(schema model.CIStr) []table.Table
5247
SchemaMetaVersion() int64
5348
FindTableByPartitionID(partitionID int64) (table.Table, *model.DBInfo, *model.PartitionDefinition)
49+
Misc
50+
}
51+
52+
// Misc contains the methods that are not closely related to InfoSchema.
53+
type Misc interface {
54+
PolicyByName(name model.CIStr) (*model.PolicyInfo, bool)
55+
ResourceGroupByName(name model.CIStr) (*model.ResourceGroupInfo, bool)
5456
// PlacementBundleByPhysicalTableID is used to get a rule bundle.
5557
PlacementBundleByPhysicalTableID(id int64) (*placement.Bundle, bool)
5658
// AllPlacementBundles is used to get all placement bundles
@@ -65,6 +67,8 @@ type InfoSchema interface {
6567
GetTableReferredForeignKeys(schema, table string) []*model.ReferredFKInfo
6668
}
6769

70+
var _ Misc = &infoSchemaMisc{}
71+
6872
type sortedTables []table.Table
6973

7074
func (s sortedTables) searchTable(id int64) int {
@@ -85,6 +89,17 @@ type schemaTables struct {
8589
const bucketCount = 512
8690

8791
type infoSchema struct {
92+
infoSchemaMisc
93+
schemaMap map[string]*schemaTables
94+
95+
// sortedTablesBuckets is a slice of sortedTables, a table's bucket index is (tableID % bucketCount).
96+
sortedTablesBuckets []sortedTables
97+
98+
// schemaMetaVersion is the version of schema, and we should check version when change schema.
99+
schemaMetaVersion int64
100+
}
101+
102+
type infoSchemaMisc struct {
88103
// ruleBundleMap stores all placement rules
89104
ruleBundleMap map[int64]*placement.Bundle
90105

@@ -96,17 +111,9 @@ type infoSchema struct {
96111
resourceGroupMutex sync.RWMutex
97112
resourceGroupMap map[string]*model.ResourceGroupInfo
98113

99-
schemaMap map[string]*schemaTables
100-
101-
// sortedTablesBuckets is a slice of sortedTables, a table's bucket index is (tableID % bucketCount).
102-
sortedTablesBuckets []sortedTables
103-
104114
// temporaryTables stores the temporary table ids
105115
temporaryTableIDs map[int64]struct{}
106116

107-
// schemaMetaVersion is the version of schema, and we should check version when change schema.
108-
schemaMetaVersion int64
109-
110117
// referredForeignKeyMap records all table's ReferredFKInfo.
111118
// referredSchemaAndTableName => child SchemaAndTableAndForeignKeyName => *model.ReferredFKInfo
112119
referredForeignKeyMap map[SchemaAndTableName][]*model.ReferredFKInfo
@@ -279,7 +286,8 @@ func (is *infoSchema) TableByID(id int64) (val table.Table, ok bool) {
279286
return slice[idx], true
280287
}
281288

282-
func (is *infoSchema) AllocByID(id int64) (autoid.Allocators, bool) {
289+
// allocByID returns the Allocators of a table.
290+
func allocByID(is *infoSchema, id int64) (autoid.Allocators, bool) {
283291
tbl, ok := is.TableByID(id)
284292
if !ok {
285293
return autoid.Allocators{}, false
@@ -334,17 +342,10 @@ func (is *infoSchema) FindTableByPartitionID(partitionID int64) (table.Table, *m
334342
}
335343

336344
// HasTemporaryTable returns whether information schema has temporary table
337-
func (is *infoSchema) HasTemporaryTable() bool {
345+
func (is *infoSchemaMisc) HasTemporaryTable() bool {
338346
return len(is.temporaryTableIDs) != 0
339347
}
340348

341-
func (is *infoSchema) Clone() (result []*model.DBInfo) {
342-
for _, v := range is.schemaMap {
343-
result = append(result, v.dbInfo.Clone())
344-
}
345-
return
346-
}
347-
348349
// GetSequenceByName gets the sequence by name.
349350
func GetSequenceByName(is InfoSchema, schema, sequence model.CIStr) (util.SequenceTable, error) {
350351
tbl, err := is.TableByName(schema, sequence)
@@ -403,23 +404,23 @@ func HasAutoIncrementColumn(tbInfo *model.TableInfo) (bool, string) {
403404
}
404405

405406
// PolicyByName is used to find the policy.
406-
func (is *infoSchema) PolicyByName(name model.CIStr) (*model.PolicyInfo, bool) {
407+
func (is *infoSchemaMisc) PolicyByName(name model.CIStr) (*model.PolicyInfo, bool) {
407408
is.policyMutex.RLock()
408409
defer is.policyMutex.RUnlock()
409410
t, r := is.policyMap[name.L]
410411
return t, r
411412
}
412413

413414
// ResourceGroupByName is used to find the resource group.
414-
func (is *infoSchema) ResourceGroupByName(name model.CIStr) (*model.ResourceGroupInfo, bool) {
415+
func (is *infoSchemaMisc) ResourceGroupByName(name model.CIStr) (*model.ResourceGroupInfo, bool) {
415416
is.resourceGroupMutex.RLock()
416417
defer is.resourceGroupMutex.RUnlock()
417418
t, r := is.resourceGroupMap[name.L]
418419
return t, r
419420
}
420421

421422
// AllResourceGroups returns all resource groups.
422-
func (is *infoSchema) AllResourceGroups() []*model.ResourceGroupInfo {
423+
func (is *infoSchemaMisc) AllResourceGroups() []*model.ResourceGroupInfo {
423424
is.resourceGroupMutex.RLock()
424425
defer is.resourceGroupMutex.RUnlock()
425426
groups := make([]*model.ResourceGroupInfo, 0, len(is.resourceGroupMap))
@@ -430,7 +431,7 @@ func (is *infoSchema) AllResourceGroups() []*model.ResourceGroupInfo {
430431
}
431432

432433
// AllPlacementPolicies returns all placement policies
433-
func (is *infoSchema) AllPlacementPolicies() []*model.PolicyInfo {
434+
func (is *infoSchemaMisc) AllPlacementPolicies() []*model.PolicyInfo {
434435
is.policyMutex.RLock()
435436
defer is.policyMutex.RUnlock()
436437
policies := make([]*model.PolicyInfo, 0, len(is.policyMap))
@@ -440,44 +441,44 @@ func (is *infoSchema) AllPlacementPolicies() []*model.PolicyInfo {
440441
return policies
441442
}
442443

443-
func (is *infoSchema) PlacementBundleByPhysicalTableID(id int64) (*placement.Bundle, bool) {
444+
func (is *infoSchemaMisc) PlacementBundleByPhysicalTableID(id int64) (*placement.Bundle, bool) {
444445
t, r := is.ruleBundleMap[id]
445446
return t, r
446447
}
447448

448-
func (is *infoSchema) AllPlacementBundles() []*placement.Bundle {
449+
func (is *infoSchemaMisc) AllPlacementBundles() []*placement.Bundle {
449450
bundles := make([]*placement.Bundle, 0, len(is.ruleBundleMap))
450451
for _, bundle := range is.ruleBundleMap {
451452
bundles = append(bundles, bundle)
452453
}
453454
return bundles
454455
}
455456

456-
func (is *infoSchema) setResourceGroup(resourceGroup *model.ResourceGroupInfo) {
457+
func (is *infoSchemaMisc) setResourceGroup(resourceGroup *model.ResourceGroupInfo) {
457458
is.resourceGroupMutex.Lock()
458459
defer is.resourceGroupMutex.Unlock()
459460
is.resourceGroupMap[resourceGroup.Name.L] = resourceGroup
460461
}
461462

462-
func (is *infoSchema) deleteResourceGroup(name string) {
463+
func (is *infoSchemaMisc) deleteResourceGroup(name string) {
463464
is.resourceGroupMutex.Lock()
464465
defer is.resourceGroupMutex.Unlock()
465466
delete(is.resourceGroupMap, name)
466467
}
467468

468-
func (is *infoSchema) setPolicy(policy *model.PolicyInfo) {
469+
func (is *infoSchemaMisc) setPolicy(policy *model.PolicyInfo) {
469470
is.policyMutex.Lock()
470471
defer is.policyMutex.Unlock()
471472
is.policyMap[policy.Name.L] = policy
472473
}
473474

474-
func (is *infoSchema) deletePolicy(name string) {
475+
func (is *infoSchemaMisc) deletePolicy(name string) {
475476
is.policyMutex.Lock()
476477
defer is.policyMutex.Unlock()
477478
delete(is.policyMap, name)
478479
}
479480

480-
func (is *infoSchema) addReferredForeignKeys(schema model.CIStr, tbInfo *model.TableInfo) {
481+
func (is *infoSchemaMisc) addReferredForeignKeys(schema model.CIStr, tbInfo *model.TableInfo) {
481482
for _, fk := range tbInfo.ForeignKeys {
482483
if fk.Version < model.FKVersion1 {
483484
continue
@@ -517,7 +518,7 @@ func (is *infoSchema) addReferredForeignKeys(schema model.CIStr, tbInfo *model.T
517518
}
518519
}
519520

520-
func (is *infoSchema) deleteReferredForeignKeys(schema model.CIStr, tbInfo *model.TableInfo) {
521+
func (is *infoSchemaMisc) deleteReferredForeignKeys(schema model.CIStr, tbInfo *model.TableInfo) {
521522
for _, fk := range tbInfo.ForeignKeys {
522523
if fk.Version < model.FKVersion1 {
523524
continue
@@ -539,7 +540,7 @@ func (is *infoSchema) deleteReferredForeignKeys(schema model.CIStr, tbInfo *mode
539540
}
540541

541542
// GetTableReferredForeignKeys gets the table's ReferredFKInfo by lowercase schema and table name.
542-
func (is *infoSchema) GetTableReferredForeignKeys(schema, table string) []*model.ReferredFKInfo {
543+
func (is *infoSchemaMisc) GetTableReferredForeignKeys(schema, table string) []*model.ReferredFKInfo {
543544
name := SchemaAndTableName{schema: schema, table: table}
544545
return is.referredForeignKeyMap[name]
545546
}

pkg/infoschema/infoschema_test.go

-6
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ func TestBasic(t *testing.T) {
129129

130130
schemas := is.AllSchemas()
131131
require.Len(t, schemas, 4)
132-
schemas = is.Clone()
133-
require.Len(t, schemas, 4)
134132

135133
require.True(t, is.SchemaExists(dbName))
136134
require.False(t, is.SchemaExists(noexist))
@@ -173,10 +171,6 @@ func TestBasic(t *testing.T) {
173171
require.False(t, ok)
174172
require.Nil(t, tb)
175173

176-
alloc, ok := is.AllocByID(tbID)
177-
require.True(t, ok)
178-
require.NotNil(t, alloc)
179-
180174
tb, err = is.TableByName(dbName, tbName)
181175
require.NoError(t, err)
182176
require.NotNil(t, tb)

0 commit comments

Comments
 (0)