diff --git a/db/db.go b/db/db.go index ed8aa90a..6acdc9db 100644 --- a/db/db.go +++ b/db/db.go @@ -16,6 +16,7 @@ package db import ( "fmt" + "io" "sync" "time" @@ -105,14 +106,30 @@ func Exit() { } -// Begin opens a new manual transaction with the data layer -func Begin(rw bool) (txn kvs.TX, err error) { - - return db.Begin(rw) - +// Import loads database operations from a reader. +// This can be used to playback a database snapshot +// into an already running database. +func Import(r io.Reader) (err error) { + return db.Import(r) } -// Execute parses the query and executes it against the data layer +// Export saves all database operations to a writer. +// This can be used to save a database snapshot +// to a secondary file or stream. +func Export(w io.Writer) (err error) { + return db.Export(w) +} + +// Begin begins a new read / write transaction +// with the underlying database, and returns +// the transaction, or any error which occured. +func Begin(rw bool) (txn kvs.TX, err error) { + return db.Begin(rw) +} + +// Execute parses a single sql query, or multiple +// sql queries, and executes them serially against +// the underlying data layer. func Execute(ctx *fibre.Context, txt interface{}, vars map[string]interface{}) (out []*Response, err error) { // If no preset variables have been defined @@ -142,6 +159,9 @@ func Execute(ctx *fibre.Context, txt interface{}, vars map[string]interface{}) ( } +// Process takes a parsed set of sql queries and +// executes them serially against the underlying +// data layer. func Process(ctx *fibre.Context, ast *sql.Query, vars map[string]interface{}) (out []*Response, err error) { // Create 2 channels, one for force quitting diff --git a/kvs/db.go b/kvs/db.go index 0e76eeee..e1bd9f8b 100644 --- a/kvs/db.go +++ b/kvs/db.go @@ -14,8 +14,12 @@ package kvs +import "io" + // DB represents a database implementation type DB interface { Begin(bool) (TX, error) + Import(io.Reader) error + Export(io.Writer) error Close() error } diff --git a/kvs/ds.go b/kvs/ds.go index d98388c8..3cee84e6 100644 --- a/kvs/ds.go +++ b/kvs/ds.go @@ -15,6 +15,7 @@ package kvs import ( + "io" "strings" "github.com/abcum/surreal/cnf" @@ -66,6 +67,20 @@ func (ds *DS) Begin(writable bool) (txn TX, err error) { return ds.db.Begin(writable) } +// Import loads database operations from a reader. +// This can be used to playback a database snapshot +// into an already running database. +func (ds *DS) Import(r io.Reader) (err error) { + return ds.db.Import(r) +} + +// Export saves all database operations to a writer. +// This can be used to save a database snapshot +// to a secondary file or stream. +func (ds *DS) Export(w io.Writer) (err error) { + return ds.db.Export(w) +} + // Close closes the underlying rixxdb / dendrodb // database connection, enabling the underlying // database to clean up remainging transactions. diff --git a/kvs/rixxdb/db.go b/kvs/rixxdb/db.go index 7f8a72b0..2d7e5ef0 100644 --- a/kvs/rixxdb/db.go +++ b/kvs/rixxdb/db.go @@ -15,6 +15,8 @@ package rixxdb import ( + "io" + "github.com/abcum/rixxdb" "github.com/abcum/surreal/kvs" ) @@ -24,22 +26,22 @@ type DB struct { } func (db *DB) Begin(writable bool) (txn kvs.TX, err error) { - - pntr, err := db.pntr.Begin(writable) - if err != nil { - err = &kvs.DSError{Err: err} - if pntr != nil { - pntr.Cancel() - } + var pntr *rixxdb.TX + if pntr, err = db.pntr.Begin(writable); err != nil { + err = &kvs.DBError{Err: err} return } - return &TX{pntr: pntr}, err +} +func (db *DB) Import(r io.Reader) (err error) { + return db.pntr.Load(r) +} + +func (db *DB) Export(w io.Writer) (err error) { + return db.pntr.Save(w) } func (db *DB) Close() (err error) { - return db.pntr.Close() - }