Skip to content

Commit

Permalink
Merge pull request pingcap#544 from pingcap/shenli/refactor-globalvar
Browse files Browse the repository at this point in the history
*: Use Bind/Get global sysvar accessor instead of type assertion
  • Loading branch information
shenli committed Nov 9, 2015
2 parents 8d6598e + a54c7a7 commit b803cab
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 8 deletions.
3 changes: 2 additions & 1 deletion expression/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (v *Variable) String() string {
func (v *Variable) Eval(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
name := strings.ToLower(v.Name)
sessionVars := variable.GetSessionVars(ctx)
globalVars := variable.GetGlobalSysVarAccessor(ctx)
if !v.IsSystem {
// user vars
if value, ok := sessionVars.Users[name]; ok {
Expand All @@ -82,7 +83,7 @@ func (v *Variable) Eval(ctx context.Context, args map[interface{}]interface{}) (
return value, nil
}
}
value, err := ctx.(variable.GlobalSysVarAccessor).GetGlobalSysVar(ctx, name)
value, err := globalVars.GetGlobalSysVar(ctx, name)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
4 changes: 3 additions & 1 deletion expression/variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ type testVariableSuite struct {
}

func (s *testVariableSuite) SetUpSuite(c *C) {
s.ctx = mock.NewContext()
nc := mock.NewContext()
s.ctx = nc
variable.BindSessionVars(s.ctx)
variable.BindGlobalSysVarAccessor(s.ctx, nc)
}

func (s *testVariableSuite) TestVariable(c *C) {
Expand Down
5 changes: 3 additions & 2 deletions plan/plans/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ func (s *ShowPlan) fetchShowTables(ctx context.Context) error {

func (s *ShowPlan) fetchShowVariables(ctx context.Context) error {
sessionVars := variable.GetSessionVars(ctx)
globalVars := variable.GetGlobalSysVarAccessor(ctx)
m := map[interface{}]interface{}{}

for _, v := range variable.SysVars {
Expand Down Expand Up @@ -384,13 +385,13 @@ func (s *ShowPlan) fetchShowVariables(ctx context.Context) error {
value = sv
} else {
// If session scope variable is not set, get the global scope value.
value, err = ctx.(variable.GlobalSysVarAccessor).GetGlobalSysVar(ctx, v.Name)
value, err = globalVars.GetGlobalSysVar(ctx, v.Name)
if err != nil {
return errors.Trace(err)
}
}
} else {
value, err = ctx.(variable.GlobalSysVarAccessor).GetGlobalSysVar(ctx, v.Name)
value, err = globalVars.GetGlobalSysVar(ctx, v.Name)
if err != nil {
return errors.Trace(err)
}
Expand Down
4 changes: 3 additions & 1 deletion plan/plans/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ type testShowSuit struct {
var _ = Suite(&testShowSuit{})

func (p *testShowSuit) SetUpSuite(c *C) {
p.ctx = mock.NewContext()
nc := mock.NewContext()
p.ctx = nc
variable.BindSessionVars(p.ctx)
variable.BindGlobalSysVarAccessor(p.ctx, nc)

p.dbName = "testshowplan"
p.store = newStore(c, p.dbName)
Expand Down
3 changes: 3 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ func CreateSession(store kv.Storage) (Session, error) {
variable.BindSessionVars(s)
variable.GetSessionVars(s).SetStatusFlag(mysql.ServerStatusAutocommit, true)

// session implements variable.GlobalSysVarAccessor. Bind it to ctx.
variable.BindGlobalSysVarAccessor(s, s)

// session implements autocommit.Checker. Bind it to ctx
autocommit.BindAutocommitChecker(s, s)
sessionMu.Lock()
Expand Down
24 changes: 24 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,27 @@ type GlobalSysVarAccessor interface {
// SetGlobalSysVar sets the global system variable name to value.
SetGlobalSysVar(ctx context.Context, name string, value string) error
}

// globalSysVarAccessorKeyType is a dummy type to avoid naming collision in context.
type globalSysVarAccessorKeyType int

// String defines a Stringer function for debugging and pretty printing.
func (k globalSysVarAccessorKeyType) String() string {
return "global_sysvar_accessor"
}

const accessorKey globalSysVarAccessorKeyType = 0

// BindGlobalSysVarAccessor binds global sysvar accessor to context.
func BindGlobalSysVarAccessor(ctx context.Context, accessor GlobalSysVarAccessor) {
ctx.SetValue(accessorKey, accessor)
}

// GetGlobalSysVarAccessor gets accessor from ctx.
func GetGlobalSysVarAccessor(ctx context.Context) GlobalSysVarAccessor {
v, ok := ctx.Value(accessorKey).(GlobalSysVarAccessor)
if !ok {
panic("Miss global sysvar accessor")
}
return v
}
4 changes: 2 additions & 2 deletions stmt/stmts/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (s *SetStmt) Exec(ctx context.Context) (_ rset.Recordset, err error) {
log.Debug("Set sys/user variables")

sessionVars := variable.GetSessionVars(ctx)

globalVars := variable.GetGlobalSysVarAccessor(ctx)
for _, v := range s.Variables {
// Variable is case insensitive, we use lower case.
name := strings.ToLower(v.Name)
Expand Down Expand Up @@ -138,7 +138,7 @@ func (s *SetStmt) Exec(ctx context.Context) (_ rset.Recordset, err error) {
if err != nil {
return nil, errors.Trace(err)
}
err = ctx.(variable.GlobalSysVarAccessor).SetGlobalSysVar(ctx, name, svalue)
err = globalVars.SetGlobalSysVar(ctx, name, svalue)
return nil, errors.Trace(err)
}
return nil, errors.Errorf("Variable '%s' is a SESSION variable and can't be used with SET GLOBAL", name)
Expand Down
2 changes: 1 addition & 1 deletion util/mock/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (c *Context) SetGlobalSysVar(ctx context.Context, name string, value string
}

// NewContext creates a new mocked context.Context.
func NewContext() context.Context {
func NewContext() *Context {
return &Context{
values: make(map[fmt.Stringer]interface{}),
}
Expand Down

0 comments on commit b803cab

Please sign in to comment.