2017-11-16 20:53:39 +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 (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/abcum/fibre"
|
2019-01-31 10:03:50 +00:00
|
|
|
"github.com/abcum/surreal/cnf"
|
2017-11-16 20:53:39 +00:00
|
|
|
"github.com/abcum/surreal/sql"
|
|
|
|
)
|
|
|
|
|
2018-04-24 23:01:29 +00:00
|
|
|
var sockets sync.Map
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
func register(fib *fibre.Context, id string) func() {
|
|
|
|
return func() {
|
|
|
|
|
2018-04-26 23:04:36 +00:00
|
|
|
sockets.Store(id, &socket{
|
2017-11-16 20:53:39 +00:00
|
|
|
fibre: fib,
|
2018-04-14 18:20:15 +00:00
|
|
|
items: make(map[string][]interface{}),
|
2017-11-16 20:53:39 +00:00
|
|
|
lives: make(map[string]*sql.LiveStatement),
|
2018-04-24 23:01:29 +00:00
|
|
|
})
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deregister(fib *fibre.Context, id string) func() {
|
|
|
|
return func() {
|
|
|
|
|
2018-04-24 23:01:29 +00:00
|
|
|
if sck, ok := sockets.Load(id); ok {
|
|
|
|
sck.(*socket).deregister(id)
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executor) executeLive(ctx context.Context, stm *sql.LiveStatement) (out []interface{}, err error) {
|
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
if err := e.access(ctx, cnf.AuthNO); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-02-06 08:08:07 +00:00
|
|
|
if sck, ok := sockets.Load(e.id); ok {
|
2018-04-24 23:01:29 +00:00
|
|
|
return sck.(*socket).executeLive(e, ctx, stm)
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
return nil, &LiveError{}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executor) executeKill(ctx context.Context, stm *sql.KillStatement) (out []interface{}, err error) {
|
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
if err := e.access(ctx, cnf.AuthNO); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2019-02-06 08:08:07 +00:00
|
|
|
if sck, ok := sockets.Load(e.id); ok {
|
2018-04-24 23:01:29 +00:00
|
|
|
return sck.(*socket).executeKill(e, ctx, stm)
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 10:03:50 +00:00
|
|
|
return nil, &LiveError{}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
}
|