github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/model/ddl.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package model
    15  
    16  import (
    17  	"encoding/json"
    18  	"fmt"
    19  
    20  	"github.com/insionng/yougam/libraries/juju/errors"
    21  )
    22  
    23  // ActionType is the type for DDL action.
    24  type ActionType byte
    25  
    26  // List DDL actions.
    27  const (
    28  	ActionNone ActionType = iota
    29  	ActionCreateSchema
    30  	ActionDropSchema
    31  	ActionCreateTable
    32  	ActionDropTable
    33  	ActionAddColumn
    34  	ActionDropColumn
    35  	ActionAddIndex
    36  	ActionDropIndex
    37  	ActionAddForeignKey
    38  	ActionDropForeignKey
    39  )
    40  
    41  func (action ActionType) String() string {
    42  	switch action {
    43  	case ActionCreateSchema:
    44  		return "create schema"
    45  	case ActionDropSchema:
    46  		return "drop schema"
    47  	case ActionCreateTable:
    48  		return "create table"
    49  	case ActionDropTable:
    50  		return "drop table"
    51  	case ActionAddColumn:
    52  		return "add column"
    53  	case ActionDropColumn:
    54  		return "drop column"
    55  	case ActionAddIndex:
    56  		return "add index"
    57  	case ActionDropIndex:
    58  		return "drop index"
    59  	case ActionAddForeignKey:
    60  		return "add foreign key"
    61  	case ActionDropForeignKey:
    62  		return "drop foreign key"
    63  	default:
    64  		return "none"
    65  	}
    66  }
    67  
    68  // Job is for a DDL operation.
    69  type Job struct {
    70  	ID       int64      `json:"id"`
    71  	Type     ActionType `json:"type"`
    72  	SchemaID int64      `json:"schema_id"`
    73  	TableID  int64      `json:"table_id"`
    74  	State    JobState   `json:"state"`
    75  	Error    string     `json:"err"`
    76  	// every time we meet an error when running job, we will increase it
    77  	ErrorCount int64         `json:"err_count"`
    78  	Args       []interface{} `json:"-"`
    79  	// we must use json raw message for delay parsing special args.
    80  	RawArgs     json.RawMessage `json:"raw_args"`
    81  	SchemaState SchemaState     `json:"schema_state"`
    82  	// snapshot version for this job.
    83  	SnapshotVer uint64 `json:"snapshot_ver"`
    84  	// unix nano seconds
    85  	// TODO: use timestamp allocated by TSO
    86  	LastUpdateTS int64 `json:"last_update_ts"`
    87  }
    88  
    89  // Encode encodes job with json format.
    90  func (job *Job) Encode() ([]byte, error) {
    91  	var err error
    92  	job.RawArgs, err = json.Marshal(job.Args)
    93  	if err != nil {
    94  		return nil, errors.Trace(err)
    95  	}
    96  
    97  	var b []byte
    98  	b, err = json.Marshal(job)
    99  	return b, errors.Trace(err)
   100  }
   101  
   102  // Decode decodes job from the json buffer, we must use DecodeArgs later to
   103  // decode special args for this job.
   104  func (job *Job) Decode(b []byte) error {
   105  	err := json.Unmarshal(b, job)
   106  	return errors.Trace(err)
   107  }
   108  
   109  // DecodeArgs decodes job args.
   110  func (job *Job) DecodeArgs(args ...interface{}) error {
   111  	job.Args = args
   112  	err := json.Unmarshal(job.RawArgs, &job.Args)
   113  	return errors.Trace(err)
   114  }
   115  
   116  // String implements fmt.Stringer interface.
   117  func (job *Job) String() string {
   118  	return fmt.Sprintf("ID:%d, Type:%s, State:%s, SchemaState:%s, SchemaID:%d, TableID:%d, Args:%s",
   119  		job.ID, job.Type, job.State, job.SchemaState, job.SchemaID, job.TableID, job.RawArgs)
   120  }
   121  
   122  // IsFinished returns whether job is finished or not.
   123  // If the job state is Done or Cancelled, it is finished.
   124  func (job *Job) IsFinished() bool {
   125  	return job.State == JobDone || job.State == JobCancelled
   126  }
   127  
   128  // IsRunning returns whether job is still running or not.
   129  func (job *Job) IsRunning() bool {
   130  	return job.State == JobRunning
   131  }
   132  
   133  // JobState is for job state.
   134  type JobState byte
   135  
   136  // List job states.
   137  const (
   138  	JobNone JobState = iota
   139  	JobRunning
   140  	JobDone
   141  	JobCancelled
   142  )
   143  
   144  // String implements fmt.Stringer interface.
   145  func (s JobState) String() string {
   146  	switch s {
   147  	case JobRunning:
   148  		return "running"
   149  	case JobDone:
   150  		return "done"
   151  	case JobCancelled:
   152  		return "cancelled"
   153  	default:
   154  		return "none"
   155  	}
   156  }
   157  
   158  // Owner is for DDL Owner.
   159  type Owner struct {
   160  	OwnerID string `json:"owner_id"`
   161  	// unix nano seconds
   162  	// TODO: use timestamp allocated by TSO
   163  	LastUpdateTS int64 `json:"last_update_ts"`
   164  }
   165  
   166  // String implements fmt.Stringer interface.
   167  func (o *Owner) String() string {
   168  	return fmt.Sprintf("ID:%s, LastUpdateTS:%d", o.OwnerID, o.LastUpdateTS)
   169  }