From 51a2cb34156baeaffdf0f43b59a037d66064e92c Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 15 Jun 2016 13:38:37 +0100 Subject: [PATCH] Enable specifying database file path --- db/db.go | 30 ++++++++++++++++++++++++++---- kvs/db.go | 8 ++------ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/db/db.go b/db/db.go index 74bbc9f4..7d4322d1 100644 --- a/db/db.go +++ b/db/db.go @@ -37,9 +37,27 @@ var db *kvs.DB // Setup sets up the connection with the data layer func Setup(opts *cnf.Options) (err error) { - log.WithPrefix("db").Infof("Connecting to database at %s", opts.Store) + log.WithPrefix("db").Infof("Starting database at %s", opts.DB.Path) - db, err = kvs.New() + db, err = kvs.New(opts.DB.Path) + + /*ticker := time.NewTicker(5 * time.Second) + quit := make(chan struct{}) + + go func() { + for { + select { + case <-ticker.C: + t := time.Now() + n := fmt.Sprintf("%d-%02d-%02dT%02d-%02d-%02d-%d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond()) + p := fmt.Sprintf("dev/%s.backup.db", n) + db.Save(p) + case <-quit: + ticker.Stop() + return + } + } + }()*/ return @@ -48,7 +66,7 @@ func Setup(opts *cnf.Options) (err error) { // Exit shuts down the connection with the data layer func Exit() { - log.WithPrefix("db").Infof("Disconnecting from database") + log.WithPrefix("db").Infof("Gracefully shutting down database") db.Close() @@ -84,6 +102,8 @@ func status(e error) interface{} { switch e.(type) { default: return "OK" + case error: + return "ERR" case *kvs.DBError: return "ERR_DB" case *kvs.TXError: @@ -96,9 +116,11 @@ func status(e error) interface{} { } func detail(e error) interface{} { - switch e.(type) { + switch err := e.(type) { default: return nil + case error: + return err.Error() case *kvs.DBError: return "A database error occured" case *kvs.TXError: diff --git a/kvs/db.go b/kvs/db.go index 4ae8428d..8811deed 100644 --- a/kvs/db.go +++ b/kvs/db.go @@ -15,8 +15,6 @@ package kvs import ( - "os" - "github.com/boltdb/bolt" ) @@ -25,13 +23,11 @@ type DB struct { db *bolt.DB } -func New() (db *DB, err error) { +func New(path string) (db *DB, err error) { var bo *bolt.DB - os.Remove("dev/bolt.db") // TODO remove this code!!! - - bo, err = bolt.Open("dev/bolt.db", 0666, nil) + bo, err = bolt.Open(path, 0666, nil) bo.Update(func(tx *bolt.Tx) error { tx.CreateBucketIfNotExists(bucket)