2016-02-27 01:00:19 +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 (
|
2016-05-11 19:23:21 +00:00
|
|
|
"fmt"
|
2017-02-16 10:12:18 +00:00
|
|
|
"io"
|
2016-05-11 19:23:21 +00:00
|
|
|
"time"
|
2016-02-27 01:00:19 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
"net/http"
|
2016-07-21 21:49:47 +00:00
|
|
|
|
2017-02-09 20:55:13 +00:00
|
|
|
"runtime/debug"
|
|
|
|
|
2016-05-11 19:23:21 +00:00
|
|
|
"github.com/abcum/fibre"
|
|
|
|
"github.com/abcum/surreal/cnf"
|
2016-05-25 11:33:05 +00:00
|
|
|
"github.com/abcum/surreal/kvs"
|
2016-05-11 19:23:21 +00:00
|
|
|
"github.com/abcum/surreal/log"
|
2017-02-12 11:56:46 +00:00
|
|
|
"github.com/abcum/surreal/mem"
|
2016-02-27 01:00:19 +00:00
|
|
|
"github.com/abcum/surreal/sql"
|
2016-07-16 13:43:53 +00:00
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
_ "github.com/abcum/surreal/kvs/rixxdb"
|
|
|
|
// _ "github.com/abcum/surreal/kvs/dendro"
|
2016-02-27 01:00:19 +00:00
|
|
|
)
|
|
|
|
|
2017-02-20 01:09:24 +00:00
|
|
|
var QueryNotExecuted = fmt.Errorf("Query not executed")
|
|
|
|
|
2016-05-11 19:23:21 +00:00
|
|
|
type Response struct {
|
2016-10-27 08:33:14 +00:00
|
|
|
Time string `codec:"time,omitempty"`
|
|
|
|
Status string `codec:"status,omitempty"`
|
|
|
|
Detail string `codec:"detail,omitempty"`
|
|
|
|
Result []interface{} `codec:"result,omitempty"`
|
2016-05-11 19:23:21 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
var db *kvs.DS
|
2016-05-11 19:23:21 +00:00
|
|
|
|
|
|
|
// Setup sets up the connection with the data layer
|
|
|
|
func Setup(opts *cnf.Options) (err error) {
|
|
|
|
|
2016-07-19 11:04:22 +00:00
|
|
|
log.WithPrefix("db").Infof("Starting database")
|
2016-06-15 12:38:37 +00:00
|
|
|
|
2016-07-18 12:33:26 +00:00
|
|
|
db, err = kvs.New(opts)
|
2016-06-15 12:38:37 +00:00
|
|
|
|
2016-05-11 19:23:21 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit shuts down the connection with the data layer
|
|
|
|
func Exit() {
|
|
|
|
|
2016-06-15 12:38:37 +00:00
|
|
|
log.WithPrefix("db").Infof("Gracefully shutting down database")
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-05-25 11:33:05 +00:00
|
|
|
db.Close()
|
2016-05-11 19:23:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-16 10:12:18 +00:00
|
|
|
// Import loads database operations from a reader.
|
|
|
|
// This can be used to playback a database snapshot
|
|
|
|
// into an already running database.
|
|
|
|
func Import(r io.Reader) (err error) {
|
|
|
|
return db.Import(r)
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2017-02-16 10:12:18 +00:00
|
|
|
// Export saves all database operations to a writer.
|
|
|
|
// This can be used to save a database snapshot
|
|
|
|
// to a secondary file or stream.
|
|
|
|
func Export(w io.Writer) (err error) {
|
|
|
|
return db.Export(w)
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2017-02-16 10:12:18 +00:00
|
|
|
// Begin begins a new read / write transaction
|
|
|
|
// 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)
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 10:12:18 +00:00
|
|
|
// Execute parses a single sql query, or multiple
|
|
|
|
// sql queries, and executes them serially against
|
|
|
|
// the underlying data layer.
|
2016-09-14 21:32:52 +00:00
|
|
|
func Execute(ctx *fibre.Context, txt interface{}, vars map[string]interface{}) (out []*Response, err error) {
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-11-05 13:59:39 +00:00
|
|
|
// If no preset variables have been defined
|
|
|
|
// then ensure that the variables is
|
|
|
|
// instantiated for future use.
|
|
|
|
|
|
|
|
if vars == nil {
|
|
|
|
vars = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
2016-10-29 11:28:20 +00:00
|
|
|
// Parse the received SQL batch query strings
|
|
|
|
// into SQL ASTs, using any immutable preset
|
|
|
|
// variables if set.
|
|
|
|
|
2016-09-14 21:32:52 +00:00
|
|
|
ast, err := sql.Parse(ctx, txt, vars)
|
2016-02-27 12:05:35 +00:00
|
|
|
if err != nil {
|
2016-05-11 19:23:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-21 18:48:25 +00:00
|
|
|
// Ensure that the current authentication data
|
|
|
|
// is made available as a runtime variable to
|
|
|
|
// the query layer.
|
|
|
|
|
|
|
|
vars["auth"] = ctx.Get("auth").(*cnf.Auth).Data
|
|
|
|
|
2016-11-21 14:36:37 +00:00
|
|
|
return Process(ctx, ast, vars)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-16 10:12:18 +00:00
|
|
|
// Process takes a parsed set of sql queries and
|
|
|
|
// executes them serially against the underlying
|
|
|
|
// data layer.
|
2016-11-21 14:36:37 +00:00
|
|
|
func Process(ctx *fibre.Context, ast *sql.Query, vars map[string]interface{}) (out []*Response, err error) {
|
|
|
|
|
2016-10-29 11:28:20 +00:00
|
|
|
// Create 2 channels, one for force quitting
|
|
|
|
// the query processor, and the other for
|
|
|
|
// receiving and buffering any query results.
|
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
quit := make(chan bool, 1)
|
|
|
|
recv := make(chan *Response)
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-10-29 11:28:20 +00:00
|
|
|
// Ensure that the force quit channel is auto
|
|
|
|
// closed when the end of the request has been
|
|
|
|
// reached, and we are not an http connection.
|
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
defer close(quit)
|
|
|
|
|
2016-10-29 11:28:20 +00:00
|
|
|
// If the current connection is a normal http
|
|
|
|
// connection then force quit any running
|
|
|
|
// queries if a socket close event occurs.
|
|
|
|
|
2017-02-27 15:30:46 +00:00
|
|
|
if _, ok := ctx.Response().Writer().(http.CloseNotifier); ok {
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
exit := ctx.Response().CloseNotify()
|
2017-02-27 15:30:46 +00:00
|
|
|
done := make(chan struct{})
|
2016-10-27 08:33:14 +00:00
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-exit:
|
|
|
|
quit <- true
|
|
|
|
}
|
|
|
|
}()
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-29 11:28:20 +00:00
|
|
|
// Create a new query executor with the query
|
|
|
|
// details, and the current runtime variables
|
|
|
|
// and execute the queries within.
|
|
|
|
|
2017-02-14 20:02:18 +00:00
|
|
|
exec := pool.Get().(*executor)
|
|
|
|
|
|
|
|
defer pool.Put(exec)
|
|
|
|
|
2017-02-23 10:13:13 +00:00
|
|
|
exec.Reset(ast, ctx, vars)
|
2017-02-14 20:02:18 +00:00
|
|
|
|
|
|
|
go exec.execute(quit, recv)
|
2016-10-29 11:28:20 +00:00
|
|
|
|
|
|
|
// Wait for all of the processed queries to
|
|
|
|
// return results, buffer the output, and
|
|
|
|
// return the output when finished.
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
for res := range recv {
|
|
|
|
out = append(out, res)
|
2016-02-27 12:05:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 19:23:21 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-29 11:28:20 +00:00
|
|
|
func (e *executor) execute(quit <-chan bool, send chan<- *Response) {
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
var err error
|
2017-02-09 20:45:45 +00:00
|
|
|
var now time.Time
|
2016-10-27 08:33:14 +00:00
|
|
|
var rsp *Response
|
|
|
|
var buf []*Response
|
|
|
|
var res []interface{}
|
|
|
|
|
|
|
|
// Ensure that the query responses channel is
|
|
|
|
// closed when the full query has been processed
|
|
|
|
// and dealt with.
|
|
|
|
|
|
|
|
defer close(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.
|
2016-09-06 13:33:02 +00:00
|
|
|
|
2016-07-16 13:44:28 +00:00
|
|
|
defer func() {
|
2017-02-09 20:43:24 +00:00
|
|
|
if e.txn != nil {
|
|
|
|
e.txn.Cancel()
|
2016-09-06 13:33:02 +00:00
|
|
|
}
|
2016-10-27 08:33:14 +00:00
|
|
|
}()
|
|
|
|
|
2017-02-09 20:44:08 +00:00
|
|
|
// If we have panicked during query execution
|
2016-10-27 08:33:14 +00:00
|
|
|
// then ensure that we recover from the error
|
|
|
|
// and print the error to the log.
|
|
|
|
|
|
|
|
defer func() {
|
2017-02-23 10:14:10 +00:00
|
|
|
if err := recover(); err != nil {
|
|
|
|
stk := string(debug.Stack())
|
|
|
|
log.WithPrefix("db").WithFields(map[string]interface{}{
|
|
|
|
"ctx": e.web, "stack": stk,
|
|
|
|
}).Errorln(err)
|
2016-07-16 13:44:28 +00:00
|
|
|
}
|
|
|
|
}()
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
// Loop over the defined query statements and
|
2017-02-28 00:00:39 +00:00
|
|
|
// process them, while listening for the quit
|
|
|
|
// channel to see if the client has gone away.
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2017-02-28 00:00:39 +00:00
|
|
|
for _, stm := range e.ast.Statements {
|
2016-10-18 12:49:46 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
select {
|
2016-10-18 12:49:46 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
case <-quit:
|
|
|
|
return
|
2016-10-18 12:49:46 +00:00
|
|
|
|
2017-02-28 00:00:39 +00:00
|
|
|
default:
|
2016-02-27 01:00:19 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
// If we are not inside a global transaction
|
|
|
|
// then reset the error to nil so that the
|
|
|
|
// next statement is not ignored.
|
2016-09-06 13:33:02 +00:00
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
if e.txn == nil {
|
2017-02-09 20:45:45 +00:00
|
|
|
err, now = nil, time.Now()
|
2016-10-27 08:33:14 +00:00
|
|
|
}
|
2016-02-27 01:00:19 +00:00
|
|
|
|
2017-02-23 10:11:52 +00:00
|
|
|
// When in debugging mode, log every sql
|
|
|
|
// query, along with the query execution
|
|
|
|
// speed, so we can analyse slow queries.
|
|
|
|
|
2017-02-23 10:14:10 +00:00
|
|
|
log.WithPrefix("sql").WithFields(map[string]interface{}{
|
|
|
|
"ctx": e.web,
|
|
|
|
}).Debugln(stm)
|
2017-02-23 10:11:52 +00:00
|
|
|
|
2016-10-27 08:33:14 +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.
|
|
|
|
|
|
|
|
switch stm.(type) {
|
|
|
|
case *sql.BeginStatement:
|
2017-02-12 11:56:46 +00:00
|
|
|
err = e.begin(true)
|
2016-10-27 08:33:14 +00:00
|
|
|
continue
|
|
|
|
case *sql.CancelStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
err, buf = e.cancel(buf, err, send)
|
2016-10-27 08:33:14 +00:00
|
|
|
continue
|
|
|
|
case *sql.CommitStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
err, buf = e.commit(buf, err, send)
|
2016-10-27 08:33:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an error has occured and we are inside
|
|
|
|
// a global transaction, then ignore all
|
|
|
|
// subsequent statements in the transaction.
|
2016-02-27 01:00:19 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
if err == nil {
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.operate(stm)
|
2016-10-18 12:49:46 +00:00
|
|
|
} else {
|
2017-02-20 01:09:24 +00:00
|
|
|
res, err = []interface{}{}, QueryNotExecuted
|
2016-10-18 12:49:46 +00:00
|
|
|
}
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
rsp = &Response{
|
|
|
|
Time: time.Since(now).String(),
|
|
|
|
Status: status(err),
|
|
|
|
Detail: detail(err),
|
|
|
|
Result: append([]interface{}{}, res...),
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are not inside a global transaction
|
|
|
|
// then we can output the statement response
|
|
|
|
// immediately to the channel.
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
if e.txn == nil {
|
2016-10-27 08:33:14 +00:00
|
|
|
send <- rsp
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are inside a global transaction we
|
|
|
|
// must buffer the responses for output at
|
|
|
|
// the end of the transaction.
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
if e.txn != nil {
|
2016-10-29 11:29:20 +00:00
|
|
|
switch stm.(type) {
|
|
|
|
case *sql.ReturnStatement:
|
|
|
|
buf = clear(buf, rsp)
|
|
|
|
default:
|
|
|
|
buf = append(buf, rsp)
|
|
|
|
}
|
2016-10-27 08:33:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-10-18 12:49:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
func (e *executor) operate(ast sql.Statement) (res []interface{}, err error) {
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
var loc bool
|
2017-02-12 11:56:46 +00:00
|
|
|
var trw bool
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
// If we are not inside a global transaction
|
|
|
|
// then grab a new transaction, ensuring that
|
|
|
|
// it is closed at the end.
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
if e.txn == nil {
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
loc = true
|
|
|
|
|
|
|
|
switch ast.(type) {
|
|
|
|
case *sql.InfoStatement:
|
2017-02-12 11:56:46 +00:00
|
|
|
trw = false
|
|
|
|
err = e.begin(trw)
|
2016-10-27 08:33:14 +00:00
|
|
|
default:
|
2017-02-12 11:56:46 +00:00
|
|
|
trw = true
|
|
|
|
err = e.begin(trw)
|
2016-10-27 08:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
2016-05-11 19:23:21 +00:00
|
|
|
}
|
2016-02-27 12:16:59 +00:00
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
defer e.txn.Cancel()
|
2016-10-27 08:33:14 +00:00
|
|
|
|
2016-02-27 01:00:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
// Execute the defined statement, receiving the
|
|
|
|
// result set, and any errors which occured
|
|
|
|
// while processing the query.
|
|
|
|
|
|
|
|
switch stm := ast.(type) {
|
|
|
|
|
|
|
|
case *sql.InfoStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeInfoStatement(stm)
|
2016-10-29 11:28:20 +00:00
|
|
|
|
|
|
|
case *sql.LetStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeLetStatement(stm)
|
2016-10-29 11:29:20 +00:00
|
|
|
case *sql.ReturnStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeReturnStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
case *sql.SelectStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeSelectStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
case *sql.CreateStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeCreateStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
case *sql.UpdateStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeUpdateStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
case *sql.DeleteStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeDeleteStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
case *sql.RelateStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeRelateStatement(stm)
|
|
|
|
|
|
|
|
case *sql.DefineNamespaceStatement:
|
|
|
|
res, err = e.executeDefineNamespaceStatement(stm)
|
|
|
|
case *sql.RemoveNamespaceStatement:
|
|
|
|
res, err = e.executeRemoveNamespaceStatement(stm)
|
|
|
|
|
|
|
|
case *sql.DefineDatabaseStatement:
|
|
|
|
res, err = e.executeDefineDatabaseStatement(stm)
|
|
|
|
case *sql.RemoveDatabaseStatement:
|
|
|
|
res, err = e.executeRemoveDatabaseStatement(stm)
|
|
|
|
|
|
|
|
case *sql.DefineLoginStatement:
|
|
|
|
res, err = e.executeDefineLoginStatement(stm)
|
|
|
|
case *sql.RemoveLoginStatement:
|
|
|
|
res, err = e.executeRemoveLoginStatement(stm)
|
|
|
|
|
|
|
|
case *sql.DefineTokenStatement:
|
|
|
|
res, err = e.executeDefineTokenStatement(stm)
|
|
|
|
case *sql.RemoveTokenStatement:
|
|
|
|
res, err = e.executeRemoveTokenStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
case *sql.DefineScopeStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeDefineScopeStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
case *sql.RemoveScopeStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeRemoveScopeStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
case *sql.DefineTableStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeDefineTableStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
case *sql.RemoveTableStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeRemoveTableStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
case *sql.DefineFieldStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeDefineFieldStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
case *sql.RemoveFieldStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeRemoveFieldStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
case *sql.DefineIndexStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeDefineIndexStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
case *sql.RemoveIndexStatement:
|
2017-02-09 20:43:24 +00:00
|
|
|
res, err = e.executeRemoveIndexStatement(stm)
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a local transaction for only the
|
|
|
|
// current statement, then commit or cancel
|
|
|
|
// depending on the result error.
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
if loc && !e.txn.Closed() {
|
2017-02-12 11:56:46 +00:00
|
|
|
if !trw || err != nil {
|
2017-02-20 01:17:05 +00:00
|
|
|
e.txn.Cancel()
|
|
|
|
e.txn = nil
|
2016-10-27 08:33:14 +00:00
|
|
|
} else {
|
2017-02-20 01:17:05 +00:00
|
|
|
e.txn.Commit()
|
|
|
|
e.txn = nil
|
2016-10-27 08:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2016-02-27 01:00:19 +00:00
|
|
|
}
|
2016-10-29 11:28:20 +00:00
|
|
|
|
|
|
|
func status(e error) (s string) {
|
|
|
|
switch e.(type) {
|
|
|
|
default:
|
|
|
|
return "OK"
|
|
|
|
case *kvs.DBError:
|
|
|
|
return "ERR_DB"
|
|
|
|
case *kvs.KVError:
|
|
|
|
return "ERR_KV"
|
|
|
|
case error:
|
|
|
|
return "ERR"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func detail(e error) (s string) {
|
|
|
|
switch err := e.(type) {
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
case error:
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func clear(buf []*Response, rsp *Response) []*Response {
|
|
|
|
for i := len(buf) - 1; i >= 0; i-- {
|
|
|
|
buf[len(buf)-1] = nil
|
|
|
|
buf = buf[:len(buf)-1]
|
|
|
|
}
|
|
|
|
return append(buf, rsp)
|
|
|
|
}
|
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
func (e *executor) begin(rw bool) (err error) {
|
2017-02-09 20:43:24 +00:00
|
|
|
if e.txn == nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
e.txn, err = db.Begin(rw)
|
|
|
|
e.mem = mem.New(e.txn)
|
2016-10-29 11:28:20 +00:00
|
|
|
}
|
2017-02-09 20:43:24 +00:00
|
|
|
return
|
2016-10-29 11:28:20 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
func (e *executor) cancel(buf []*Response, err error, chn chan<- *Response) (error, []*Response) {
|
2016-10-29 11:28:20 +00:00
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
defer func() {
|
|
|
|
e.txn = nil
|
|
|
|
e.mem = nil
|
|
|
|
}()
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
if e.txn == nil {
|
|
|
|
return nil, buf
|
2016-10-29 11:28:20 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.Cancel()
|
2016-10-29 11:28:20 +00:00
|
|
|
|
|
|
|
for _, v := range buf {
|
|
|
|
v.Status = "ERR"
|
|
|
|
chn <- v
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(buf) - 1; i >= 0; i-- {
|
|
|
|
buf[len(buf)-1] = nil
|
|
|
|
buf = buf[:len(buf)-1]
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
return nil, buf
|
2016-10-29 11:28:20 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
func (e *executor) commit(buf []*Response, err error, chn chan<- *Response) (error, []*Response) {
|
2016-10-29 11:28:20 +00:00
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
defer func() {
|
|
|
|
e.txn = nil
|
|
|
|
e.mem = nil
|
|
|
|
}()
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
if e.txn == nil {
|
|
|
|
return nil, buf
|
2016-10-29 11:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.Cancel()
|
2016-10-29 11:28:20 +00:00
|
|
|
} else {
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.Commit()
|
2016-10-29 11:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range buf {
|
|
|
|
if err != nil {
|
|
|
|
v.Status = "ERR"
|
|
|
|
}
|
|
|
|
chn <- v
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(buf) - 1; i >= 0; i-- {
|
|
|
|
buf[len(buf)-1] = nil
|
|
|
|
buf = buf[:len(buf)-1]
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
return nil, buf
|
2016-10-29 11:28:20 +00:00
|
|
|
|
|
|
|
}
|