2017-02-23 10:13:13 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2019-11-20 13:20:27 +00:00
|
|
|
"sync"
|
2017-11-16 20:53:39 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"runtime/debug"
|
|
|
|
|
2020-09-22 09:46:00 +00:00
|
|
|
"github.com/abcum/surreal/cnf"
|
2017-02-23 10:13:13 +00:00
|
|
|
"github.com/abcum/surreal/kvs"
|
2017-11-16 20:53:39 +00:00
|
|
|
"github.com/abcum/surreal/log"
|
2017-02-23 10:13:13 +00:00
|
|
|
"github.com/abcum/surreal/sql"
|
2019-11-20 13:20:27 +00:00
|
|
|
"github.com/abcum/surreal/txn"
|
2017-02-23 10:13:13 +00:00
|
|
|
)
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
type executor struct {
|
2019-11-20 13:20:27 +00:00
|
|
|
id string
|
|
|
|
ns string
|
|
|
|
db string
|
|
|
|
tx *txn.TX
|
|
|
|
err error
|
|
|
|
buf []*Response
|
|
|
|
time time.Time
|
|
|
|
lock *mutex
|
|
|
|
opts *options
|
|
|
|
data sync.Map
|
|
|
|
send chan *Response
|
2017-02-23 10:13:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
func newExecutor(id, ns, db string) (e *executor) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
e = executorPool.Get().(*executor)
|
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
e.id = id
|
|
|
|
e.ns = ns
|
|
|
|
e.db = db
|
|
|
|
|
2019-12-03 11:23:48 +00:00
|
|
|
e.tx = nil
|
2019-11-20 13:20:27 +00:00
|
|
|
e.err = nil
|
2019-11-29 10:50:13 +00:00
|
|
|
e.buf = nil
|
2019-11-20 13:20:27 +00:00
|
|
|
|
|
|
|
e.data = sync.Map{}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-01-23 01:27:30 +00:00
|
|
|
e.opts = newOptions()
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
e.send = make(chan *Response)
|
|
|
|
|
2017-02-28 00:17:10 +00:00
|
|
|
return
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executor) execute(ctx context.Context, ast *sql.Query) {
|
|
|
|
|
|
|
|
// Ensure that the executor is added back into
|
|
|
|
// the executor pool when the executor has
|
|
|
|
// finished processing the request.
|
|
|
|
|
|
|
|
defer executorPool.Put(e)
|
|
|
|
|
|
|
|
// Ensure that the query responses channel is
|
|
|
|
// closed when the full query has been processed
|
|
|
|
// and dealt with.
|
|
|
|
|
|
|
|
defer close(e.send)
|
|
|
|
|
|
|
|
// If we are making use of a global transaction
|
|
|
|
// which is not committed at the end of the
|
|
|
|
// query set, then cancel the transaction.
|
|
|
|
|
|
|
|
defer func() {
|
2019-11-20 13:20:27 +00:00
|
|
|
if e.tx != nil {
|
|
|
|
e.tx.Cancel()
|
2019-01-31 10:03:50 +00:00
|
|
|
clear(e.id)
|
2019-12-03 10:07:19 +00:00
|
|
|
e.tx = nil
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// If we have panicked during query execution
|
|
|
|
// then ensure that we recover from the error
|
|
|
|
// and print the error to the log.
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2019-11-18 10:44:14 +00:00
|
|
|
if log.IsError() {
|
|
|
|
log.WithPrefix(logKeyDB).WithFields(map[string]interface{}{
|
|
|
|
logKeyId: e.id, logKeyStack: string(debug.Stack()),
|
|
|
|
}).Errorln(err)
|
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Loop over the defined query statements and
|
|
|
|
// process them, while listening for the quit
|
|
|
|
// channel to see if the client has gone away.
|
|
|
|
|
|
|
|
for _, stm := range ast.Statements {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
2019-01-31 10:03:50 +00:00
|
|
|
e.conduct(ctx, stm)
|
|
|
|
}
|
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
func (e *executor) conduct(ctx context.Context, stm sql.Statement) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
var rsp *Response
|
|
|
|
var res []interface{}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
// If we are not inside a global transaction
|
|
|
|
// then reset the error to nil so that the
|
|
|
|
// next statement is not ignored.
|
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
if e.tx == nil {
|
|
|
|
e.err = nil
|
2019-01-31 10:03:50 +00:00
|
|
|
}
|
2018-05-02 01:31:31 +00:00
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
// Check to see if the current statement is
|
|
|
|
// a TRANSACTION statement, and if it is
|
|
|
|
// then deal with it and move on to the next.
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
switch stm.(type) {
|
|
|
|
case *sql.BeginStatement:
|
2019-11-20 13:20:27 +00:00
|
|
|
e.err = e.begin(ctx, true)
|
|
|
|
if e.err != nil {
|
|
|
|
clear(e.id)
|
|
|
|
}
|
2019-01-31 10:03:50 +00:00
|
|
|
return
|
|
|
|
case *sql.CancelStatement:
|
2019-11-20 13:20:27 +00:00
|
|
|
e.err = e.cancel(e.send)
|
|
|
|
if e.err != nil {
|
2019-01-31 10:03:50 +00:00
|
|
|
clear(e.id)
|
|
|
|
} else {
|
|
|
|
clear(e.id)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
case *sql.CommitStatement:
|
2019-11-20 13:20:27 +00:00
|
|
|
e.err = e.commit(e.send)
|
|
|
|
if e.err != nil {
|
2019-01-31 10:03:50 +00:00
|
|
|
clear(e.id)
|
|
|
|
} else {
|
|
|
|
flush(e.id)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
// If an error has occured and we are inside
|
|
|
|
// a global transaction, then ignore all
|
|
|
|
// subsequent statements in the transaction.
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
if e.err == nil {
|
|
|
|
res, e.err = e.operate(ctx, stm)
|
2019-01-31 10:03:50 +00:00
|
|
|
} else {
|
2019-11-20 13:20:27 +00:00
|
|
|
res, e.err = []interface{}{}, errQueryNotExecuted
|
2019-01-31 10:03:50 +00:00
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
// Generate the response
|
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
rsp = &Response{
|
2019-11-20 13:20:27 +00:00
|
|
|
Time: time.Since(e.time).String(),
|
|
|
|
Status: status(e.err),
|
|
|
|
Detail: detail(e.err),
|
2019-01-31 10:03:50 +00:00
|
|
|
Result: append([]interface{}{}, res...),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log the sql statement along with the
|
|
|
|
// query duration time, and mark it as
|
|
|
|
// an error if the query failed.
|
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
switch e.err.(type) {
|
2019-01-31 10:03:50 +00:00
|
|
|
default:
|
2019-11-18 10:44:14 +00:00
|
|
|
if log.IsDebug() {
|
|
|
|
log.WithPrefix(logKeySql).WithFields(map[string]interface{}{
|
|
|
|
logKeyId: e.id,
|
|
|
|
logKeyNS: e.ns,
|
|
|
|
logKeyDB: e.db,
|
|
|
|
logKeyKind: ctx.Value(ctxKeyKind),
|
|
|
|
logKeyVars: ctx.Value(ctxKeyVars),
|
2019-11-20 13:20:27 +00:00
|
|
|
logKeyTime: time.Since(e.time).String(),
|
2019-11-18 10:44:14 +00:00
|
|
|
}).Debugln(stm)
|
|
|
|
}
|
2019-01-31 10:03:50 +00:00
|
|
|
case error:
|
2019-11-18 10:44:14 +00:00
|
|
|
if log.IsError() {
|
|
|
|
log.WithPrefix(logKeySql).WithFields(map[string]interface{}{
|
|
|
|
logKeyId: e.id,
|
|
|
|
logKeyNS: e.ns,
|
|
|
|
logKeyDB: e.db,
|
|
|
|
logKeyKind: ctx.Value(ctxKeyKind),
|
|
|
|
logKeyVars: ctx.Value(ctxKeyVars),
|
2019-11-20 13:20:27 +00:00
|
|
|
logKeyTime: time.Since(e.time).String(),
|
|
|
|
logKeyError: detail(e.err),
|
2019-11-18 10:44:14 +00:00
|
|
|
}).Errorln(stm)
|
|
|
|
}
|
2019-01-31 10:03:50 +00:00
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
// If we are not inside a global transaction
|
|
|
|
// then we can output the statement response
|
|
|
|
// immediately to the channel.
|
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
if e.tx == nil {
|
2019-01-31 10:03:50 +00:00
|
|
|
e.send <- rsp
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are inside a global transaction we
|
|
|
|
// must buffer the responses for output at
|
|
|
|
// the end of the transaction.
|
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
if e.tx != nil {
|
2019-01-31 10:03:50 +00:00
|
|
|
switch stm.(type) {
|
|
|
|
case *sql.ReturnStatement:
|
2019-11-20 13:20:27 +00:00
|
|
|
for i := len(e.buf) - 1; i >= 0; i-- {
|
|
|
|
e.buf[len(e.buf)-1] = nil
|
|
|
|
e.buf = e.buf[:len(e.buf)-1]
|
|
|
|
}
|
|
|
|
e.buf = append(e.buf, rsp)
|
2019-01-31 10:03:50 +00:00
|
|
|
default:
|
2019-11-20 13:20:27 +00:00
|
|
|
e.buf = append(e.buf, rsp)
|
2019-01-31 10:03:50 +00:00
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executor) operate(ctx context.Context, stm sql.Statement) (res []interface{}, err error) {
|
|
|
|
|
|
|
|
var loc bool
|
|
|
|
var trw bool
|
|
|
|
var canc context.CancelFunc
|
|
|
|
|
|
|
|
// If we are not inside a global transaction
|
|
|
|
// then grab a new transaction, ensuring that
|
|
|
|
// it is closed at the end.
|
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
if e.tx == nil {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
switch stm := stm.(type) {
|
|
|
|
case sql.WriteableStatement:
|
2019-11-20 13:20:27 +00:00
|
|
|
loc, trw = true, stm.Writeable()
|
2017-11-16 20:53:39 +00:00
|
|
|
default:
|
2019-11-20 13:20:27 +00:00
|
|
|
loc, trw = true, false
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 17:07:42 +00:00
|
|
|
err = e.begin(ctx, trw)
|
2017-11-16 20:53:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the beginning of this statement so we
|
|
|
|
// can monitor the running time, and ensure
|
|
|
|
// it runs no longer than specified.
|
|
|
|
|
2020-09-22 09:46:00 +00:00
|
|
|
if cnf.Settings.Query.Timeout > 0 {
|
|
|
|
if perm(ctx) != cnf.AuthKV {
|
|
|
|
ctx, canc = context.WithTimeout(ctx, cnf.Settings.Query.Timeout)
|
|
|
|
defer func() {
|
|
|
|
if tim := ctx.Err(); err == nil && tim != nil {
|
|
|
|
res, err = nil, &TimerError{timer: cnf.Settings.Query.Timeout}
|
|
|
|
}
|
|
|
|
canc()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the beginning of this statement so we
|
|
|
|
// can monitor the running time, and ensure
|
|
|
|
// it runs no longer than specified.
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
if stm, ok := stm.(sql.KillableStatement); ok {
|
2020-03-07 09:55:25 +00:00
|
|
|
if dur := stm.Duration(); dur > 0 {
|
|
|
|
ctx, canc = context.WithTimeout(ctx, dur)
|
2017-11-16 20:53:39 +00:00
|
|
|
defer func() {
|
|
|
|
if tim := ctx.Err(); err == nil && tim != nil {
|
2020-03-07 09:55:25 +00:00
|
|
|
res, err = nil, &TimerError{timer: dur}
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
2017-12-06 13:21:12 +00:00
|
|
|
canc()
|
2017-11-16 20:53:39 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the defined statement, receiving the
|
|
|
|
// result set, and any errors which occured
|
|
|
|
// while processing the query.
|
|
|
|
|
|
|
|
switch stm := stm.(type) {
|
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
case *sql.OptStatement:
|
|
|
|
res, err = e.executeOpt(ctx, stm)
|
|
|
|
|
|
|
|
case *sql.UseStatement:
|
|
|
|
res, err = e.executeUse(ctx, stm)
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-04-14 17:36:28 +00:00
|
|
|
case *sql.RunStatement:
|
|
|
|
res, err = e.executeRun(ctx, stm)
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
case *sql.InfoStatement:
|
|
|
|
res, err = e.executeInfo(ctx, stm)
|
|
|
|
|
|
|
|
case *sql.LetStatement:
|
|
|
|
res, err = e.executeLet(ctx, stm)
|
|
|
|
case *sql.ReturnStatement:
|
|
|
|
res, err = e.executeReturn(ctx, stm)
|
|
|
|
|
|
|
|
case *sql.LiveStatement:
|
|
|
|
res, err = e.executeLive(ctx, stm)
|
|
|
|
case *sql.KillStatement:
|
|
|
|
res, err = e.executeKill(ctx, stm)
|
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
case *sql.IfelseStatement:
|
|
|
|
res, err = e.executeIfelse(ctx, stm)
|
2017-11-16 20:53:39 +00:00
|
|
|
case *sql.SelectStatement:
|
|
|
|
res, err = e.executeSelect(ctx, stm)
|
|
|
|
case *sql.CreateStatement:
|
|
|
|
res, err = e.executeCreate(ctx, stm)
|
|
|
|
case *sql.UpdateStatement:
|
|
|
|
res, err = e.executeUpdate(ctx, stm)
|
|
|
|
case *sql.DeleteStatement:
|
|
|
|
res, err = e.executeDelete(ctx, stm)
|
|
|
|
case *sql.RelateStatement:
|
|
|
|
res, err = e.executeRelate(ctx, stm)
|
|
|
|
|
|
|
|
case *sql.InsertStatement:
|
|
|
|
res, err = e.executeInsert(ctx, stm)
|
|
|
|
case *sql.UpsertStatement:
|
|
|
|
res, err = e.executeUpsert(ctx, stm)
|
|
|
|
|
|
|
|
case *sql.DefineNamespaceStatement:
|
|
|
|
res, err = e.executeDefineNamespace(ctx, stm)
|
|
|
|
case *sql.RemoveNamespaceStatement:
|
|
|
|
res, err = e.executeRemoveNamespace(ctx, stm)
|
|
|
|
|
|
|
|
case *sql.DefineDatabaseStatement:
|
|
|
|
res, err = e.executeDefineDatabase(ctx, stm)
|
|
|
|
case *sql.RemoveDatabaseStatement:
|
|
|
|
res, err = e.executeRemoveDatabase(ctx, stm)
|
|
|
|
|
|
|
|
case *sql.DefineLoginStatement:
|
|
|
|
res, err = e.executeDefineLogin(ctx, stm)
|
|
|
|
case *sql.RemoveLoginStatement:
|
|
|
|
res, err = e.executeRemoveLogin(ctx, stm)
|
|
|
|
|
|
|
|
case *sql.DefineTokenStatement:
|
|
|
|
res, err = e.executeDefineToken(ctx, stm)
|
|
|
|
case *sql.RemoveTokenStatement:
|
|
|
|
res, err = e.executeRemoveToken(ctx, stm)
|
|
|
|
|
|
|
|
case *sql.DefineScopeStatement:
|
|
|
|
res, err = e.executeDefineScope(ctx, stm)
|
|
|
|
case *sql.RemoveScopeStatement:
|
|
|
|
res, err = e.executeRemoveScope(ctx, stm)
|
|
|
|
|
|
|
|
case *sql.DefineTableStatement:
|
|
|
|
res, err = e.executeDefineTable(ctx, stm)
|
|
|
|
case *sql.RemoveTableStatement:
|
|
|
|
res, err = e.executeRemoveTable(ctx, stm)
|
|
|
|
|
|
|
|
case *sql.DefineEventStatement:
|
|
|
|
res, err = e.executeDefineEvent(ctx, stm)
|
|
|
|
case *sql.RemoveEventStatement:
|
|
|
|
res, err = e.executeRemoveEvent(ctx, stm)
|
|
|
|
|
|
|
|
case *sql.DefineFieldStatement:
|
|
|
|
res, err = e.executeDefineField(ctx, stm)
|
|
|
|
case *sql.RemoveFieldStatement:
|
|
|
|
res, err = e.executeRemoveField(ctx, stm)
|
|
|
|
|
|
|
|
case *sql.DefineIndexStatement:
|
|
|
|
res, err = e.executeDefineIndex(ctx, stm)
|
|
|
|
case *sql.RemoveIndexStatement:
|
|
|
|
res, err = e.executeRemoveIndex(ctx, stm)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-14 17:02:58 +00:00
|
|
|
// If the context is already closed or failed,
|
2018-01-10 11:33:26 +00:00
|
|
|
// then ignore this result, clear all queued
|
|
|
|
// changes, and reset the transaction.
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
e.tx.Cancel()
|
2019-01-31 10:03:50 +00:00
|
|
|
clear(e.id)
|
2020-02-17 13:47:09 +00:00
|
|
|
e.tx = nil
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
|
2018-01-10 11:33:26 +00:00
|
|
|
// If this is a local transaction for only the
|
|
|
|
// current statement, then commit or cancel
|
|
|
|
// depending on the result error.
|
|
|
|
|
2019-11-30 01:50:19 +00:00
|
|
|
if loc && e.tx.Closed() == false {
|
2018-01-10 11:33:26 +00:00
|
|
|
|
|
|
|
// If there was an error with the query
|
|
|
|
// then clear the queued changes and
|
|
|
|
// return immediately.
|
|
|
|
|
|
|
|
if err != nil {
|
2019-11-20 13:20:27 +00:00
|
|
|
e.tx.Cancel()
|
2019-01-31 10:03:50 +00:00
|
|
|
clear(e.id)
|
2020-02-17 13:47:09 +00:00
|
|
|
e.tx = nil
|
2018-01-10 11:33:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise check if this is a read or
|
|
|
|
// a write transaction, and attempt to
|
|
|
|
// Cancel or Commit, returning any errors.
|
|
|
|
|
|
|
|
if !trw {
|
2019-11-20 13:20:27 +00:00
|
|
|
if err = e.tx.Cancel(); err != nil {
|
2019-01-31 10:03:50 +00:00
|
|
|
clear(e.id)
|
2020-02-17 13:47:09 +00:00
|
|
|
e.tx = nil
|
2018-01-10 11:33:26 +00:00
|
|
|
} else {
|
2019-01-31 10:03:50 +00:00
|
|
|
clear(e.id)
|
2020-02-17 13:47:09 +00:00
|
|
|
e.tx = nil
|
2018-01-10 11:33:26 +00:00
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
} else {
|
2019-11-20 13:20:27 +00:00
|
|
|
if err = e.tx.Commit(); err != nil {
|
2019-01-31 10:03:50 +00:00
|
|
|
clear(e.id)
|
2020-02-17 13:47:09 +00:00
|
|
|
e.tx = nil
|
2018-01-10 11:33:26 +00:00
|
|
|
} else {
|
2019-01-31 10:03:50 +00:00
|
|
|
flush(e.id)
|
2020-02-17 13:47:09 +00:00
|
|
|
e.tx = nil
|
2018-01-10 11:33:26 +00:00
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
2018-04-14 18:20:15 +00:00
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-06 17:07:42 +00:00
|
|
|
func (e *executor) begin(ctx context.Context, rw bool) (err error) {
|
2019-11-20 13:20:27 +00:00
|
|
|
e.tx, err = txn.New(ctx, rw)
|
|
|
|
e.time = time.Now()
|
|
|
|
e.lock = new(mutex)
|
2017-11-16 20:53:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
func (e *executor) cancel(chn chan<- *Response) (err error) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
defer func() {
|
|
|
|
e.tx.Cancel()
|
|
|
|
e.tx = nil
|
|
|
|
e.buf = nil
|
|
|
|
e.err = nil
|
|
|
|
}()
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
for _, v := range e.buf {
|
|
|
|
v.Time = time.Since(e.time).String()
|
2017-11-16 20:53:39 +00:00
|
|
|
v.Status = "ERR"
|
2018-01-10 11:33:26 +00:00
|
|
|
v.Result = []interface{}{}
|
|
|
|
v.Detail = "Transaction cancelled"
|
2017-11-16 20:53:39 +00:00
|
|
|
chn <- v
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
return
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
func (e *executor) commit(chn chan<- *Response) (err error) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
defer func() {
|
|
|
|
e.tx.Cancel()
|
|
|
|
e.tx = nil
|
|
|
|
e.buf = nil
|
|
|
|
e.err = nil
|
|
|
|
}()
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
if e.err != nil {
|
|
|
|
err = e.tx.Cancel()
|
2017-11-16 20:53:39 +00:00
|
|
|
} else {
|
2019-11-20 13:20:27 +00:00
|
|
|
err = e.tx.Commit()
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
for _, v := range e.buf {
|
2017-11-16 20:53:39 +00:00
|
|
|
if err != nil {
|
2019-11-20 13:20:27 +00:00
|
|
|
v.Time = time.Since(e.time).String()
|
2017-11-16 20:53:39 +00:00
|
|
|
v.Status = "ERR"
|
2018-01-10 11:33:26 +00:00
|
|
|
v.Result = []interface{}{}
|
|
|
|
v.Detail = "Transaction failed: " + err.Error()
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
chn <- v
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:20:27 +00:00
|
|
|
return
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2017-02-28 00:17:10 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
func status(e error) (s string) {
|
|
|
|
switch e.(type) {
|
|
|
|
default:
|
|
|
|
return "OK"
|
|
|
|
case *kvs.DBError:
|
|
|
|
return "ERR_DB"
|
|
|
|
case *PermsError:
|
|
|
|
return "ERR_PE"
|
|
|
|
case *ExistError:
|
2018-04-23 08:12:00 +00:00
|
|
|
return "ERR_EX"
|
2017-11-16 20:53:39 +00:00
|
|
|
case *FieldError:
|
|
|
|
return "ERR_FD"
|
|
|
|
case *IndexError:
|
|
|
|
return "ERR_IX"
|
2018-05-09 21:51:23 +00:00
|
|
|
case *TimerError:
|
|
|
|
return "ERR_TO"
|
2017-11-16 20:53:39 +00:00
|
|
|
case error:
|
|
|
|
return "ERR"
|
|
|
|
}
|
2017-02-23 10:13:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
func detail(e error) (s string) {
|
|
|
|
switch err := e.(type) {
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
case error:
|
|
|
|
return err.Error()
|
|
|
|
}
|
2017-02-23 10:13:13 +00:00
|
|
|
}
|