-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
infoschema: add a simply store for DM's SchemaTracker #35954
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
80db4ab
ddl: add a simply store for InfoSchema
lance6716 5980b98
fix lint
lance6716 8f3522d
Merge branch 'master' into info-store
hawkingrei 5d55c4b
move and rename
lance6716 b529f8f
Merge branch 'info-store' of github.com:lance6716/tidb into info-store
lance6716 464e829
Merge branch 'master' into info-store
xhebox 420faf5
Merge branch 'master' into info-store
ti-chi-bot f10a998
Merge branch 'master' into info-store
ti-chi-bot 9d1ba7f
Merge branch 'master' into info-store
ti-chi-bot 569f7ce
Merge branch 'master' into info-store
ti-chi-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ddl | ||
|
||
import ( | ||
"github.com/pingcap/tidb/infoschema" | ||
"github.com/pingcap/tidb/parser/model" | ||
"github.com/pingcap/tidb/table" | ||
"github.com/pingcap/tidb/table/tables" | ||
) | ||
|
||
// infoStore is a simple structure that stores DBInfo and TableInfo. It's not thread-safe. | ||
type infoStore struct { | ||
lowerCaseTableNames int // same as variable lower_case_table_names | ||
|
||
dbs map[string]*model.DBInfo | ||
tables map[string]map[string]*model.TableInfo | ||
} | ||
|
||
func newInfoStore(lowerCaseTableNames int) *infoStore { | ||
return &infoStore{ | ||
lowerCaseTableNames: lowerCaseTableNames, | ||
dbs: map[string]*model.DBInfo{}, | ||
tables: map[string]map[string]*model.TableInfo{}, | ||
} | ||
} | ||
|
||
func (i *infoStore) ciStr2Key(name model.CIStr) string { | ||
if i.lowerCaseTableNames == 0 { | ||
return name.O | ||
} | ||
return name.L | ||
} | ||
|
||
// SchemaByName is exported to be used when infoStore is embedded into another public struct. | ||
func (i *infoStore) SchemaByName(name model.CIStr) *model.DBInfo { | ||
key := i.ciStr2Key(name) | ||
return i.dbs[key] | ||
} | ||
|
||
func (i *infoStore) putSchema(dbInfo *model.DBInfo) { | ||
key := i.ciStr2Key(dbInfo.Name) | ||
i.dbs[key] = dbInfo | ||
if i.tables[key] == nil { | ||
i.tables[key] = map[string]*model.TableInfo{} | ||
} | ||
} | ||
|
||
func (i *infoStore) deleteSchema(name model.CIStr) bool { | ||
key := i.ciStr2Key(name) | ||
_, ok := i.dbs[key] | ||
if !ok { | ||
return false | ||
} | ||
delete(i.dbs, key) | ||
delete(i.tables, key) | ||
return true | ||
} | ||
|
||
// TableByName is exported to be used when infoStore is embedded into another public struct. | ||
func (i *infoStore) TableByName(schema, table model.CIStr) (*model.TableInfo, error) { | ||
schemaKey := i.ciStr2Key(schema) | ||
tables, ok := i.tables[schemaKey] | ||
if !ok { | ||
return nil, infoschema.ErrDatabaseNotExists.GenWithStackByArgs(schema) | ||
} | ||
|
||
tableKey := i.ciStr2Key(table) | ||
tbl, ok := tables[tableKey] | ||
if !ok { | ||
return nil, infoschema.ErrTableNotExists.GenWithStackByArgs(schema, table) | ||
} | ||
return tbl, nil | ||
} | ||
|
||
func (i *infoStore) putTable(schemaName model.CIStr, tblInfo *model.TableInfo) error { | ||
schemaKey := i.ciStr2Key(schemaName) | ||
tables, ok := i.tables[schemaKey] | ||
if !ok { | ||
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(schemaName) | ||
} | ||
tableKey := i.ciStr2Key(tblInfo.Name) | ||
tables[tableKey] = tblInfo | ||
return nil | ||
} | ||
|
||
func (i *infoStore) deleteTable(schema, table model.CIStr) error { | ||
schemaKey := i.ciStr2Key(schema) | ||
tables, ok := i.tables[schemaKey] | ||
if !ok { | ||
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(schema) | ||
} | ||
|
||
tableKey := i.ciStr2Key(table) | ||
_, ok = tables[tableKey] | ||
if !ok { | ||
return infoschema.ErrTableNotExists.GenWithStackByArgs(schema, table) | ||
} | ||
delete(tables, tableKey) | ||
return nil | ||
} | ||
|
||
// infoSchemaAdaptor convert infoStore to infoschema.InfoSchema, it only implements a part of InfoSchema interface to be | ||
// used by DDL interface. | ||
// nolint:unused | ||
zimulala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type infoSchemaAdaptor struct { | ||
infoschema.InfoSchema | ||
inner *infoStore | ||
} | ||
|
||
// SchemaByName implements the InfoSchema interface. | ||
// nolint:unused | ||
func (i infoSchemaAdaptor) SchemaByName(schema model.CIStr) (*model.DBInfo, bool) { | ||
dbInfo := i.inner.SchemaByName(schema) | ||
return dbInfo, dbInfo != nil | ||
} | ||
|
||
// TableExists implements the InfoSchema interface. | ||
// nolint:unused | ||
func (i infoSchemaAdaptor) TableExists(schema, table model.CIStr) bool { | ||
tableInfo, _ := i.inner.TableByName(schema, table) | ||
return tableInfo != nil | ||
} | ||
|
||
// TableIsView implements the InfoSchema interface. | ||
// nolint:unused | ||
func (i infoSchemaAdaptor) TableIsView(schema, table model.CIStr) bool { | ||
tableInfo, _ := i.inner.TableByName(schema, table) | ||
if tableInfo == nil { | ||
return false | ||
} | ||
return tableInfo.IsView() | ||
} | ||
|
||
// TableByName implements the InfoSchema interface. | ||
// nolint:unused | ||
func (i infoSchemaAdaptor) TableByName(schema, table model.CIStr) (t table.Table, err error) { | ||
tableInfo, err := i.inner.TableByName(schema, table) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return tables.MockTableFromMeta(tableInfo), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ddl | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pingcap/tidb/infoschema" | ||
"github.com/pingcap/tidb/parser/model" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestInfoStoreLowerCaseTableNames(t *testing.T) { | ||
dbName := model.NewCIStr("DBName") | ||
lowerDBName := model.NewCIStr("dbname") | ||
tableName := model.NewCIStr("TableName") | ||
lowerTableName := model.NewCIStr("tablename") | ||
dbInfo := &model.DBInfo{Name: dbName} | ||
tableInfo := &model.TableInfo{Name: tableName} | ||
|
||
// case-sensitive | ||
|
||
is := newInfoStore(0) | ||
is.putSchema(dbInfo) | ||
got := is.SchemaByName(dbName) | ||
require.NotNil(t, got) | ||
got = is.SchemaByName(lowerDBName) | ||
require.Nil(t, got) | ||
|
||
err := is.putTable(lowerDBName, tableInfo) | ||
require.True(t, infoschema.ErrDatabaseNotExists.Equal(err)) | ||
err = is.putTable(dbName, tableInfo) | ||
require.NoError(t, err) | ||
got2, err := is.TableByName(dbName, tableName) | ||
require.NoError(t, err) | ||
require.NotNil(t, got2) | ||
got2, err = is.TableByName(lowerTableName, tableName) | ||
require.True(t, infoschema.ErrDatabaseNotExists.Equal(err)) | ||
require.Nil(t, got2) | ||
got2, err = is.TableByName(dbName, lowerTableName) | ||
require.True(t, infoschema.ErrTableNotExists.Equal(err)) | ||
require.Nil(t, got2) | ||
|
||
// compare-insensitive | ||
|
||
is = newInfoStore(2) | ||
is.putSchema(dbInfo) | ||
got = is.SchemaByName(dbName) | ||
require.NotNil(t, got) | ||
got = is.SchemaByName(lowerDBName) | ||
require.NotNil(t, got) | ||
require.Equal(t, dbName, got.Name) | ||
|
||
err = is.putTable(lowerDBName, tableInfo) | ||
require.NoError(t, err) | ||
got2, err = is.TableByName(dbName, tableName) | ||
require.NoError(t, err) | ||
require.NotNil(t, got2) | ||
got2, err = is.TableByName(dbName, lowerTableName) | ||
require.NoError(t, err) | ||
require.NotNil(t, got2) | ||
require.Equal(t, tableName, got2.Name) | ||
} | ||
|
||
func TestInfoStoreDeleteTables(t *testing.T) { | ||
is := newInfoStore(0) | ||
dbName1 := model.NewCIStr("DBName1") | ||
dbName2 := model.NewCIStr("DBName2") | ||
tableName1 := model.NewCIStr("TableName1") | ||
tableName2 := model.NewCIStr("TableName2") | ||
dbInfo1 := &model.DBInfo{Name: dbName1} | ||
dbInfo2 := &model.DBInfo{Name: dbName2} | ||
tableInfo1 := &model.TableInfo{Name: tableName1} | ||
tableInfo2 := &model.TableInfo{Name: tableName2} | ||
|
||
is.putSchema(dbInfo1) | ||
err := is.putTable(dbName1, tableInfo1) | ||
require.NoError(t, err) | ||
err = is.putTable(dbName1, tableInfo2) | ||
require.NoError(t, err) | ||
|
||
// db2 not created | ||
ok := is.deleteSchema(dbName2) | ||
require.False(t, ok) | ||
err = is.putTable(dbName2, tableInfo1) | ||
require.True(t, infoschema.ErrDatabaseNotExists.Equal(err)) | ||
err = is.deleteTable(dbName2, tableName1) | ||
require.True(t, infoschema.ErrDatabaseNotExists.Equal(err)) | ||
|
||
is.putSchema(dbInfo2) | ||
err = is.putTable(dbName2, tableInfo1) | ||
require.NoError(t, err) | ||
|
||
err = is.deleteTable(dbName2, tableName2) | ||
require.True(t, infoschema.ErrTableNotExists.Equal(err)) | ||
err = is.deleteTable(dbName2, tableName1) | ||
require.NoError(t, err) | ||
|
||
// delete db will remove its tables | ||
ok = is.deleteSchema(dbName1) | ||
require.True(t, ok) | ||
_, err = is.TableByName(dbName1, tableName1) | ||
require.True(t, infoschema.ErrDatabaseNotExists.Equal(err)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that this is not needed at present. Can you remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's needed in
ciStr2Key
. In DM's use case, upstream is MySQL so we should set lowerCaseTableNames to MySQL's value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I see
lower_case_table_names
is read-only.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, in the big PR #35867, I use struct literal to build infoStore in this package. I can write a newInfoStore.newInfoStore is already here. The usage is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. DM needs use it