plan: make plan reusable after close

This commit is contained in:
Ewan Chou
2015-09-13 07:59:56 +08:00
parent 36cc69ad36
commit d4e160faf1
14 changed files with 33 additions and 18 deletions

View File

@ -47,6 +47,7 @@ type Plan interface {
Next(ctx context.Context) (row *Row, err error)
// Close closes the underlying iterator of the plan.
// If you call Next after Close, it will start iteration from beginning.
Close() error
}

View File

@ -147,6 +147,8 @@ func (r *DistinctDefaultPlan) fetchAll(ctx context.Context) error {
// Close implements plan.Plan Close interface.
func (r *DistinctDefaultPlan) Close() error {
r.rows = nil
r.cursor = 0
return r.Src.Close()
}

View File

@ -92,6 +92,8 @@ func (r *ExplainDefaultPlan) Next(ctx context.Context) (row *plan.Row, err error
// Close implements plan.Plan Close interface.
func (r *ExplainDefaultPlan) Close() error {
r.lines = nil
r.cursor = 0
return nil
}

View File

@ -167,6 +167,7 @@ func (s *SelectEmptyFieldListPlan) Next(ctx context.Context) (row *plan.Row, err
// Close implements plan.Plan Close interface.
func (s *SelectEmptyFieldListPlan) Close() error {
s.done = false
return nil
}

View File

@ -76,10 +76,10 @@ func (s *testFieldsSuit) TestDefaultFieldsPlan(c *C) {
},
},
}
tblPlan.Close()
rset.Plan = &plans.SelectFieldsDefaultPlan{
SelectList: sl2,
Src: tblPlan.reset(),
Src: tblPlan,
}
rset.Do(func(data []interface{}) (bool, error) {
@ -99,9 +99,10 @@ func (s *testFieldsSuit) TestDefaultFieldsPlan(c *C) {
},
},
}
tblPlan.Close()
rset.Plan = &plans.SelectFieldsDefaultPlan{
SelectList: sl3,
Src: tblPlan.reset(),
Src: tblPlan,
}
err := rset.Do(func(data []interface{}) (bool, error) {
return true, nil

View File

@ -107,6 +107,7 @@ func (r *SelectFinalPlan) Next(ctx context.Context) (row *plan.Row, err error) {
// Close implements plan.Plan Close interface.
func (r *SelectFinalPlan) Close() error {
r.infered = false
return r.Src.Close()
}

View File

@ -124,7 +124,10 @@ func (r *TableNilPlan) Next(ctx context.Context) (row *plan.Row, err error) {
// Close implements plan.Plan Close interface.
func (r *TableNilPlan) Close() error {
r.iter.Close()
if r.iter != nil {
r.iter.Close()
}
r.iter = nil
return nil
}
@ -411,7 +414,10 @@ func (r *TableDefaultPlan) Next(ctx context.Context) (row *plan.Row, err error)
// Close implements plan.Plan Close interface.
func (r *TableDefaultPlan) Close() error {
r.iter.Close()
if r.iter != nil {
r.iter.Close()
}
r.iter = nil
return nil
}

View File

@ -427,7 +427,10 @@ func (r *indexPlan) Next(ctx context.Context) (row *plan.Row, err error) {
func (r *indexPlan) Close() error {
if r.iter != nil {
r.iter.Close()
r.iter = nil
}
r.cursor = 0
r.skipLowCmp = false
return nil
}

View File

@ -584,6 +584,8 @@ func (isp *InfoSchemaPlan) fetchCharacterSets() {
// Close implements plan.Plan Close interface.
func (isp *InfoSchemaPlan) Close() error {
isp.rows = nil
isp.cursor = 0
return nil
}

View File

@ -89,6 +89,7 @@ func (r *LimitDefaultPlan) Next(ctx context.Context) (row *plan.Row, err error)
// Close implements plan.Plan Close interface.
func (r *LimitDefaultPlan) Close() error {
r.cursor = 0
return r.Src.Close()
}
@ -147,6 +148,7 @@ func (r *OffsetDefaultPlan) Next(ctx context.Context) (row *plan.Row, err error)
// Close implements plan.Plan Close interface.
func (r *OffsetDefaultPlan) Close() error {
r.cursor = 0
return r.Src.Close()
}

View File

@ -73,6 +73,7 @@ func (p *testTablePlan) Next(ctx context.Context) (row *plan.Row, err error) {
}
func (p *testTablePlan) Close() error {
p.cursor = 0
return nil
}
@ -80,11 +81,6 @@ func (p *testTablePlan) UseNext() bool {
return true
}
func (p *testTablePlan) reset() *testTablePlan {
p.cursor = 0
return p
}
type testLimitSuit struct {
data []*testRowData
sess tidb.Session

View File

@ -261,6 +261,8 @@ func (r *OrderByDefaultPlan) fetchAll(ctx context.Context) error {
// Close implements plan.Plan Close interface.
func (r *OrderByDefaultPlan) Close() error {
r.ordTable = nil
r.cursor = 0
return r.Src.Close()
}

View File

@ -369,6 +369,8 @@ func (s *ShowPlan) fetchAll(ctx context.Context) error {
// Close implements plan.Plan Close interface.
func (s *ShowPlan) Close() error {
s.rows = nil
s.cursor = 0
return nil
}
@ -376,9 +378,3 @@ func (s *ShowPlan) Close() error {
func (s *ShowPlan) UseNext() bool {
return true
}
// Reset used for test
func (s *ShowPlan) Reset() {
s.rows = nil
s.cursor = 0
}

View File

@ -91,7 +91,7 @@ func (p *testShowSuit) TestShowVariables(c *C) {
c.Assert(v, Equals, "latin1")
// Set session variable to utf8
sessionVars.Systems["character_set_results"] = "utf8"
pln.Reset()
pln.Close()
rset.Do(func(data []interface{}) (bool, error) {
ret[data[0].(string)] = data[1].(string)
return true, nil
@ -103,7 +103,7 @@ func (p *testShowSuit) TestShowVariables(c *C) {
c.Assert(v, Equals, "latin1")
pln.GlobalScope = false
pln.Reset()
pln.Close()
rset.Do(func(data []interface{}) (bool, error) {
ret[data[0].(string)] = data[1].(string)
return true, nil