From d5604f589c79df23bd2090a7bce2ccb2d7c6d929 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 21 Nov 2016 18:47:23 +0000 Subject: [PATCH] Add package for memory storage of database runtime Database configuration data for all namespaces, databases, logins, tokens, scopes, tables, fields, indexes should be stored in memory for quick access when needed without querying the KV store. It should be kept in sync with the master data in the unerlying KV store, but it can be used for propagating the database configuration across the cluster. --- mem/db.go | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++ mem/kv.go | 39 ++++++++++++++++++++ mem/mem.go | 90 +++++++++++++++++++++++++++++++++++++++++++++ mem/ns.go | 82 +++++++++++++++++++++++++++++++++++++++++ mem/sc.go | 38 +++++++++++++++++++ mem/tb.go | 81 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 436 insertions(+) create mode 100644 mem/db.go create mode 100644 mem/kv.go create mode 100644 mem/mem.go create mode 100644 mem/ns.go create mode 100644 mem/sc.go create mode 100644 mem/tb.go diff --git a/mem/db.go b/mem/db.go new file mode 100644 index 00000000..7638e4d5 --- /dev/null +++ b/mem/db.go @@ -0,0 +1,106 @@ +// 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 mem + +import "github.com/abcum/surreal/sql" + +// -------------------------------------------------- + +func (this *DB) GetAC(name string) *AC { + if ac, ok := this.AC[name]; ok { + return ac + } + return nil +} + +func (this *DB) AddAC(ast *sql.DefineLoginStatement) { + if ac, ok := this.AC[ast.User]; ok { + ac.User = ast.User + ac.Pass = ast.Pass + } else { + this.AC[ast.User] = &AC{ + User: ast.User, + Pass: ast.Pass, + } + } +} + +// -------------------------------------------------- + +func (this *DB) GetTK(name string) *TK { + if tk, ok := this.TK[name]; ok { + return tk + } + return nil +} + +func (this *DB) AddTK(ast *sql.DefineTokenStatement) { + if tk, ok := this.TK[ast.Name]; ok { + tk.Name = ast.Name + tk.Text = ast.Text + } else { + this.TK[ast.Name] = &TK{ + Name: ast.Name, + Text: ast.Text, + } + } +} + +// -------------------------------------------------- + +func (this *DB) GetSC(name string) *SC { + if sc, ok := this.SC[name]; ok { + return sc + } + return nil +} + +func (this *DB) AddSC(ast *sql.DefineScopeStatement) { + if sc, ok := this.SC[ast.Name]; ok { + sc.Name = ast.Name + sc.Time = ast.Time + sc.Signup = ast.Signup + sc.Signin = ast.Signin + } else { + this.SC[ast.Name] = &SC{ + Name: ast.Name, + Time: ast.Time, + Signup: ast.Signup, + Signin: ast.Signin, + } + } +} + +// -------------------------------------------------- + +func (this *DB) GetTB(name string) *TB { + if tb, ok := this.TB[name]; ok { + return tb + } + return nil +} + +func (this *DB) AddTB(ast *sql.DefineTableStatement) { + for _, name := range ast.What { + if tb, ok := this.TB[name]; ok { + tb.Name = name + } else { + this.TB[name] = &TB{ + Name: name, + FD: make(map[string]*FD), + } + } + } +} diff --git a/mem/kv.go b/mem/kv.go new file mode 100644 index 00000000..e95e6876 --- /dev/null +++ b/mem/kv.go @@ -0,0 +1,39 @@ +// 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 mem + +import "github.com/abcum/surreal/sql" + +// -------------------------------------------------- + +func GetNS(name string) *NS { + if ns, ok := store[name]; ok { + return ns + } + return nil +} + +func AddNS(ast *sql.DefineNamespaceStatement) { + if ns, ok := store[ast.Name]; ok { + ns.Name = ast.Name + } else { + store[ast.Name] = &NS{ + Name: ast.Name, + AC: make(map[string]*AC), + TK: make(map[string]*TK), + DB: make(map[string]*DB), + } + } +} diff --git a/mem/mem.go b/mem/mem.go new file mode 100644 index 00000000..52f65fc5 --- /dev/null +++ b/mem/mem.go @@ -0,0 +1,90 @@ +// 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 mem + +import ( + "time" + + "github.com/abcum/surreal/sql" +) + +var store map[string]*NS + +type NS struct { + AC map[string]*AC + TK map[string]*TK + DB map[string]*DB + Name string +} + +type DB struct { + AC map[string]*AC + TK map[string]*TK + SC map[string]*SC + TB map[string]*TB + Name string +} + +type TB struct { + FD map[string]*FD + IX map[string]*IX + Name string +} + +type AC struct { + User string + Pass string + Uniq string +} + +type TK struct { + Name string + Type string + Text string +} + +type SC struct { + TK map[string]*TK + Name string + Uniq string + Time time.Duration + Signup sql.Expr + Signin sql.Expr +} + +type FD struct { + Name string + Type string + Enum []interface{} + Code string + Min float64 + Max float64 + Match string + Default interface{} + Notnull bool + Readonly bool + Mandatory bool + Validate bool +} + +type IX struct { + Name string + Cols []string + Uniq bool +} + +func init() { + store = make(map[string]*NS) +} diff --git a/mem/ns.go b/mem/ns.go new file mode 100644 index 00000000..dca1c67b --- /dev/null +++ b/mem/ns.go @@ -0,0 +1,82 @@ +// 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 mem + +import "github.com/abcum/surreal/sql" + +// -------------------------------------------------- + +func (this *NS) GetAC(name string) *AC { + if ac, ok := this.AC[name]; ok { + return ac + } + return nil +} + +func (this *NS) AddAC(ast *sql.DefineLoginStatement) { + if ac, ok := this.AC[ast.User]; ok { + ac.User = ast.User + ac.Pass = ast.Pass + } else { + this.AC[ast.User] = &AC{ + User: ast.User, + Pass: ast.Pass, + } + } +} + +// -------------------------------------------------- + +func (this *NS) GetTK(name string) *TK { + if tk, ok := this.TK[name]; ok { + return tk + } + return nil +} + +func (this *NS) AddTK(ast *sql.DefineTokenStatement) { + if tk, ok := this.TK[ast.Name]; ok { + tk.Name = ast.Name + tk.Text = ast.Text + } else { + this.TK[ast.Name] = &TK{ + Name: ast.Name, + Text: ast.Text, + } + } +} + +// -------------------------------------------------- + +func (this *NS) GetDB(name string) *DB { + if db, ok := this.DB[name]; ok { + return db + } + return nil +} + +func (this *NS) AddDB(ast *sql.DefineDatabaseStatement) { + if db, ok := this.DB[ast.Name]; ok { + db.Name = ast.Name + } else { + this.DB[ast.Name] = &DB{ + Name: ast.Name, + AC: make(map[string]*AC), + TK: make(map[string]*TK), + SC: make(map[string]*SC), + TB: make(map[string]*TB), + } + } +} diff --git a/mem/sc.go b/mem/sc.go new file mode 100644 index 00000000..31775987 --- /dev/null +++ b/mem/sc.go @@ -0,0 +1,38 @@ +// 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 mem + +import "github.com/abcum/surreal/sql" + +// -------------------------------------------------- + +func (this *SC) GetTK(name string) *TK { + if tk, ok := this.TK[name]; ok { + return tk + } + return nil +} + +func (this *SC) AddTK(ast *sql.DefineTokenStatement) { + if tk, ok := this.TK[ast.Name]; ok { + tk.Name = ast.Name + tk.Text = ast.Text + } else { + this.TK[ast.Name] = &TK{ + Name: ast.Name, + Text: ast.Text, + } + } +} diff --git a/mem/tb.go b/mem/tb.go new file mode 100644 index 00000000..1e821cc8 --- /dev/null +++ b/mem/tb.go @@ -0,0 +1,81 @@ +// 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 mem + +import "github.com/abcum/surreal/sql" + +// -------------------------------------------------- + +func (this *TB) GetFD(name string) *FD { + if fd, ok := this.FD[name]; ok { + return fd + } + return nil +} + +func (this *TB) AddFD(ast *sql.DefineFieldStatement) { + if fd, ok := this.FD[ast.Name]; ok { + fd.Name = ast.Name + fd.Type = ast.Type + fd.Enum = ast.Enum + fd.Code = ast.Code + fd.Min = ast.Min + fd.Max = ast.Max + fd.Match = ast.Match + fd.Default = ast.Default + fd.Notnull = ast.Notnull + fd.Readonly = ast.Readonly + fd.Mandatory = ast.Mandatory + fd.Validate = ast.Validate + } else { + this.FD[ast.Name] = &FD{ + Name: ast.Name, + Type: ast.Type, + Enum: ast.Enum, + Code: ast.Code, + Min: ast.Min, + Max: ast.Max, + Match: ast.Match, + Default: ast.Default, + Notnull: ast.Notnull, + Readonly: ast.Readonly, + Mandatory: ast.Mandatory, + Validate: ast.Validate, + } + } +} + +// -------------------------------------------------- + +func (this *TB) GetIX(name string) *IX { + if ix, ok := this.IX[name]; ok { + return ix + } + return nil +} + +func (this *TB) AdIX(ast *sql.DefineIndexStatement) { + if ix, ok := this.IX[ast.Name]; ok { + ix.Name = ast.Name + ix.Cols = ast.Cols + ix.Uniq = ast.Uniq + } else { + this.IX[ast.Name] = &IX{ + Name: ast.Name, + Cols: ast.Cols, + Uniq: ast.Uniq, + } + } +}