Enable passing context when beginning a transaction

This commit is contained in:
Tobie Morgan Hitchcock 2018-02-06 17:07:42 +00:00
parent 788d19c498
commit dc6a357e26
6 changed files with 17 additions and 11 deletions

View file

@ -83,7 +83,7 @@ func Export(w io.Writer) (err error) {
// with the underlying database, and returns
// the transaction, or any error which occured.
func Begin(rw bool) (txn kvs.TX, err error) {
return db.Begin(rw)
return db.Begin(context.Background(), rw)
}
// Socket registers a websocket for live queries

View file

@ -145,7 +145,7 @@ func (e *executor) execute(ctx context.Context, ast *sql.Query) {
switch stm.(type) {
case *sql.BeginStatement:
err = e.begin(true)
err = e.begin(ctx, true)
trc.Finish()
continue
case *sql.CancelStatement:
@ -243,7 +243,7 @@ func (e *executor) operate(ctx context.Context, stm sql.Statement) (res []interf
trw = false
}
err = e.begin(trw)
err = e.begin(ctx, trw)
if err != nil {
return
}
@ -420,9 +420,9 @@ func (e *executor) operate(ctx context.Context, stm sql.Statement) (res []interf
}
func (e *executor) begin(rw bool) (err error) {
func (e *executor) begin(ctx context.Context, rw bool) (err error) {
if e.dbo.TX == nil {
e.dbo.TX, err = db.Begin(rw)
e.dbo.TX, err = db.Begin(ctx, rw)
}
return
}

View file

@ -244,7 +244,7 @@ func (s *socket) deregister(id string) {
delete(sockets, id)
txn, _ := db.Begin(true)
txn, _ := db.Begin(context.Background(), true)
defer txn.Commit()

View file

@ -14,11 +14,14 @@
package kvs
import "io"
import (
"context"
"io"
)
// DB represents a database implementation
type DB interface {
Begin(bool) (TX, error)
Begin(context.Context, bool) (TX, error)
Import(io.Reader) error
Export(io.Writer) error
Close() error

View file

@ -15,6 +15,7 @@
package kvs
import (
"context"
"io"
"strings"
@ -63,8 +64,8 @@ func New(opts *cnf.Options) (ds *DS, err error) {
// Begin begins a new read / write transaction
// with the underlying database, and returns
// the transaction, or any error which occured.
func (ds *DS) Begin(writable bool) (txn TX, err error) {
return ds.db.Begin(writable)
func (ds *DS) Begin(ctx context.Context, writable bool) (txn TX, err error) {
return ds.db.Begin(ctx, writable)
}
// Import loads database operations from a reader.

View file

@ -17,6 +17,8 @@ package rixxdb
import (
"io"
"context"
"github.com/abcum/rixxdb"
"github.com/abcum/surreal/kvs"
)
@ -25,7 +27,7 @@ type DB struct {
pntr *rixxdb.DB
}
func (db *DB) Begin(writable bool) (txn kvs.TX, err error) {
func (db *DB) Begin(ctx context.Context, writable bool) (txn kvs.TX, err error) {
var pntr *rixxdb.TX
if pntr, err = db.pntr.Begin(writable); err != nil {
err = &kvs.DBError{Err: err}