Switch underlying KV store to rixxdb/dendrodb

This commit is contained in:
Tobie Morgan Hitchcock 2017-02-09 20:39:19 +00:00
parent f56196a150
commit 6807d4c338
3 changed files with 226 additions and 172 deletions

View file

@ -27,9 +27,8 @@ import (
"github.com/abcum/surreal/sql"
"github.com/abcum/surreal/util/data"
_ "github.com/abcum/surreal/kvs/boltdb"
_ "github.com/abcum/surreal/kvs/mysql"
_ "github.com/abcum/surreal/kvs/pgsql"
_ "github.com/abcum/surreal/kvs/rixxdb"
// _ "github.com/abcum/surreal/kvs/dendro"
)
type executor struct {
@ -61,7 +60,7 @@ type Response struct {
Result []interface{} `codec:"result,omitempty"`
}
var db *kvs.DB
var db *kvs.DS
// Setup sets up the connection with the data layer
func Setup(opts *cnf.Options) (err error) {

View file

@ -22,25 +22,120 @@ import (
"github.com/abcum/surreal/util/pack"
)
func (e *executor) executeDefineNamespaceStatement(txn kvs.TX, ast *sql.DefineNamespaceStatement) (out []interface{}, err error) {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.Name}
_, err = txn.Put(0, nkey.Encode(), ast.Encode())
return
}
func (e *executor) executeDefineDatabaseStatement(txn kvs.TX, ast *sql.DefineDatabaseStatement) (out []interface{}, err error) {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.Name}
_, err = txn.Put(0, dkey.Encode(), ast.Encode())
return
}
func (e *executor) executeDefineLoginStatement(txn kvs.TX, ast *sql.DefineLoginStatement) (out []interface{}, err error) {
if ast.Kind == sql.NAMESPACE {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the login
ukey := &keys.NU{KV: ast.KV, NS: ast.NS, US: ast.User}
_, err = txn.Put(0, ukey.Encode(), ast.Encode())
}
if ast.Kind == sql.DATABASE {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
// Set the login
ukey := &keys.DU{KV: ast.KV, NS: ast.NS, DB: ast.DB, US: ast.User}
_, err = txn.Put(0, ukey.Encode(), ast.Encode())
}
return
}
func (e *executor) executeDefineTokenStatement(txn kvs.TX, ast *sql.DefineTokenStatement) (out []interface{}, err error) {
if ast.Kind == sql.NAMESPACE {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the token
tkey := &keys.NT{KV: ast.KV, NS: ast.NS, TK: ast.Name}
_, err = txn.Put(0, tkey.Encode(), ast.Encode())
}
if ast.Kind == sql.DATABASE {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
// Set the token
tkey := &keys.DT{KV: ast.KV, NS: ast.NS, DB: ast.DB, TK: ast.Name}
_, err = txn.Put(0, tkey.Encode(), ast.Encode())
}
return
}
func (e *executor) executeDefineScopeStatement(txn kvs.TX, ast *sql.DefineScopeStatement) (out []interface{}, err error) {
// Set the namespace definition
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
if err := txn.Put(nkey.Encode(), nil); err != nil {
return nil, err
}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the database definition
// Set the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
if err := txn.Put(dkey.Encode(), nil); err != nil {
return nil, err
}
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
// Set the scope definition
// Set the scope
skey := &keys.SC{KV: ast.KV, NS: ast.NS, DB: ast.DB, SC: ast.Name}
if err := txn.Put(skey.Encode(), nil); err != nil {
return nil, err
}
_, err = txn.Put(0, skey.Encode(), ast.Encode())
return
@ -48,23 +143,21 @@ func (e *executor) executeDefineScopeStatement(txn kvs.TX, ast *sql.DefineScopeS
func (e *executor) executeDefineTableStatement(txn kvs.TX, ast *sql.DefineTableStatement) (out []interface{}, err error) {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
for _, TB := range ast.What {
// Set the namespace definition
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
if err := txn.Put(nkey.Encode(), nil); err != nil {
return nil, err
}
// Set the database definition
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
if err := txn.Put(dkey.Encode(), nil); err != nil {
return nil, err
}
// Set the table definition
// Set the table
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
if err := txn.Put(tkey.Encode(), nil); err != nil {
if _, err = txn.Put(0, tkey.Encode(), ast.Encode()); err != nil {
return nil, err
}
@ -74,69 +167,26 @@ func (e *executor) executeDefineTableStatement(txn kvs.TX, ast *sql.DefineTableS
}
func (e *executor) executeDefineRulesStatement(txn kvs.TX, ast *sql.DefineRulesStatement) (out []interface{}, err error) {
for _, TB := range ast.What {
for _, RU := range ast.When {
// Set the namespace definition
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
if err := txn.Put(nkey.Encode(), nil); err != nil {
return nil, err
}
// Set the database definition
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
if err := txn.Put(dkey.Encode(), nil); err != nil {
return nil, err
}
// Set the table definition
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
if err := txn.Put(tkey.Encode(), nil); err != nil {
return nil, err
}
// Set the field definition
rkey := &keys.RU{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, RU: RU}
if err := txn.Put(rkey.Encode(), pack.Encode(ast)); err != nil {
return nil, err
}
}
}
return
}
func (e *executor) executeDefineFieldStatement(txn kvs.TX, ast *sql.DefineFieldStatement) (out []interface{}, err error) {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
txn.Put(0, nkey.Encode(), nil)
// Set the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
txn.Put(0, dkey.Encode(), nil)
for _, TB := range ast.What {
// Set the namespace definition
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
if err := txn.Put(nkey.Encode(), nil); err != nil {
return nil, err
}
// Set the database definition
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
if err := txn.Put(dkey.Encode(), nil); err != nil {
return nil, err
}
// Set the table definition
// Set the table
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
if err := txn.Put(tkey.Encode(), nil); err != nil {
return nil, err
}
tval := &sql.DefineTableStatement{What: ast.What}
txn.PutC(0, tkey.Encode(), tval.Encode(), nil)
// Set the field definition
// Set the field
fkey := &keys.FD{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, FD: ast.Name}
if err := txn.Put(fkey.Encode(), pack.Encode(ast)); err != nil {
if _, err = txn.Put(0, fkey.Encode(), pack.Encode(ast)); err != nil {
return nil, err
}
@ -148,43 +198,38 @@ func (e *executor) executeDefineFieldStatement(txn kvs.TX, ast *sql.DefineFieldS
func (e *executor) executeDefineIndexStatement(txn kvs.TX, ast *sql.DefineIndexStatement) (out []interface{}, err error) {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
txn.Put(0, nkey.Encode(), nil)
// Set the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
txn.Put(0, dkey.Encode(), nil)
for _, TB := range ast.What {
// Set the namespace definition
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
if err := txn.Put(nkey.Encode(), nil); err != nil {
return nil, err
}
// Set the database definition
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
if err := txn.Put(dkey.Encode(), nil); err != nil {
return nil, err
}
// Set the table definition
// Set the table
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
if err := txn.Put(tkey.Encode(), nil); err != nil {
return nil, err
}
tval := &sql.DefineTableStatement{What: ast.What}
txn.PutC(0, tkey.Encode(), tval.Encode(), nil)
// Set the index definition
// Set the index
ikey := &keys.IX{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, IX: ast.Name}
if err := txn.Put(ikey.Encode(), pack.Encode(ast)); err != nil {
if _, err = txn.Put(0, ikey.Encode(), ast.Encode()); err != nil {
return nil, err
}
// Remove all index data
dbeg := &keys.Index{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, IX: keys.Prefix, FD: keys.Ignore}
dend := &keys.Index{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, IX: keys.Suffix, FD: keys.Ignore}
if err := txn.RDel(dbeg.Encode(), dend.Encode(), 0); err != nil {
if _, err = txn.DelR(0, dbeg.Encode(), dend.Encode(), 0); err != nil {
return nil, err
}
// Fetch the items
ibeg := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, ID: keys.Prefix}
iend := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, ID: keys.Suffix}
kvs, _ := txn.RGet(ibeg.Encode(), iend.Encode(), 0)
kvs, _ := txn.GetR(0, ibeg.Encode(), iend.Encode(), 0)
for _, kv := range kvs {
doc := item.New(kv, txn, nil, e.ctx)
if err := doc.StoreIndex(); err != nil {

View file

@ -20,13 +20,75 @@ import (
"github.com/abcum/surreal/util/keys"
)
func (e *executor) executeRemoveNamespaceStatement(txn kvs.TX, ast *sql.RemoveNamespaceStatement) (out []interface{}, err error) {
// Remove the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.Name}
_, err = txn.DelP(0, nkey.Encode(), 0)
return
}
func (e *executor) executeRemoveDatabaseStatement(txn kvs.TX, ast *sql.RemoveDatabaseStatement) (out []interface{}, err error) {
// Remove the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.Name}
_, err = txn.DelP(0, dkey.Encode(), 0)
return
}
func (e *executor) executeRemoveLoginStatement(txn kvs.TX, ast *sql.RemoveLoginStatement) (out []interface{}, err error) {
if ast.Kind == sql.NAMESPACE {
// Remove the login
ukey := &keys.NU{KV: ast.KV, NS: ast.NS, US: ast.User}
_, err = txn.DelP(0, ukey.Encode(), 0)
}
if ast.Kind == sql.DATABASE {
// Remove the login
ukey := &keys.DU{KV: ast.KV, NS: ast.NS, DB: ast.DB, US: ast.User}
_, err = txn.DelP(0, ukey.Encode(), 0)
}
return
}
func (e *executor) executeRemoveTokenStatement(txn kvs.TX, ast *sql.RemoveTokenStatement) (out []interface{}, err error) {
if ast.Kind == sql.NAMESPACE {
// Remove the token
tkey := &keys.NT{KV: ast.KV, NS: ast.NS, TK: ast.Name}
_, err = txn.DelP(0, tkey.Encode(), 0)
}
if ast.Kind == sql.DATABASE {
// Remove the token
tkey := &keys.DT{KV: ast.KV, NS: ast.NS, DB: ast.DB, TK: ast.Name}
_, err = txn.DelP(0, tkey.Encode(), 0)
}
return
}
func (e *executor) executeRemoveScopeStatement(txn kvs.TX, ast *sql.RemoveScopeStatement) (out []interface{}, err error) {
// Remove the scope config
// Remove the scope
skey := &keys.SC{KV: ast.KV, NS: ast.NS, DB: ast.DB, SC: ast.Name}
if err := txn.Del(skey.Encode()); err != nil {
return nil, err
}
_, err = txn.DelP(0, skey.Encode(), 0)
return
@ -36,55 +98,9 @@ func (e *executor) executeRemoveTableStatement(txn kvs.TX, ast *sql.RemoveTableS
for _, TB := range ast.What {
// Remove the table config
// Remove the table
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
if err := txn.Del(tkey.Encode()); err != nil {
return nil, err
}
// Remove the rules config
rkey := &keys.RU{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, RU: keys.Ignore}
if err := txn.PDel(rkey.Encode()); err != nil {
return nil, err
}
// Remove the field config
fkey := &keys.FD{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, FD: keys.Ignore}
if err := txn.PDel(fkey.Encode()); err != nil {
return nil, err
}
// Remove the index config
ikey := &keys.IX{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, IX: keys.Ignore}
if err := txn.PDel(ikey.Encode()); err != nil {
return nil, err
}
// Remove all table data
dkey := &keys.Table{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
if err := txn.PDel(dkey.Encode()); err != nil {
return nil, err
}
}
return
}
func (e *executor) executeRemoveRulesStatement(txn kvs.TX, ast *sql.RemoveRulesStatement) (out []interface{}, err error) {
for _, TB := range ast.What {
for _, RU := range ast.When {
// Remove the rules config
ckey := &keys.RU{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, RU: RU}
if err := txn.Del(ckey.Encode()); err != nil {
return nil, err
}
}
_, err = txn.DelP(0, tkey.Encode(), 0)
}
@ -96,11 +112,9 @@ func (e *executor) executeRemoveFieldStatement(txn kvs.TX, ast *sql.RemoveFieldS
for _, TB := range ast.What {
// Remove the field config
ckey := &keys.FD{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, FD: ast.Name}
if err := txn.Del(ckey.Encode()); err != nil {
return nil, err
}
// Remove the field
fkey := &keys.FD{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, FD: ast.Name}
_, err = txn.DelP(0, fkey.Encode(), 0)
}
@ -112,17 +126,13 @@ func (e *executor) executeRemoveIndexStatement(txn kvs.TX, ast *sql.RemoveIndexS
for _, TB := range ast.What {
// Remove the index config
ckey := &keys.IX{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, IX: ast.Name}
if err := txn.Del(ckey.Encode()); err != nil {
return nil, err
}
// Remove the index
ikey := &keys.IX{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, IX: ast.Name}
_, err = txn.DelP(0, ikey.Encode(), 0)
// Remove all index data
// Remove the index
dkey := &keys.Index{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, IX: ast.Name, FD: keys.Ignore}
if err := txn.PDel(dkey.Encode()); err != nil {
return nil, err
}
_, err = txn.DelP(0, dkey.Encode(), 0)
}