// Set sets DBInfo to information schema. func (h *Handle) Set(newInfo []*model.DBInfo) { info := &infoSchema{ schemaNameToID: map[string]int64{}, tableNameToID: map[tableName]int64{}, columnNameToID: map[columnName]int64{}, schemas: map[int64]*model.DBInfo{}, tables: map[int64]table.Table{}, columns: map[int64]*model.ColumnInfo{}, indices: map[indexName]*model.IndexInfo{}, columnIndices: map[int64][]*model.IndexInfo{}, } 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) info.tables[t.ID] = table.TableFromMeta(di.Name.L, alloc, t) 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) } } } } h.value.Store(info) }
func (d *ddl) dropSchemaData(dbInfo *model.DBInfo, tables []*model.TableInfo) error { for _, tblInfo := range tables { alloc := autoid.NewAllocator(d.store, dbInfo.ID) t := table.TableFromMeta(alloc, tblInfo) err := d.dropTableData(t) if err != nil { return errors.Trace(err) } } return nil }
func testGetTable(c *C, d *ddl, schemaID int64, tableID int64) table.Table { var tblInfo *model.TableInfo kv.RunInNewTxn(d.store, false, func(txn kv.Transaction) error { t := meta.NewMeta(txn) var err error tblInfo, err = t.GetTable(schemaID, tableID) c.Assert(err, IsNil) c.Assert(tblInfo, NotNil) return nil }) alloc := autoid.NewAllocator(d.store, schemaID) tbl := table.TableFromMeta(alloc, tblInfo) return tbl }
// If create table with auto_increment option, we should rebase tableAutoIncID value. func (d *ddl) handleAutoIncID(tbInfo *model.TableInfo, schemaID int64) error { alloc := autoid.NewAllocator(d.store, schemaID) tbInfo.State = model.StatePublic tb, err := table.TableFromMeta(alloc, tbInfo) if err != nil { return errors.Trace(err) } // The operation of the minus 1 to make sure that the current value doesn't be used, // the next Alloc operation will get this value. // Its behavior is consistent with MySQL. if err = tb.RebaseAutoID(tbInfo.AutoIncID-1, false); err != nil { return errors.Trace(err) } return nil }
func (d *ddl) handleTableOptions(options []*ast.TableOption, tbInfo *model.TableInfo, schemaID int64) error { for _, op := range options { if op.Tp == ast.TableOptionAutoIncrement { alloc := autoid.NewAllocator(d.store, schemaID) tbInfo.State = model.StatePublic tb, err := table.TableFromMeta(alloc, tbInfo) if err != nil { return errors.Trace(err) } // The operation of the minus 1 to make sure that the current value doesn't be used, // the next Alloc operation will get this value. // Its behavior is consistent with MySQL. if err = tb.RebaseAutoID(int64(op.UintValue-1), false); err != nil { return errors.Trace(err) } } } return nil }
// 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{}, columns: map[int64]*model.ColumnInfo{}, indices: map[indexName]*model.IndexInfo{}, columnIndices: map[int64][]*model.IndexInfo{}, schemaMetaVersion: schemaMetaVersion, } var err error 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) 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) } } } } h.value.Store(info) return nil }
func getCurrentTable(d *ddl, schemaID, tableID int64) (table.Table, error) { var tblInfo *model.TableInfo err := kv.RunInNewTxn(d.store, false, func(txn kv.Transaction) error { t := meta.NewMeta(txn) var err error tblInfo, err = t.GetTable(schemaID, tableID) if err != nil { return errors.Trace(err) } return nil }) if err != nil { return nil, errors.Trace(err) } alloc := autoid.NewAllocator(d.store, schemaID) tbl, err := table.TableFromMeta(alloc, tblInfo) if err != nil { return nil, errors.Trace(err) } return tbl, err }
// InitWithDBInfos initializes an empty new InfoSchema with a slice of DBInfo and schema version. func (b *Builder) InitWithDBInfos(dbInfos []*model.DBInfo, schemaVersion int64) (*Builder, error) { err := b.initMemorySchemas() if err != nil { return nil, errors.Trace(err) } info := b.is info.schemaMetaVersion = schemaVersion for _, di := range dbInfos { info.schemas[di.ID] = di info.schemaNameToID[di.Name.L] = di.ID for _, t := range di.Tables { alloc := autoid.NewAllocator(b.handle.store, di.ID) var tbl table.Table tbl, err = table.TableFromMeta(alloc, t) if err != nil { return nil, errors.Trace(err) } info.tables[t.ID] = tbl tname := makeTableName(di.Name.L, t.Name.L) info.tableNameToID[string(tname)] = t.ID } } return b, nil }
func (d *ddl) getTable(schemaID int64, tblInfo *model.TableInfo) (table.Table, error) { alloc := autoid.NewAllocator(d.store, schemaID) tbl, err := table.TableFromMeta(alloc, tblInfo) return tbl, errors.Trace(err) }
func (d *ddl) getTable(t *meta.Meta, schemaID int64, tblInfo *model.TableInfo) (table.Table, error) { alloc := autoid.NewAllocator(d.store, schemaID) tbl := table.TableFromMeta(alloc, tblInfo) return tbl, nil }
// 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 }