diff --git a/mem/kv.go b/mem/kv.go index e95e6876..483ee4f8 100644 --- a/mem/kv.go +++ b/mem/kv.go @@ -19,17 +19,21 @@ import "github.com/abcum/surreal/sql" // -------------------------------------------------- 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 nil } 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 } else { - store[ast.Name] = &NS{ + store.NS[ast.Name] = &NS{ Name: ast.Name, AC: make(map[string]*AC), TK: make(map[string]*TK), diff --git a/mem/mem.go b/mem/mem.go index b65b6021..7f1ad98f 100644 --- a/mem/mem.go +++ b/mem/mem.go @@ -15,14 +15,21 @@ package mem import ( + "sync" "time" "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 { + sync.RWMutex AC map[string]*AC TK map[string]*TK DB map[string]*DB @@ -30,6 +37,7 @@ type NS struct { } type DB struct { + sync.RWMutex AC map[string]*AC TK map[string]*TK SC map[string]*SC @@ -38,24 +46,29 @@ type DB struct { } type TB struct { + sync.RWMutex + RU map[string]*RU FD map[string]*FD IX map[string]*IX Name string } type AC struct { + sync.RWMutex User string Pass []byte Code []byte } type TK struct { + sync.RWMutex Name string Type string Code []byte } type SC struct { + sync.RWMutex TK map[string]*TK Name string Code []byte @@ -65,6 +78,7 @@ type SC struct { } type FD struct { + sync.RWMutex Name string Type string Enum []interface{} @@ -80,11 +94,14 @@ type FD struct { } type IX struct { + sync.RWMutex Name string Cols []string Uniq bool } func init() { - store = make(map[string]*NS) + store = &KV{ + NS: make(map[string]*NS), + } } diff --git a/mem/ns.go b/mem/ns.go index e87fe01c..506f855c 100644 --- a/mem/ns.go +++ b/mem/ns.go @@ -19,6 +19,8 @@ import "github.com/abcum/surreal/sql" // -------------------------------------------------- func (this *NS) GetAC(name string) *AC { + this.RLock() + defer this.RUnlock() if ac, ok := this.AC[name]; ok { return ac } @@ -26,6 +28,8 @@ func (this *NS) GetAC(name string) *AC { } func (this *NS) AddAC(ast *sql.DefineLoginStatement) { + this.RLock() + defer this.RUnlock() if ac, ok := this.AC[ast.User]; ok { ac.User = ast.User ac.Pass = ast.Pass @@ -40,6 +44,8 @@ func (this *NS) AddAC(ast *sql.DefineLoginStatement) { // -------------------------------------------------- func (this *NS) GetTK(name string) *TK { + this.RLock() + defer this.RUnlock() if tk, ok := this.TK[name]; ok { return tk } @@ -47,6 +53,8 @@ func (this *NS) GetTK(name string) *TK { } func (this *NS) AddTK(ast *sql.DefineTokenStatement) { + this.RLock() + defer this.RUnlock() if tk, ok := this.TK[ast.Name]; ok { tk.Name = ast.Name tk.Code = ast.Code @@ -61,6 +69,8 @@ func (this *NS) AddTK(ast *sql.DefineTokenStatement) { // -------------------------------------------------- func (this *NS) GetDB(name string) *DB { + this.RLock() + defer this.RUnlock() if db, ok := this.DB[name]; ok { return db } @@ -68,6 +78,8 @@ func (this *NS) GetDB(name string) *DB { } func (this *NS) AddDB(ast *sql.DefineDatabaseStatement) { + this.RLock() + defer this.RUnlock() if db, ok := this.DB[ast.Name]; ok { db.Name = ast.Name } else { diff --git a/mem/sc.go b/mem/sc.go index fb976e6a..9fa1d56d 100644 --- a/mem/sc.go +++ b/mem/sc.go @@ -19,6 +19,8 @@ import "github.com/abcum/surreal/sql" // -------------------------------------------------- func (this *SC) GetTK(name string) *TK { + this.RLock() + defer this.RUnlock() if tk, ok := this.TK[name]; ok { return tk } @@ -26,6 +28,8 @@ func (this *SC) GetTK(name string) *TK { } func (this *SC) AddTK(ast *sql.DefineTokenStatement) { + this.RLock() + defer this.RUnlock() if tk, ok := this.TK[ast.Name]; ok { tk.Name = ast.Name tk.Code = ast.Code diff --git a/mem/tb.go b/mem/tb.go index c9b48c9e..a5977467 100644 --- a/mem/tb.go +++ b/mem/tb.go @@ -19,6 +19,8 @@ import "github.com/abcum/surreal/sql" // -------------------------------------------------- func (this *TB) GetFD(name string) *FD { + this.RLock() + defer this.RUnlock() if fd, ok := this.FD[name]; ok { return fd } @@ -26,6 +28,8 @@ func (this *TB) GetFD(name string) *FD { } func (this *TB) AddFD(ast *sql.DefineFieldStatement) { + this.RLock() + defer this.RUnlock() if fd, ok := this.FD[ast.Name]; ok { fd.Name = ast.Name fd.Type = ast.Type @@ -60,6 +64,8 @@ func (this *TB) AddFD(ast *sql.DefineFieldStatement) { // -------------------------------------------------- func (this *TB) GetIX(name string) *IX { + this.RLock() + defer this.RUnlock() if ix, ok := this.IX[name]; ok { return ix } @@ -67,6 +73,8 @@ func (this *TB) GetIX(name string) *IX { } func (this *TB) AddIX(ast *sql.DefineIndexStatement) { + this.RLock() + defer this.RUnlock() if ix, ok := this.IX[ast.Name]; ok { ix.Name = ast.Name ix.Cols = ast.Cols