@@ -35,22 +35,24 @@ import (
35
35
// InfoSchema is the interface used to retrieve the schema information.
36
36
// It works as a in memory cache and doesn't handle any schema change.
37
37
// InfoSchema is read-only, and the returned value is a copy.
38
- // TODO: add more methods to retrieve tables and columns.
39
38
type InfoSchema interface {
40
39
SchemaByName (schema model.CIStr ) (* model.DBInfo , bool )
41
40
SchemaExists (schema model.CIStr ) bool
42
41
TableByName (schema , table model.CIStr ) (table.Table , error )
43
42
TableExists (schema , table model.CIStr ) bool
44
43
SchemaByID (id int64 ) (* model.DBInfo , bool )
45
- PolicyByName (name model.CIStr ) (* model.PolicyInfo , bool )
46
- ResourceGroupByName (name model.CIStr ) (* model.ResourceGroupInfo , bool )
47
44
TableByID (id int64 ) (table.Table , bool )
48
- AllocByID (id int64 ) (autoid.Allocators , bool )
49
45
AllSchemas () []* model.DBInfo
50
- Clone () (result []* model.DBInfo )
51
46
SchemaTables (schema model.CIStr ) []table.Table
52
47
SchemaMetaVersion () int64
53
48
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 )
54
56
// PlacementBundleByPhysicalTableID is used to get a rule bundle.
55
57
PlacementBundleByPhysicalTableID (id int64 ) (* placement.Bundle , bool )
56
58
// AllPlacementBundles is used to get all placement bundles
@@ -65,6 +67,8 @@ type InfoSchema interface {
65
67
GetTableReferredForeignKeys (schema , table string ) []* model.ReferredFKInfo
66
68
}
67
69
70
+ var _ Misc = & infoSchemaMisc {}
71
+
68
72
type sortedTables []table.Table
69
73
70
74
func (s sortedTables ) searchTable (id int64 ) int {
@@ -85,6 +89,17 @@ type schemaTables struct {
85
89
const bucketCount = 512
86
90
87
91
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 {
88
103
// ruleBundleMap stores all placement rules
89
104
ruleBundleMap map [int64 ]* placement.Bundle
90
105
@@ -96,17 +111,9 @@ type infoSchema struct {
96
111
resourceGroupMutex sync.RWMutex
97
112
resourceGroupMap map [string ]* model.ResourceGroupInfo
98
113
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
-
104
114
// temporaryTables stores the temporary table ids
105
115
temporaryTableIDs map [int64 ]struct {}
106
116
107
- // schemaMetaVersion is the version of schema, and we should check version when change schema.
108
- schemaMetaVersion int64
109
-
110
117
// referredForeignKeyMap records all table's ReferredFKInfo.
111
118
// referredSchemaAndTableName => child SchemaAndTableAndForeignKeyName => *model.ReferredFKInfo
112
119
referredForeignKeyMap map [SchemaAndTableName ][]* model.ReferredFKInfo
@@ -279,7 +286,8 @@ func (is *infoSchema) TableByID(id int64) (val table.Table, ok bool) {
279
286
return slice [idx ], true
280
287
}
281
288
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 ) {
283
291
tbl , ok := is .TableByID (id )
284
292
if ! ok {
285
293
return autoid.Allocators {}, false
@@ -334,17 +342,10 @@ func (is *infoSchema) FindTableByPartitionID(partitionID int64) (table.Table, *m
334
342
}
335
343
336
344
// HasTemporaryTable returns whether information schema has temporary table
337
- func (is * infoSchema ) HasTemporaryTable () bool {
345
+ func (is * infoSchemaMisc ) HasTemporaryTable () bool {
338
346
return len (is .temporaryTableIDs ) != 0
339
347
}
340
348
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
-
348
349
// GetSequenceByName gets the sequence by name.
349
350
func GetSequenceByName (is InfoSchema , schema , sequence model.CIStr ) (util.SequenceTable , error ) {
350
351
tbl , err := is .TableByName (schema , sequence )
@@ -403,23 +404,23 @@ func HasAutoIncrementColumn(tbInfo *model.TableInfo) (bool, string) {
403
404
}
404
405
405
406
// 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 ) {
407
408
is .policyMutex .RLock ()
408
409
defer is .policyMutex .RUnlock ()
409
410
t , r := is .policyMap [name .L ]
410
411
return t , r
411
412
}
412
413
413
414
// 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 ) {
415
416
is .resourceGroupMutex .RLock ()
416
417
defer is .resourceGroupMutex .RUnlock ()
417
418
t , r := is .resourceGroupMap [name .L ]
418
419
return t , r
419
420
}
420
421
421
422
// AllResourceGroups returns all resource groups.
422
- func (is * infoSchema ) AllResourceGroups () []* model.ResourceGroupInfo {
423
+ func (is * infoSchemaMisc ) AllResourceGroups () []* model.ResourceGroupInfo {
423
424
is .resourceGroupMutex .RLock ()
424
425
defer is .resourceGroupMutex .RUnlock ()
425
426
groups := make ([]* model.ResourceGroupInfo , 0 , len (is .resourceGroupMap ))
@@ -430,7 +431,7 @@ func (is *infoSchema) AllResourceGroups() []*model.ResourceGroupInfo {
430
431
}
431
432
432
433
// AllPlacementPolicies returns all placement policies
433
- func (is * infoSchema ) AllPlacementPolicies () []* model.PolicyInfo {
434
+ func (is * infoSchemaMisc ) AllPlacementPolicies () []* model.PolicyInfo {
434
435
is .policyMutex .RLock ()
435
436
defer is .policyMutex .RUnlock ()
436
437
policies := make ([]* model.PolicyInfo , 0 , len (is .policyMap ))
@@ -440,44 +441,44 @@ func (is *infoSchema) AllPlacementPolicies() []*model.PolicyInfo {
440
441
return policies
441
442
}
442
443
443
- func (is * infoSchema ) PlacementBundleByPhysicalTableID (id int64 ) (* placement.Bundle , bool ) {
444
+ func (is * infoSchemaMisc ) PlacementBundleByPhysicalTableID (id int64 ) (* placement.Bundle , bool ) {
444
445
t , r := is .ruleBundleMap [id ]
445
446
return t , r
446
447
}
447
448
448
- func (is * infoSchema ) AllPlacementBundles () []* placement.Bundle {
449
+ func (is * infoSchemaMisc ) AllPlacementBundles () []* placement.Bundle {
449
450
bundles := make ([]* placement.Bundle , 0 , len (is .ruleBundleMap ))
450
451
for _ , bundle := range is .ruleBundleMap {
451
452
bundles = append (bundles , bundle )
452
453
}
453
454
return bundles
454
455
}
455
456
456
- func (is * infoSchema ) setResourceGroup (resourceGroup * model.ResourceGroupInfo ) {
457
+ func (is * infoSchemaMisc ) setResourceGroup (resourceGroup * model.ResourceGroupInfo ) {
457
458
is .resourceGroupMutex .Lock ()
458
459
defer is .resourceGroupMutex .Unlock ()
459
460
is .resourceGroupMap [resourceGroup .Name .L ] = resourceGroup
460
461
}
461
462
462
- func (is * infoSchema ) deleteResourceGroup (name string ) {
463
+ func (is * infoSchemaMisc ) deleteResourceGroup (name string ) {
463
464
is .resourceGroupMutex .Lock ()
464
465
defer is .resourceGroupMutex .Unlock ()
465
466
delete (is .resourceGroupMap , name )
466
467
}
467
468
468
- func (is * infoSchema ) setPolicy (policy * model.PolicyInfo ) {
469
+ func (is * infoSchemaMisc ) setPolicy (policy * model.PolicyInfo ) {
469
470
is .policyMutex .Lock ()
470
471
defer is .policyMutex .Unlock ()
471
472
is .policyMap [policy .Name .L ] = policy
472
473
}
473
474
474
- func (is * infoSchema ) deletePolicy (name string ) {
475
+ func (is * infoSchemaMisc ) deletePolicy (name string ) {
475
476
is .policyMutex .Lock ()
476
477
defer is .policyMutex .Unlock ()
477
478
delete (is .policyMap , name )
478
479
}
479
480
480
- func (is * infoSchema ) addReferredForeignKeys (schema model.CIStr , tbInfo * model.TableInfo ) {
481
+ func (is * infoSchemaMisc ) addReferredForeignKeys (schema model.CIStr , tbInfo * model.TableInfo ) {
481
482
for _ , fk := range tbInfo .ForeignKeys {
482
483
if fk .Version < model .FKVersion1 {
483
484
continue
@@ -517,7 +518,7 @@ func (is *infoSchema) addReferredForeignKeys(schema model.CIStr, tbInfo *model.T
517
518
}
518
519
}
519
520
520
- func (is * infoSchema ) deleteReferredForeignKeys (schema model.CIStr , tbInfo * model.TableInfo ) {
521
+ func (is * infoSchemaMisc ) deleteReferredForeignKeys (schema model.CIStr , tbInfo * model.TableInfo ) {
521
522
for _ , fk := range tbInfo .ForeignKeys {
522
523
if fk .Version < model .FKVersion1 {
523
524
continue
@@ -539,7 +540,7 @@ func (is *infoSchema) deleteReferredForeignKeys(schema model.CIStr, tbInfo *mode
539
540
}
540
541
541
542
// 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 {
543
544
name := SchemaAndTableName {schema : schema , table : table }
544
545
return is .referredForeignKeyMap [name ]
545
546
}
0 commit comments