Enable import / export of database transaction log

This commit is contained in:
Tobie Morgan Hitchcock 2017-02-16 10:12:18 +00:00
parent 58ac9d9424
commit e86857bdf7
4 changed files with 57 additions and 16 deletions

View file

@ -16,6 +16,7 @@ package db
import ( import (
"fmt" "fmt"
"io"
"sync" "sync"
"time" "time"
@ -105,14 +106,30 @@ func Exit() {
} }
// Begin opens a new manual transaction with the data layer // Import loads database operations from a reader.
func Begin(rw bool) (txn kvs.TX, err error) { // This can be used to playback a database snapshot
// into an already running database.
return db.Begin(rw) 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) { func Execute(ctx *fibre.Context, txt interface{}, vars map[string]interface{}) (out []*Response, err error) {
// If no preset variables have been defined // 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) { func Process(ctx *fibre.Context, ast *sql.Query, vars map[string]interface{}) (out []*Response, err error) {
// Create 2 channels, one for force quitting // Create 2 channels, one for force quitting

View file

@ -14,8 +14,12 @@
package kvs package kvs
import "io"
// DB represents a database implementation // DB represents a database implementation
type DB interface { type DB interface {
Begin(bool) (TX, error) Begin(bool) (TX, error)
Import(io.Reader) error
Export(io.Writer) error
Close() error Close() error
} }

View file

@ -15,6 +15,7 @@
package kvs package kvs
import ( import (
"io"
"strings" "strings"
"github.com/abcum/surreal/cnf" "github.com/abcum/surreal/cnf"
@ -66,6 +67,20 @@ func (ds *DS) Begin(writable bool) (txn TX, err error) {
return ds.db.Begin(writable) 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 // Close closes the underlying rixxdb / dendrodb
// database connection, enabling the underlying // database connection, enabling the underlying
// database to clean up remainging transactions. // database to clean up remainging transactions.

View file

@ -15,6 +15,8 @@
package rixxdb package rixxdb
import ( import (
"io"
"github.com/abcum/rixxdb" "github.com/abcum/rixxdb"
"github.com/abcum/surreal/kvs" "github.com/abcum/surreal/kvs"
) )
@ -24,22 +26,22 @@ type DB struct {
} }
func (db *DB) Begin(writable bool) (txn kvs.TX, err error) { func (db *DB) Begin(writable bool) (txn kvs.TX, err error) {
var pntr *rixxdb.TX
pntr, err := db.pntr.Begin(writable) if pntr, err = db.pntr.Begin(writable); err != nil {
if err != nil { err = &kvs.DBError{Err: err}
err = &kvs.DSError{Err: err}
if pntr != nil {
pntr.Cancel()
}
return return
} }
return &TX{pntr: pntr}, err 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) { func (db *DB) Close() (err error) {
return db.pntr.Close() return db.pntr.Close()
} }