Skip to content

Commit

Permalink
ddl: support mock table info. (pingcap#5759)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfei1991 authored Feb 1, 2018
1 parent ddca655 commit afd49aa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
9 changes: 9 additions & 0 deletions cmd/importer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func NewConfig() *Config {
fs.StringVar(&cfg.DDLCfg.TableSQL, "t", "", "create table sql")
fs.StringVar(&cfg.DDLCfg.IndexSQL, "i", "", "create index sql")

fs.StringVar(&cfg.StatsCfg.Path, "s", "", "load stats file path")

fs.IntVar(&cfg.SysCfg.WorkerCount, "c", 2, "parallel worker count")
fs.IntVar(&cfg.SysCfg.JobCount, "n", 10000, "total job count")
fs.IntVar(&cfg.SysCfg.Batch, "b", 1000, "insert batch commit count")
Expand Down Expand Up @@ -85,6 +87,11 @@ type SysConfig struct {
Batch int `toml:"batch" json:"batch"`
}

// StatsConfig is the configuration for statistics file.
type StatsConfig struct {
Path string `toml:"stats-file-path" json:"stats-file-path"`
}

// Config is the configuration.
type Config struct {
*flag.FlagSet `json:"-"`
Expand All @@ -93,6 +100,8 @@ type Config struct {

DDLCfg DDLConfig `toml:"ddl" json:"ddl"`

StatsCfg StatsConfig `toml:"stats" json:"stats"`

SysCfg SysConfig `toml:"sys" json:"sys"`

configFile string
Expand Down
26 changes: 17 additions & 9 deletions cmd/importer/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ import (

"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/mock"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -117,12 +120,12 @@ func (col *column) parseColumnOptions(ops []*ast.ColumnOption) {
}

type table struct {
name string
columns []*column
columnList string
indices map[string]*column
uniqIndices map[string]*column
unsignedCols map[string]*column
name string
columns []*column
columnList string
indices map[string]*column
uniqIndices map[string]*column
tblInfo *model.TableInfo
}

func (t *table) printColumns() string {
Expand Down Expand Up @@ -160,9 +163,8 @@ func (t *table) String() string {

func newTable() *table {
return &table{
indices: make(map[string]*column),
uniqIndices: make(map[string]*column),
unsignedCols: make(map[string]*column),
indices: make(map[string]*column),
uniqIndices: make(map[string]*column),
}
}

Expand Down Expand Up @@ -204,6 +206,12 @@ func parseTable(t *table, stmt *ast.CreateTableStmt) error {
t.name = stmt.Table.Name.L
t.columns = make([]*column, 0, len(stmt.Cols))

mockTbl, err := ddl.MockTableInfo(mock.NewContext(), stmt, 1)
if err != nil {
return errors.Trace(err)
}
t.tblInfo = mockTbl

for i, col := range stmt.Cols {
column := &column{idx: i + 1, table: t, step: defaultStep, data: newDatum()}
column.parseColumn(col)
Expand Down
14 changes: 9 additions & 5 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,17 @@ func checkConstraintNames(constraints []*ast.Constraint) error {
return nil
}

func (d *ddl) buildTableInfo(tableName model.CIStr, cols []*table.Column, constraints []*ast.Constraint, ctx context.Context) (tbInfo *model.TableInfo, err error) {
func buildTableInfo(d *ddl, tableName model.CIStr, cols []*table.Column, constraints []*ast.Constraint, ctx context.Context) (tbInfo *model.TableInfo, err error) {
tbInfo = &model.TableInfo{
Name: tableName,
}
tbInfo.ID, err = d.genGlobalID()
if err != nil {
return nil, errors.Trace(err)
// When this function is called by MockTableInfo, we should set a particular table id.
// So the `ddl` structure may be nil.
if d != nil {
tbInfo.ID, err = d.genGlobalID()
if err != nil {
return nil, errors.Trace(err)
}
}
for _, v := range cols {
v.ID = allocateColumnID(tbInfo)
Expand Down Expand Up @@ -768,7 +772,7 @@ func (d *ddl) CreateTable(ctx context.Context, ident ast.Ident, colDefs []*ast.C
return errors.Trace(err)
}

tbInfo, err := d.buildTableInfo(ident.Name, cols, newConstraints, ctx)
tbInfo, err := buildTableInfo(d, ident.Name, cols, newConstraints, ctx)
if err != nil {
return errors.Trace(err)
}
Expand Down
16 changes: 16 additions & 0 deletions ddl/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

"github.com/coreos/etcd/clientv3"
"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/model"
goctx "golang.org/x/net/context"
)
Expand Down Expand Up @@ -119,3 +121,17 @@ func (dr *mockDelRange) start() {
func (dr *mockDelRange) clear() {
return
}

// MockTableInfo mocks a table info by create table stmt ast and a specified table id.
func MockTableInfo(ctx context.Context, stmt *ast.CreateTableStmt, tableID int64) (*model.TableInfo, error) {
cols, newConstraints, err := buildColumnsAndConstraints(ctx, stmt.Cols, stmt.Constraints)
if err != nil {
return nil, errors.Trace(err)
}
tbl, err := buildTableInfo(nil, stmt.Table.Name, cols, newConstraints, ctx)
if err != nil {
return nil, errors.Trace(err)
}
tbl.ID = tableID
return tbl, nil
}

0 comments on commit afd49aa

Please sign in to comment.