Ensure in-memory config can be accessed concurrently

This commit is contained in:
Tobie Morgan Hitchcock 2016-11-23 15:42:32 +00:00
parent bf60887dc0
commit 3442275878
5 changed files with 50 additions and 5 deletions

View file

@ -19,17 +19,21 @@ import "github.com/abcum/surreal/sql"
// -------------------------------------------------- // --------------------------------------------------
func GetNS(name string) *NS { func GetNS(name string) *NS {
if ns, ok := store[name]; ok { store.RLock()
defer store.RUnlock()
if ns, ok := store.NS[name]; ok {
return ns return ns
} }
return nil return nil
} }
func AddNS(ast *sql.DefineNamespaceStatement) { func AddNS(ast *sql.DefineNamespaceStatement) {
if ns, ok := store[ast.Name]; ok { store.RLock()
defer store.RUnlock()
if ns, ok := store.NS[ast.Name]; ok {
ns.Name = ast.Name ns.Name = ast.Name
} else { } else {
store[ast.Name] = &NS{ store.NS[ast.Name] = &NS{
Name: ast.Name, Name: ast.Name,
AC: make(map[string]*AC), AC: make(map[string]*AC),
TK: make(map[string]*TK), TK: make(map[string]*TK),

View file

@ -15,14 +15,21 @@
package mem package mem
import ( import (
"sync"
"time" "time"
"github.com/abcum/surreal/sql" "github.com/abcum/surreal/sql"
) )
var store map[string]*NS var store *KV
type KV struct {
sync.RWMutex
NS map[string]*NS
}
type NS struct { type NS struct {
sync.RWMutex
AC map[string]*AC AC map[string]*AC
TK map[string]*TK TK map[string]*TK
DB map[string]*DB DB map[string]*DB
@ -30,6 +37,7 @@ type NS struct {
} }
type DB struct { type DB struct {
sync.RWMutex
AC map[string]*AC AC map[string]*AC
TK map[string]*TK TK map[string]*TK
SC map[string]*SC SC map[string]*SC
@ -38,24 +46,29 @@ type DB struct {
} }
type TB struct { type TB struct {
sync.RWMutex
RU map[string]*RU
FD map[string]*FD FD map[string]*FD
IX map[string]*IX IX map[string]*IX
Name string Name string
} }
type AC struct { type AC struct {
sync.RWMutex
User string User string
Pass []byte Pass []byte
Code []byte Code []byte
} }
type TK struct { type TK struct {
sync.RWMutex
Name string Name string
Type string Type string
Code []byte Code []byte
} }
type SC struct { type SC struct {
sync.RWMutex
TK map[string]*TK TK map[string]*TK
Name string Name string
Code []byte Code []byte
@ -65,6 +78,7 @@ type SC struct {
} }
type FD struct { type FD struct {
sync.RWMutex
Name string Name string
Type string Type string
Enum []interface{} Enum []interface{}
@ -80,11 +94,14 @@ type FD struct {
} }
type IX struct { type IX struct {
sync.RWMutex
Name string Name string
Cols []string Cols []string
Uniq bool Uniq bool
} }
func init() { func init() {
store = make(map[string]*NS) store = &KV{
NS: make(map[string]*NS),
}
} }

View file

@ -19,6 +19,8 @@ import "github.com/abcum/surreal/sql"
// -------------------------------------------------- // --------------------------------------------------
func (this *NS) GetAC(name string) *AC { func (this *NS) GetAC(name string) *AC {
this.RLock()
defer this.RUnlock()
if ac, ok := this.AC[name]; ok { if ac, ok := this.AC[name]; ok {
return ac return ac
} }
@ -26,6 +28,8 @@ func (this *NS) GetAC(name string) *AC {
} }
func (this *NS) AddAC(ast *sql.DefineLoginStatement) { func (this *NS) AddAC(ast *sql.DefineLoginStatement) {
this.RLock()
defer this.RUnlock()
if ac, ok := this.AC[ast.User]; ok { if ac, ok := this.AC[ast.User]; ok {
ac.User = ast.User ac.User = ast.User
ac.Pass = ast.Pass ac.Pass = ast.Pass
@ -40,6 +44,8 @@ func (this *NS) AddAC(ast *sql.DefineLoginStatement) {
// -------------------------------------------------- // --------------------------------------------------
func (this *NS) GetTK(name string) *TK { func (this *NS) GetTK(name string) *TK {
this.RLock()
defer this.RUnlock()
if tk, ok := this.TK[name]; ok { if tk, ok := this.TK[name]; ok {
return tk return tk
} }
@ -47,6 +53,8 @@ func (this *NS) GetTK(name string) *TK {
} }
func (this *NS) AddTK(ast *sql.DefineTokenStatement) { func (this *NS) AddTK(ast *sql.DefineTokenStatement) {
this.RLock()
defer this.RUnlock()
if tk, ok := this.TK[ast.Name]; ok { if tk, ok := this.TK[ast.Name]; ok {
tk.Name = ast.Name tk.Name = ast.Name
tk.Code = ast.Code tk.Code = ast.Code
@ -61,6 +69,8 @@ func (this *NS) AddTK(ast *sql.DefineTokenStatement) {
// -------------------------------------------------- // --------------------------------------------------
func (this *NS) GetDB(name string) *DB { func (this *NS) GetDB(name string) *DB {
this.RLock()
defer this.RUnlock()
if db, ok := this.DB[name]; ok { if db, ok := this.DB[name]; ok {
return db return db
} }
@ -68,6 +78,8 @@ func (this *NS) GetDB(name string) *DB {
} }
func (this *NS) AddDB(ast *sql.DefineDatabaseStatement) { func (this *NS) AddDB(ast *sql.DefineDatabaseStatement) {
this.RLock()
defer this.RUnlock()
if db, ok := this.DB[ast.Name]; ok { if db, ok := this.DB[ast.Name]; ok {
db.Name = ast.Name db.Name = ast.Name
} else { } else {

View file

@ -19,6 +19,8 @@ import "github.com/abcum/surreal/sql"
// -------------------------------------------------- // --------------------------------------------------
func (this *SC) GetTK(name string) *TK { func (this *SC) GetTK(name string) *TK {
this.RLock()
defer this.RUnlock()
if tk, ok := this.TK[name]; ok { if tk, ok := this.TK[name]; ok {
return tk return tk
} }
@ -26,6 +28,8 @@ func (this *SC) GetTK(name string) *TK {
} }
func (this *SC) AddTK(ast *sql.DefineTokenStatement) { func (this *SC) AddTK(ast *sql.DefineTokenStatement) {
this.RLock()
defer this.RUnlock()
if tk, ok := this.TK[ast.Name]; ok { if tk, ok := this.TK[ast.Name]; ok {
tk.Name = ast.Name tk.Name = ast.Name
tk.Code = ast.Code tk.Code = ast.Code

View file

@ -19,6 +19,8 @@ import "github.com/abcum/surreal/sql"
// -------------------------------------------------- // --------------------------------------------------
func (this *TB) GetFD(name string) *FD { func (this *TB) GetFD(name string) *FD {
this.RLock()
defer this.RUnlock()
if fd, ok := this.FD[name]; ok { if fd, ok := this.FD[name]; ok {
return fd return fd
} }
@ -26,6 +28,8 @@ func (this *TB) GetFD(name string) *FD {
} }
func (this *TB) AddFD(ast *sql.DefineFieldStatement) { func (this *TB) AddFD(ast *sql.DefineFieldStatement) {
this.RLock()
defer this.RUnlock()
if fd, ok := this.FD[ast.Name]; ok { if fd, ok := this.FD[ast.Name]; ok {
fd.Name = ast.Name fd.Name = ast.Name
fd.Type = ast.Type fd.Type = ast.Type
@ -60,6 +64,8 @@ func (this *TB) AddFD(ast *sql.DefineFieldStatement) {
// -------------------------------------------------- // --------------------------------------------------
func (this *TB) GetIX(name string) *IX { func (this *TB) GetIX(name string) *IX {
this.RLock()
defer this.RUnlock()
if ix, ok := this.IX[name]; ok { if ix, ok := this.IX[name]; ok {
return ix return ix
} }
@ -67,6 +73,8 @@ func (this *TB) GetIX(name string) *IX {
} }
func (this *TB) AddIX(ast *sql.DefineIndexStatement) { func (this *TB) AddIX(ast *sql.DefineIndexStatement) {
this.RLock()
defer this.RUnlock()
if ix, ok := this.IX[ast.Name]; ok { if ix, ok := this.IX[ast.Name]; ok {
ix.Name = ast.Name ix.Name = ast.Name
ix.Cols = ast.Cols ix.Cols = ast.Cols