forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfoschema.go
528 lines (481 loc) · 15.8 KB
/
infoschema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// Copyright 2015 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package infoschema
import (
"strings"
"sync/atomic"
"github.com/juju/errors"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/perfschema"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/terror"
// import table implementation to init table.TableFromMeta
_ "github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/util/types"
)
var (
// ErrDatabaseDropExists returns for dropping a non-existent database.
ErrDatabaseDropExists = terror.ClassSchema.New(codeDBDropExists, "database doesn't exist")
// ErrDatabaseNotExists returns for database not exists.
ErrDatabaseNotExists = terror.ClassSchema.New(codeDatabaseNotExists, "database not exists")
// ErrTableNotExists returns for table not exists.
ErrTableNotExists = terror.ClassSchema.New(codeTableNotExists, "table not exists")
// ErrColumnNotExists returns for column not exists.
ErrColumnNotExists = terror.ClassSchema.New(codeColumnNotExists, "field not exists")
// ErrForeignKeyNotMatch returns for foreign key not match.
ErrForeignKeyNotMatch = terror.ClassSchema.New(codeCannotAddForeign, "foreign key not match")
// ErrForeignKeyExists returns for foreign key exists.
ErrForeignKeyExists = terror.ClassSchema.New(codeCannotAddForeign, "foreign key already exists")
// ErrForeignKeyNotExists returns for foreign key not exists.
ErrForeignKeyNotExists = terror.ClassSchema.New(codeForeignKeyNotExists, "foreign key not exists")
// ErrDatabaseExists returns for database already exists.
ErrDatabaseExists = terror.ClassSchema.New(codeDatabaseExists, "database already exists")
// ErrTableExists returns for table already exists.
ErrTableExists = terror.ClassSchema.New(codeTableExists, "table already exists")
// ErrTableDropExists returns for dropping a non-existent table.
ErrTableDropExists = terror.ClassSchema.New(codeBadTable, "unknown table")
// ErrColumnExists returns for column already exists.
ErrColumnExists = terror.ClassSchema.New(codeColumnExists, "Duplicate column")
// ErrIndexExists returns for index already exists.
ErrIndexExists = terror.ClassSchema.New(codeIndexExists, "Duplicate Index")
)
// InfoSchema is the interface used to retrieve the schema information.
// It works as a in memory cache and doesn't handle any schema change.
// InfoSchema is read-only, and the returned value is a copy.
// TODO: add more methods to retrieve tables and columns.
type InfoSchema interface {
SchemaByName(schema model.CIStr) (*model.DBInfo, bool)
SchemaExists(schema model.CIStr) bool
TableByName(schema, table model.CIStr) (table.Table, error)
TableExists(schema, table model.CIStr) bool
ColumnByName(schema, table, column model.CIStr) (*model.ColumnInfo, bool)
ColumnExists(schema, table, column model.CIStr) bool
IndexByName(schema, table, index model.CIStr) (*model.IndexInfo, bool)
SchemaByID(id int64) (*model.DBInfo, bool)
TableByID(id int64) (table.Table, bool)
AllocByID(id int64) (autoid.Allocator, bool)
ColumnByID(id int64) (*model.ColumnInfo, bool)
ColumnIndicesByID(id int64) ([]*model.IndexInfo, bool)
AllSchemaNames() []string
AllSchemas() []*model.DBInfo
Clone() (result []*model.DBInfo)
SchemaTables(schema model.CIStr) []table.Table
SchemaMetaVersion() int64
}
// Information Schema Name.
const (
Name = "INFORMATION_SCHEMA"
)
type infoSchema struct {
schemaNameToID map[string]int64
tableNameToID map[tableName]int64
columnNameToID map[columnName]int64
schemas map[int64]*model.DBInfo
tables map[int64]table.Table
tableAllocators map[int64]autoid.Allocator
columns map[int64]*model.ColumnInfo
indices map[indexName]*model.IndexInfo
columnIndices map[int64][]*model.IndexInfo
// We should check version when change schema.
schemaMetaVersion int64
}
// MockInfoSchema only serves for test.
func MockInfoSchema(tbList []*model.TableInfo) InfoSchema {
result := &infoSchema{}
result.schemaNameToID = make(map[string]int64)
result.tableNameToID = make(map[tableName]int64)
result.schemas = make(map[int64]*model.DBInfo)
result.tables = make(map[int64]table.Table)
result.schemaNameToID["test"] = 0
result.schemas[0] = &model.DBInfo{ID: 0, Name: model.NewCIStr("test"), Tables: tbList}
for i, tb := range tbList {
result.tableNameToID[tableName{schema: "test", table: tb.Name.L}] = int64(i)
result.tables[int64(i)] = table.MockTableFromMeta(tb)
}
return result
}
var _ InfoSchema = (*infoSchema)(nil)
type tableName struct {
schema string
table string
}
type columnName struct {
tableName
name string
}
type indexName struct {
tableName
name string
}
func (is *infoSchema) SchemaByName(schema model.CIStr) (val *model.DBInfo, ok bool) {
id, ok := is.schemaNameToID[schema.L]
if !ok {
return
}
val, ok = is.schemas[id]
return
}
func (is *infoSchema) SchemaMetaVersion() int64 {
return is.schemaMetaVersion
}
func (is *infoSchema) SchemaExists(schema model.CIStr) bool {
_, ok := is.schemaNameToID[schema.L]
return ok
}
func (is *infoSchema) TableByName(schema, table model.CIStr) (t table.Table, err error) {
id, ok := is.tableNameToID[tableName{schema: schema.L, table: table.L}]
if !ok {
return nil, ErrTableNotExists.Gen("table %s.%s does not exist", schema, table)
}
t = is.tables[id]
return
}
func (is *infoSchema) TableExists(schema, table model.CIStr) bool {
_, ok := is.tableNameToID[tableName{schema: schema.L, table: table.L}]
return ok
}
func (is *infoSchema) ColumnByName(schema, table, column model.CIStr) (val *model.ColumnInfo, ok bool) {
id, ok := is.columnNameToID[columnName{tableName: tableName{schema: schema.L, table: table.L}, name: column.L}]
if !ok {
return
}
val, ok = is.columns[id]
return
}
func (is *infoSchema) ColumnExists(schema, table, column model.CIStr) bool {
_, ok := is.columnNameToID[columnName{tableName: tableName{schema: schema.L, table: table.L}, name: column.L}]
return ok
}
func (is *infoSchema) IndexByName(schema, table, index model.CIStr) (val *model.IndexInfo, ok bool) {
val, ok = is.indices[indexName{tableName: tableName{schema: schema.L, table: table.L}, name: index.L}]
return
}
func (is *infoSchema) SchemaByID(id int64) (val *model.DBInfo, ok bool) {
val, ok = is.schemas[id]
return
}
func (is *infoSchema) TableByID(id int64) (val table.Table, ok bool) {
val, ok = is.tables[id]
return
}
func (is *infoSchema) AllocByID(id int64) (val autoid.Allocator, ok bool) {
val, ok = is.tableAllocators[id]
return
}
func (is *infoSchema) ColumnByID(id int64) (val *model.ColumnInfo, ok bool) {
val, ok = is.columns[id]
return
}
func (is *infoSchema) ColumnIndicesByID(id int64) (indices []*model.IndexInfo, ok bool) {
indices, ok = is.columnIndices[id]
return
}
func (is *infoSchema) AllSchemaNames() (names []string) {
for _, v := range is.schemas {
names = append(names, v.Name.O)
}
return
}
func (is *infoSchema) AllSchemas() (schemas []*model.DBInfo) {
for _, v := range is.schemas {
schemas = append(schemas, v)
}
return
}
func (is *infoSchema) SchemaTables(schema model.CIStr) (tables []table.Table) {
di, ok := is.SchemaByName(schema)
if !ok {
return
}
for _, ti := range di.Tables {
tables = append(tables, is.tables[ti.ID])
}
return
}
func (is *infoSchema) Clone() (result []*model.DBInfo) {
for _, v := range is.schemas {
result = append(result, v.Clone())
}
return
}
// Handle handles information schema, including getting and setting.
type Handle struct {
value atomic.Value
store kv.Storage
memSchema *memSchemaHandle
}
// NewHandle creates a new Handle.
func NewHandle(store kv.Storage) (*Handle, error) {
h := &Handle{
store: store,
}
// init memory tables
var err error
h.memSchema, err = newMemSchemaHandle()
if err != nil {
return nil, errors.Trace(err)
}
return h, nil
}
// Init memory schemas including infoschema and perfshcema.
func newMemSchemaHandle() (*memSchemaHandle, error) {
h := &memSchemaHandle{
nameToTable: make(map[string]table.Table),
}
err := initMemoryTables(h)
if err != nil {
return nil, errors.Trace(err)
}
initMemoryTables(h)
h.perfHandle, err = perfschema.NewPerfHandle()
if err != nil {
return nil, errors.Trace(err)
}
return h, nil
}
// memSchemaHandle is used to store memory schema information.
type memSchemaHandle struct {
// Information Schema
isDB *model.DBInfo
schemataTbl table.Table
tablesTbl table.Table
columnsTbl table.Table
statisticsTbl table.Table
charsetTbl table.Table
collationsTbl table.Table
filesTbl table.Table
defTbl table.Table
profilingTbl table.Table
partitionsTbl table.Table
nameToTable map[string]table.Table
// Performance Schema
perfHandle perfschema.PerfSchema
}
func initMemoryTables(h *memSchemaHandle) error {
// Init Information_Schema
var (
err error
tbl table.Table
)
dbID := autoid.GenLocalSchemaID()
isTables := make([]*model.TableInfo, 0, len(tableNameToColumns))
for name, cols := range tableNameToColumns {
meta := buildTableMeta(name, cols)
isTables = append(isTables, meta)
meta.ID = autoid.GenLocalSchemaID()
for _, c := range meta.Columns {
c.ID = autoid.GenLocalSchemaID()
}
alloc := autoid.NewMemoryAllocator(dbID)
tbl, err = createMemoryTable(meta, alloc)
if err != nil {
return errors.Trace(err)
}
h.nameToTable[meta.Name.L] = tbl
}
h.schemataTbl = h.nameToTable[strings.ToLower(tableSchemata)]
h.tablesTbl = h.nameToTable[strings.ToLower(tableTables)]
h.columnsTbl = h.nameToTable[strings.ToLower(tableColumns)]
h.statisticsTbl = h.nameToTable[strings.ToLower(tableStatistics)]
h.charsetTbl = h.nameToTable[strings.ToLower(tableCharacterSets)]
h.collationsTbl = h.nameToTable[strings.ToLower(tableCollations)]
// CharacterSets/Collations contain static data. Init them now.
err = insertData(h.charsetTbl, dataForCharacterSets())
if err != nil {
return errors.Trace(err)
}
err = insertData(h.collationsTbl, dataForColltions())
if err != nil {
return errors.Trace(err)
}
// create db
h.isDB = &model.DBInfo{
ID: dbID,
Name: model.NewCIStr(Name),
Charset: mysql.DefaultCharset,
Collate: mysql.DefaultCollationName,
Tables: isTables,
}
return nil
}
func insertData(tbl table.Table, rows [][]types.Datum) error {
for _, r := range rows {
_, err := tbl.AddRecord(nil, r)
if err != nil {
return errors.Trace(err)
}
}
return nil
}
func refillTable(tbl table.Table, rows [][]types.Datum) error {
err := tbl.Truncate(nil)
if err != nil {
return errors.Trace(err)
}
return insertData(tbl, rows)
}
// Set sets DBInfo to information schema.
func (h *Handle) Set(newInfo []*model.DBInfo, schemaMetaVersion int64) error {
info := &infoSchema{
schemaNameToID: map[string]int64{},
tableNameToID: map[tableName]int64{},
columnNameToID: map[columnName]int64{},
schemas: map[int64]*model.DBInfo{},
tables: map[int64]table.Table{},
tableAllocators: map[int64]autoid.Allocator{},
columns: map[int64]*model.ColumnInfo{},
indices: map[indexName]*model.IndexInfo{},
columnIndices: map[int64][]*model.IndexInfo{},
schemaMetaVersion: schemaMetaVersion,
}
var err error
var hasOldInfo bool
infoschema := h.Get()
if infoschema != nil {
hasOldInfo = true
}
for _, di := range newInfo {
info.schemas[di.ID] = di
info.schemaNameToID[di.Name.L] = di.ID
for _, t := range di.Tables {
alloc := autoid.NewAllocator(h.store, di.ID)
if hasOldInfo {
val, ok := infoschema.AllocByID(t.ID)
if ok {
alloc = val
}
}
info.tableAllocators[t.ID] = alloc
info.tables[t.ID], err = table.TableFromMeta(alloc, t)
if err != nil {
return errors.Trace(err)
}
tname := tableName{di.Name.L, t.Name.L}
info.tableNameToID[tname] = t.ID
for _, c := range t.Columns {
info.columns[c.ID] = c
info.columnNameToID[columnName{tname, c.Name.L}] = c.ID
}
for _, idx := range t.Indices {
info.indices[indexName{tname, idx.Name.L}] = idx
for _, idxCol := range idx.Columns {
columnID := t.Columns[idxCol.Offset].ID
columnIndices := info.columnIndices[columnID]
info.columnIndices[columnID] = append(columnIndices, idx)
}
}
}
}
// Build Information_Schema
info.schemaNameToID[h.memSchema.isDB.Name.L] = h.memSchema.isDB.ID
info.schemas[h.memSchema.isDB.ID] = h.memSchema.isDB
for _, t := range h.memSchema.isDB.Tables {
tbl, ok := h.memSchema.nameToTable[t.Name.L]
if !ok {
return ErrTableNotExists.Gen("table `%s` is missing.", t.Name)
}
info.tables[t.ID] = tbl
tname := tableName{h.memSchema.isDB.Name.L, t.Name.L}
info.tableNameToID[tname] = t.ID
for _, c := range t.Columns {
info.columns[c.ID] = c
info.columnNameToID[columnName{tname, c.Name.L}] = c.ID
}
}
// Add Performance_Schema
psDB := h.memSchema.perfHandle.GetDBMeta()
info.schemaNameToID[psDB.Name.L] = psDB.ID
info.schemas[psDB.ID] = psDB
for _, t := range psDB.Tables {
tbl, ok := h.memSchema.perfHandle.GetTable(t.Name.O)
if !ok {
return ErrTableNotExists.Gen("table `%s` is missing.", t.Name)
}
info.tables[t.ID] = tbl
tname := tableName{psDB.Name.L, t.Name.L}
info.tableNameToID[tname] = t.ID
for _, c := range t.Columns {
info.columns[c.ID] = c
info.columnNameToID[columnName{tname, c.Name.L}] = c.ID
}
}
// Should refill some tables in Information_Schema.
// schemata/tables/columns/statistics
dbNames := make([]string, 0, len(info.schemas))
dbInfos := make([]*model.DBInfo, 0, len(info.schemas))
for _, v := range info.schemas {
dbNames = append(dbNames, v.Name.L)
dbInfos = append(dbInfos, v)
}
err = refillTable(h.memSchema.schemataTbl, dataForSchemata(dbNames))
if err != nil {
return errors.Trace(err)
}
err = refillTable(h.memSchema.tablesTbl, dataForTables(dbInfos))
if err != nil {
return errors.Trace(err)
}
err = refillTable(h.memSchema.columnsTbl, dataForColumns(dbInfos))
if err != nil {
return errors.Trace(err)
}
err = refillTable(h.memSchema.statisticsTbl, dataForStatistics(dbInfos))
if err != nil {
return errors.Trace(err)
}
h.value.Store(info)
return nil
}
// Get gets information schema from Handle.
func (h *Handle) Get() InfoSchema {
v := h.value.Load()
schema, _ := v.(InfoSchema)
return schema
}
// GetPerfHandle gets performance schema from handle.
func (h *Handle) GetPerfHandle() perfschema.PerfSchema {
return h.memSchema.perfHandle
}
// Schema error codes.
const (
codeDBDropExists terror.ErrCode = 1008
codeDatabaseNotExists = 1049
codeTableNotExists = 1146
codeColumnNotExists = 1054
codeCannotAddForeign = 1215
codeForeignKeyNotExists = 1091
codeDatabaseExists = 1007
codeTableExists = 1050
codeBadTable = 1051
codeColumnExists = 1060
codeIndexExists = 1831
)
func init() {
schemaMySQLErrCodes := map[terror.ErrCode]uint16{
codeDBDropExists: mysql.ErrDBDropExists,
codeDatabaseNotExists: mysql.ErrBadDB,
codeTableNotExists: mysql.ErrNoSuchTable,
codeColumnNotExists: mysql.ErrBadField,
codeCannotAddForeign: mysql.ErrCannotAddForeign,
codeForeignKeyNotExists: mysql.ErrCantDropFieldOrKey,
codeDatabaseExists: mysql.ErrDBCreateExists,
codeTableExists: mysql.ErrTableExists,
codeBadTable: mysql.ErrBadTable,
codeColumnExists: mysql.ErrDupFieldName,
codeIndexExists: mysql.ErrDupIndex,
}
terror.ErrClassToMySQLCodes[terror.ClassSchema] = schemaMySQLErrCodes
}