surrealpatch/db/define.go
Tobie Morgan Hitchcock d0d566bbd9 Improve database speed
2019-11-20 13:20:27 +00:00

291 lines
6.8 KiB
Go

// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance wdbh 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 (
"context"
"golang.org/x/crypto/bcrypt"
"github.com/abcum/surreal/cnf"
"github.com/abcum/surreal/sql"
"github.com/abcum/surreal/util/keys"
"github.com/abcum/surreal/util/rand"
)
func (e *executor) executeDefineNamespace(ctx context.Context, ast *sql.DefineNamespaceStatement) (out []interface{}, err error) {
if err := e.access(ctx, cnf.AuthKV); err != nil {
return nil, err
}
// Save the namespace definition
nkey := &keys.NS{KV: KV, NS: ast.Name.VA}
_, err = e.tx.Put(ctx, 0, nkey.Encode(), ast.Encode())
return
}
func (e *executor) executeDefineDatabase(ctx context.Context, ast *sql.DefineDatabaseStatement) (out []interface{}, err error) {
if err := e.access(ctx, cnf.AuthNS); err != nil {
return nil, err
}
e.tx.AddNS(ctx, e.ns)
// Save the database definition
dkey := &keys.DB{KV: KV, NS: e.ns, DB: ast.Name.VA}
_, err = e.tx.Put(ctx, 0, dkey.Encode(), ast.Encode())
return
}
func (e *executor) executeDefineLogin(ctx context.Context, ast *sql.DefineLoginStatement) (out []interface{}, err error) {
ast.Code = rand.New(128)
switch len(ast.Hash) {
default:
ast.Pass = ast.Hash
case 0:
ast.Pass, _ = bcrypt.GenerateFromPassword(ast.Pass, bcrypt.DefaultCost)
}
switch ast.Kind {
case sql.NAMESPACE:
if err := e.access(ctx, cnf.AuthNS); err != nil {
return nil, err
}
e.tx.AddNS(ctx, e.ns)
// Save the login definition
ukey := &keys.NU{KV: KV, NS: e.ns, US: ast.User.VA}
_, err = e.tx.Put(ctx, 0, ukey.Encode(), ast.Encode())
case sql.DATABASE:
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
e.tx.AddDB(ctx, e.ns, e.db)
// Save the login definition
ukey := &keys.DU{KV: KV, NS: e.ns, DB: e.db, US: ast.User.VA}
_, err = e.tx.Put(ctx, 0, ukey.Encode(), ast.Encode())
}
return
}
func (e *executor) executeDefineToken(ctx context.Context, ast *sql.DefineTokenStatement) (out []interface{}, err error) {
switch ast.Kind {
case sql.NAMESPACE:
if err := e.access(ctx, cnf.AuthNS); err != nil {
return nil, err
}
e.tx.AddNS(ctx, e.ns)
// Save the token definition
tkey := &keys.NT{KV: KV, NS: e.ns, TK: ast.Name.VA}
_, err = e.tx.Put(ctx, 0, tkey.Encode(), ast.Encode())
case sql.DATABASE:
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
e.tx.AddDB(ctx, e.ns, e.db)
// Save the token definition
tkey := &keys.DT{KV: KV, NS: e.ns, DB: e.db, TK: ast.Name.VA}
_, err = e.tx.Put(ctx, 0, tkey.Encode(), ast.Encode())
case sql.SCOPE:
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
e.tx.AddDB(ctx, e.ns, e.db)
// Save the token definition
tkey := &keys.ST{KV: KV, NS: e.ns, DB: e.db, SC: ast.What.VA, TK: ast.Name.VA}
_, err = e.tx.Put(ctx, 0, tkey.Encode(), ast.Encode())
}
return
}
func (e *executor) executeDefineScope(ctx context.Context, ast *sql.DefineScopeStatement) (out []interface{}, err error) {
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
ast.Code = rand.New(128)
e.tx.AddDB(ctx, e.ns, e.db)
// Remove the scope definition
skey := &keys.SC{KV: KV, NS: e.ns, DB: e.db, SC: ast.Name.VA}
_, err = e.tx.Put(ctx, 0, skey.Encode(), ast.Encode())
return
}
func (e *executor) executeDefineEvent(ctx context.Context, ast *sql.DefineEventStatement) (out []interface{}, err error) {
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
for _, TB := range ast.What {
e.tx.AddTB(ctx, e.ns, e.db, TB.TB)
// Remove the event definition
ekey := &keys.EV{KV: KV, NS: e.ns, DB: e.db, TB: TB.TB, EV: ast.Name.VA}
if _, err = e.tx.Put(ctx, 0, ekey.Encode(), ast.Encode()); err != nil {
return nil, err
}
}
return
}
func (e *executor) executeDefineField(ctx context.Context, ast *sql.DefineFieldStatement) (out []interface{}, err error) {
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
for _, TB := range ast.What {
e.tx.AddTB(ctx, e.ns, e.db, TB.TB)
// Save the field definition
fkey := &keys.FD{KV: KV, NS: e.ns, DB: e.db, TB: TB.TB, FD: ast.Name.VA}
if _, err = e.tx.Put(ctx, 0, fkey.Encode(), ast.Encode()); err != nil {
return nil, err
}
}
return
}
func (e *executor) executeDefineIndex(ctx context.Context, ast *sql.DefineIndexStatement) (out []interface{}, err error) {
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
for _, TB := range ast.What {
e.tx.AddTB(ctx, e.ns, e.db, TB.TB)
// Save the index definition
ikey := &keys.IX{KV: KV, NS: e.ns, DB: e.db, TB: TB.TB, IX: ast.Name.VA}
if _, err = e.tx.Put(ctx, 0, ikey.Encode(), ast.Encode()); err != nil {
return nil, err
}
// Remove the index resource data
dkey := &keys.Index{KV: KV, NS: e.ns, DB: e.db, TB: TB.TB, IX: ast.Name.VA, FD: keys.Ignore}
if _, err = e.tx.ClrP(ctx, dkey.Encode(), 0); err != nil {
return nil, err
}
// Process the index resource data
uctx := context.WithValue(ctx, ctxKeyForce, true)
ustm := &sql.UpdateStatement{What: []sql.Expr{TB}}
if _, err = e.executeUpdate(uctx, ustm); err != nil {
return nil, err
}
}
return
}
func (e *executor) executeDefineTable(ctx context.Context, ast *sql.DefineTableStatement) (out []interface{}, err error) {
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
e.tx.AddDB(ctx, e.ns, e.db)
for _, TB := range ast.What {
ast.Name = sql.NewIdent(TB.TB)
// Save the table definition
tkey := &keys.TB{KV: KV, NS: e.ns, DB: e.db, TB: TB.TB}
if _, err = e.tx.Put(ctx, 0, tkey.Encode(), ast.Encode()); err != nil {
return nil, err
}
if ast.Lock {
// Remove the table resource data
dkey := &keys.Table{KV: KV, NS: e.ns, DB: e.db, TB: TB.TB}
if _, err = e.tx.ClrP(ctx, dkey.Encode(), 0); err != nil {
return nil, err
}
for _, FT := range ast.From {
// Save the foreign table definition
tkey := &keys.FT{KV: KV, NS: e.ns, DB: e.db, TB: FT.TB, FT: TB.TB}
if _, err = e.tx.Put(ctx, 0, tkey.Encode(), ast.Encode()); err != nil {
return nil, err
}
// Process the table resource data
uctx := context.WithValue(ctx, ctxKeyForce, true)
ustm := &sql.UpdateStatement{What: []sql.Expr{FT}}
if _, err = e.executeUpdate(uctx, ustm); err != nil {
return nil, err
}
}
}
}
return
}