本文整理匯總了Golang中github.com/pingcap/tidb/model.Job.DecodeArgs方法的典型用法代碼示例。如果您正苦於以下問題:Golang Job.DecodeArgs方法的具體用法?Golang Job.DecodeArgs怎麽用?Golang Job.DecodeArgs使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/pingcap/tidb/model.Job
的用法示例。
在下文中一共展示了Job.DecodeArgs方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: onModifyColumn
func (d *ddl) onModifyColumn(t *meta.Meta, job *model.Job) error {
tblInfo, err := d.getTableInfo(t, job)
if err != nil {
return errors.Trace(err)
}
newCol := &model.ColumnInfo{}
oldColName := &model.CIStr{}
err = job.DecodeArgs(newCol, oldColName)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
oldCol := findCol(tblInfo.Columns, oldColName.L)
if oldCol == nil || oldCol.State != model.StatePublic {
job.State = model.JobCancelled
return infoschema.ErrColumnNotExists.GenByArgs(newCol.Name, tblInfo.Name)
}
*oldCol = *newCol
err = t.UpdateTable(job.SchemaID, tblInfo)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
ver, err := updateSchemaVersion(t, job)
if err != nil {
return errors.Trace(err)
}
job.SchemaState = model.StatePublic
job.State = model.JobDone
addTableHistoryInfo(job, ver, tblInfo)
return nil
}
示例2: delReorgSchema
func (d *ddl) delReorgSchema(t *meta.Meta, job *model.Job) error {
dbInfo := &model.DBInfo{}
if err := job.DecodeArgs(dbInfo); err != nil {
// arg error, cancel this job.
job.State = model.JobCancelled
return errors.Trace(err)
}
tables, err := t.ListTables(dbInfo.ID)
if terror.ErrorEqual(meta.ErrDBNotExists, err) {
job.State = model.JobDone
return nil
}
if err != nil {
return errors.Trace(err)
}
if err = d.dropSchemaData(dbInfo, tables); err != nil {
return errors.Trace(err)
}
// finish this background job
job.SchemaState = model.StateNone
job.State = model.JobDone
return nil
}
示例3: onDropForeignKey
func (d *ddl) onDropForeignKey(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
tblInfo, err := d.getTableInfo(t, job)
if err != nil {
return errors.Trace(err)
}
var (
fkName model.CIStr
found bool
fkInfo model.FKInfo
)
err = job.DecodeArgs(&fkName)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
for _, fk := range tblInfo.ForeignKeys {
if fk.Name.L == fkName.L {
found = true
fkInfo = *fk
}
}
if !found {
return infoschema.ErrForeignKeyNotExists.Gen("foreign key doesn't exist", fkName)
}
nfks := tblInfo.ForeignKeys[:0]
for _, fk := range tblInfo.ForeignKeys {
if fk.Name.L != fkName.L {
nfks = append(nfks, fk)
}
}
tblInfo.ForeignKeys = nfks
_, err = t.GenSchemaVersion()
if err != nil {
return errors.Trace(err)
}
switch fkInfo.State {
case model.StatePublic:
// We just support record the foreign key, so we just make it none.
// public -> none
job.SchemaState = model.StateNone
fkInfo.State = model.StateNone
err = t.UpdateTable(schemaID, tblInfo)
if err != nil {
return errors.Trace(err)
}
// finish this job
job.State = model.JobDone
return nil
default:
return ErrInvalidForeignKeyState.Gen("invalid fk state %v", fkInfo.State)
}
}
示例4: delReorgTable
func (d *ddl) delReorgTable(t *meta.Meta, job *model.Job) error {
tblInfo := &model.TableInfo{}
err := job.DecodeArgs(tblInfo)
if err != nil {
// arg error, cancel this job.
job.State = model.JobCancelled
return errors.Trace(err)
}
tblInfo.State = model.StateDeleteReorganization
tbl, err := d.getTable(job.SchemaID, tblInfo)
if err != nil {
return errors.Trace(err)
}
err = d.dropTableData(tbl)
if err != nil {
return errors.Trace(err)
}
// finish this background job
job.SchemaState = model.StateNone
job.State = model.JobDone
return nil
}
示例5: onCreateSchema
func (d *ddl) onCreateSchema(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
var name model.CIStr
if err := job.DecodeArgs(&name); err != nil {
// arg error, cancel this job.
job.State = model.JobCancelled
return errors.Trace(err)
}
dbInfo := &model.DBInfo{
ID: schemaID,
Name: name,
State: model.StateNone,
}
dbs, err := t.ListDatabases()
if err != nil {
return errors.Trace(err)
}
for _, db := range dbs {
if db.Name.L == name.L {
if db.ID != schemaID {
// database exists, can't create, we should cancel this job now.
job.State = model.JobCancelled
return errors.Trace(ErrExists)
}
dbInfo = db
}
}
_, err = t.GenSchemaVersion()
if err != nil {
return errors.Trace(err)
}
switch dbInfo.State {
case model.StateNone:
// none -> public
job.SchemaState = model.StatePublic
dbInfo.State = model.StatePublic
err = t.CreateDatabase(dbInfo)
if err != nil {
return errors.Trace(err)
}
// finish this job
job.State = model.JobDone
return nil
default:
// we can't enter here.
return errors.Errorf("invalid db state %v", dbInfo.State)
}
}
示例6: onCreateTable
func (d *ddl) onCreateTable(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
tbInfo := &model.TableInfo{}
if err := job.DecodeArgs(tbInfo); err != nil {
// arg error, cancel this job.
job.State = model.JobCancelled
return errors.Trace(err)
}
tbInfo.State = model.StateNone
tables, err := t.ListTables(schemaID)
if terror.ErrorEqual(err, meta.ErrDBNotExists) {
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrDatabaseNotExists)
} else if err != nil {
return errors.Trace(err)
}
for _, tbl := range tables {
if tbl.Name.L == tbInfo.Name.L {
if tbl.ID != tbInfo.ID {
// table exists, can't create, we should cancel this job now.
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrTableExists)
}
tbInfo = tbl
}
}
ver, err := updateSchemaVersion(t, job)
if err != nil {
return errors.Trace(err)
}
switch tbInfo.State {
case model.StateNone:
// none -> public
job.SchemaState = model.StatePublic
tbInfo.State = model.StatePublic
err = t.CreateTable(schemaID, tbInfo)
if err != nil {
return errors.Trace(err)
}
// finish this job
job.State = model.JobDone
addTableHistoryInfo(job, ver, tbInfo)
return nil
default:
return ErrInvalidTableState.Gen("invalid table state %v", tbInfo.State)
}
}
示例7: onCreateSchema
func (d *ddl) onCreateSchema(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
dbInfo := &model.DBInfo{}
if err := job.DecodeArgs(dbInfo); err != nil {
// arg error, cancel this job.
job.State = model.JobCancelled
return errors.Trace(err)
}
dbInfo.ID = schemaID
dbInfo.State = model.StateNone
dbs, err := t.ListDatabases()
if err != nil {
return errors.Trace(err)
}
for _, db := range dbs {
if db.Name.L == dbInfo.Name.L {
if db.ID != schemaID {
// database exists, can't create, we should cancel this job now.
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrDatabaseExists)
}
dbInfo = db
}
}
ver, err := updateSchemaVersion(t, job)
if err != nil {
return errors.Trace(err)
}
switch dbInfo.State {
case model.StateNone:
// none -> public
job.SchemaState = model.StatePublic
dbInfo.State = model.StatePublic
err = t.CreateDatabase(dbInfo)
if err != nil {
return errors.Trace(err)
}
// finish this job
job.State = model.JobDone
addDBHistoryInfo(job, ver, dbInfo)
return nil
default:
// we can't enter here.
return errors.Errorf("invalid db state %v", dbInfo.State)
}
}
示例8: delReorgTable
func (d *ddl) delReorgTable(t *meta.Meta, job *model.Job) error {
var startKey kv.Key
if err := job.DecodeArgs(&startKey); err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
limit := reorgTableDeleteLimit
delCount, err := d.dropTableData(startKey, job, limit)
if err != nil {
return errors.Trace(err)
}
// Finish this background job.
if delCount < limit {
job.SchemaState = model.StateNone
job.State = model.JobDone
}
return nil
}
示例9: onTruncateTable
// onTruncateTable delete old table meta, and creates a new table identical to old table except for table ID.
// As all the old data is encoded with old table ID, it can not be accessed any more.
// A background job will be created to delete old data.
func (d *ddl) onTruncateTable(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
tableID := job.TableID
var newTableID int64
err := job.DecodeArgs(&newTableID)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
tblInfo, err := t.GetTable(schemaID, tableID)
if terror.ErrorEqual(err, meta.ErrDBNotExists) {
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrDatabaseNotExists)
} else if err != nil {
return errors.Trace(err)
}
if tblInfo == nil {
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrTableNotExists)
}
err = t.DropTable(schemaID, tableID)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
tblInfo.ID = newTableID
err = t.CreateTable(schemaID, tblInfo)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
ver, err := updateSchemaVersion(t, job)
if err != nil {
return errors.Trace(err)
}
job.State = model.JobDone
addTableHistoryInfo(job, ver, tblInfo)
startKey := tablecodec.EncodeTablePrefix(tableID)
job.Args = append(job.Args, startKey)
return nil
}
示例10: delReorgSchema
func (d *ddl) delReorgSchema(t *meta.Meta, job *model.Job) error {
var startKey kv.Key
var tableIDs []int64
if err := job.DecodeArgs(&tableIDs, &startKey); err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
isFinished, err := d.dropSchemaData(tableIDs, startKey, job, t)
if err != nil {
return errors.Trace(err)
}
if !isFinished {
return nil
}
// finish this background job
job.SchemaState = model.StateNone
job.State = model.JobDone
return nil
}
示例11: onCreateForeignKey
func (d *ddl) onCreateForeignKey(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
tblInfo, err := d.getTableInfo(t, job)
if err != nil {
return errors.Trace(err)
}
var fkInfo model.FKInfo
err = job.DecodeArgs(&fkInfo)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
fkInfo.ID = allocateIndexID(tblInfo)
tblInfo.ForeignKeys = append(tblInfo.ForeignKeys, &fkInfo)
ver, err := updateSchemaVersion(t, job)
if err != nil {
return errors.Trace(err)
}
switch fkInfo.State {
case model.StateNone:
// We just support record the foreign key, so we just make it public.
// none -> public
job.SchemaState = model.StatePublic
fkInfo.State = model.StatePublic
err = t.UpdateTable(schemaID, tblInfo)
if err != nil {
return errors.Trace(err)
}
// Finish this job.
job.State = model.JobDone
addTableHistoryInfo(job, ver, tblInfo)
return nil
default:
return ErrInvalidForeignKeyState.Gen("invalid fk state %v", fkInfo.State)
}
}
示例12: updateSchemaVersion
// updateSchemaVersion increments the schema version by 1 and sets SchemaDiff.
func updateSchemaVersion(t *meta.Meta, job *model.Job) (int64, error) {
schemaVersion, err := t.GenSchemaVersion()
if err != nil {
return 0, errors.Trace(err)
}
diff := &model.SchemaDiff{
Version: schemaVersion,
Type: job.Type,
SchemaID: job.SchemaID,
}
if job.Type == model.ActionTruncateTable {
// Truncate table has two table ID, should be handled differently.
err = job.DecodeArgs(&diff.TableID)
if err != nil {
return 0, errors.Trace(err)
}
diff.OldTableID = job.TableID
} else {
diff.TableID = job.TableID
}
err = t.SetSchemaDiff(schemaVersion, diff)
return schemaVersion, errors.Trace(err)
}
示例13: onDropIndex
func (d *ddl) onDropIndex(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
tblInfo, err := d.getTableInfo(t, job)
if err != nil {
return errors.Trace(err)
}
var indexName model.CIStr
if err = job.DecodeArgs(&indexName); err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
var indexInfo *model.IndexInfo
for _, idx := range tblInfo.Indices {
if idx.Name.L == indexName.L {
indexInfo = idx
}
}
if indexInfo == nil {
job.State = model.JobCancelled
return ErrCantDropFieldOrKey.Gen("index %s doesn't exist", indexName)
}
_, err = t.GenSchemaVersion()
if err != nil {
return errors.Trace(err)
}
switch indexInfo.State {
case model.StatePublic:
// public -> write only
job.SchemaState = model.StateWriteOnly
indexInfo.State = model.StateWriteOnly
err = t.UpdateTable(schemaID, tblInfo)
return errors.Trace(err)
case model.StateWriteOnly:
// write only -> delete only
job.SchemaState = model.StateDeleteOnly
indexInfo.State = model.StateDeleteOnly
err = t.UpdateTable(schemaID, tblInfo)
return errors.Trace(err)
case model.StateDeleteOnly:
// delete only -> reorganization
job.SchemaState = model.StateDeleteReorganization
indexInfo.State = model.StateDeleteReorganization
err = t.UpdateTable(schemaID, tblInfo)
return errors.Trace(err)
case model.StateDeleteReorganization:
// reorganization -> absent
tbl, err := d.getTable(schemaID, tblInfo)
if err != nil {
return errors.Trace(err)
}
err = d.runReorgJob(func() error {
return d.dropTableIndex(tbl, indexInfo)
})
if terror.ErrorEqual(err, errWaitReorgTimeout) {
// if timeout, we should return, check for the owner and re-wait job done.
return nil
}
if err != nil {
return errors.Trace(err)
}
// all reorganization jobs done, drop this index
newIndices := make([]*model.IndexInfo, 0, len(tblInfo.Indices))
for _, idx := range tblInfo.Indices {
if idx.Name.L != indexName.L {
newIndices = append(newIndices, idx)
}
}
tblInfo.Indices = newIndices
// set column index flag.
dropIndexColumnFlag(tblInfo, indexInfo)
if err = t.UpdateTable(schemaID, tblInfo); err != nil {
return errors.Trace(err)
}
// finish this job
job.SchemaState = model.StateNone
job.State = model.JobDone
return nil
default:
return ErrInvalidTableState.Gen("invalid table state %v", tblInfo.State)
}
}
示例14: onCreateIndex
func (d *ddl) onCreateIndex(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
tblInfo, err := d.getTableInfo(t, job)
if err != nil {
return errors.Trace(err)
}
var (
unique bool
indexName model.CIStr
indexID int64
idxColNames []*ast.IndexColName
)
err = job.DecodeArgs(&unique, &indexName, &indexID, &idxColNames)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
var indexInfo *model.IndexInfo
for _, idx := range tblInfo.Indices {
if idx.Name.L == indexName.L {
if idx.State == model.StatePublic {
// we already have a index with same index name
job.State = model.JobCancelled
return infoschema.ErrIndexExists.Gen("CREATE INDEX: index already exist %s", indexName)
}
indexInfo = idx
}
}
if indexInfo == nil {
indexInfo, err = buildIndexInfo(tblInfo, unique, indexName, indexID, idxColNames)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
tblInfo.Indices = append(tblInfo.Indices, indexInfo)
}
_, err = t.GenSchemaVersion()
if err != nil {
return errors.Trace(err)
}
switch indexInfo.State {
case model.StateNone:
// none -> delete only
job.SchemaState = model.StateDeleteOnly
indexInfo.State = model.StateDeleteOnly
err = t.UpdateTable(schemaID, tblInfo)
return errors.Trace(err)
case model.StateDeleteOnly:
// delete only -> write only
job.SchemaState = model.StateWriteOnly
indexInfo.State = model.StateWriteOnly
err = t.UpdateTable(schemaID, tblInfo)
return errors.Trace(err)
case model.StateWriteOnly:
// write only -> reorganization
job.SchemaState = model.StateWriteReorganization
indexInfo.State = model.StateWriteReorganization
// initialize SnapshotVer to 0 for later reorganization check.
job.SnapshotVer = 0
err = t.UpdateTable(schemaID, tblInfo)
return errors.Trace(err)
case model.StateWriteReorganization:
// reorganization -> public
reorgInfo, err := d.getReorgInfo(t, job)
if err != nil || reorgInfo.first {
// if we run reorg firstly, we should update the job snapshot version
// and then run the reorg next time.
return errors.Trace(err)
}
var tbl table.Table
tbl, err = d.getTable(schemaID, tblInfo)
if err != nil {
return errors.Trace(err)
}
err = d.runReorgJob(func() error {
return d.addTableIndex(tbl, indexInfo, reorgInfo)
})
if terror.ErrorEqual(err, errWaitReorgTimeout) {
// if timeout, we should return, check for the owner and re-wait job done.
return nil
}
if err != nil {
return errors.Trace(err)
}
indexInfo.State = model.StatePublic
// set column index flag.
addIndexColumnFlag(tblInfo, indexInfo)
if err = t.UpdateTable(schemaID, tblInfo); err != nil {
return errors.Trace(err)
//.........這裏部分代碼省略.........
示例15: onCreateIndex
func (d *ddl) onCreateIndex(t *meta.Meta, job *model.Job) error {
// Handle rollback job.
if job.State == model.JobRollback {
err := d.onDropIndex(t, job)
if err != nil {
return errors.Trace(err)
}
return nil
}
// Handle normal job.
schemaID := job.SchemaID
tblInfo, err := d.getTableInfo(t, job)
if err != nil {
return errors.Trace(err)
}
var (
unique bool
indexName model.CIStr
idxColNames []*ast.IndexColName
)
err = job.DecodeArgs(&unique, &indexName, &idxColNames)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
indexInfo := findIndexByName(indexName.L, tblInfo.Indices)
if indexInfo != nil && indexInfo.State == model.StatePublic {
job.State = model.JobCancelled
return errDupKeyName.Gen("index already exist %s", indexName)
}
if indexInfo == nil {
indexInfo, err = buildIndexInfo(tblInfo, unique, indexName, idxColNames)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
indexInfo.ID = allocateIndexID(tblInfo)
tblInfo.Indices = append(tblInfo.Indices, indexInfo)
}
ver, err := updateSchemaVersion(t, job)
if err != nil {
return errors.Trace(err)
}
switch indexInfo.State {
case model.StateNone:
// none -> delete only
job.SchemaState = model.StateDeleteOnly
indexInfo.State = model.StateDeleteOnly
err = t.UpdateTable(schemaID, tblInfo)
return errors.Trace(err)
case model.StateDeleteOnly:
// delete only -> write only
job.SchemaState = model.StateWriteOnly
indexInfo.State = model.StateWriteOnly
err = t.UpdateTable(schemaID, tblInfo)
return errors.Trace(err)
case model.StateWriteOnly:
// write only -> reorganization
job.SchemaState = model.StateWriteReorganization
indexInfo.State = model.StateWriteReorganization
// Initialize SnapshotVer to 0 for later reorganization check.
job.SnapshotVer = 0
err = t.UpdateTable(schemaID, tblInfo)
return errors.Trace(err)
case model.StateWriteReorganization:
// reorganization -> public
reorgInfo, err := d.getReorgInfo(t, job)
if err != nil || reorgInfo.first {
// If we run reorg firstly, we should update the job snapshot version
// and then run the reorg next time.
return errors.Trace(err)
}
var tbl table.Table
tbl, err = d.getTable(schemaID, tblInfo)
if err != nil {
return errors.Trace(err)
}
err = d.runReorgJob(func() error {
return d.addTableIndex(tbl, indexInfo, reorgInfo, job)
})
if terror.ErrorEqual(err, errWaitReorgTimeout) {
// if timeout, we should return, check for the owner and re-wait job done.
return nil
}
if err != nil {
if terror.ErrorEqual(err, kv.ErrKeyExists) {
log.Warnf("[ddl] run DDL job %v err %v, convert job to rollback job", job, err)
err = d.convert2RollbackJob(t, job, tblInfo, indexInfo)
}
return errors.Trace(err)
}
//.........這裏部分代碼省略.........