diff --git a/db/create.go b/db/create.go index fba50d53..6871ea28 100644 --- a/db/create.go +++ b/db/create.go @@ -26,7 +26,7 @@ func (e *executor) executeCreateStatement(ast *sql.CreateStatement) (out []inter for k, w := range ast.What { if what, ok := w.(*sql.Param); ok { - ast.What[k] = e.Get(what.ID) + ast.What[k] = e.get(what.ID) } } diff --git a/db/db.go b/db/db.go index 6d90c09e..8d6cd237 100644 --- a/db/db.go +++ b/db/db.go @@ -160,11 +160,9 @@ func Process(ctx *fibre.Context, ast *sql.Query, vars map[string]interface{}) (o // details, and the current runtime variables // and execute the queries within. - exec := pool.Get().(*executor) + exec := newExec(ast, ctx, vars) - defer pool.Put(exec) - - exec.Reset(ast, ctx, vars) + defer exec.done() go exec.execute(quit, recv) diff --git a/db/delete.go b/db/delete.go index 701d7653..27f90e64 100644 --- a/db/delete.go +++ b/db/delete.go @@ -25,7 +25,7 @@ func (e *executor) executeDeleteStatement(ast *sql.DeleteStatement) (out []inter for k, w := range ast.What { if what, ok := w.(*sql.Param); ok { - ast.What[k] = e.Get(what.ID) + ast.What[k] = e.get(what.ID) } } diff --git a/db/exec.go b/db/executor.go similarity index 65% rename from db/exec.go rename to db/executor.go index 712e847c..bad10305 100644 --- a/db/exec.go +++ b/db/executor.go @@ -24,34 +24,38 @@ import ( "github.com/abcum/surreal/util/data" ) -var pool sync.Pool - -func init() { - - pool.New = func() interface{} { +var executors = sync.Pool{ + New: func() interface{} { return &executor{} - } - + }, } type executor struct { - txn kvs.TX - ctx *data.Doc - ast *sql.Query - mem *mem.Store - web *fibre.Context + txn kvs.TX + ctx *data.Doc + ast *sql.Query + mem *mem.Store + web *fibre.Context + closed chan struct{} } -func (e *executor) Reset(ast *sql.Query, web *fibre.Context, vars map[string]interface{}) { +func newExec(ast *sql.Query, web *fibre.Context, vars map[string]interface{}) (e *executor) { + e = executors.Get().(*executor) e.ast = ast e.web = web e.ctx = data.Consume(vars) + e.closed = make(chan struct{}, 1) + return } -func (e *executor) Set(key string, val interface{}) { +func (e *executor) done() { + executors.Put(e) +} + +func (e *executor) set(key string, val interface{}) { e.ctx.Set(val, key) } -func (e *executor) Get(key string) (val interface{}) { +func (e *executor) get(key string) (val interface{}) { return e.ctx.Get(key).Data() } diff --git a/db/let.go b/db/let.go index 6e757c81..d2d50b2e 100644 --- a/db/let.go +++ b/db/let.go @@ -22,9 +22,9 @@ func (e *executor) executeLetStatement(ast *sql.LetStatement) (out []interface{} switch what := ast.What.(type) { default: - e.Set(ast.Name, what) + e.set(ast.Name, what) case *sql.Param: - e.Set(ast.Name, e.Get(what.ID)) + e.set(ast.Name, e.get(what.ID)) } return diff --git a/db/return.go b/db/return.go index a31931d9..47eef091 100644 --- a/db/return.go +++ b/db/return.go @@ -30,7 +30,7 @@ func (e *executor) executeReturnStatement(ast *sql.ReturnStatement) (out []inter case *sql.Empty: // Ignore case *sql.Param: - out = append(out, e.Get(what.ID)) + out = append(out, e.get(what.ID)) } return diff --git a/db/select.go b/db/select.go index 1ee19972..5631acd1 100644 --- a/db/select.go +++ b/db/select.go @@ -24,7 +24,7 @@ func (e *executor) executeSelectStatement(ast *sql.SelectStatement) (out []inter for k, w := range ast.What { if what, ok := w.(*sql.Param); ok { - ast.What[k] = e.Get(what.ID) + ast.What[k] = e.get(what.ID) } } diff --git a/db/update.go b/db/update.go index 2f7948c8..00b93f75 100644 --- a/db/update.go +++ b/db/update.go @@ -25,7 +25,7 @@ func (e *executor) executeUpdateStatement(ast *sql.UpdateStatement) (out []inter for k, w := range ast.What { if what, ok := w.(*sql.Param); ok { - ast.What[k] = e.Get(what.ID) + ast.What[k] = e.get(what.ID) } }